Question

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

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));

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
In Java Create a testing class that does the following to the given codes below: To...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the...

    Java help: 1.1) Create a class TA  that extends class Student. 1.2) Add a variable to the TA class to represent the course the student is TA'ing. 1.3) Provide a constructor for the class so the code in the PeopleFactory will work as provided. 1.4) Provide a toString() method for the TA  class so that TA's will output as shown in the sample code. Provide a class Staff so that you can un-comment the lines of code in the PeopleFactory class that...

  • A general shape class is shown below. Shape has a dimension. It also defines constructors, getters,...

    A general shape class is shown below. Shape has a dimension. It also defines constructors, getters, setters and a toString method. class Shape{ int dimension; public Shape(){} public Shape(int newDimension){ dimension = newDimension; } public int getDimension(){ return dimension; } public void setDimension(int newDimension){ dimension = newDimension; } public String toString(){ return "Shape has dimension "+dimension; } } a. Define classes Circle and Square, which inherit from Shape class. b. Both classes must have two constructors similar to Shape class....

  • I have to create a java graphics program which will draw 10 rectangles and 10 ellipses...

    I have to create a java graphics program which will draw 10 rectangles and 10 ellipses on the screen. These shapes are to be stored in an arrayList. I have to do this using 6 different class files. I am not sure whether I successfully created an ellipse in the Oval Class & also need help completing the print Class. I need someone to check whether my Oval Class is correct (it successfully creates an ellipse with x, y, width,...

  • Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they...

    Programming: Java: Fixing errors in code help: The errors are in square.java and rectangle.java and they both say: constructor Shape in class Shape cannot be applied to given types; required: no arguments found: String reason: actual and formal argument lists differ in length The directions say: The files Shape.java and TestArea.java have been finished already, YOU DONT ADD ANYTHING TO THESE TWO. According to the requirement, you need to modify in Square.java and Rectangle.java, respectively a. Implementing constructor with no...

  • Question: Modify the Rectangle and Box classes that are below, so that they implement the Comparable...

    Question: Modify the Rectangle and Box classes that are below, so that they implement the Comparable interface. The compareTo() method shall compare rectangles and boxes by areas and volumes. In main(), create an ArrayList of rectangles and an ArrayList of boxes. Randomly generate ten rectangles and ten boxes adding to the two ArrayLists. Use Collections.sort() to sort the two ArrayLists, respectively. Print the ArrayLists before and after being sorted. Three classes that need to be edited are below: Main class,...

  • Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes...

    Inheritance and Polymorphism (use Pet.java) You are given a class named Pet.java.   Create two child classes named Cat.java and Dog.java. Cat.java should add attributes for color and breed. Dog.java should add attributes for breed and size (use String for small, medium, large). Add appropriate constructors, getter/setter methods and toString() methods.   In a separate file named PetDriver.java, create a main. In the main, create an ArrayList of Pet. Add an instance of Cat and an instance of Dog to the ArrayList....

  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

  • Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a...

    Update your Assignment 4 as follows (Assignment 4 codes will be on the bottom) Create a new method called displayAll, that takes an ArrayList (of your base class) as a parameter, and doesn't return anything [25 pts] The displayAll method will loop through the ArrayList, and call the display (or toString) method on each object   [25 pts] In the main method, create an ArrayList containing objects of both your base class and subclass. (You should have at least 4 objects)  [25...

  • Task 3: Main Program Create a main program class that: Creates three or more Nurse instances...

    Task 3: Main Program Create a main program class that: Creates three or more Nurse instances assigned to different shifts. Creates three or more Doctor instances. Creates three or more Patient instances with pre-determined names and manually assigned physicians chosen from the pool of Doctor instances previously created. Generates another 20 Patient instances using randomly generated names and randomly assigns them physicians chosen from the pool of Doctor instances previously created. Prints the toString() values for all employees. Prints the...

  • PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is to...

    PLEASE HELP this is the last part of my lab and I need help ): add comments please (: if it is too difficult you do not have to do part 3 but it would be greatly appreciated if you do ! Obtain example code files Circle.java, Shape.java, CircleShape2.java, Sphere.java, and CircleShapeApp.java from the downloaded files in Ch8. Compile and execute the example and understand the polymorphism it performs. (I have copied and pasted all of these files below) Modify...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT