Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java Create a testing class that does the following to the given codes below:

ID: 2247378 • Letter: I

Question

In Java Create a testing class that does the following to the given codes below:

To demonstrate polymorphism do the following:

Create an arraylist to hold 4 base class objects

Populate the arraylist with one object of each data type

Code a loop that will process each element of the arraylist

Call the first ‘common functionality’ method

Call the second ‘common functionality’ method

Call the third ‘common functionality’ method

Verify that each of these method calls produces unique results

Call the ‘getWidth’ method – does it work?

Create a derived class object and call the base class ‘get’ method-how does it work?

Given Codes:

public class Shape {

protected int length;

/**

* @param length

*/

Shape(int length) {

this.setLength(length);

}

/**

* @return the length

*/

public int getLength() {

return length;

}

/**

* @param length the length to set

*/

public void setLength(int length) {

this.length = length;

}

/**

* draws the shape

*/

public void draw() {

System.out.println("Can't draw - no specifics");

}

/**

* calculates the area

*/

public void area() {

System.out.println("Can't calc area - no specifics");

}

/* (non-Javadoc)

* @see java.lang.Object#toString()

*/

public String toString() {

return "Length is " + length;

}

public class Line extends Shape {

Line (int length) {

super(length);

}

/**

* draws the line shape

*/

public void draw() {

System.out.println("****");

}

/* (non-Javadoc)

* @see Shape#toString()

*/

public String toString() {

return super.toString() + "(Line)";

}

/**

* calculates the area

*/

public void area() {

System.out.println("The area of a line is " + 1);

}

}

public class Rectangles extends Shape {

private int width;

Rectangles (int length, int width) {

super(length);

this.setWidth(width);

}

/**

* @return the width

*/

public int getWidth() {

return width;

}

/**

* @param width the width to set

*/

public void setWidth(int width) {

this.width = width;

}

/**

* draws the line shape

*/

public void draw() {

System.out.println("*******");

System.out.println("*******");

System.out.println("*******");

System.out.println("*******");

}

/* (non-Javadoc)

* @see Shape#toString()

*/

public String toString() {

return super.toString() + "(Rectangles)";

}

/**

* calculates the area

*/

public void area() {

System.out.println("The area of a square is " + (length * width));

}

}

public class Square extends Shape {

Square (int length) {

super(length);

}

/**

* draws the line shape

*/

public void draw() {

System.out.println("****");

System.out.println("****");

System.out.println("****");

System.out.println("****");

}

/* (non-Javadoc)

* @see Shape#toString()

*/

public String toString() {

return super.toString() + "(Square)";

}

/**

* calculates the area

*/

public void area() {

System.out.println("The area of a square is " + (length * length));

}

}

Explanation / Answer

Shape.java

public class Shape {

protected int length;

/**

* @param length

*/

Shape(int length) {

this.setLength(length);

}

/**

* @return the length

*/

public int getLength() {

return length;

}

/**

* @param length

* the length to set

*/

public void setLength(int length) {

this.length = length;

}

/**

* draws the shape

*/

public void draw() {

System.out.println("Can't draw - no specifics");

}

/**

* calculates the area

*/

public void area() {

System.out.println("Can't calc area - no specifics");

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

public String toString() {

return "Length is " + length;

}

}

_______________

Line.java

public class Line extends Shape {

Line(int length) {

super(length);

}

/**

* draws the line shape

*/

public void draw() {

System.out.println("****");

}

/*

* (non-Javadoc)

*

* @see Shape#toString()

*/

public String toString() {

return super.toString() + "(Line)";

}

/**

* calculates the area

*/

public void area() {

System.out.println("The area of a line is " + 1);

}

}

___________________

Rectangles.java

public class Rectangles extends Shape {

private int width;

Rectangles(int length, int width) {

super(length);

this.setWidth(width);

}

/**

* @return the width

*/

public int getWidth() {

return width;

}

/**

* @param width

* the width to set

*/

public void setWidth(int width) {

this.width = width;

}

/**

* draws the line shape

*/

public void draw() {

System.out.println("*******");

System.out.println("*******");

System.out.println("*******");

System.out.println("*******");

}

/*

* (non-Javadoc)

*

* @see Shape#toString()

*/

public String toString() {

return super.toString() + "(Rectangles)";

}

/**

* calculates the area

*/

public void area() {

System.out.println("The area of a square is " + (length * width));

}

}

__________________

Square.java

public class Square extends Shape {

Square(int length) {

super(length);

}

/**

* draws the line shape

*/

public void draw() {

System.out.println("****");

System.out.println("****");

System.out.println("****");

System.out.println("****");

}

/*

* (non-Javadoc)

*

* @see Shape#toString()

*/

public String toString() {

return super.toString() + "(Square)";

}

/**

* calculates the area

*/

public void area() {

System.out.println("The area of a square is " + (length * length));

}

}

__________________

Test.java

import java.util.ArrayList;

public class Test {

public static void main(String[] args) {

//Creating an ArrayList which stores each class object

ArrayList<Shape> arl=new ArrayList<Shape>();

//Adding each object to the ArrayList

arl.add(new Shape(2));

arl.add(new Line(3));

arl.add(new Rectangles(2,3));

arl.add(new Square(4));

//Calling the methods on each class

for(int i=0;i<arl.size();i++)

{

arl.get(i).draw();

arl.get(i).area();

System.out.println(arl.get(i).toString());

System.out.println("______________");

}

}

}

___________________

Output:

Can't draw - no specifics
Can't calc area - no specifics
Length is 2
______________
****
The area of a line is 1
Length is 3(Line)
______________
*******
*******
*******
*******
The area of a square is 6
Length is 2(Rectangles)
______________
****
****
****
****
The area of a square is 16
Length is 4(Square)
______________

_________________Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote