In this assignment, you will be building upon the work that you did in Lab #5A by expanding the original classes that you implemented to represent circles and simple polygons. Assuming that you have completed Lab #5A (and do not move on to this assignment unless you have!), copy your Circle, Rectangle, and Triangle classes from that assignment into a new NetBeans project, then make the following changes: Create a new Point class containing X and Y coordinates as its only instance fields. This class should include a constructor which accepts the values for these fields as integer arguments. The constructor should initialize these fields and ensure that the X and Y coordinates are positive integers that do not have values greater than 500; for now, if either coordinate is less than zero, the constructor should initialize it to zero, and if either coordinate is greater than 500, the constructor should initialize it to 500. The class should also provide accessor methods ("getter" methods) for each field. Revise your Circle, Rectangle, and Triangle classes to add a Point instance field called center. This is an example of aggregation: each of your shape objects will now contain a Point object, representing the location of the center of the shape within a two-dimensional plane. This field should be initialized by the constructor, so you will need to add x and y integer arguments to your constructors. Use these values to create a new Point object in the constructor for the center instance field. You will also need to add accessor methods (getter methods) for these coordinates. Expand the toString() method of the shape classes to print the X and Y coordinates inside parentheses, along with the area of the shape. For example, a triangle with an X and Y coordinate of zero should be printed this way: (0,0): / 4.0 \ Finally, create a new main class (or reuse the one from Lab #5A) which creates at least two objects of each shape. These objects can be declared as individual variables; later, we will see how to unify these objects into a common collection. Remember that the X and Y coordinates should be given as arguments to the shape objects' constructors. As in Lab #5A, this main class should also print the shapes' individual string representations, using their respective toString() methods, and then the total areas of each type of shapes
________________________________________________________
This is the code I have been working with in the prior assignment. Circle.java : //Java class public class Circle { // instance fields/data fields private double radius; // default constructor public Circle() { this.radius = 0.0;// set radius to 0.0 } // parameterized constructor public Circle(double r) { this.radius = r; } // getter method public double getRadius() { return this.radius;// return radius } // method to get area public double area() { // return area of circle return Math.PI * this.radius * this.radius; } // method to string public String toString() { return "(" + this.area() + ")"; } } ****************************
Rectangle.java : //java class public class Rectangle { // instance fields/data fields private double length; private double width; // default constructor public Rectangle() { this.length = 0.0;// set length to 0.0 this.width = 0.0;// set width to 0.0 } // parameterized constructor public Rectangle(double l,double w) { this.length = l;// set length this.width = w;// set width } // getter method public double getLength() { return this.length;// return length } public double getWidth() { return this.width;// return width } // method to get area public double area() { // return area of rectangle return this.length * this.width; } // method to string public String toString() { return "[" + this.area() + "]"; } }
**************************
Triangle.java : //Java class public class Triangle { // instance fields/data fields private double base; private double height; // default constructor public Triangle() { this.base = 0.0;// set base to 0.0 this.height = 0.0;// set height to 0.0 } // parameterized constructor public Triangle(double b,double h) { this.base = b;// set base this.height = h;// set height } // getter method public double getBase() { return this.base;// return base } public double getHeight() { return this.height;// return width } // method to get area public double area() { // return area of triangle return (this.base * this.height)/2; } // method to string public String toString() { return "/" + this.area() + "\\"; } }
*************************
geometricShapes.java : //import package import java.util.*; //Java class public class geometricShapes { // entry point of program , main() method public static void main(String[] args) { // create object of Circle class Circle c = new Circle(4.0); // create object of Rectangle class Rectangle rect = new Rectangle(4.0, 10.0); // creating object of Triangle class Triangle t = new Triangle(4.0, 8.0); // print shape of circle System.out.println(c.toString()); // print shape of Triangle System.out.println(t.toString()); // print shape of Rectangle System.out.println(rect.toString()); // print area of circle System.out.printf("Area of circle :%.1f ", c.area()); System.out.println();// used for new line // print area of circle System.out.printf("Area of Rectangle :%.1f ", rect.area()); // print area of Triangle System.out.printf("\nArea of Triangle :%.1f ", t.area()); } }
Here is the complete code . Save public class in different files.
// Point.java
//public class Point {
// instance variables
private int x, y;
// constructor
public Point(int x, int y){
if(x < 0)
x = 0;
if(y < 0)
y = 0;
if(x > 500)
x=500;
if(y > 500)
y= 500;
this.x = x;
this.y = y;
}
//getter methods
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
// method toString
public String toString() {
return "(" + this.x + "," + this.y + ")";
}
}
-----------------------------------------------------------------------------------
//Circle.java //Java class
public class Circle {
// instance fields/data fields
private double radius;
private Point center;
// default constructor
public Circle() {
this.radius = 0.0; // set radius to 0.0
this.center = new Point(0,0);
}
//parameterized constructor
public Circle(int x, int y, double r) {
this.center = new Point(x,y);
this.radius = r;
}
// getter method
public double getRadius() {
return this.radius; // return radius
}
public Point getCenter(){
return this.center;
}
// method to get area
public double area() {
// return area of circle
return Math.PI * this.radius * this.radius;
}
// method toString
public String toString() {
return this.center + ":" + "(" + this.area() + ")";
}
}
-----------------------------------------------------------------------------------
//Rectangle.java : //java class
public class Rectangle {
// instance fields/data fields
private double length;
private double width;
private Point center;
// default constructor
public Rectangle() {
this.length = 0.0;// set length to 0.0
this.width = 0.0;// set width to 0.0
this.center = new Point(0,0);
}
// parameterized constructor
public Rectangle(int x, int y, double l,double w) {
this.length = l;// set length
this.width = w;// set width
this.center = new Point(x,y);
}
// getter method
public double getLength() {
return this.length;// return length
}
public double getWidth() {
return this.width;// return width
}
public Point getCenter(){
return this.center;
}
// method to get area
public double area() { // return area of rectangle
return this.length * this.width;
}
// method to string
public String toString() {
return this.center + ":" + "[" + this.area() + "]";
}
}
-----------------------------------------------------------------------------------
// Triangle.java : //Java class
public class Triangle {
// instance fields/data fields
private double base;
private double height;
private Point center;
// default constructor
public Triangle() {
this.base = 0.0;// set base to 0.0
this.height = 0.0;// set height to 0.0
this.center = new Point(0,0);
}
// parameterized constructor
public Triangle(int x, int y, double b,double h) {
this.base = b;// set base
this.height = h;// set height
this.center = new Point(x,y);
}
// getter method
public double getBase() {
return this.base;// return base
}
public double getHeight() {
return this.height;// return width
}
public Point getCenter(){
return this.center;
}
// method to get area
public double area() { // return area of triangle
return (this.base * this.height)/2;
}
// method to string
public String toString() {
return this.center + ":" + "/" + this.area() + "\\";
}
}
-------------------------------------------------
//geometricShapes.java :
//Java class
//public class geometricShapes {
public class Main {
// entry point of program , main() method
public static void main(String[] args) {
// create object of Circle class
Circle c = new Circle(2,3,4.0);
// create object of Rectangle class
Rectangle rect = new Rectangle(5,5,4.0, 10.0);
// creating object of Triangle class
Triangle t = new Triangle(2,1,4.0, 8.0);
// print shape of circle
System.out.println(c.toString());
// print shape of Triangle
System.out.println(t.toString());
// print shape of Rectangle
System.out.println(rect.toString());
// print area of circle
System.out.printf("Area of circle :%.1f ", c.area());
System.out.println();// used for new line
// print area of circle
System.out.printf("Area of Rectangle :%.1f ", rect.area());
// print area of Triangle
System.out.printf("\nArea of Triangle :%.1f ", t.area());
}
}
Program output is:
(2,3):(50.26548245743669)
(2,1):/16.0\
(5,5):[40.0]
Area of circle :50.3
Area of Rectangle :40.0
Area of Triangle :16.0
Get Answers For Free
Most questions answered within 1 hours.