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

Please use java for the following: 1. Write a method that sums the areas of all

ID: 664369 • Letter: P

Question

Please use java for the following:

1. Write a method that sums the areas of all the geometric objects in an array.

The method signature is:

public static double sumArea(GeometricObject[] a)

Write a test program that creates an array of 10 objects (5 circles and 5 rectangles using cloning) and computes their total area using the sumArea method.

2. (NumberFormatException)

Implement a simple command-line calculator. It should run as follows:

javac Calculator.java

java Calculator 1 + 2

3. (Exception Handler)

Write the program with an exception handler that deals with illegal operations. Your program should display a

message that informs the user of the wrong operand type before exiting.

java Calculator 1 / 0

Division by zero is illegal

3. (IllegalTriangleException) Define the Triangle class with three sides. In a triangle, the sum of any two sides is greater than the other side. The Triangle class must adhere to this rule. Create the IllegalTriangleException class, and modify the constructor of the Triangle class to throw an IllegalTriangleException object if a triangle is created with sides that violate the rule, as follows:

/** Construct a triangle with the specified sides */

public Triangle(double side1, double side2, double side3) throws IllegalTriangleException {

// Implement it

}

4. (HexFormatException)

Implement the parseHex that transforms from hexadecimal to decimal method to throw a NumberFormatException if the string is NOT a hex string. Define a custom exception called HexFormatException.

Explanation / Answer

This question has lot of subparts. Please post one more question. I have answered the first 2.

1)

public static void main (String args[]) {

GeometricObject[] shape=new GeometricObject[10];

shape[0] = new Circle(5);
shape[1] = new Circle(5);
shape[2] = new Circle(5);
shape[3] = new Circle(5);
shape[4] = new Circle(5);
shape[5] = new Rectangle(2,4);
shape[6] = new Rectangle(2,4);
shape[7] = new Rectangle(2,4);
shape[8] = new Rectangle(2,4);
shape[9] = new Rectangle(2,4);

double totalArea = 0;
for(int i = 0; i < shape.length; i++)
{
System.out.println("Area is:" + shape[i].getArea());
totalArea = totalArea += shape[i].getArea();
}
}

2)