Question

In: Computer Science

The assignment will be graded both running and reading your code. Readability: Your code should be...

The assignment will be graded both running and reading your code.

Readability: Your code should be readable. Add comments wherever is necessary.
If needed, write helper functions to break the code into small, readable chunks.

Write two concrete classes implementing GeometricShape

The class Circle. Circle will have the following public methods:

// a constructor
Circle(Point center, int radius);

// returns the area of the circle
float getArea();

// returns the perimeter of the circle
float getPerimeter();

// prints the center of the circle and its radius
virtual void print();

The class Rectangle. Rectangle will have the following public methods:

// a constructor
Rectangle(Point topLeftPoint, int length, int width);

// returns the area of the rectangle
float getArea();

// returns the perimeter of the circle
float getPerimeter();

// prints the top left point of the rectangle, its length and width
virtual void print();

Write a function test() that tests your solutions.

Solutions

Expert Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Point
{
private int x;
private int y;

public Point()
{
this.x = 0;
this.y = 0;
}
public Point(int x,int y)
{
this.x = x;
this.y = y;
}

public int getX()
{
return this.x;
}
public int getY()
{
return this.y;
}
public void setX(int x)
{
this.x = x;
}
public void setY(int y)
{
this.y = y;
}
public String toString()
{
return "Point("+getX() +","+getY()+")";
}

};
class Rectangle extends Point
{
private Point p;
private double length;
private double width;

public Rectangle()
{
p.setX(0);
p.setY(0);
length = 0;
width =0;
}
public Rectangle(Point p,double length,double width)
{
this.p = p;
this.length = length;
this.width = width;
}
public double getWidth()
{
return width;

}
public double getLength()
{

return length;
}
public Point getPoint()
{
return this.p;
}
public double getArea() //compute the area
of the rectangle.
{
double area;
area = getWidth() * getLength();
return area;
}
public double getPerimeter() //compute the
perimeter of the rectangle
{
double perimeter;
perimeter = 2*(getWidth()+getLength());
return perimeter;
}
public String toString()
{
return "Rectangle : Top left Point :"+ getPoint() +" width
:"+getWidth() + " height :"+getLength();
}
}

class Circle extends Point
{
private Point p;
private double radius;


public Circle()
{
p.setX(0);
p.setY(0);
radius =0;
}
public Circle(Point p,double radius)
{
this.p = p;
this.radius = radius;
}
public double getRadius()
{
return radius;

}

public Point getPoint()
{
return this.p;
}
public double getArea() //compute the area
of the circle.
{
double area;
area = 3.14 * getRadius() * getRadius();
return area;
}
public double getPerimeter() //compute the
perimeter of the circle
{
double perimeter;
perimeter = 2* 3.14 *getRadius();
return perimeter;
}
public String toString()
{
return "Circle : center Point:"+ getPoint() +" radius "+getRadius();
}
}

class Square extends Point
{
private Point p;
private double length;


public Square()
{
p.setX(0);
p.setY(0);
length = 0;

}
public Square(Point p,double length)
{
this.p = p;
this.length = length;
}

public double getLength()
{

return length;
}
public Point getPoint()
{
return this.p;
}
public void setLength(double length)
{
this.length = length;
}
public double getArea() //compute the area
of the square.
{
double area;
area = getLength() * getLength();
return area;
}
public double getPerimeter() //compute the
perimeter of the square
{
double perimeter;
perimeter = 4*(getLength());
return perimeter;
}
public String toString()
{
return "Square : Top left Point :"+ getPoint() +" length
:"+getLength() ;
}
}


