Question

In: Computer Science

In this assignment, students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll,...

In this assignment, students should create five Java classes called Point, Circle, Square, Rectangle, and TestAll, as well as a Java interface called FigureGeometry. The TestAll class should implement the main method which tests all of the other files created in the assignment. After the assignment has been completed, all six files should be submitted for grading into the Dropbox for Assignment 3, which can be found in the Dropbox menu on the course website. Students should note that only the *.java file for each class /interface needs to be submitted; No *.class files need to be submitted into the Dropbox. Please see below specific requirements for the files that must be created and submitted:

Point.java Description:

The Point class should be declared as a public class and should meet all the requirements listed below. Its purpose is to store the Point (width and height) of a two-dimensional, rectangular geometric figure.

Instance Variables:

private int width;//stores the width of a Point object private

private int height;//stores the height of a Point object

Constructor: Point()

Parameters:

int theWidth,

int theHeight

Purpose: initializes the width and height of a Point object in the following manner:

width = theWidth;

height = theHeight;

Methods:

public int getWidth(){//returns the width of a Point object in the following manner

return width;}

public int getHeight(){//returns the height of a Point object in the following manner:

return height;}

public void setWidth(int theWidth){//assigns the width of a Point object as follows:

width = theWidth;}

public void setHeight(int theHeight{//assigns the height of a Point object as follows:

height = theHeight;

}

FigureGeometry.java Description:

The FigureGeometry interface should be declared as a public interface and should meet all the requirements listed below. Its purpose is to declare all the necessary methods that any geometric figure, such as a circle, rectangle, or square, should contain. The FigureGeometry interface should also declare a numeric constant, called PI, which can be used by classes that implement the FigureGeometry interface. Remember that method declarations in an interface should not include modifiers such as public, static, or abstract.

Declaring a method within an interface as static is illegal and will cause a compilation error. Additionally, the declaration of interface methods using public or abstract modifiers is redundant, and soon such declarations will be deprecated. Students should also note that the inclusion of instance variables within an interface declaration is not allowed; only static constants may be defined within an interface declaration, and the use of the static modifier on constants is also redundant. Basically, students should remember one general rule of thumb concerning interface declarations: Only one modifier should be used in an interface declaration--final should be used to declare constants.

public interface FigureGeometry{final float PI = 3.14f;

//Classes that implement the FigureGeometry interface MUST override this method which should return the geometric area of a figure:

//In an interface, methods are always public and abstract. Using these unnecessary modifiers is redundant, and future versions of

//Java may not support them.

float getArea ();

//Classes that implement the FigureGeometry interface MUST also override this method which should return the geometric perimeter of a //figure:

float getPerimeter ();}

Circle.java Description:

The Circle class should be declared as a public class that implements the FigureGeometry interface described above. Its purpose is to store the radius of a circular figure and provide the methods necessary to calculate the area and perimeter of such a figure.

Instance Variables:

private float radius;//stores the radius of a Circle object

Constructor: Circle()

Parameters:

float theRadius;

Purpose:initializes the radius of a Circle in the following manner:

radius = theRadius;

Methods:

1-public float getRadius(){//returns the radius of a Circle object as follows:

return radius;

}

2-public float getArea(){//returns the area of a Circle object as follows:

return getRadius() * getRadius() * PI;

3-public float getPerimeter(){//returns the perimeter of a Circle object as follows:

return getRadius() * 2 * PI;

}

4-public void setRadius(float theRadius){//assigns the radius of a Circle object as follows:

radius = theRadius;

}

The following coding example illustrates a version of the Circle class:

public class Circle implements FigureGeometry{//Stores the radius of this figure:

private float radius;

//Returns the radius of this figure:

public float getRadius (){return radius;}

//Returns the area of this figure:

public float getArea (){

return getRadius() * getRadius() * PI;}

//Returns the perimeter of this figure:

public float getPerimeter (){ return getRadius() * 2 * PI;}

//Assigns the radius of this figure:

public void setRadius (float theRadius){radius = theRadius;

}

}

Square.java Description:

The Square class should be declared as a public class that implements the FigureGeometry interface and should meet all the requirements listed below. Its purpose is to store the Point of a square figure (using the Point class described above and provide the methods necessary to calculate the area and perimeter of such a figure.

Instance Variables:

private Point point; //stores the Point of a Square object

Constructor: Square()

Parameters:

Point p;

Purpose:initializes the object point of the Square object in the following manner:

point= p;

Methods:

1-public float getSideLength(){//returns the length of the side of the square as follows:

return point.getWidth();}

2-public float getArea(){//returns the area of a Square object as follows:

return getSideLength() *getSideLength();}

3-public float getPerimeter(){//returns the perimeter of a Square object as follows:

return getSideLength() * 4;}

4-public void setPoint(Point p){//assigns the point of a Square object as follows:

point= p;}

Rectangle.java Description:

The Rectangle class should be declared as a public class that implements the FigureGeometry interface described above. Its purpose is to store the Point of a rectangular figure (using the Point class described above) and provide the methods necessary to calculate the area and perimeter of such a figure.

Instance Variables:

private Point point;//stores the point of the Rectangle object

Constructor: Rectangle()

Parameters:

Point p;

Purpose: initializes the Point of a Rectangle object in the following manner:

point = p;

Methods:

1-public int getWidth()

{//returns the width of a Rectangle object as follows:

return point.getWidth();}

2-public int getHeight()

{//returns the height of a Rectangle object as follows:

return point.getHeight();}

3-public float getArea()

{//returns the area of a Rectangle object as follows:

return getWidth() * getHeight ();}

4-public float getPerimeter()

{//returns the perimeter of a Rectangle object as follows:

return (getWidth+getHeight()) * 2;}

5-public void setPoint(Point p)

{//assigns the point of a Rectangle object as follows:

point = p;}

TestAll.java Description:

The TestAll class should be declared as a public class and should meet all the requirements listed below. Its purpose is to implement a main method which creates three objects--a Circle object, a Square object, and a Rectangle object-- and test each of the files that have been designed above. You should already be familiar with how to instantiate objects and print values to the screen using System.out.println(...). Therefore, the actual implementation code for this assignment will not be provided. You may organize the output of data according to their own specifications. However, the main method must perform the following tasks:

  1. Create an instance of Circle, called c1, with a radius of 5.
  2. Create an instance of a Point, called p1, with a side length of 5.
  3. Create an instance of a Point, called p2, with a side length of 5 and a side height of 7.
  4. Create an instance of a Square, called s1, with a parameter p1.
  5. Create an instance of a Rectangle, called r1, with a parameter p2.
  6. Print the radius of c1.
  7. Print the area of c1.
  8. Print the perimeter of c1.
  9. Print the side length of s1.
  10. Print the area of s1.
  11. Print the perimeter of s1.
  12. Print the width of r1.
  13. Print the height of r1.
  14. Print the area of r1.
  15. Print the perimeter of r1.

Solutions

Expert Solution

Point.java


public class Point {
   private int width;
   private int height;
   public Point(int width, int height) {
       super();
       this.width = width;
       this.height = height;
   }
   public int getWidth() {
       return width;
   }
   public void setWidth(int width) {
       this.width = width;
   }
   public int getHeight() {
       return height;
   }
   public void setHeight(int height) {
       this.height = height;
   }
  

}

/////

FigureGeometry.java


public interface FigureGeometry {
   final float pi=3.14f;
  
   float getArea();
   float getParameter();
  

}

/////

Circle.java


public class Circle implements FigureGeometry {
   private float radius;

   public float getRadius() {
       return radius;
   }

   public void setRadius(float radius) {
       this.radius = radius;
   }

   public Circle(float radius) {
       super();
       this.radius = radius;
   }

   @Override
   public float getArea() {
  
       return getRadius()*getRadius()*pi;
   }

   @Override
   public float getParameter() {
  
       return getRadius()*2*pi;
   }
  

}

/////

Square.java


public class Square implements FigureGeometry {
   private Point point;

   public Square(Point point) {
       super();
       this.point = point;
   }
  
   public float getSideLength(){

       return point.getWidth();
       }
  

   @Override
   public float getArea(){

       return getSideLength() *getSideLength();
       }
  


   @Override
   public float getParameter() {
      
       return getSideLength() * 4;
   }
  
  
   public void setPoint(Point p){
      

       point= p;
       }

}

////

Rectangle.java


public class Rectangle implements FigureGeometry {
  
   private Point point;

   public Rectangle(Point point) {
       super();
       this.point = point;
   }

   public Point getPoint() {
       return point;
   }

   public void setPoint(Point point) {
       this.point = point;
   }
  
  
   public int getWidth()

   {
   return point.getWidth();
   }
  
   public int getHeight()

   {

   return point.getHeight();
   }
  
   @Override
   public float getArea()

   {

   return getWidth() * getHeight ();
   }

   @Override
   public float getParameter() {
       // TODO Auto-generated method stub
       return (getWidth()+getHeight()) * 2;
   }
  

  

}

/////

Testall.java


public class Testall {
  
   public static void main(String[] args) {
       Circle c1=new Circle(5);
       Point p1=new Point(5,7);
       Point p2=new Point(5,7);
       Square s1=new Square(p1);
       Rectangle r1=new Rectangle(p2);
      
      
      
       System.out.println(c1.getRadius());
       System.out.println(c1.getArea());
       System.out.println(c1.getParameter());
       System.out.println(s1.getSideLength());
       System.out.println(s1.getArea());
       System.out.println(s1.getParameter());
       System.out.println(r1.getWidth());
       System.out.println(r1.getHeight());
       System.out.println(r1.getParameter());
       System.out.println(r1.getArea());
      
      
      
   }

}

THank YOU!!!!


Related Solutions

Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Write a java class called circle that represents a circle. It should have following three fields:...
Write a java class called circle that represents a circle. It should have following three fields: int x (x-coordinate), int y (y-coordinate), double radius (radius of the circle). Your circle object should have following methods: public int getRadius () public int getX () public int getY () public double area () public double perimeter() public String toString() write a client program called CircleClient that creates objects of the circle class called c1 and c2. Assign values to the fields when...
This is in Java 1. Create an application called registrar that has the following classes: a....
This is in Java 1. Create an application called registrar that has the following classes: a. A student class that minimally stores the following data fields for a student:  Name  Student id number  Number of credits  Total grade points earned             And this class should also be provides the following methods:  A constructor that initializes the name and id fields  A method that returns the student name field  A method that returns the student...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
For this assignment, you will create a hierarchy of five classes to describe various elements of...
For this assignment, you will create a hierarchy of five classes to describe various elements of a school setting. The classes you will write are: Person, Student,Teacher, HighSchoolStudent, and School. Detailed below are the requirements for the variables and methods of each class. You may need to add a few additional variables and/or methods; figuring out what is needed is part of your task with this assignment. Person Variables: String firstName - Holds the person's first name String lastName -...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The...
Write a program to calculate the area of four shapes (Rectangle, triangle, circle and square). The program to present the user with a menu where one of the shapes can be selected. Based on the selection made, the user enters the proper input, the program validates the input (i.e all entries must be greater than zero). Once the input is entered and validated, the intended area is calculated and the entered information along with the area are displayed. Area of...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This...
C++ HW Aim of the assignment is to write classes. Create a class called Student. This class should contain information of a single student. last name, first name, credits, gpa, date of birth, matriculation date, ** you need accessor and mutator functions. You need a constructor that initializes a student by accepting all parameters. You need a default constructor that initializes everything to default values. write the entire program.
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT