Community DiscussionCategory: JavaHow to Design related classes apply inheritance on these classes?
paw asked 4 years ago

 

Appreciate if someone can help me with our activity. Thanks 🙂
Problem 1
Given the MyPoint class, override the toString() and the equals() methods.

Problem 2 (Rectangle.java)

Create a class that uses the Point class to implement a Rectangle. The Rectangle class has two data members

private MyPoint upperLeft – to represent upper left coordinate
private MyPoint lowerRight – to represent the lower right coordinate.

Implement the correct constructor for the Rectangle class
— See MyLine for guidance–.

Implement the necessary get and set methods for the class

Implement the following methods:

public double length()
– that returns the length of the rectangle

public double width()
– that returns the width of the rectangle

public double area()
– that returns the area of the rectangle by calling the length() and width()

double perimeter()
– that returns the perimeter of the rectangle by calling the length() and width()

Problem 3 (Circle.java)

  1. Implement a class called Circle for representing a circle. The circle has two data members, a MyPoint object representing the center of the circle and a float value representing the radius.
  2. Include appropriate constructors for your Circle class and the necessary getter and setter methods.
  3. Also include methods for finding the area and circumference of the circle.

area = pi * radius * radius and circumference = 2 * pi * radius.
(You may use the static constant pi in the Math class)

  1. Override the toString method of the Object class to give a string representation of your object
  2. Override the equals method of the Object class. Two circle objects are equal if they have the same center and the same radius.
  3. 6. Write a simple main method that creates Circle objects and tests each of the methods that you have defined.

Problem 4 (Cylinder.java)

After you have completed Problem 2,

  1. Implement a class called Cylinder that extends the Circle class. A cylinder has one additional data member for representing the height (type float).
  2. Create appropriate constructors for your Cylinder class
  3. Include the necessary getters and setter for your additional attributes
  4. Include methods for finding the volume and area of your Cylinder.

area = 2 * (area of the circle in this cylinder) + 2 * pi * radius + height
volume = (area of the circle in this cylinder) * height

  1. Override the toString method of the Object class. Try to use the toString of the Circle class and then just concatenate the remaining values
  2. Override the equals method of the Object class. Two Cylinder objects are equal if they have the same center and the same radius and the same height.
  3. Write a simple main method that creates Cylinder objects and tests each of the methods that you have defined.

 

paw replied 4 years ago

Hi bry,There is no mypoint class given. Only the myline.// Fig. 8.21: MyLine.java// Declaration of class MyLine.import java.awt.Color;import java.awt.Graphics;public class MyLine{ private int x1; // x coordinate of first endpoint private int y1; // y coordinate of first endpoint private int x2; // x coordinate of second endpoint private int y2; // y coordinate of second endpoint private Color myColor; // color of this shape // constructor with input values public MyLine( int x1, int y1, int x2, int y2, Color color ) { this.x1 = x1; // set x coordinate of first endpoint this.y1 = y1; // set y coordinate of first endpoint this.x2 = x2; // set x coordinate of second endpoint this.y2 = y2; // set y coordinate of second endpoint myColor = color; // set the color } // end MyLine constructor // Actually draws the line public void draw( Graphics g ) { g.setColor( myColor ); g.drawLine( x1, y1, x2, y2 ); } // end method draw} // end class MyLine/************************************************************************** * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and * * Pearson Education, Inc. All Rights Reserved. * * * * DISCLAIMER: The authors and publisher of this book have used their * * best efforts in preparing the book. These efforts include the * * development, research, and testing of the theories and programs * * to determine their effectiveness. The authors and publisher make * * no warranty of any kind, expressed or implied, with regard to these * * programs or to the documentation contained in these books. The authors * * and publisher shall not be liable in any event for incidental or * * consequential damages in connection with, or arising out of, the * * furnishing, performance, or use of these programs. * *************************************************************************/

Bryan Lusica Staff replied 4 years ago

Can you post the given MyPoint class?

You must login to answer this question. Register an account.