Question: Please help with the following request: Goals Learn test-driven development. Learn more about classes, objects, and inheritance. Learn to handle thrown exceptions. Problem The Weight

Please help with the following request:

Goals

Learn test-driven development.

Learn more about classes, objects, and inheritance.

Learn to handle thrown exceptions.

Problem

The Weight program is not extensible to more than calculated weights. To address this, we are going to a more object oriented approach to Weight calculation that isolates shapes where we can calculate the weight of the shape with given its dimensions. Later, we can add shapes that require us to look up the values.

In addition to refactoring the program to facilitate steel shapes of all shapes (no pun intended) and sizes, wealso need to facilitate adding up a collection of shapes that make up a shipment.

Because we are gathering up summary information from multiple entries, terminating the program because of a user data entry error would result in losing the previous entries. Therefore, we will catch all exceptions (user entry errors or other runtime exceptions that Java encounters), produce an error message to the user, and continue prompting for input until end is entered.

Also, feet and inches are usually specified like feet-inches. For example, 5 feet 6 inches is 5-6. We need to change the interface that processes the values to support this new format.

User Story

Users want essentially the same interface as the current Weight program but will provide the form feet-inches. In addition, after a shape is provided, a message like Plate weighs... is output. After an end is given for the shape, the total weight of the shipment is displayed. For example:

Enter shape and dimensions: P 1/4 2 5-6 2-3

Plate weighs 126.0703125

Enter shape and dimensions: R 3/4 5-7

Rod weighs 8.376302092249544

Enter shape and dimensions: R 1/2 6 8

Unable to add item to shipment because the argument "6" is not recognized

Enter shape and dimensions: Enter end to stop entering shapes

Total shipment weight: 134.44661459224955

Tasks

1. Create a new project called Project6b.

2. Copy to the Project6b.java file the contents of the Project6b.java file in the Examples folder in the Google drive set up for the class. CODE PROVIDE:

package project6b;

import java.util.Scanner;

class Shipment {

private double _weight = 0.0;

public double weight() {

return _weight;

}

public static double inFeet(int feet, double inches) {

// Return feet and inches in to feet and fractions of a foot.

// For example 5 feet 6 inches returns 5.5 from here.

// Note inches is a double. This is to support 0 feet 3/4 of an inch.

// Return the feet plus the inches divided by 12 here.

}

public static double fracInFeet(String frac) {

// Take in string like "3/4" and return .0625.

// Use inFeet(feet, inches) to covert to feet.

// This is just like the thickness calculation you did but

// this time you can use inFeet(0, fractionOfAnInch)

// So split frac by a /. Convert the numerator and denominator

// into inches. Then divide them. Watch out for integer divide.

}

public static double feetAndInchesInFeet(String feetAndInches) {

// Take in string like "5-6" and return 5.5.

// Use inFeet(feet, inches) to covert to feet.

// This is similar to the fraction code except you are going

// to split on "-". You will have feet in the [0] element of

// the array and inches in the [1] element of the array.

// You will need to parse each into an integer.

// Then just return inFeet(feet, inches).

}

public static double getValueInFeet(String s) {

double value;

// If parameter s has an / in it then it must be a fraction like "3/4".

// If parameter s has a - in it, then it must be feet and inches like "5-6".

// So check the parameter s to see if it contains a / or a -. To do

// this you will use s.indexOf(). Look up string.indexOf() to see what

// you give it.

// Note that for fractions you can use fracInFeet(s) and for the

// the feet-inches (e.g. 5-6), you can use feetAndInchesInFeet()

}

public SteelShape addItem(int quantity, String description) {

String[] desc_values = description.split(" ");

double[] values = new double[desc_values.length - 1];

String shape_letter = desc_values[0];

for (int i = 1; i < desc_values.length; i++) {

values[i-1] = getValueInFeet(desc_values[i]);

}

SteelShape shape = null;

if (shape_letter.equals("P")) {

if (values.length != 3) {

throw new IllegalArgumentException("Plate needs three dimensions");

}

shape = new Plate(values[0], values[1], values[2]);

}

else if (shape_letter.equals("R")) {

if (values.length != 2) {

throw new IllegalArgumentException("Rod needs two dimensions");

}

shape = new Rod(values[0], values[1]);

}

else {

throw new IllegalArgumentException("shape letter '" + shape_letter +

"' not recognized");

}

_weight += quantity * shape.weight();

return shape;

}

}

class SteelShape {

private double _length = 0.0;

private double _weight = 0.0;

public void length(double length) {

_length = length;

}

public double length() {

return _length;

}

public void weight(double weight) {

_weight = weight;

}

public double weight() {

return 0.0;

}

}

class CalcShape extends SteelShape {

private double _area = 0.0;

public void area(double area) {

_area = area;

}

@Override

public double weight() {

return _area * this.length() * 489;

}

}

class Rod extends CalcShape {

Rod(double diameter, double length) {

double radius = diameter / 2;

super.area(Math.PI * Math.pow(radius, 2));

super.length(length);

}

}

class Plate extends CalcShape {

Plate(double thick, double width, double length) {

super.area(thick * width);

super.length(length);

}

}

public class Project6b {

static boolean TESTIT = true;

private static String[] parseInput(String line) {

String[] input_values;

input_values = line.split(" ");

return input_values;

}

private static int test() {

int errors = 0;

double expected_result = 5 + 6 / 12.0;

double result = Shipment.inFeet(5, 6);

if (result == expected_result) {

System.out.println("PASSED: Shipment.infeet(5, 6)");

}

else {

System.out.println("FAILED: Shipment.infeet(5, 6) returned " +

result + " and not " + expected_result);

++errors;

}

expected_result = (3 / 4.0) / 12.0;

result = Shipment.fracInFeet("3/4");

if (result == expected_result) {

System.out.println("PASSED: Shipment.fracInFeet(\"3/4\")");

}

else {

System.out.println("FAILED: Shipment.fracInFeet(\"3/4\") returned " +

result + " and not " + expected_result);

++errors;

}

expected_result = 5 + 6 / 12.0;

result = Shipment.feetAndInchesInFeet("5-6");

if (result == expected_result) {

System.out.println("PASSED: Shipment.feetAndInchesInFeet(\"5-6\")");

}

else {

System.out.println("FAILED: Shipment.feetAndInchesInFeet(\"5-6\") returned " +

result + " and not " + expected_result);

++errors;

}

Shipment shipment = new Shipment();

SteelShape item;

item = shipment.addItem(1, "P 1/4 5-6 2-6");

expected_result = 140.078125;

result = item.weight();

if (result == expected_result) {

System.out.println("PASSED: Weight of P 1/4 5-6 2-6");

}

else {

System.out.println("FAILED: Weight of P 1/4 5-6 2-6 is " +

result + " and not " + expected_result);

++errors;

}

item = shipment.addItem(1, "R 3/4 5-7");

expected_result = 8.376302092249544;

result = item.weight();

if (result == expected_result) {

System.out.println("PASSED: Weight of R 3/4 5-7");

}

else {

System.out.println("FAILED: Weight of R 3/4 5-7 is " +

result + " and not " + expected_result);

++errors;

}

return errors;

}

public static void main(String[] args) {

if (TESTIT) {

int errors = test();

System.exit(1);

}

Scanner input = new Scanner(System.in);

String line;

String[] values;

Shipment shipment = new Shipment();

SteelShape shape;

while (true) {

System.out.print("Enter shape and dimensions: ");

line = input.nextLine();

if (line.equals("end")) {

break;

}

shape = shipment.addItem(1, line);

if (shape instanceof Plate) {

System.out.println("Plate weighs " + shape.weight());

}

else if (shape instanceof Rod) {

System.out.println("Rod weighs " + shape.weight());

}

}

System.out.println("Total shipment weight: " + shipment.weight());

}

}

3. There will be compile errors mostly around missing return statements. Temporarily get rid of the

compile errors by adding return statements like return 0;.

4. Once your program builds cleanly, run it with TESTIT = true. This will run the extensive unit tests.

Initially you will get all failures. One by one you will want to implement the code that is failing such

that the output of the unit tests looks like this:

run:

PASSED: Shipment.infeet(5, 6)

PASSED: Shipment.fracInFeet("3/4")

PASSED: Shipment.feetAndInchesInFeet("5-6")

PASSED: Weight of P 1/4 5-6 2-6

PASSED: Weight of R 3/4 5-7

5. Once all the tests are passing, change TESTIT to false and test with the test items below.

6. After you have tested with the test items below and output the correct shipment weight, enter some

bad data like 5 6 instead of 5-6. Notice that the program terminates and the shipment weight for

any previous items entered is lost. Fix this by wrapping the addItem (and related if-then-else

dependent on a successfully returned shape) in a try-catch statement. Catch all exceptions (not just

the IllegalArgumentException thrown in the processing routines.

Checklist

Use the following as guidance for getting a 10 on this project:

Working Shipment.inFeet(). (1 point)

Working Shipment.fracInFeet(). (2 points)

Working Shipment.feetAndInchesInFeet (). (2 points)

Working Shipment.getValueInFeet (). (3 points)

Catch thrown data entry error and produce meaningful message and continue prompting. (2 points)

Shape Thick. Or Diameter Width Length Weight

P 5-6 2-3 126.07

R 5-7 8.376302

This is my current code for the Weight program i've used in the previous assigment and I need to make the adjustment to this code I believe?

package weight.pkg3;

import java.util.Scanner;

public class Weight3 {

static boolean TESTIT = false; final static int PLATE = 0; final static int ROD = 1;

private static double thickness(String thickFrac) { double thickness = 0; String[] subString = thickFrac.split("/"); int denominator = Integer.parseInt(subString[1]); int numerator = Integer.parseInt(subString[0]); double fraction = (double) numerator / denominator; thickness = (double) fraction / 12; return thickness;

// Turn any string fraction "1/4" into its // double value (.25). This takes in any fraction so no switch statement will work. // Split on "/", used Integer.parseInt() on numerator and // denominator. Divide the two and then divide by 12. }

private static double[] parseInput(String line) { String[] input_values; double[] values; int feet; int inches;

// Parameter "line" string will look like this:"P 1/4 5 6 4 5" or "R 1/4 8 2" // You will return an array of doubles that will look something like this: // {0, .0208, 5.5, 4.4166} for "P 1/4 5 6 4 5" // {1, .0208, 8.1666} for "R 1/4 8 2" input_values = line.split(" ");

if (input_values[0].equals("P")) { values = new double[4];

values[0] = PLATE;

values[1] = thickness(input_values[1]); // Replace this by using thickness() above for this. feet = Integer.parseInt(input_values[2]); inches = Integer.parseInt(input_values[3]);

values[2] = (double) inches / 12 + feet; // Replace this by using feet and inches. feet = Integer.parseInt(input_values[4]); inches = Integer.parseInt(input_values[5]);

values[3] = (double) inches / 12 + feet; // Replace this by using feet and inches.

} else if (input_values[0].equals("R")) { values = new double[3];

values[0] = ROD;

values[1] = thickness(input_values[1]); // Use logic similar to the above to set this feet = Integer.parseInt(input_values[2]); inches = Integer.parseInt(input_values[3]);

values[2] = (double) inches / 12 + feet; // Use logic similar to the above to set this } else { values = new double[0]; } return values; }

private static double weight(double thick, double width, double length) { double weight = 0; weight = thick * width * length * 489; // (Plate) Thick x Width x Length weight calculation here. // System.out.println(thick + " " + width + " " + length); return weight; }

private static double weight(double diameter, double length) { double weight = 0; // ?r2? length ? 489lb / ft3 double areaOfCircle = Math.PI * Math.pow(diameter / 2, 2); weight = areaOfCircle * length * 489; // (Rod) Area of circle x Length weight calculation here. return weight; }

private static int test() { int errors = 0; double thick; thick = thickness("1/2"); if (thick != .5 / 12) { System.out.println("thickess FAILED thick=" + thick); ++errors; } double[] values; values = parseInput("P 1/4 5 6 4 0"); if (values[0] != 0 || values[1] != .25 || values[2] != 5.5 || values[3] != 4.0) { System.out.println("parseInput FAILED thick=" + values[1] + " width=" + values[2] + " length=" + values[3]); ++errors; } return errors; }

public static void main(String[] args) { if (TESTIT) { int errors = test(); System.exit(errors); } Scanner input = new Scanner(System.in); String line; double[] values; double weight; while (true) { System.out.print("Enter shape and dimensions: "); line = input.nextLine(); values = parseInput(line); if (values.length == 0) { break; } weight = 0; int value = (int) values[0]; switch (value) {

case PLATE: weight = weight(values[1], values[2], values[3]); break;

case ROD: weight = weight(values[1], values[2]); // Put rod code here. Note that rods do not need a width // so copy the above code but make the appropriate changes // to exclude width. break; default: System.out.println("Unknown shape"); break; } if (weight != 0) { String[] lineArray = line.split(" "); System.out.println("Shape\t ThickOrDiamater\t WidthFt.\t Width In.\t LengthFt.\t Lengthln.\t Weight"); if (value == 0) { System.out.println("P\t\t" + lineArray[1] + "\t\t\t" + lineArray[2] + "\t\t" + lineArray[3] + "\t\t" + lineArray[4] + "\t\t" + lineArray[5] + "\t" + weight); } else { System.out.println("R\t\t" + lineArray[1] + "\t\t\t" + "" + "\t\t" + "" + "\t\t" + lineArray[2] + "\t\t" + lineArray[3] + "\t" + weight); } System.out.println(); } } } }

Thank you.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!