For my HW assignment we\'re working with composition within Java, and I\'m strug
ID: 3889215 • Letter: F
Question
For my HW assignment we're working with composition within Java, and I'm struggling a lot of with it at the moment. I'll post what the assignment is about and the code following it so anyone reading this can maybe understand where I am at with it. I know this should a concept that should be understood well but I am still not the greatest at programming.
Directions for Java assignment:
Create a Java project named PA1.
A point in the x-y plane is represented by its x-coordinate and y-coordinate. For example, the origin has zero in x-coordinate and zero in y-coordinate. Define a simple class Point to model a point in the x-y plane.
What properties a Point object should contain? When you define the class Point, use instance variable definitions for these properties.
Add one alternate constructor in the class Point, which takes arguments (parameters) to initialize instance variables.
Add queries to return x-coordinate and y-coordinate in the class Point.
Add commands to set x-coordinate and y-coordinate in the class Point.
Add toString() method in the class Point, which returns “(x-coordinate, y-coordinate)", i.e., (0, 0).
Define a driver (test) class called TestPoint to test the class Point. You are asked to do the following things in the main of TestPoint:
Create a Point object for (5, 3);
Print the above Point object.
Using the class Point, define another class Circle to model a circle. A circle should have a center and a radius. You are asked to use composition. The Circle class contains:
A method that returns the center of circle, which should be a Point type
A method that returns circle’s area
A method that moves the circle (’s center) to another position, the method heading is like:
public void move (Point newCenter) {}
A method that resize the circle radius according to the parameter. i.e., when the parameter is 0.5, the method halves the radius; when the parameter is 2.0, the method doubles the radius. The method heading is like:
public void resize (double resizeRate) {}
At least one alternate constructor which takes arguments (parameters) to initialize instance variables.
Define a driver (test) class called TestCircle to test the class Circle. You are asked to do the following things in the main of TestCircle:
Create a circle with origin as its center and radius of 1.0.
Print the circle’s center and area
Move the circle to position (-3, 6)
Shrink the circle’s radius to the half of original radius
Print the circle’s center and area
My code so far:
package assignment1;
public class Point {
//Instance Variables
private double xcoord;
private double ycoord;
//Alternate Constructor
public Point(double x, double y) {
this.xcoord = x;
this.ycoord = y;
}
//Getter for xcoord
public double getxcoord() {
return xcoord;
}
//Getter for ycoord
public double getycoord() {
return ycoord;
}
//Setter for xcoord
public void setXcoord(double x) {
xcoord = x;
}
//Setter for ycoord
public void setYcoord(double y) {
ycoord = y;
}
//toString Method
public String toString() {
return " " + xcoord + "," + ycoord;
}
}
//End of class Point
package assignment1;
public class TestPoint {
public static void main(String[] args) {
//Test for the Point class
Point p1 = new Point (5,3);
//Print the point object
System.out.println("The X and Y coordinates are: " + p1);
}
}
//End of TestPoint class
package assignment1;
public class Circle {
//Instance Variables
private double radius;
private Point center;
//Alternate Constructor
public Circle (double r, double xcoord, double ycoord) {
this.radius = r;
this.center = new Point(xcoord, ycoord);
}
//Getter for Radius
public double getRadius() {
return this.radius;
}
//Getter for Center
public Point getCenter() {
return this.center;
}
//Setter for Radius
public void setRadius(double r) {
this.radius = r;
}
//Setter for Center
public void setCenter(Point point) {
this.center = point;
}
//Method that returns area of Circle
public double getArea() {
return (3.14)*(radius)*(radius);
}
//Method that moves the circle's center to another position
public void move (Point newCenter) {
center.setXcoord(newCenter.getxcoord());
center.setYcoord(newCenter.getycoord());
}
//Method to resize radius according to parameter
public void resize (double resizeRate) {
radius = radius*(resizeRate);
}
//ToString Method
public String toString() {
return "Center:" + center.toString() + "Area: " + getArea();
}
}
//End of Circle Class
package assignment1;
public class TestCircle {
public static void main(String[] args) {
Circle c = new Circle(0,0,1.0);
Point center = c.getCenter();
System.out.println(center);
double area = c.getArea();
System.out.println(area);
Point newCenter = new Point(-3,6);
c.move(newCenter);
c.resize(0.5);
}
}//End of Test Circle
The code is outputting 0.0,1.0 0.0 which i am very sure is incorrect
Explanation / Answer
Point.java
public class Point {
// Instance Variables
private double xcoord;
private double ycoord;
// Alternate Constructor
public Point(double x, double y) {
this.xcoord = x;
this.ycoord = y;
}
// Getter for xcoord
public double getxcoord() {
return xcoord;
}
// Getter for ycoord
public double getycoord() {
return ycoord;
}
// Setter for xcoord
public void setXcoord(double x) {
xcoord = x;
}
// Setter for ycoord
public void setYcoord(double y) {
ycoord = y;
}
// toString Method
public String toString() {
return "[" + xcoord + "," + ycoord+"]";
}
}
// End of class Point
_______________
TestPoint.java
public class TestPoint {
public static void main(String[] args) {
Point p1=new Point(5, 3);
//Print the point object
System.out.println("Point is : " + p1.toString());
}
}
__________________
Circle.java
import java.text.DecimalFormat;
public class Circle {
private Point center;
private double radius;
// Alternate Constructor
public Circle(double r, Point center) {
this.radius = r;
this.center = center;
}
// Getter for Radius
public double getRadius() {
return this.radius;
}
// Getter for Center
public Point getCenter() {
return this.center;
}
// Setter for Radius
public void setRadius(double r) {
this.radius = r;
}
// Setter for Center
public void setCenter(Point point) {
this.center = point;
}
// Method that returns area of Circle
public double getArea() {
return (3.14159) * (radius) * (radius);
}
// Method that moves the circle's center to another position
public void move(Point newCenter) {
center.setXcoord(newCenter.getxcoord());
center.setYcoord(newCenter.getycoord());
}
// Method to resize radius according to parameter
public void resize(double resizeRate) {
radius = radius * (resizeRate);
}
// ToString Method
public String toString() {
//DecimalFormat class is used to format the output
DecimalFormat df = new DecimalFormat("#.##");
return "Center:" + center.toString() + " Area: " + df.format(getArea());
}
}
____________________
TestCircle.java
public class TestCircle {
public static void main(String[] args) {
//Creating an Point class object by passing the coordinates as input
Point p1 = new Point(0, 0);
//Creating an Circle class object by passing the radius and Point as input
Circle c = new Circle(1.0, p1);
//Displaying the center and area of the circle
System.out.println(c.toString());
//Creating an Point class object by passing the coordinates as input
Point newCenter = new Point(-3, 6);
//Moving the circle to new center
c.move(newCenter);
//changing the radius
c.resize(0.5);
//Creating an Point class object by passing the coordinates as input
System.out.println(c.toString());
}
}
______________________
Output:
Center:[0.0,0.0] Area: 3.14
Center:[-3.0,6.0] Area: 0.79
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.