Java code
Problem 1.
Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates.
Tip: Keep x and y separate in the calculation.
Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points) of a rectangle that encloses the shape (a bounding box). These will be overloaded by the derived classes; therefore, the default methods for Shape will only print a simple message to standard output.
Create a display() function for Shape, which will display the name of the class and all stored information about the class (including area, circumference and bounding box).
Build the hierarchy by creating the Shape classes Circle, Rectangle and Triangle. Search the Internet for the rules governing these shapes, if necessary.
For these three Shape classes, create default constructors, as well as constructors whose arguments will initialize the shapes appropriately using the correct number of Point objects (i.e., Circle requires a Point center and a radius; Rectangle requires four Point vertices; and Triangle requires three Point vertices). Add error-checking to the constructors, such that they print a warning if the arguments do not conform to the appropriate shape.
Tip: Not all four-sided shapes are rectangles.
Also add a check to Rectangle, such that it tests for the special case of a “square” and prints an appropriate message if the test is true.
In main(), create several instances of each shape object and display the information for each object. Be sure to create at least one non-rectangle shape to demonstrate your error handling, and at least one square.
I have implemented the Shape, Point, Rectangle,Circle, Triangle class as described in the Task and MyShapeDemo.java to test the classes.Please find the Following Code screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT:
2.OUTPUT:
3.CODE:
//to create a point class with the variables and methods
mentioned in the task class Point{ int x,y; public Point(int x,int y){ this.x=x; this.y=y; } public void show(){ System.out.println("(x,y)=("+x+","+y+")"); } public Point add(Point p){ return new Point(x+p.x,y+p.y); } public Point subtract(Point p){ return new Point(x-p.x,y-p.y); } } //To Create a shape class with dummy methods will be overidden by deriven classes class Shape{ public double area(){ System.out.println("Area Method..."); return 0; } public double circumference(){ System.out.println("Circumference Method..."); return 0; } public void display() { System.out.println("Display Method..."); } } //Create a circle class with point p and radius class Circle extends Shape{ Point p; double radius; public Circle(int x,int y,double r){ p=new Point(x,y); radius=r; } public Circle(Point p,double r){ this.p=p; radius=r; } public double area(){ return 3.141*radius*radius; } public double circumference(){ return 2*3.141*radius; } public void display() { System.out.println("Circle :\nArea="+area()+"\nCircumference="+circumference()); } } //To Create a Triange class` class Triangle extends Shape{ Point p1,p2,p3; double s1,s2,s3; public Triangle(Point p1,Point p2,Point p3){ this.p1=p1; this.p2=p2; this.p3=p3; calSides(); } //calcualte the sidesof the triangle public void calSides(){ s1=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); s2=Math.sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y)); s3=Math.sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y)); if(!((s1+s2>s3)&&(s1+s3>s2)&&(s2+s3>s1))){ System.out.println("Invalid Point to Construct a triangle"); s1=s2=s3=0; } } //to calcluate the area public double area(){ double s = (s1 + s2+ s3)/2,area; area = Math.sqrt(s*(s-s1)*(s-s2)*(s-s3)); return area; } public double circumference(){ return s1+s2+s3; } public void display() { System.out.println("Triange :\nArea="+area()+"\nCircumference="+circumference()); } } //To create a Rectangle Class class Rectangle extends Shape{ Point p1,p2,p3,p4; double length,width; public Rectangle(Point p1,Point p2,Point p3,Point p4){ this.p1=p1; this.p2=p2; this.p3=p3; this.p4=p4; calSides(); } //to Validate the rectangel or not public void calSides(){ double ab,bc,cd,da; ab=Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); bc=Math.sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y)); cd=Math.sqrt((p3.x-p4.x)*(p3.x-p4.x)+(p3.y-p4.y)*(p3.y-p4.y)); da=Math.sqrt((p1.x-p4.x)*(p1.x-p4.x)+(p1.y-p4.y)*(p1.y-p4.y)); if((ab==cd)&&(bc==da)){ length=ab; width=bc; System.out.println("Lenght="+length+"\tWidth ="+width); }else{ System.out.println("Invalid Input for Rectangle..."); length=0; width=0; } } public double area(){ return length*width; } public double circumference(){ return length+width; } public void display() { if(length==width&&length!=0) System.out.println("Square :\nArea="+area()+"\nCircumference="+circumference()); else if(length!=0) System.out.println("Rectangle :\nArea="+area()+"\nCircumference="+circumference()); } } public class MyShapeDemo{ public static void main(String args[]){ /*To demonstrate a Square */ Point p1=new Point(10,10); Point p2=new Point(10,20); Point p3=new Point(20,20); Point p4=new Point(20,10); Rectangle r=new Rectangle(p1,p2,p3,p4); r.display(); /*To demonstrate invalid rectangle since p1 is the fourth point */ Rectangle r1=new Rectangle(p1,p2,p3,p1); /*To deomstrate a triangel */ Triangle t=new Triangle(p1,p2,p3); t.display(); /*To Demostrate a Circle*/ Circle c=new Circle(p1,3); c.display(); } } |
Get Answers For Free
Most questions answered within 1 hours.