Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help with this code and why I am not getting the output being asked. Usually I am off by a small amount but this

Please help with this code and why I am not getting the output being asked. Usually I am off by a small amount but this time Its much bigger. This is the code:

package project7;

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.

double inchesToFeet = inches / 12;

return feet + inchesToFeet;

}

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.

String inches[] = frac.split("/");

double fraction = Double.parseDouble(inches[0]) / Double.parseDouble(inches[1]);

return inFeet(0, fraction);

}

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).

String inches[] = feetAndInches.split("-");

return inFeet(Integer.parseInt(inches[0]), Double.parseDouble(inches[1]));

}

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()

if (s.indexOf("/") != -1) {

return fracInFeet(s);

} else if (s.indexOf("-") != -1) {

return feetAndInchesInFeet(s);

} else {

return 0;

}

}

public SteelShape addItem(int quantity, String description) {

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

String angle_size = "";

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

String shape_letter = desc_values[0];

int values_index = 0;

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

if (desc_values[i].indexOf("x") > 0) {

angle_size = desc_values[i];

} else {

values[values_index] = getValueInFeet(desc_values[i]);

++values_index;

}

}

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 if (shape_letter.equals("L")) {

if (values.length != 3) {

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

}

shape = new Angle(angle_size, 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 LookupShape extends SteelShape {

private double _weight_per_foot = 0.0;

public void weightPerFoot(double weight_per_foot) {

_weight_per_foot = weight_per_foot;

}

@Override

public double weight() {

return _weight_per_foot * length();

}

}

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);

}

}

class Angle extends LookupShape {

private static double[][] _table = {{2, 2, .25/12, 3.19}, {3, 3, .25/12, 4.9}};

public static double weightPerFoot(String angleSize, double thick) {

double wpf = 0.0;

// The parameter angleSize is a string like "2x2". Convert it into two

// doubles for the first side and second side. You are going to use the

// to look up the weight per foot.

String[] splits = angleSize.split("x");

double firstSide = Double.parseDouble(splits[0]);

double secondSide = Double.parseDouble(splits[1]);

boolean found = false;

// Loop through each row in the table to see if there is a row that matches the two sides

// and thickness. The first two elements of a row are the two sides. The third element

// in the row is the thickness (in feet) and the fourth is the weight to be returned from

// this function if there is a match. When you do find a match, do the following:

// 1. Set wpf to the fourth element

// 2. Set found to true

// 3. Break out of the loop because we are done.

// Put lookup loop here.

for (int i = 0; i < _table.length; i++) {

double[] row = _table[i];

if (row[0] == firstSide && row[1] == secondSide) {

wpf = row[3];

found = true;

break;

}

}

if (!found) {

throw new IllegalArgumentException("Angle of " + angleSize + " x " + thick + " not found");

}

return wpf;

}

Angle(String angleSize, double thick, double length) {

double wpf = weightPerFoot(angleSize, thick);

super.weightPerFoot(wpf);

super.length(length);

}

}

public class Project7 {

static boolean TESTIT = false;

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;

}

// Put this at the end of test() *but before* the return errors; statement.

expected_result = 3.19;

result = Angle.weightPerFoot("2x2", .25 / 12);

if (result == expected_result) {

System.out.println("PASSED: Angle.weightPerFoot(\"2x2\", .25)");

} else {

System.out.println("FAILED: Angle.weightPerFoot(\"2x2\", .25) returned "

+ result + " and not " + expected_result);

++errors;

}

item = shipment.addItem(1, "L 2x2 1/4 5-7");

expected_result = 17.81083333333333;

result = item.weight();

if (result == expected_result) {

System.out.println("PASSED: Weight of L 2x2 1/4 5-7");

} else {

System.out.println("FAILED: Weight of L 2x2 1/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());

}

}

}

Current Output:

run: Enter shape and dimensions: P 1/4 5-6 2-3 Plate weighs 126.0703125 Total shipment weight: 126.0703125 Enter shape and dimensions: R 3/4 5-7 Rod weighs 8.376302092249544 Total shipment weight: 134.44661459224955 Enter shape and dimensions: L 2x2 1/4 3-4 Total shipment weight: 145.07994792558287 Enter shape and dimensions: L 3x3 1/4 4-6 Total shipment weight: 167.12994792558288 Enter shape and dimensions:

These are the outputs I should be getting close too. Could it be because my code is being written for double, and i believe in these examples they are written as floats?

Shape Size Thick. orDiameter Width Length Weight

P 1/4 5-6 2-3 126.07

R 3/4 5-7 8.376302

L 2x2 1/4 3-4 10.63333

L 3x3 4-6 22.05

Thank you.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Implementing Ai And Machine Learning For Business Optimization

Authors: Robert K Wiley

1st Edition

B0CPQJW72N, 979-8870675855

More Books

Students also viewed these Databases questions