Question

In this assignment, you will be building upon the work that you did in Lab #5A...

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

Homework Answers

Answer #1

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

Know the answer?
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
Similar Questions
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following...
Write a Java class called Rectangle that represents a rectangular two-dimensional region. It should have following 4 fields: int x (x-coordinate for its top left corner) int y (y coordinate for its top left corner) double height (height of the rectangle) double width (width of the rectangle) Your rectangle objects should have the following methods: public Rectangle(int newx, int newy, int newwidth, int newheight) Constructor that initializes the x-coordinate, y-coordinate for the top left corner and its width and height....
Write an interface Shape with methods getShapeName and getPerimeter. Write classes called Square, Rectangle, Circle and...
Write an interface Shape with methods getShapeName and getPerimeter. Write classes called Square, Rectangle, Circle and Triangle that implement the interface. Supply the proper parameters to the Square, Rectangle, Triangle and Circle constructors such that the perimeter can be calculated. Run THRou JDK compiler please Sample code: //Save this in Shape.java public interface Shape{ public double getPerimeter(); public String getShapeName(); } //Save this in Triangle.java public class Triangle implements Shape{ private double side1; private double side2; Triangle(double length1, double length2,...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its...
Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass: public class Employee9C {    //declaring instance variables private String firstName; private String lastName; //declaring & initializing static int variable to keep running total of the number of paychecks calculated static int counter = 0;    //constructor to set instance variables public Employee9C(String...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a class named Triangle that extends GeometricObject. The class contains: Three double fields named side1, side2, and side3 with default values = 1.0. These denote the three sides of the triangle A no-arg constructor that creates a default triangle A constructor that creates a triangle with parameters specified for side1, side2, and side3. An accessor method for each side. (No mutator methods for the sides)...
Hello. I have an assignment that is completed minus one thing, I can't get the resize...
Hello. I have an assignment that is completed minus one thing, I can't get the resize method in Circle class to actually resize the circle in my TestCircle claass. Can someone look and fix it? I would appreciate it! If you dont mind leaving acomment either in the answer or just // in the code on what I'm doing wrong to cause it to not work. That's all I've got. Just a simple revision and edit to make the resize...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
(The Rectangle class) (WOULD APPRECIATE IT IF THE PROGRAM/ANSWER COULD BE DIRECTLY COPY AND PASTED, also...
(The Rectangle class) (WOULD APPRECIATE IT IF THE PROGRAM/ANSWER COULD BE DIRECTLY COPY AND PASTED, also this should be in java) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. - A no-arg constructor that creates a default rectangle....
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...