class GeometricTest
{
public static void main (String[] args)
{
Point pc = new Point(7,3);
Circle c = new Circle(pc,4.5);

System.out.println(c.toString());
System.out.println("Area of circle = "+c.getArea());
System.out.println("Perimeter of circle = "+c.getPerimeter());


Point pr = new Point(3,-1);
Rectangle r = new Rectangle(pr,4.0,6.0);

System.out.println(r.toString());
System.out.println("Area of rectangle = "+r.getArea());
System.out.println("Perimeter of rectangle = "+r.getPerimeter());


Point ps = new Point(5,8);
Square s = new Square(ps,2.0);

s.setLength(5.0);
System.out.println(s.toString());
System.out.println("Area of square = "+s.getArea());
System.out.println("Perimeter of square = "+s.getPerimeter());


Point p1 = r.getPoint();
Point p2 = s.getPoint();

if(p1.getX() >p2.getX())
System.out.println("x-coordinate of rectangle is greater than
x-coordinate of square");
else
System.out.println("x-coordinate of square is greater than
x-coordinate of rectangle");
}


}

output:

Circle : center Point:Point(7,3) radius 4.5
Area of circle = 63.585
Perimeter of circle = 28.26
Rectangle : Top left Point :Point(3,-1) width :6.0 height :4.0
Area of rectangle = 24.0
Perimeter of rectangle = 20.0
Square : Top left Point :Point(5,8) length :5.0
Area of square = 25.0
Perimeter of square = 20.0
x-coordinate of square is greater than x-coordinate of rectangle


Related Solutions

Computing Assignment 1 You must upload both your code (to Assignment 1 scripts/codes) and your report...
Computing Assignment 1 You must upload both your code (to Assignment 1 scripts/codes) and your report (to Assignment 1 computing report). The assignment is due at 11:00pm. I have set the due time in Canvas to 11:05pm and if Canvas indicates that you submitted late, you will be given 0 on the assignment. Your computing report must be exactly 1 page. There will be a penalty given if your report is longer than one page. • Please read the Guidelines...
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should...
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should only include the function or the script files that generate the results. The Get-ChildItem cmdlet is very useful in obtaining files and directory information. Your challenge is to create a PowerShell function that displays all ‘.exe’ and ‘.dll’ files stored under C:\Windows\System32, whose size is more than 5 MB. Group the files based on their extension and store the list into a text file.
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should...
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should only include the function or the script files that generate the results. For this question, your challenge is to create a PowerShell function that accesses all the log files stored under C:\Windows\System32\winevt\Logs. The function should display only those event logs whose size is more than 1MB. Sort the result by LastWriteTime in descending order.
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should...
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should only include the function or the script files that generate the results. For this question, your challenge is to create a PowerShell function that accesses all the log files stored under C:\Windows\System32\winevt\Logs. The function should display only those event logs whose size is more than 1MB. Sort the result by LastWriteTime in descending order.
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should...
For this assignment, you will need any computer running PowerShell v5 or later. Your answer should only include the function or the script files that generate the results. The Get-ChildItem cmdlet is very useful in obtaining files and directory information. Your challenge is to create a PowerShell function that displays all ‘.exe’ and ‘.dll’ files stored under C:\Windows\System32, whose size is more than 5 MB. Group the files based on their extension and store the list into a text file.
I am struggling with this java code Objectives: Your program will be graded according to the...
I am struggling with this java code Objectives: Your program will be graded according to the rubric below. Please review each objective before submitting your work so you don’t lose points. 1. Create a new project and class in Eclipse and add the main method. (10 points) 2. Construct a Scanner to read input from the keyboard. (10 points) 3. Use the Scanner to read the name, number of balls, and yards per ball of a yarn type specified by...
I am struggling with this assignment for my java class. Objectives: Your program will be graded...
I am struggling with this assignment for my java class. Objectives: Your program will be graded according to the rubric below. Please review each objective before submitting your work so you don’t lose points. 1.Create a new project and class in Eclipse and add the main method. (5 points) 2. Construct a Scanner to read input from the keyboard. (5 points) 3. Prompt the user with three questions from the Psychology Today quiz, and use the Scanner to read their...
HA 440                  GRADED Assignment # 2               Name _____________________________ Compl
HA 440                  GRADED Assignment # 2               Name _____________________________ Completion Complete each statement.             1.   Expenses are ________________________ costs that have been __________________________ while doing business             2.   Three categories of healthcare expenses are ______________________________, ________________________________ and Operations Expenses             3.   A program can be defined as a __________________________ that has its own objectives             4.   _____________________ costs can be specifically associated with a particular unit, department or patient             5.   A Responsibility Center makes a manager responsible for both the...
This question is based on the Anello et al. reading and will be graded later. Briefly...
This question is based on the Anello et al. reading and will be graded later. Briefly explain one projected benefit of applying Evolutionary Medicine to disease, or teaching about Evolutionary Medicine in Life Science courses.
In the assignment below please fix the errors in each code. The first code should have...
In the assignment below please fix the errors in each code. The first code should have a total of 3 erros. The second and third code should have 4 errors. Thanks //DEBUG05-01 // This program is supposed to display every fifth year // starting with 2017; that is, 2017, 2022, 2027, 2032, // and so on, for 30 years. // There are 3 errors you should find. start    Declarations       num year       num START_YEAR = 2017       num...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT