Question: Add the three Java classes Applications , Rectangle and ArrayPractice as of your lab9 project to lab10 and copy over the codes from lab9 ...
Add the three Java classes Applications, Rectangle and ArrayPractice as of your lab9 project to lab10 and copy over the codes from lab9 ... which is included below
package dom;
public class Rectangle {
private double length; private double width; public Rectangle() { } public Rectangle(double len,double w){ length=len; width=w; } public double getLength() { return length; } public void setLength(double length) { if(length<0) // for negative values setting to zero this.length=0; this.length = length; } public double getWidth() { return width; } public void setWidth(double width) { if(width<0) // for negative values setting to zero this.width=0; this.width = width; } public double computeArea( ){ return this.length*this.width; } public double computePerimeter(){ return 2*(this.length+this.width); } @Override public String toString(){ return "The length is: "+this.length +" "+"The width is: "+this.width; } public void displayRectangle( ){ System.out.println(toString()); } public boolean equals(Rectangle other){ if(this.length==other.length&&this.width==other.width) return true; else return false; } }
Applications.java
package dom;
import java.util.Scanner;
public class Applications {
public static void main(String args[]) { double length=0; double width=0; Scanner sc = new Scanner(System.in); System.out.print("Enter length:"); length=sc.nextDouble(); System.out.print("Enter width:"); width=sc.nextDouble(); Rectangle box =new Rectangle(length, width); Rectangle box2=new Rectangle(); box2.setLength(10); box2.setWidth(20); System.out.println("box.equals(box2):"+box.equals(box2)); Rectangle box3 = new Rectangle(); length=box.getLength(); width=box.getLength(); box3.setLength(length); box3.setWidth(width); System.out.println("box.equals(box3):"+box.equals(box3)); System.out.println("box area:"+box.computeArea()); System.out.println("box perimeter:"+box.computePerimeter()); System.out.println("box2 area:"+box2.computeArea()); System.out.println("box2 perimeter:"+box2.computePerimeter()); System.out.println("box3 area:"+box3.computeArea()); System.out.println("box3 perimeter:"+box3.computePerimeter()); }
}
ArrayPractice.java
import java.util.Random;
public class ArrayPractice { private int []numbers; private Rectangle [] boxes; private String [] listOfNames; int baseLength = 10; ArrayPractice(){ numbers = new int[baseLength]; boxes = new Rectangle[baseLength]; listOfNames = new String[baseLength]; } ArrayPractice(int k,int j,String[] a){ numbers = new int[k]; boxes = new Rectangle[j]; listOfNames = a; loadBoxes(); loadNumbers(); } //populates numbers array with randomly selected integer from range private void loadNumbers() { Random random = new Random();
int randomNumber; for (int k = 0;k < numbers.length;k++){ randomNumber = random.nextInt(100 + 1 - (-100)) + (-100);
numbers[k] = randomNumber; } } //populates the boxes array with Rectangles private void loadBoxes() { Random random = new Random();
int w,l; for(int k = 0;k < boxes.length;k++) { w = random.nextInt(); l = random.nextInt(); boxes[k] = new Rectangle(w,l); } } //prints the corresponding array entries public void displayNumbers(){ for(int k=0;k ) Exercises Make sure that the Rectangle class contains the equals( ) method as described in your Lab 8 assignment. This method was not used in Lab 9 but you will need it in this lab. (2pts)Add one more constructor to the Rectangle class, the so called copy constructor. This constructor also initializes the fields, but the values passed to the fields are the fields of another Rectangle object which already exists, and the other rectangle is the parameter : public Rectangle (Rectangle other){ length = other.length; width = other.width; } 3. (6pts) Add three new methods to the ArrayPractice class, each is meant to create and return a copy of an array field: copyNumbers( ), copyBoxes( ), copyList( ). Here is the specification with the code of the method that copies the array of names: //the method returns a String[ ] array, takes no parameter: public String[ ] copyList(){ //declare a local array variable and instantiate it to the length of listOfNames[ ] String[]clone = new String[listOfNames.length]; //populate clone with the copy of elements of listOfNames[ ] for(int k = 0; k< listOfNames.length; k++) clone[k] = new String(listOfNames[k]; //return clone return clone; } The copyBoxes( ) method follows the pattern above, you just must change the type, the method name and the array name (not the local variable clone). The copyNumbers( ) is also analogous, but populating the clone array is simpler. The numbers can directly be assigned to the clone entries, copy constructor does not apply for primitive types. 4. (6pts) Class Add three new accessor methods to the ArrayPractice class. However these getters do not directly return the fields, but rather a copy of the fields created by the corresponding copy method above. For instance, the getter for the numbers array is public int [ ] getNumbers(){ return copyNumbers(); } The other two getters are analogous. 5. For testing and demonstration purposes you add to the ArrayPractice class an ordinary accessor method as well, just for the boxes field, such that it directly returns boxes. Name this method getBoxesUnsafe( ). Applications class: test the code In the main method of the Application class keep the list array and the ArrayPractice object instantiated by the parametrized constructor, delete the rest of the code. (2pts)Call the getListOfNames( ) method, save the retuned array in a local String[ ] array named namesCopy and run a for loop to print its element to the console. Reading the output check if you see the elements of the list array defined in main. Print the bool-eans list[0]==namesCopy[0] and list[0].equals(namesCopy[0]) to the console and explain the result in a comment. (4pts)Call the getBoxesUnsafe( ) and getBoxes( ) methods and save the retuned values in local variables named original and cloned, respectively. Run a for loop to print the elements of both arrays to the console, compare and comment. Print the booleans original[0]==cloned[0] and original[0].equals(cloned[0])to the console and explain the result in a comment. Change cloned[0] by the assignment cloned[0] = new Rectangle(1000,1000); Call again getBoxesUnsafe( ) and save it in original2, then print to the console original[0], original2[0] and cloned[0] and comment about the output. Change original[0] by the assignment original[0] = new Rectangle(2000,2000); Call again getBoxesUnsafe( ) and save it in original3, then print to the console original[0], original3[0] together with the boolean original[0]==original3[0] and comment about the output
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
