For this lab you will write a class to create a user-defined type called Shapes to represent different shapes, their perimeters and their areas. The class should have three fields: shape, which is of type string, and perimeter and area of type double. The program will have to be able to calculate perimeters and areas for circles, triangles, squares and rectangles, which are the known shapes.
2.1 Constructors
Write these constructors:
1. Default constructor that sets the shape to "unknown" and the perimeter and area to 0
2. Constructor with one parameter, shape, that sets the shape, other fields are 0
2.2 Accessors & Mutators
Write the following member methods:
1. setShape()
Modifies the shape field
2. getShape()
Returns the shape field
3. getPerimeter()
Returns the perimeter field
4. getArea()
Returns the area field
Arithmetic Methods in Interface
Write the following member methods to perform arithmetic:
1. setPerimeter(Scanner scnr)
Depending on the shape, prompts the user for appropriate variables (ie width & height for a square, radius for a circle, etc.) and calculates the perimeter of the appropriate shape. The perimeter field should be updates with the new value. If the shape is unknown, then print to the screen that the user must first define a shape before a perimeter can be calculated and set the perimeter to 0.
2. setArea(Scanner scnr)
Depending on the shape, prompts the user for appropriate variables (ie width & height for a square, radius for a circle, etc.) and calculates the area of the appropriate shape. The area field should be updates with the new value. If the shape is unknown, then print to the screen that the user must first define a shape before an area can be calculated and set the area to 0.
2.3 Additional Methods
Create two additional methods of your choice. Be sure to clearly label both of these methods as your own.
2.4 Using another .java file called ShapesTest.java that tests that all of your methods work correctly

Respuesta :

Answer:

========= Shape.java  ===========

//import the Scanner class

import java.util.Scanner;

public class Shape{

   //required fields

  private String shape;

   private double area;

   private double perimeter;

   //default constructor

  public Shape(){

       this.shape = "unknown";

       this.area = 0.0;

       this.perimeter = 0.0;

   }

   //constructor with one parameter

   public Shape(String shape){

       this.setShape(shape);

       this.area = 0.0;

       this.perimeter = 0.0;

   }

   //accessors and mutators

  public void setShape(String shape){

       this.shape = shape;

   }

  public String getShape(){

       return this.shape;

   }

   public double getPerimeter(){

       return this.perimeter;

   }

  public double getArea(){

       return this.area;

   }

  public void setPerimeter(Scanner scr){

       if(this.getShape().equals("circle")){

           System.out.println("Enter the radius of the circle");

           double radius = scr.nextDouble();

           this.perimeter = 2 * 3.142 * radius;

       }

       else if(this.getShape().equals("rectangle")){

           System.out.println("Enter the width");

           double width = scr.nextDouble();

           System.out.println("Enter the height");

           double height = scr.nextDouble();

           this.perimeter = 2 * (width + height);

       }

       else if(this.getShape().equals("square")){

           System.out.println("Enter the height or width");

           double height = scr.nextDouble();

           this.perimeter = 4 * height;

       }

       else if(this.getShape().equals("unknown")){

           System.out.println("You must define a shape first before calculating perimeter");

           this.perimeter = 0.0;

       }

       else {

           System.out.println("You must define a shape first before calculating perimeter");

           this.perimeter = 0.0;

       }

   }

   public void setArea(Scanner scr){

       if(this.getShape().equals("circle")){

           System.out.println("Enter the radius of the circle");

           double radius = scr.nextDouble();

           this.area = 3.142 * radius * radius;

       }

       else if(this.getShape().equals("rectangle")){

           System.out.println("Enter the width");

           double width = scr.nextDouble();

           System.out.println("Enter the height");

           double height = scr.nextDouble();

           this.area = width * height;

       }

       else if(this.getShape().equals("square")){

           System.out.println("Enter the height or width");

           double height = scr.nextDouble();

           this.area = height * height;

       }

       else if(this.getShape().equals("unknown")){

           System.out.println("You must define a shape first before calculating area");

           this.area = 0.0;

       }

       else {

           System.out.println("You must define a shape first before calculating area");

           this.area = 0.0;

       }

   }

   //Own methods

   //1. Method to show the properties of a shape

   public void showProperties(){

       System.out.println();

       System.out.println("The properties of the shape are");

       System.out.println("Shape : " + this.getShape());

       System.out.println("Perimeter : " + this.getPerimeter());

       System.out.println("Area : " + this.getArea());

   

   }

   //2. Method to find and show the difference between the area and perimeter of a shape

   public void getDifference(){

       double diff = this.getArea() - this.getPerimeter();

       System.out.println();

       System.out.println("The difference is " + diff);

   }

}

========= ShapeTest.java  ===========

import java.util.Scanner;

public class ShapeTest {

   public static void main(String [] args){

       Scanner scanner = new Scanner(System.in);

       // create an unknown shape

       Shape shape_unknown = new Shape();

       //get the shape

       System.out.println("The shape is " + shape_unknown.getShape());

       //set the area

       shape_unknown.setArea(scanner);

       //get the area

       System.out.println("The area is " + shape_unknown.getArea());

       //set the perimeter

       shape_unknown.setPerimeter(scanner);

       //get the perimeter

       System.out.println("The perimeter is " + shape_unknown.getPerimeter());

       // create another shape - circle

       Shape shape_circle = new Shape("circle");

       //set the area

       shape_circle.setArea(scanner);

       //get the area

       System.out.println("The area is " + shape_circle.getArea());

       //set the perimeter

       shape_circle.setPerimeter(scanner);

       //get the area

       System.out.println("The perimeter is " + shape_circle.getArea());

       //get the properties

       shape_circle.showProperties();

       //get the difference between area and perimeter

       shape_circle.getDifference();

   }

}

Sample output:

The shape is unknown

You must define a shape first before calculating area

The area is 0.0

You must define a shape first before calculating perimeter

The perimeter is 0.0

Enter the radius of the circle

>> 12

The area is 452.448

Enter the radius of the circle

>> 12

The perimeter is 452.448

The properties of the shape are

Shape : circle

Perimeter : 75.408

Area : 452.448

The difference is 377.03999999999996

Explanation:

The code above is written in Java. It contains comments explaining important parts of the code. Please go through the code for more explanations. For better formatting, the sample output together with the code files have also been attached to this response.

Ver imagen stigawithfun
Ver imagen stigawithfun
Ver imagen stigawithfun
ACCESS MORE
ACCESS MORE
ACCESS MORE
ACCESS MORE