Question

Java code Problem 1. Create a Point class to hold x and y values for a...

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.

Homework Answers

Answer #1

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();
   }
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN C++ - most of this is done it's just missing the bolded part... Write a...
IN C++ - most of this is done it's just missing the bolded part... Write a program that creates a class hierarchy for simple geometry. Start with a Point class to hold x and y values of a point. Overload the << operator to print point values, and the + and – operators to add and subtract point coordinates (Hint: keep x and y separate in the calculation). Create a pure abstract base class Shape, which will form the basis...
To gain coding XP, complete this coding challenge by adding constructors to the Point class you...
To gain coding XP, complete this coding challenge by adding constructors to the Point class you completed for the previous challenge.Point should have three constructors:One that takes no arguments and does nothing (the default constructor).One that takes the x- and y-coordinates for the point to create as arguments. The x- and y-coordinates of a point should be non-negative. If the arguments provided are negative values, use 0 instead as the construction value.A copy constructor
A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class,...
A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, “pointType”, that can store and process a point in the x-y plane. You should then perform operations on the point, such as setting the coordinates of the point, printing the coordinates of the point, returning thex-coordinate, and returning the y-coordinate. Also, write a program to testvarious operations on the point. x-y plane and you designed the class to capture the properties of a point...
JAVA. The question mentioned was: Create a class called Point that represents a point in the...
JAVA. The question mentioned was: Create a class called Point that represents a point in the cartesian coordinate system. The class should have fields representing the x coordinate and y coordinate (both are integer type). Question I need an answer to: Write a client class that would create two objects in the Point class above called p1 and p2 and assigns values to the filed in these objects. Print out the x coordinate followed by x comma and last coordinate...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers....
PHP calculator problem Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. It doesn't need an HTML form, all the arguments are put in through the method. The class must check for a correct operator (+,*,-,/), and a...
   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public...
   import javax.swing.*; import java.awt.*; import java.awt.event.*; //Class that paints according to the user wish. public class RapidPrototyping extends JFrame implements MouseListener,ItemListener,ActionListener,MouseMotionListener {       //panel to hold color,shapes and thickness components    JPanel panel;    //shapes combobox    JComboBox shapes;    //color radio buttons    JRadioButton red, green, blue;    //thickness combobox    JComboBox thicknesses;    //clear button.    JButton clear;    JPanel center;       /*values of each selection*/    Color color;    int thickness;    String shape;...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
This program is in C++: Write a program to allow the user to: 1. Create two...
This program is in C++: Write a program to allow the user to: 1. Create two classes. Employee and Departments. The Department class will have: DepartmentID, Departmentname, DepartmentHeadName. The Employee class will have employeeID, emploeename, employeesalary, employeeage, employeeDepartmentID. Both of the above classes should have appropriate constructors, accessor methods. 2. Create two arrays . One for Employee with the size 5 and another one for Department with the size 3. Your program should display a menu for the user to...