Reputation: 235
Here is my main method code:
public class WeightCalculatorTest {
public static void main(String[] args) {
WeightCalculator weCa_init = new WeightCalculator(100);
weCa_init.displayWeight();
WeightCalculator weCa_def = new WeightCalculator();
weCa_def.displayWeight();
WeightCalculator weCa = new WeightCalculator(123);
weCa.setWeight();
weCa.displayWeight();
}
}
Here is the method:
import java.util.Scanner;
public class WeightCalculator {
private int weight = 0;
private int hundreds;
private int fifties;
private int twenties;
private int tens;
private int fives;
private int ones;
private int total;
private int[] result;
// Default constructor
public WeightCalculator()
{
weight=0;
System.out.println();
}
// Initial constructor
public WeightCalculator(int w)
{
weight=w;
System.out.println();
}
public void setWeight( ) {
Scanner keyboard = new Scanner (System.in);
System.out.println("Life needs a balance");
System.out.print("How many pounds does the item weight? ");
weight = keyboard.nextInt();
}// end inputWeight
public int[] calculateBalanceWeights() {
hundreds = weight/100;
weight %= 100;
fifties = weight/50;
weight %= 50;
twenties = weight/20;
weight %= 20;
tens = weight/10;
weight %= 10;
fives = weight/5;
ones = weight %5;
total = hundreds+fifties+twenties+tens+fives+ones;
int [] result = {hundreds, fifties, twenties, tens, fives, ones, total};
return result;
}// end calcDays
public void displayWeight() {
System.out.println("You need "+calculateBalanceWeights()[6]+" weights in total:");
System.out.println(" "+hundreds +" 100 lbs");
System.out.println(" "+fifties +" 50 lbs");
System.out.println(" "+twenties +" 20 lbs");
System.out.println(" "+tens +" 10 lbs");
System.out.println(" "+fives +" 5 lbs");
System.out.println(" "+ones +" 1 lbs");
}// end of display
}// end of class Time
My question is in this part of my code:
System.out.println("You need "+calculateBalanceWeights()[6]+" weights in total:");
why if I reference total it does not work - shows 0, while other variables can be referenced by name. And if I reference for other varibales in displayWeight as calculateBalanceWeights()[0] for hundreds it does not work?
I`m really confused how return variables are stored and referenced by other methods?
Aslo from the main method I can print the result array with
System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));
But the results are wrong for the number entered?
Upvotes: 0
Views: 743
Reputation: 37691
I didn't get the problem. I guess your program is working correctly. I ran your code and got the following.
For the following part of your code:
WeightCalculator weCa_init = new WeightCalculator(100);
weCa_init.displayWeight();
Program outputs the following.
You need 1 weights in total:
1 100 lbs
0 50 lbs
0 20 lbs
0 10 lbs
0 5 lbs
0 1 lbs
The result contains 1 100 lbs
because of new WeightCalculator(100)
.
For the following part of your code:
WeightCalculator weCa_def = new WeightCalculator();
weCa_def.displayWeight();
Program outputs the following.
You need 0 weights in total:
0 100 lbs
0 50 lbs
0 20 lbs
0 10 lbs
0 5 lbs
0 1 lbs
This time you initialized object with no value [new WeightCalculator()
]. Thats why everything is zero here.
Please note that you have two constructor, one with parameter and one without parameter.
For the following part of your code:
WeightCalculator weCa = new WeightCalculator(123);
weCa.setWeight();
weCa.displayWeight();
Program outputs the following.
Life needs a balance
How many pounds does the item weight? 373
You need 8 weights in total:
3 100 lbs
1 50 lbs
1 20 lbs
0 10 lbs
0 5 lbs
3 1 lbs
This time you initialized object with a value [new WeightCalculator(123)
] but after calling setWeight()
, program gets 373
as user input and thus provide output accordingly.
So, what is the unexpected thing happening here?
Update
Why if I reference
total
it does not work - shows 0, while other variables can be referenced by name.
You are getting zero as output when you use,
System.out.println("You need "+ total +" weights in total:");
inside displayWeight()
function.
But your code works correctly when you write,
System.out.println("You need " + calculateBalanceWeights()[6] + " weights in total:");
This is obvious because a call to the function calculateBalanceWeights()
updates all the variables - hundreds, fifties, twenties, tens, fives, ones and total
.
When you simply use total
inside displayWeight()
, you are not making a call to the calculateBalanceWeights()
function. As a result, no variable gets updated.
Do the following to achieve your desired behavior.
public void displayWeight() {
calculateBalanceWeights();
System.out.println("You need "+total+ " weights in total:");
System.out.println(" "+hundreds +" 100 lbs");
System.out.println(" "+fifties +" 50 lbs");
System.out.println(" "+twenties +" 20 lbs");
System.out.println(" "+tens +" 10 lbs");
System.out.println(" "+fives +" 5 lbs");
System.out.println(" "+ones +" 1 lbs");
}
This code snippet provides output correctly.
If I reference for other varibales in displayWeight as
calculateBalanceWeights()[0]
for hundreds it does not work!
Because you can not call calculateBalanceWeights()
many times. Once you call calculateBalanceWeights()
, all the parameter values (hundreds, fifties, twenties, tens, fives, ones and total
) gets updated.
So, just call calculateBalanceWeights()
once and save the return value in a variable and then use it for printing results.
Aslo from the main method I can print the result array with
System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));
But the results are wrong for the number entered?
The numbers are not wrong. I checked it. For example, if user gives 373
as input,
System.out.print(Arrays.toString(weCa.calculateBalanceWeights()));
prints the following.
[3, 1, 1, 0, 0, 3, 8]
Just compare it with what you are returning from calculateBalanceWeights()
as given below.
int [] result = {hundreds, fifties, twenties, tens, fives, ones, total};
Hopefully, the updated answer will sort your confusion.
Upvotes: 1