Question

In: Computer Science

IDS 201 Lab Exercise 5 Within a class named LX5, write methods as follows: 1. a...

IDS 201 Lab Exercise 5

Within a class named LX5, write methods as follows:

1. a method named valueCheck that accepts a parameter int named value and, using a switch statement, displays text output as follows:

  • if value is 4 or greater: "LOTS"
  • if value equals 3: "three"
  • if value equals 2: "two"
  • if value equals 1: "one"
  • if value equals 0: "zero"
  • if value is negative: "NEGATIVE"

2. a method named textMatch that accepts two parameter String objects named string1 and string2 and returns true if their text is identical and false otherwise

3. a method named sameObject that accepts two Objects as parameters and returns true if they are the same object and false otherwise (note that the Object class does not require any import statement)

4. a method named factorCheck that accepts a parameter int named value and returns values as follows:

  • if value is greater than 49 or less than 1, return -1
  • if value equals 1, return 0
  • if value is evenly divisible by 7, return 7
  • if value is evenly divisible by 5, return 5
  • if value is evenly divisible by 3, return 3
  • if value is evenly divisible by 2, return 2
  • if none of the above are true, return 1

5. Write an equivalent version of String's indexOf() method. The method will accept as parameters two Strings, big and small. If the same text of small is contained in big, the method will return the index of the first character of small from its first appearance in big.

6. Write an extended version of String's indexOf() method. The method will accept as parameters two Strings, big and small, and two ints, start and end. Like 3) above, this method will search big for matching text with the String small and return the index of the first character of small from its first appearance in big. However, in this case the search will cover not the entire String big but the match must begin at or after index start and end at or before index end.

Solutions

Expert Solution

Lab Exercise 5:

Java code:

// Lab Exercise 5
public class LX5 {
    // Part 1
    public void valueCheck(int value) {
        switch (value) {
            case 0:
                System.out.println("zero");
                break;

            case 1:
                System.out.println("one");
                break;

            case 2:
                System.out.println("two");
                break;

            case 3:
                System.out.println("three");
                break;

            default:
                if (value >= 4)
                    System.out.println("LOTS");
                else
                    System.out.println("NEGATIVE");
        }
    }

    // Part 2
    public boolean textMatch(String string1, String string2) {
        if (string1.equalsIgnoreCase(string2))
            return true;
        else
            return false;
    }

    // Part 3
    public boolean sameObject(Object object1, Object object2) {
        if (object1.getClass().equals(object2.getClass()))
            return true;
        else
            return false;
    }

    // Part 4
    public int factorCheck(int value) {
        if (value > 49 || value < 1)
            return -1;
        if (value == 1)
            return 0;
        if (value % 7 == 0)
            return 7;
        if (value % 5 == 0)
            return 5;
        if (value % 3 == 0)
            return 3;
        if (value % 2 == 0)
            return 2;
        return 1;
    }

    // Part 5
    public int indexOf(String big, String small) {
        int index = -1;
        for (int i = 0; i <= big.length() - small.length(); i++) {
            boolean contains = true;
            for (int j = 0; j < small.length(); j++) {
                if (big.charAt(i + j) != small.charAt(j)) {
                    contains = false;
                    break;
                }
            }
            if (contains) {
                index = i;
                break;
            }
        }
        return index;
    }

    // Part 6
    public int indexOfExtended(String big, String small, int start, int end) {
        int index = -1;
        for (int i = start; i <= end - small.length(); i++) {
            boolean contains = true;
            for (int j = 0; j < small.length(); j++) {
                if (big.charAt(i + j) != small.charAt(j)) {
                    contains = false;
                    break;
                }
            }
            if (contains) {
                index = i;
                break;
            }
        }
        return index;
    }

}

Kindly rate the answer and for any help just drop a comment


Related Solutions

Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor...
Lab to be performed in Java. Lab: 1.) Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Write a driver class to test that demonstrates that an exception happens for these scenarios 2.) Write a class named InvalidTestScore...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks...
Exercise 1. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the pattern shown below. Write a function named printBox to print a 5 x 10 box of asterisks using nested loops. When printBox() is called your function should display the following information. ********** ********** ********** ********** ********** """ # place the code for the printBox function here printBox( numRows, numCols ) Exercise 2....
The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print...
The class Person, uploaded on Blackboard with Lab 5, only has methods to set and print the name of a person. Redefine the class Person to include the following operations: Set the last name only Set the first name only Set the middle name Check whether a given last name is the same as the last name of this person Check whether a give first name is the same as the first name of this person Check whether a given...
Create a class named GameCharacter to define an object as follows: The class should contain class...
Create a class named GameCharacter to define an object as follows: The class should contain class variables for charName (a string to store the character's name), charType (a string to store the character's type), charHealth (an int to store the character's health rating), and charScore (an int to store the character's current score). Provide a constructor with parameters for the name, type, health and score and include code to assign the received values to the four class variables. Provide a...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write a default constructor. Write a constructor that takes values for all member variables as parameters. Write a copy constructor. Implement Get/Set methods for all member variables. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue(). Add a member overload...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows:...
Java Create a Project named Chap4b 1. Create a Student class with instance data as follows: student id, test1, test2, and test3. 2. Create one constructor with parameter values for all instance data fields. 3. Create getters and setters for all instance data fields. 4. Provide a method called calcAverage that computes and returns the average test score for an object to the driver program. 5. Create a displayInfo method that receives the average from the driver program and displays...
In Lab 4, you made a class with static methods. The static methods converted an integer...
In Lab 4, you made a class with static methods. The static methods converted an integer in binary, Hex, and Octal. To do this, you made a class of static methods. The class did not have any fields. The static methods received the integer value as a parameter. For this lab, make all of the static methods, non-static methods. Lab 5 Requirements Create a class named “Lab5_IntConv_Class Have one private field name intValue. Add two constructors: One is a default...
write a java code to represent a sales class as follows: 1- The Sales class contains...
write a java code to represent a sales class as follows: 1- The Sales class contains the names of sellers (strings) and the sales/seller/day (matrix of integers). Assume the number of sellers is set dynamically by the constructor and that the sellers work 6 days/week. Example: names/days 0 1 2 3 4 5 Ali 30 5 89 71 90 9 Ahmad 15 81 51 69 78 25 Omar 85 96 7 87 41 54 The class should contain the following...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT