Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The primary focus ofthis assignmentis the use ofinterfaces.In programming, an interface is a publicavenue by which data can be accessed and/or manipulated. five Java classes

The primary focus ofthis assignmentis the use ofinterfaces.In programming, an interface is a publicavenue by which data can be accessed and/or manipulated.

five Java classes calledPoint,Circle,Square, Rectangle,andTestAll, as well as a Java interface calledFigureGeometry.TheTestAllclass should implementthemainmethod which tests all of the other files

Point.javaDescription:

ThePointclass should be declared as apublic classand should meet all therequirements listed below. Its purpose is to store the Point (width and height) of a two-dimensional,rectangular geometric figure.

Instance Variables:

privateintwidth;//stores the width of a Point objectprivate

private intheight;//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:

publicintgetWidth(){//returns the width of a Point object in the following manner

return width;}

publicintgetHeight(){//returns the height of a Pointobject in the following manner:

return height;}

publicvoidsetWidth(int theWidth){//assigns the width of a Pointobjectas follows:

width = theWidth;}

publicvoidsetHeight(int theHeight{//assigns the height of a Pointobject as follows:

height = theHeight;

}

FigureGeometry.javaDescription:

TheFigureGeometryinterface should be declared as apublic interfaceand shouldmeet all the requirements listed below. Its purpose is to declare all the necessary methods that anygeometric figure, such as a circle, rectangle, or square, should contain. TheFigureGeometryinterfaceshould also declare a numeric constant, calledPI, which can be used by classes that implement the FigureGeometryinterface.Remember that method declarations in an interface should not includemodifiers such aspublic,static, orabstract.

Declaring a method within an interface asstaticisillegal and will cause a compilation error. Additionally, the declaration of interface methodsusingpublicorabstractmodifiers is redundant, and soon such declarations will be deprecated.Students should also note that the inclusion of instance variables within an interface declaration is notallowed; only static constants may be defined within an interface declaration,and the use ofthestaticmodifier on constants is also redundant. Basically, students should remember one generalrule of thumb concerning interface declarations: Only one modifier should be used in an interfacedeclaration--finalshould be used to declare constants.

publicinterfaceFigureGeometry{finalfloatPI =3.14f;

//Classes that implement the FigureGeometry interface MUST override thismethod 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.

floatgetArea ();

//Classes that implement the FigureGeometry interface MUSTalso overridethis method which should return thegeometric perimeter of a //figure:

float getPerimeter ();}

Circle.javaDescription:

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:

privatefloatradius;//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:

publicfloatgetRadius (){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:

TheRectangleclass should be declared as apublicclassthatimplementstheFigureGeometryinterface describedabove. Its purpose is to store thePoint of a rectangular figure (using thePointclass described above) and provide the methods necessaryto calculate the area and perimeter of such a figure.

Instance Variables:

privatePointpoint;//stores thepoint of the Rectangleobject

Constructor:Rectangle()

Parameters:

Point p;

Purpose: initializes the Point of aRectangleobject in the following manner:

point = p;

Methods:

1-publicintgetWidth()

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

return point.getWidth();}

2-publicintgetHeight()

{//returns the height of a Rectangle objectas follows:

return point.getHeight();}

3-publicfloatgetArea()

{//returns the area of a Rectangleobject as follows:

returngetWidth() *getHeight();}

4-publicfloatgetPerimeter()

{//returns the perimeter of a Rectangleobject as follows:

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

5-publicvoidsetPoint(Point p)

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

point= p;}

TestAll.javaDescription:

TheTestAllclass should be declared as apublic classand should meet all therequirements listed below. Its purpose is to implement amainmethod which makes three objects--aCircleobject, aSquareobject, and aRectangleobject--and test each of the filesthat have beendesigned above.Youshould 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 notbe provided. Youmay organize the output of data according to their own specifications. However,themainmethodmustperform the following tasks:

  1. an instance ofCircle, calledc1, with a radius of5.
  2. an instance of a Point, called p1, with a side length of 5.
  3. an instance of a Point, called p2, with a side length of 5 and a side height of 7.
  4. an instance of a Square, called s1, with a parameter p1.
  5. an instance ofRectangle, called r1, with a parameter p2.
  6. Print the radius ofc1.
  7. Print the area ofc1.
  8. Print the perimeter ofc1.
  9. Print the side length ofs1.
  10. Print the area ofs1.
  11. Print the perimeter ofs1,
  12. Print the width ofr1.
  13. Print the height ofr1.
  14. Print the area ofr1.
  15. Print the perimeter ofr1.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database System Concepts

Authors: Henry F. Korth, S. Sudarshan

4th Edition

978-0073523323, 0-07-255481-9, 73523321, 978-0072554816

More Books

Students also viewed these Programming questions

Question

Verify the formula given for the Pi of the M/M/k.

Answered: 1 week ago