Question

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) |
| + getX(): double |
| + getY(): double |
| + area(): double |
| + testShape(): void |(this method is static)
+------------------------------------+
where the x and y instance variables store the position of the central point of the shape. The area method computes as result the area of the shape: unfortunately an unknown shape has an unknown area (we only know how to compute the area of specific shapes, like triangles, for example) so the area method just prints a message “An unknown shape has an unknown area!” and returns a meaningless result such as -1.0 (because the area method must return something which is of type double). The testShape method is static.
Add the following code to your program to test the Shape class:
public class Start {
public static void main(String[] args) {
Shape.testShape();
}
}
Question 2
Add a Circle class that derives from the Shape class and has the following UML specification:
+---------------------------------------------+
| Circle |
+---------------------------------------------+
| - radius: double |
+---------------------------------------------+
| + Circle(double x, double y, double radius) |
| + area(): double |
| + testCircle(): void |(static)
+---------------------------------------------+
Use Math.PI to compute the area of a circle.
Do not forget to change the main method of the Start class to run the unit tests of the new Circle class.
Question 3
Add a Dot class that derives from the Shape class and has the following UML specification:
+----------------------------------+
| Dot |
+----------------------------------+
+----------------------------------+
| + Dot(double x, double y) |
| + area(): double |
| + testDot(): void |(static)
+----------------------------------+
Do not forget to change the main method of the Start class to run the unit tests of the new Dot class.
Question 4
Add two new classes Rectangle and Square. Rectangle derives from Shape and Square derives from
Rectangle. Rectangle has the following UML specification:
+--------------------------------------------------------------+
| Rectangle |
+--------------------------------------------------------------+
| - width: double |
| - length: double |
+--------------------------------------------------------------+
| + Rectangle(double x, double y, double width, double length) |
| + area(): double |
| + testRectangle(): void |(static)
+--------------------------------------------------------------+
The constructor for Square takes three arguments: the x and y positions of the center of the square, and the size of
the square.
Does the Square class need its own area method?
Do not forget to change the main method of the Start class to run the unit tests of the new Rectangle and
Square classes.
Question 5
We now want to be able to manipulate many shapes together in our software, not just one shape at a time. So add a
ManyShapes class to your program with the following UML diagram:
+---------------------------+
| ManyShapes |
+---------------------------|
| - allShapes: ArrayList |
+---------------------------+
| + ManyShapes() |
| + addShape(Shape s): void |
| + listAllShapes(): void |
| + testManyShapes (): void |(static)
+---------------------------+
The allShapes instance variable is of type ArrayList which is a class provided to you by Java that you need to
import into your program using: import java.util.ArrayList;
You can then define the allShapes instance variable like this: private ArrayList allShapes;
In the ManyShapes constructor you need to create a new ArrayList object and store it in the instance variable
allShapes, like this: this.allShapes = new ArrayList();
(if you forget to do this then the instance variable allShapes will point at nothing and you will get an error when you
run your program and you try to call a method of the nonexistent arraylist object).
An ArrayList object works both like an array and like a list, in which you can store as many other objects of type
Object as you want:
 you can add a new object o to the arraylist by using the add(o) method of the arraylist;
 you can get the number of elements in the arraylist by using the size() method of the arraylist;
 you can access a specific element of the arraylist at index i by using the get(i) method of the arraylist
(element indexes start at zero in an arraylist).
The addShape method takes a shape as argument and adds it to the arraylist.
The listAllShapes method prints on the screen the area of each shape in the arraylist, one by one, using a loop. For
example, if the arraylist currently contains a Square object of size 5 and a Dot object then the listAllShapes
method should print:
Shape has area 25.0
Shape has area 0.0

Here is the code of the testManyShapes method:
public static void testManyShapes() {
ManyShapes m = new ManyShapes();
m.addShape(new Circle(1.2, 3.4, 4.0)); // Upcast from Circle to Shape.
m.addShape(new Dot(1.2, 3.4)); // Upcast from Dot to Shape.
m.addShape(new Rectangle(1.2, 3.4, 4.0, 5.0)); // Upcast from Rectangle to Shape.
m.addShape(new Shape(1.2, 3.4));
m.addShape(new Square(1.2, 3.4, 5.0)); // Upcast from Square to Shape.
m.listAllShapes();
}
Do not forget to change the main method of the Start class to run the unit tests of the new ManyShapes class.
Question 6
The listAllShapes method tells us the area of every shape in the arraylist but it does not tell us the type of the
shapes in the arraylist. Modify the listAllShapes method to tell us both the area and the type for each shape in the
arraylist. For example, if the arraylist currently contains a Square object of size 5 and a Dot object then the
listAllShapes method should print:
Square has area 25.0
Dot has area 0.0

Use the instanceof operator in the listAllShapes method to determine the type of each shape.
Question 7
Using instanceof in the listAllShapes method works fine, but there is a nicer, more object-oriented way to do
the same thing: just have every shape object tell about itself when it is asked! To do this, add a new method toString
to the Shape class that overrides the toString method which is inherited by the Shape class from the Object
class. This method should then return the string “Shape has area XXX”, where XXX is the result of the area
method from the same class Shape. Then override the toString method in every subclass of Shape to return the
right string for the subclass in a similar way.
After you have added the toString method to the Shape class and all its subclasses, delete all the instanceof
tests in the listAllShapes method of the ManyShapes class and use System.out.println to directly print
every element of the arraylist (System.out.println will then automatically call the toString method of each
object that you are printing; no downcast is needed then because Java’s dynamic dispatch will automatically call the
right toString method coming from the right class for each object stored in the arraylist that you are printing).

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

/*************************Shape.java**********************/

package shape;

public class Shape {

   private double x;
   private double y;

   public Shape(double x, double y) {
       this.x = x;
       this.y = y;
   }

   public double getX() {
       return x;
   }

   public double getY() {
       return y;
   }

   public double area() {

       System.out.println("An unknown shape has an unknown area!");

       return -1;

   }

   public static void testShape() {

   }

   @Override
   public String toString() {
       return "Shape has area " + area();
   }

}
/**********************************Circle.java************************/

package shape;

public class Circle extends Shape {

   private double radius;

   public Circle(double x, double y, double radius) {
       super(x, y);
       this.radius = radius;
   }

   public double area() {

       return Math.PI * radius * radius;
   }

   public static void testCircle() {

   }
  
   @Override
   public String toString() {
       return "Circle has area " + area();
   }
}
/*************************************Dot.java***************************/

package shape;

public class Dot extends Shape {

   public Dot(double x, double y) {
       super(x, y);
   }

   public double area() {

       return 0;
   }

   public static void testDot() {

   }

   @Override
   public String toString() {
       return "Dot has area " + area();
   }
}
/***********************************Rectangle.java*******************************/

package shape;

public class Rectangle extends Shape {

   private double width;
   private double length;

   public Rectangle(double x, double y, double width, double length) {
       super(x, y);
       this.width = width;
       this.length = length;
   }

   public double area() {

       return length * width;
   }

   public static void testRectangle() {

   }
   @Override
   public String toString() {
       return "Rectangle has area " + area();
   }
}
/************************************Square.java****************************/

package shape;

public class Square extends Shape {

   private double size;

   public Square(double x, double y, double size) {
       super(x, y);
       this.size = size;
   }

   public double area() {

       return size * size;
   }

   public static void testSquare() {
      
   }
   @Override
   public String toString() {
       return "Square has area " + area();
   }
}
/*********************************ManyShapes.java**************************/

package shape;

import java.util.ArrayList;

public class ManyShapes {

   private ArrayList<Shape> shapes;

   public ManyShapes() {

       shapes = new ArrayList<>();
   }

   public void addShape(Shape s) {

       shapes.add(s);
   }

   public void listAllShapes() {

       for (Shape shape : shapes) {

           System.out.println(shape.toString());
       }
   }
  
   public static void testManyShapes() {
      
       ManyShapes m = new ManyShapes();
       m.addShape(new Circle(1.2, 3.4, 4.0)); // Upcast from Circle to Shape.
       m.addShape(new Dot(1.2, 3.4)); // Upcast from Dot to Shape.
       m.addShape(new Rectangle(1.2, 3.4, 4.0, 5.0)); // Upcast from Rectangle to Shape.
       m.addShape(new Shape(1.2, 3.4));
       m.addShape(new Square(1.2, 3.4, 5.0)); // Upcast from Square to Shape.
       m.listAllShapes();
   }
}
/*************************Start.java**********************/

package shape;

public class Start {

   public static void main(String[] args) {
      
       ManyShapes.testManyShapes();
   }
}
/***********************output**********************/

Circle has area 50.26548245743669
Dot has area 0.0
Rectangle has area 20.0
An unknown shape has an unknown area!
Shape has area -1.0
Square has area 25.0

Please let me know if you have any doubt or modify the answer, Thanks:)

Add a comment
Know the answer?
Add Answer to:
This question is about java program. Please show the output and the detail code and comment.And...
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
  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a...

    I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side;    //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); }    //setter and getter methods for Square public void setSide(double side) { this.side = side; }    public double getSide() { return...

  • java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectang...

    java language Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...

  • Add another method public static void displayObject(ArrayList list, int n) that will display then information on...

    Add another method public static void displayObject(ArrayList list, int n) that will display then information on the nth object of list. If n is not a valid index, the method should generate and throw and exception that the main can then process. You get to decide what exception (one built into Java or a custom exception) and how you would like to “handle” the exception (terminate the program, prompt for more input, etc). Here is the program so far: import...

  • An abstract class doesn't have a constructor (because you cannot make an object of the abstract...

    An abstract class doesn't have a constructor (because you cannot make an object of the abstract class). It should have at least one method, which then has to be overridden in all derived classes. Here is an example:   abstract void run();   class Honda4 extends Bike{   public static void main(String args[]){   obj.run();   } You are to create an abstract class called Shape, which has an abstract method called computeArea(). Derive a Circle class from Shape. (Circle is similar to your previous...

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

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

  • this is for java programming Please Use Comments I am not 100% sure if my code...

    this is for java programming Please Use Comments I am not 100% sure if my code needs work, this code is for the geometric if you feel like you need to edit it please do :) Problem Description: Modify the GeometricObject class to implement the Comparable interface and define a static max method in the GeometricObject class for finding the larger (in area) of two GeometricObject objects. Draw the UML and implement the new GeometricObject class and its two subclasses...

  • Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing...

    Question: A. Write an Octagon class that inherits from GeometricObject and implements the Comparable interface, comparing the area of the shape. You can assume that all the sides are equal in length. B. Implement the Comparable interface in the Circle and Rectangle from Question2 comparing the area of each shape. (use the same classes in Question 2) C. Also add the overloaded toString() method to each class that prints a summary of the object. Then, in another class, create an...

  • This is the Java Object-oriend project. Please show the detail code and comment for each class....

    This is the Java Object-oriend project. Please show the detail code and comment for each class. Thank you Create a class Door with the following UML specification: +---------------------+ | Door | +---------------------+ | - isOpen: boolean | +---------------------+ | + Door() | | + isOpen(): boolean | | + open(): void | | + close(): void | | + testDoor(): void | +---------------------+ where isOpen is an instance variable indicating whether the door is currently open or not. The default...

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