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
  • 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...

  • 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...

  • Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override...

    Create a Java Project in Eclipse with the following: Include the Rectangle class supplied below. Override the toString method for Rectangle. Override the equals method for Rectangle. Implement the comparable Interface for Rectangle (Compare by area) Rectangle class: public class Rectangle {       private double length;    private double width;       public Rectangle(double l, double w)    {        length = l;        width = w;    }       public double getLength()    {   ...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • This question is about java program. Please show the output and the detail code and comment.And...

    This question is about java program. Please show the output and the detail code and comment.And the output (the test mthod )must be all the true! Thank you! And the question is contain 7 parts: Question 1 Create a Shape class with the following UML specification: (All the UML "-" means private and "+" means public) +------------------------------------+ | Shape | +------------------------------------+ | - x: double | | - y: double | +------------------------------------+ | + Shape(double x, double y) | |...

  • Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The...

    Programming Language: Java Write a class named ColoredRectangle that extends the Rectangle class (given below). The ColoredRectangle class will have an additional private String field named Color. The ColoredRectangle class should have four constructors: a no-arg constructor; a three-arg constructor; a two-arg constructor that accepts a Rectangle object and a color; and a copy constructor. The ColoredRectangle class should have an Equals and toString methods. The ColoredRectangle class should have mutators and accessors for all three fields. public class Rectangle...

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
ADVERTISEMENT