Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CS 1113 LAB #10: Inheritance and the classpath Dr Cline Lab Goals This lab gives

ID: 3817903 • Letter: C

Question

CS 1113 LAB #10: Inheritance and the classpath Dr Cline Lab Goals This lab gives you a look at some of the object oriented features built into Java, namely: 1) Inheritance, which allows you to create subclasses to override the behavior of parent classes 2 The classpath and packages, which help to organize the classes of a large project into multiple directories 3) Jar files, which pack multiple class files into a single compressed file for easy distribution Motivation Inheritance is an important aspect of object-orientated design. It allows one class to customize its behavior while keeping the same methods as the parent class. For example, a class called "Vehicle" might have a method called "move". The subclasses of vehicle "Boat", Car" and "Tank" will also have comove" methods, but will accomplish this in different ways. In this lab, you will write a set of classes that descend from a parent class "Geometricobjects. Each of these will have different data and be able to calculate their area and perimeter. In addition, this lab will introduce you to a number of features and utilities provided by the Java language and the jdk: namely the classpath, packages, and jar files. Java packages allow you to manage large projects that have more content than you want to put in a single directory Thejar utiltiy is a convenient publishing method for ajava program. It zips up a bunch of class files into a single file that can be executed by the virtual machine (VM In this lab, you will learn how to make a jar file.

Explanation / Answer

//GeometricObject.java
public abstract class GeometricObject {

   //abstract area and perimeter
   public abstract double getArea();
   public abstract double getPerimeter();
   //tostring method
   public String toString() {  
       return "GeometricObject :Area =?, Perimeter= ?";
   }
}//end of GeometricObject

--------------------------------------------------------------------------------------------------------

//Circle.java
public class Circle extends GeometricObject{

   //declare radius as double type
   private double radius;
   //constructor to set radius
   public Circle(double radius) {
       this.radius=radius;
   }
  
   //return area
   public double getArea() {          
       return Math.PI*radius*radius;
   }

   //return perimter
   public double getPerimeter() {          
       return 2.0*Math.PI*radius;
   }
  
   //returns the circle object as string value
   public String toString() {  
       return String.format("Circle : Area = %5.2f Perimeter= %5.2f",getArea(),getPerimeter());
   }

}

--------------------------------------------------------------------------------------------------------

//Rectangle.jaa
public class Rectangle extends GeometricObject{  
   //declare two double variables
   private double width;
   private double height;
  
   //constructor to set width and heigh
   public Rectangle(double width,double height) {
       this.width=width;
       this.height=height;
   }

   //return area
   public double getArea() {      
       return width*height;
   }

   //return perimeter
   public double getPerimeter() {      
       return 2.0*(width+height);
   }
   //return string represenation of rectangle
   public String toString() {  
       return String.format("Rectangle : Area = %5.2f Perimeter= %5.2f",getArea(),getPerimeter());
   }
  
  
}

--------------------------------------------------------------------------------------------------------


//EquilateralTriangle.java
public class EquilateralTriangle extends GeometricObject{

   //declare side as double
   private double side;
   //constructor to set side
   public EquilateralTriangle(double side) {
       this.side=side;
   }

   //Returns area
   public double getArea() {      
       return (Math.sqrt(3)*(side*side))/4;
   }

   //Returns perimter
   public double getPerimeter() {      
       return 3.0*side;
   }
   //return string represenation of equilateral triangle
   public String toString() {
       return String.format("EquilateralTriangle : Area = %5.2f Perimeter= %5.2f",getArea(),getPerimeter());

   }

}

--------------------------------------------------------------------------------------------------------

/**
* The java program ObjectCalculator that tests
* Circle, Rectangle and EquilateralTriangle classes.
* */
//ObjectCalculator.java
import java.util.ArrayList;
import java.util.Scanner;
public class ObjectCalculator {  
   public static void main(String[] args) {
  
       double totalArea=0;
       double totalPerimeter=0;
       //Create an arrylist of type GeometricObject
       ArrayList<GeometricObject> objects=new ArrayList<GeometricObject>();
       //create a Scanner class object
       Scanner scanner=new Scanner(System.in);
       System.out.println("Enter radius of circle ");
       double radius=Double.parseDouble(scanner.nextLine());
      
       //add Circle object
       objects.add(new Circle(radius));
      
       System.out.println("Enter side of Triangle");
       double side=Double.parseDouble(scanner.nextLine());
      
       //add EquilateralTriangle object
       objects.add(new EquilateralTriangle(side));
      
       System.out.println("Enter width of the rectangle ");
       double width=Double.parseDouble(scanner.nextLine());
       System.out.println("Enter height of the rectangle ");
       double height=Double.parseDouble(scanner.nextLine());
      
       //add Rectangle object
       objects.add(new Rectangle(width, height));
              
      
      
       //print GeometricObjects to the console
       for (GeometricObject object : objects) {
           System.out.println(object);
           //add area to total area
           totalArea+=object.getArea();
           //add perimeter to total perimeter
           totalPerimeter+=object.getPerimeter();
       }
      
       //print total area and total perimeter
       System.out.printf("Total area : %5.2f ",totalArea);
       System.out.printf("Total perimeter : %5.2f",totalPerimeter);      
   }
}//end of the class

--------------------------------------------------------------------------------------------------------

Sample Output:

Enter radius of circle
2.0
Enter side of Triangle
3.0
Enter width of the rectangle
4.0
Enter height of the rectangle
2.0
Circle : Area = 12.57 Perimeter= 12.57
EquilateralTriangle : Area = 3.90 Perimeter= 9.00
Rectangle : Area = 8.00 Perimeter= 12.00
Total area : 24.46
Total perimeter : 33.57