Java Programming, computer science homework help

Background: Perry Programmer has decided to decorate his cubicle with shapes made of paper with a wire outline. He has decided to start with equilateral triangles of various sizes.



In order to estimate how much wire and paper he should buy, he has written a program that will take a list of shapes and compute the total perimeter (i.e. amount of wire needed) and total area (i.e. amount of paper needed) for those shapes. His program consists of the following two classes:

public class SupplyEstimator{_x000D_
	static Triangle[] shapeList = {_x000D_
	   new Triangle(4.0),_x000D_
	   new Triangle(2.5),_x000D_
	   new Triangle(2.5),_x000D_
	   new Triangle(10.0)_x000D_
	};_x000D_
_x000D_
	public static void main(String[] args){_x000D_
		double perrymeter = 0.0; //Perry loves puns_x000D_
		double area = 0.0;_x000D_
		for (int i=0; i<shapeList.length; i++){_x000D_
			perrymeter += shapeList[i].getPerimeter();_x000D_
			area += shapeList[i].getArea();_x000D_
		}_x000D_
		System.out.println("For the "+shapeList.length+" shapes given");_x000D_
		System.out.println("the total perimeter is "+perrymeter);_x000D_
		System.out.println("and the total area is "+area);_x000D_
	}_x000D_
}_x000D_
_x000D_
public class Triangle{_x000D_
	double size;_x000D_
_x000D_
	public Triangle(double s){_x000D_
		size = s;_x000D_
	}_x000D_
_x000D_
	public double getPerimeter(){_x000D_
		return 3*size;_x000D_
	}_x000D_
_x000D_
	public double getArea(){_x000D_
		return size*size*Math.sqrt(3)/4.0;_x000D_
	}_x000D_
}_x000D_

Goal: For this assignment, you should expand Perry’s program to deal with squares and hexagons as well.



You should define a new parent class Shape with one attribute, size. Triangle, Square, and Hexagon should be subclasses of Shape. The constructors of the subclasses should use the “super” constructor to initialize size. Each of the subclasses should also have a no-parameter constructor which uses a default size of 1. You should use the “this” constructor to avoid code duplication.

Your program should print a brief description of each shape in shapeList rather than just giving the number of shapes.

Useful Fact: The area of a regular hexagon with sides of length S is 1.5 times the square root of 3 times S squared.

Method: First copy Perry’s classes into a new Eclipse project and make sure the program runs.

In SupplyEstimator, you don’t need to change a lot. The declaration of shapeList should be changed to an array of Shape. The first println should be replaced with a loop that prints each element of shapeList. In order to get a readable output, you should define a toString() method in Shape that uses getClass() to show the type of shape. Something like:

a Square of size 2.0_x000D_

Each subclass should override getPerimeter() and getArea() to provide the appropiate computations for that shape.

Hand-in: Your program and a few sample runs using the dropbox on the class D2L site. For the sample runs you should change the initialization of shapeList to show off the various classes and constructors. Be sure your code contains Javadoc-style comments saying who wrote it and why.

"Is this question part of your assignment? We can help"

ORDER NOW