Question
The primary focus of this assignment is the use of interfaces. In programming, an interface is a public avenue by which data can be accessed
The primary focus of this assignment is the use of interfaces. In programming, an interface is a public avenue by which data can be accessed and/or manipulated. In essence, all Java classes contain an interface-- that is, they declare at least some public methods that can be called by other objects. If a class was designed in such a way that it provided no public interface, the class would be completely useless. For example, an object of a class cannot be instantiated without a public constructor; additionally, non-public instance variables defined within a class would have no purpose if no methods were furnished to manipulate or (at least) access those variables. Interfaces are essential; they provide the means by which objects may communicate with each other and perform tasks. An object that has no interface is deaf, dumb, and blind --it cannot receive data, return data, or interact with other objects in any way. For an object to be useful, it must implement an interface--that is, it must contain public methods that can be called by other objects. For this reason and others, the Java programming language provides the interface declaration. In Java, an interface declaration is very similar to the declaration of a class, except that an interface declaration is completely abstract and public. Interfaces declare methods with specific names and arguments but do not define the bodies of those methods. Interfaces also do not declare instance variables or contain any kind of private declarations. A Java interface is like a silhouette--an outline of a class that describes what the class can do but not how it works. Once an interface is declared, any number of classes can implement the interface in order to signal that they are capable of performing specific tasks. Files to be submitted: 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 a 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:(7.5 points) 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 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:(7.5 points) 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 implements 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:(20 points) 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: public 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:(20 points) 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: private 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:(20 points) 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(25 points): 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: Create an instance of Circle, called c1, with a radius of 5. (2.5 points) Create an instance of a Point, called p1, with a side length of 5. (2.5 points) Create an instance of a Point, called p2, with a side length of 5 and a side height of 7.(2.5 points) Create an instance of a Square, called s1, with a parameter p1.(2.5 points) Create an instance of Rectangle, called r1, with a parameter p2.(2.5 points) Print the radius of c1.(1.25 points) Print the area of c1.(1.25 points) Print the perimeter of c1.(1.25 points) Print the side length of s1.(1.25 points) Print the area of s1.(1.25 points) Print the perimeter of s1.(1.25 points) Print the width of r1.(1.25 points) Print the height of r1.(1.25 points) Print the area of r1.(1.25 points) Print the perimeter of r1.(1.25 points)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started