Question

In: Computer Science

Array of Hope! Write code for a Java class ArrayPair with the following fields and methods:...

Array of Hope! Write code for a Java class ArrayPair with the following fields and methods: ArrayPair - arr1 [] : int - arr2 [] : int Fields - length1 : int - length2 : int + ArrayPair (int arraySize) Methods + add (int arrNumber, int value) : void + remove (int arrNumber) : void + equal () : boolean + greater () : boolean + print () : void Thus, there are two arrays of integers in the class with associated lengths (number of elements in each array).

Methods: ArrayPair: Constructor, instantiates the two arrays with the given size as a parameter. Sets the private lengths equal to zero since the arrays are initially empty. The constructor should not print or read anything.

add: Adds the given value to the right end of the array specified by the arrNumber (if equal to 1, adds to the arr1, if 2 adds to arr2, otherwise prints an error message and does not add the value). add should not print anything other than the error message when appropriate.

remove: Removes the rightmost element of the array specified by arrNumber. If the array is empty, an error message should be printed. remove should not print anything other than an error message when appropriate.

equal: Returns true if the two arrays have the same number of values and the exact same values in the same order. Otherwise, equal should return false. equal should not print anything to output.

greater: greater returns true if arr1 has more elements than arr2. greater returns false if arr2 has more elements than arr1. If the two arrays have the same number of elements, compare corresponding elements in the two arrays. For the first non-equal pair of values: if the arr1 value is greater than the arr2 value, true is returned; if the arr2 value is greater than the arr1 value, false is returned. If all the values are equal, false is returned.

print: Prints the values in both of the arrays with labels.

Main Program (Java class PairApp) Program first creates an ArrayPair object. The program then repeatedly reads commands of add, remove, equal, greater, print, or quit, until the user enters quit. If the user enters: add: Prompts the user for array number and value and calls add method to add that value to the given array. remove: Prompts the user for array number and calls the remove method to remove that element from the given array. equal: Calls equal method and prints whether or not the two arrays are equal. greater: Calls greater method prints whether or the not the first array is greater than the second. print: Calls print to print the values in the arrays. Note these further requirements: • User options should be accepted as uppercase or lowercase. • An error message is printed for an illegal choice. • The program should print an ending message when stopping. There is a sample run on the next page. Your output should follow the given text and spacing. Include a header comment describing the program and comment all variables, control structures and methods in the program.

Solutions

Expert Solution

ArrayPair.java

class ArrayPair{
//private variables
private int arr1[];
private int arr2[];
private int length1;
private int length2;
//Constructor
public ArrayPair(int arraySize){
arr1=new int[arraySize]; //instanstiate array
arr2=new int[arraySize];
length1=0;
length2=0;
}
//methods
public void add(int arrNumber, int value){
if(arrNumber==1){
arr1[length1++]=value;
}
else if(arrNumber==2){
arr2[length2++]=value;
}
else{
System.out.println("Error");
}
}
public void remove(int arrNumber){
if(arrNumber==1){
if(length1==0){
System.out.println("Error, array1 is empty");
}
else{
--length1;
}
}
else if(arrNumber==2){
if(length2==0){
System.out.println("Error, array2 is empty");
}
else{
--length2;
}
}
else{
System.out.println("Error");
}
}
public boolean equal(){
if(length1!=length2){
return false;
}
else{
for(int i=0;i<length1;i++){
if(arr1[i]!=arr2[i]){
return false;
}
}
return true;
}
}
public boolean graeter(){
if(length1>length2){
return true;
}
else if(length1<length2){
return false;
}
else{
for(int i=0;i<length1;i++){
if(arr1[i]!=arr2[i]){
if(arr1[i]>arr2[i]){
return true;
}
else if(arr2[i]>arr1[i]){
return false;
}
}
}
}
return false;
}
public void print(){
for(int i=0;i<length1;i++){
System.out.print(arr1[i]+" ");
}
System.out.println();
for(int i=0;i<length2;i++){
System.out.print(arr2[i]+" ");
}
}
  
}

Screenshot of code

For main program sample output is required


Related Solutions

in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for...
Create a Class with Data Fields and Methods in Java. Provide a Java coding solution for the following: 1. Name the class SalonServices 2. Add private data fields: a. salonServiceDescription – This field is a String type b. price - This field is a double type 3. Create two methods that will set the field values. a. The first method setSalonServiceDescription() accepts a String parameter defined as service and assigns it to the salonServiceDescription. The method is not static b....
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Given an array, write code to scan the array for a particular purpose. Given a class,...
Given an array, write code to scan the array for a particular purpose. Given a class, write a getter and/or a setter. Given a class with a list, write code that manages the list.
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
Write a Java PATRON class with fields that reflect the structure of your patrons table. Write...
Write a Java PATRON class with fields that reflect the structure of your patrons table. Write a java FACTORY class with these two methods: getPatron (String patronID) that returns a patron object for the requested patronID. The patron object that is returned should have field values that were loaded from the database using JDBC. updatePatron(Patron PatronToUpdate) that returns a string indicating whether the patron object that was passed in was successfully updated in the database or not. The method should...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give...
Write in Java Modify the parent class (Plant) by adding the following abstract methods:(The class give in the end of question) a method to return the botanical (Latin) name of the plant a method that describes how the plant is used by humans (as food, to build houses, etc) Add a Vegetable class with a flavor variable (sweet, salty, tart, etc) and 2 methods that return the following information: list 2 dishes (meals) that the vegetable can be used in...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT