Question

In: Computer Science

For this lab, you will design classes to represent three simple geometric shapes: circles, rectangles, and...

For this lab, you will design classes to represent three simple geometric shapes: circles, rectangles, and triangles.

Implement the classes for these three shapes in Java, each in a separate class file, according to the guidelines given below:

  • The classes must include instance fields (also known as attributes or data members) for the information necessary to represent objects of these classes. Recall that the instance fields represent the properties of objects. The most fundamental property of a circle is its radius, so this would be an ideal candidate for an instance field. Likewise, rectangles have a length and width, and a triangle has a base and a height (we will assume that these are all isosceles triangles). These fields must all be floating-point values, so you should use double as the data type for these fields.

  • The classes should include constructors, which allow us to initialize new objects of these classes with sensible starting values. The constructors should allow us to set the values of every instance field in each class.

  • The classes should include accessors (also known as "getter methods") for all of the instance fields.

  • The classes should include a method to compute and return the area of each of the three types of shapes. The area() method does not need to accept an argument, since the object already "knows" (through the value(s) of its instance field(s)) all the information it needs to compute its own area.

  • The classes should also include a toString() method which returns the string representation of the object, in the form of its area surrounded by symbols which indicate the shape of the object. For example, printing each of the three types of shape objects to the screen should look like this (assume the area is 4 units for each of these shapes):

    • / 4.0 \ (for triangles)

    • [ 4.0 ] (for rectangles)

    • ( 4.0 ) (for circles)

  • Finally, write a main() method in a fourth class which does the following ...

    • Create at least two shape objects of each class, initialized to different sizes ...

    • Print a string representation of every shape object, and ...

    • Call the area() method of every shape object to get its area, and then compute the total area of all objects of the same kind (that is, the total area of all rectangles, the total area of all circles, and the total area of all triangles). It should then print these totals along with a descriptive label; for example ...

      Total Triangle Area = 8.0

(These shape objects can be declared as individual variables; later, we will see how to unify these objects into a collection under a common type.)

this is java language

Solutions

Expert Solution

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Below java classes are created.

Circle.java :

//Java class
public class Circle {
   // instance fields/data fields
   private double radius;

   // default constructor
   public Circle() {
       this.radius = 0.0;// set radius to 0.0
   }

   // parameterized constructor
   public Circle(double r) {
       this.radius = r;
   }

   // getter method
   public double getRadius() {
       return this.radius;// return radius
   }
   // method to get area
   public double area() {
       // return area of circle
       return Math.PI * this.radius * this.radius;
   }

   // method to string
   public String toString() {
       return "(" + this.area() + ")";
   }

}
****************************

Rectangle.java :

//java class
public class Rectangle {
   // instance fields/data fields
       private double length;
       private double width;

       // default constructor
       public Rectangle() {
           this.length = 0.0;// set length to 0.0
           this.width = 0.0;// set width to 0.0
       }
       // parameterized constructor
       public Rectangle(double l,double w) {
           this.length = l;// set length
           this.width = w;// set width
       }

       // getter method
       public double getLength() {
           return this.length;// return length
       }
       public double getWidth() {
           return this.width;// return width
       }
       // method to get area
       public double area() {
           // return area of rectangle
           return this.length * this.width;
       }

       // method to string
       public String toString() {
           return "[" + this.area() + "]";
       }
}
**************************

Triangle.java :

//Java class
public class Triangle {
   // instance fields/data fields
           private double base;
           private double height;

           // default constructor
           public Triangle() {
               this.base = 0.0;// set base to 0.0
               this.height = 0.0;// set height to 0.0
           }
           // parameterized constructor
           public Triangle(double b,double h) {
               this.base = b;// set base
               this.height = h;// set height
           }

           // getter method
           public double getBase() {
               return this.base;// return base
           }
           public double getHeight() {
               return this.height;// return width
           }
           // method to get area
           public double area() {
               // return area of triangle
               return (this.base * this.height)/2;
           }

           // method to string
           public String toString() {
               return "/" + this.area() + "\\";
           }

}
*************************

geometricShapes.java :

//import package
import java.util.*;
//Java class
public class geometricShapes {
   // entry point of program , main() method
   public static void main(String[] args) {
       // create object of Circle class
       Circle c = new Circle(4.0);
       // create object of Rectangle class
       Rectangle rect = new Rectangle(4.0, 10.0);
       // creating object of Triangle class
       Triangle t = new Triangle(4.0, 8.0);
       // print shape of circle
       System.out.println(c.toString());
       // print shape of Triangle
       System.out.println(t.toString());
       // print shape of Rectangle
       System.out.println(rect.toString());
       // print area of circle
       System.out.printf("Area of circle :%.1f ", c.area());
       System.out.println();// used for new line
       // print area of circle
       System.out.printf("Area of Rectangle :%.1f ", rect.area());
       // print area of Triangle
       System.out.printf("\nArea of Triangle :%.1f ", t.area());

   }

}

======================================================

Output : Compile and Run geometricShapes.java to get the screen as shown below

Screen 1 :geometricShapes.java

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.


Related Solutions

0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane...
0. Introduction. In this lab assignment, you will extend some simple Java classes that represent plane figures from elementary geometry. The object of this assignment is not to do anything useful, but rather to demonstrate how methods can be inherited by extending classes. 1. Theory. A polygon is a closed plane figure with three or more sides, all of which are line segments. The perimeter of a polygon is the sum of the lengths of its sides. A rectangle is...
Requirements:   You are to write a class called Point – this will represent a geometric point...
Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. A copy...
Requirements:   You are to write a class called Point – this will represent a geometric point...
Requirements:   You are to write a class called Point – this will represent a geometric point in a Cartesian plane (but x and y should be ints).   Point should have the following: Data:   that hold the x-value and the y-value. They should be ints and must be private. Constructors: A default constructor that will set the values to (2,-7) A parameterized constructor that will receive 2 ints (x then y) and set the data to what is received. A copy...
You will design and implement a graphics program that draws four concentric rectangles in a window,...
You will design and implement a graphics program that draws four concentric rectangles in a window, using four different colors of your choice, meeting the following specifications. • The size of the window is determined by the user of the program. To be specific, you should prompt the user for both the width and height of the window (in pixels), and then you should set the size of the window using these values. (Hint: use the setup() method of the...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
i just need the Polygon class Shapes In this lab you will create an interface, and...
i just need the Polygon class Shapes In this lab you will create an interface, and then implement that interface in three derived classes. Your interface will be called Shape, and will define the following functions: Return Type Name Parameters Description double area none computes the area of the shape double perimeter none computes the perimeter of the shape Point2d center none computes the center of the shape You will then create 3 implementations of this interface: Rectangle, Circle, and...
In this lab you will be encrypting a message. It is a simple encryption described in...
In this lab you will be encrypting a message. It is a simple encryption described in the problem. To program it remember that the ASCII code for ‘A’ is 65 and the other capital letters are in order 66, 67, 68, … The ASCII code for ‘a’ is 97 and the lowercase continues 98, 99, and so on. The hint is that a if a user type in a char with an ASCII code between 65 and 90, or 97...
For this assignment you will create a set of simple classes that model a cookbook. The...
For this assignment you will create a set of simple classes that model a cookbook. The cookbook will consist of set or recipes. Each recipe will consist of a set of ingredients and instructions. Your submission will consist of the below three classes and a test driver (the only class with a main()) that exercises the functionality of your system. You will need to implement the following classes based on the description of their attributes and operations: Ingredient name -...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience...
Lab 3 Java Classes and Memory Management Multi-file Project In this lab you will gain experience using classes and arrays of classes. Use the following as the beginning of a student abstract data type. public class Student { private String fName ; private String lName ; private double[] grades; } This class should be in the package com.csc241. Write a program that prompts a user for a total number of students and dynamically allocates memory for just that number of...
2 – The CPU design team is designing an instruction set with three classes of instructions....
2 – The CPU design team is designing an instruction set with three classes of instructions. Parameters are given in the following table. Consider a program with 65% ALU instructions, 20% memory access instructions, and 15% control instructions. What is the average CPI for this CPU? Clock Rate: 4GHz CPI for ALU Inst.: 4 CPI for Memory Inst.: 8 CPI for Control Inst.: 2
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT