Question:
Why is my Java code making these calculation errors?
2013-02-01 16:37:49 UTC
General question: WHY WOULD A SIMPLE AREA CALCULATOR KEEP YIELDING A RESULT OF 0?

HERE'S THE CODE THAT DOES THE CALCULATION:

import java.util.*;
import java.text.*;

/**
*
* @author oadeyemi
*/
public class CircleCarpet extends Carpet{

private int radius;

public CircleCarpet(String ID, double price2, int radius1){
super(ID, price2);
radius = radius1;
}

DecimalFormat form1 = new DecimalFormat("$##,##0.00");


public void computedTotalPrice(){
int rad2 = radius*radius;
double area = ((float)rad2)*Math.PI;
double hold = area*unitPrice;
totalPrice = hold;
}

public String toString(){
String line01 = "\nThe Carpet Shape:\tCircle\n";
String line02 = "The Radius:\t\t"+radius+"\n";
String line1 = "The CarpetId:\t\t"+carpetID+"\n";
String line2 = "The Area:\t\t"+area+"\n";
String line3 = "The Unit Price:\t\t"+form1.format(unitPrice)+"\n";
String line4 = "The Total Price:\t"+form1.format(totalPrice)+"\n\n";

return line01+line02+line1+line2+line3+line4;
}
}

HERE'S THE COMPLETE PROJECT CODE

Driver class: http://paste.ideaslabs.com/show/UE4glMCiY6
Carpet.java: http://paste.ideaslabs.com/show/9hCfEYZs2p
CarpetParser: http://paste.ideaslabs.com/show/g0AAQi1yxW

CLASSES THAT INVOLVE CALCULATION:
CircleCarpet: http://paste.ideaslabs.com/show/cNf1UjFvNj
RectangleCarpet: http://paste.ideaslabs.com/show/Cm7gOxXYk6

When the following inputs are received (when running Assignment5.java),

A
Circle/0002/2.20/20
A
Circle/0004/2.10/15
A
Rectangle/0005/3.10/30/20
L
C
L
D
0001
D
0004
Q

This is the output

Choice Action
------ ------
A Add Carpet
C Compute Total Price For Each Carpet
D Search for Carpet
L List Carpets
Q Quit
? Display Help

What action would you like to perform?
Please enter a carpet information to add:
What action would you like to perform?
Please enter a carpet information to add:
What action would you like to perform?
Please enter a carpet information to add:
What action would you like to perform?

The Carpet Shape: Circle
The Radius: 20
The CarpetId: 0002
The Area: 0
The Unit Price: $2.20
The Total Price: $0.00


The Carpet Shape: Circle
The Radius: 15
The CarpetId: 0004
The Area: 0
The Unit Price: $2.10
The Total Price: $0.00


The Carpet Shape: Rectangle
The Height: 30
The Width: 20
The CarpetId: 0005
The Area: 0
The Unit Price: $3.10
The Total Price: $0.00

What action would you like to perform?
total prices computed
What action would you like to perform?

The Carpet Shape: Circle
The Radius: 20
The CarpetId: 0002
The Area: 0 (ERROR SHOULD BE 1256)
The Unit Price: $2.20
The Total Price: $2,764.60 (ERROR SHOULD BE 2.764.60)


The Carpet Shape: Circle
The Radius: 15
The CarpetId: 0004
The Area: 0 (ERROR SHOULD BE 706)
The Unit Price: $2.10
The Total Price: $1,484.40 (ERROR: Should be 1482.60!!!)


The Carpet Shape: Rectangle
The Height: 30
The Width: 20
The CarpetId: 0005
The Area: 0 (ERROR)
The Unit Price: $3.10
The Total Price: $1,860.00

What action would you like to perform?
Please enter a carpetID to search:
carpet not found
What action would you like to perform?
Please enter a carpetID to search:
carpet found
What action would you like to perform?
Three answers:
AnalProgrammer
2013-02-01 17:36:15 UTC
In the Carpet class add

public void setArea(int area) {

this.area = area;

}



In the CircleCarpet class, computedTotalPrice method

Change

double area = ((float)rad2) * Math.PI;

to

super.setArea((int)((double)rad2 * Math.PI));



In the RectangleCarpet class, computedTotalPrice method

Change

double area = hold1;

to

super.setArea((int)hold1);



Have fun.
David W
2013-02-01 17:21:06 UTC
The function that does the calculation of area has a local variable names 'area' but this is beyond the scope of the function that is doing the output.



String line2 = "The Area:\t\t"+area+"\n";



This is not using the 'area' variable from the previous function, because the 'area' variable that stores the value you want is only valid within the scope of the function it was defined in. A really quick (but not technically proper) solution is simply to define the 'area' variable right after you define 'radius' in the class itself...that way it's usable anywhere in the class. (And remove the local declaration from the calculation function)
finney
2016-12-04 12:11:45 UTC
undertaking a million: Your different tactics are interior significant. undertaking 2: Your private variables should not be interior the scope of significant, yet with the classification's scope. place them when you declare the classification. Doing so will enable this methodology to hold mutually good. public classification Calculation { private double AnnualSalary; private double AnnualSales; private double cost; private double acceleration; public static void significant( String[] args ) { } public Calculation() { AnnualSalary = 0; AnnualSales = 0; cost = 0; acceleration = 0; } public Calculation( double aSalary, double aSales, double acommission, double aAcceleration ) { AnnualSalary = aSalary; AnnualSales = aSales; cost = acommission; acceleration = aAcceleration; } public void setAnnualSalary(double aSalary) { AnnualSalary = aSalary; } public void setAnnualSales( double aSales ) { AnnualSales = aSales; } public void setcommission( double acommission ) { cost = acommission; } public void setacceleration( double aAcceleration ) { acceleration = aAcceleration; } public double getAnnualSales() { return AnnualSales; } public double calculateCalculation() { if ( AnnualSales < 64000 ) { return AnnualSalary; } else if ( AnnualSales >= 64000 && AnnualSales < 80000) { return AnnualSalary + ( AnnualSales * cost ); } else { return AnnualSalary + ( AnnualSales * ( cost * acceleration )); } } } What i observed above i fastened throughout this. in case you create a driving force classification, and attempt to create gadgets from this classification, it is going to artwork out completely great. sturdy success, ~Barolb


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...