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

Lab Goals This lab gives you a look at some of the object oriented features buil

ID: 3817712 • Letter: L

Question

Lab Goals This lab gives you a look at some of the object oriented features built into Java, namely: I) 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 "move" methods, but will accomplish this in different ways. In this lab, you will write a set of classes that descend from a parent class "GeometricObj ect". Each of these will have different dat 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. The jar 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 Inheritance Part 1) Copy the abstract class GeometricObject below, and save it in the file Geometricobjectjava: Lab 10

Explanation / Answer

Jar files cannot be shared. Please use these files to create the jar

GeometricObject.java

package geometry;

//Lab 10

//<Your Name>

//<Your section>

public abstract class GeometricObject {

  

   public abstract double getArea();

   public abstract double getPerimeter();

   public String toString()

   {

       return "GeometricObject: Area=?, Perimeter=?";

   }

}

Circle.java

package geometry;

//Lab 10

//<Your Name>

//<Your section>

public class Circle extends GeometricObject{

   double radius;

  

   public Circle(double rad) {

       radius = rad;

   }

   @Override

   public double getArea() {

       return Math.PI*radius*radius;

   }

   @Override

   public double getPerimeter() {

       return 2*Math.PI*radius;

   }

   @Override

   public String toString() {

       return "Circle: Radius="+radius + ", " + "Area=" + String.format("%.2f", getArea()) + ", " + "Perimeter=" + String.format("%.2f", getPerimeter());

   }

}

Rectangle.java

package geometry;

//Lab 10

//<Your Name>

//<Your section>

public class Rectangle extends GeometricObject{

   double width;

   double height;

  

   public Rectangle(double w, double h) {

       width = w;

       height = h;

   }

   @Override

   public double getArea() {

       return width*height;

   }

   @Override

   public double getPerimeter() {

       return 2*(width+height);

   }

   @Override

   public String toString() {

       return "Rectangle: Width="+width + ", " +"Height="+height + ", " + "Area=" + String.format("%.2f", getArea()) + ", " + "Perimeter=" + String.format("%.2f", getPerimeter());

   }

}

EquilateralTriangle.java

package geometry;

//Lab 10

//<Your Name>

//<Your section>

public class EquilateralTriangle extends GeometricObject{

   double sideLength;

  

   public EquilateralTriangle(double len) {

       sideLength = len;

   }

   @Override

   public double getArea() {

       return (Math.sqrt(3)*sideLength*sideLength)/4.0;

   }

   @Override

   public double getPerimeter() {

       return 3*sideLength;

   }

   @Override

   public String toString() {

       return "Equilateral Triangle: Side="+sideLength + ", " + "Area=" + String.format("%.2f", getArea()) + ", " + "Perimeter=" + String.format("%.2f", getPerimeter());

   }

}

ObjectCalculator.java

package geometry;

import java.util.ArrayList;

import java.util.Scanner;

//Lab 10

//<Your Name>

//<Your section>

public class ObjectCalculator {

   public static void main(String[] args) {

       ArrayList<GeometricObject> objects = new ArrayList<GeometricObject>();

       Scanner keyboard = new Scanner(System.in);

      

       System.out.print("Circle radius: ");

       double radius = keyboard.nextDouble();

       objects.add(new Circle(radius));

      

       System.out.print("Equilateral triangle side: ");

       double side = keyboard.nextDouble();

       objects.add(new EquilateralTriangle(side));

      

       System.out.print("Rectangle width and height: ");

       double width = keyboard.nextDouble();

       double height = keyboard.nextDouble();

       objects.add(new Rectangle(width, height));

      

       for(int i=0; i<objects.size(); i++)

           System.out.println(objects.get(i));

      

       double totalPerimeter = 0, totalArea = 0;

       for(int i=0; i<objects.size(); i++)

       {

           totalPerimeter += objects.get(i).getPerimeter();

           totalArea += objects.get(i).getArea();

       }

       System.out.printf("Total Area: %.2f" , totalArea);

       System.out.printf(" Total Perimeter: %.2f" , totalPerimeter);

   }

}

OUTPUT:

Circle radius: 20

Equilateral triangle side: 3.0

Rectangle width and height: 4.0

2.0

Circle: Radius=20.0, Area=1256.64, Perimeter=125.66

Equilateral Triangle: Side=3.0, Area=3.90, Perimeter=9.00

Rectangle: Width=4.0, Height=2.0, Area=8.00, Perimeter=12.00

Total Area: 1268.53

Total Perimeter: 146.66