Hi All. Wondering if someone can assist me with this code im trying to write. I have to make a small calculation with random variables but the problem i run into is that i have to pass the calculated variable back to the main method and display it that way. Im not very good with Java but im trying to learn. Here is what i have so far:

public class Calculator
{
	public static void main(String[] args)
	{
	int price = 20000;
	int commission = 10;
	int discount = 15;
	int endPrice;
	endPrice = calculation(price);
	System.out.println("The total price after a " + commission +
	"% commission and a " + discount + "% discount is " + endPrice); 
	}	
	public static int calculation(int finalPrice)
	{
	int productPrice = 20000;
	
	int withSalesCommission = productPrice * (10/100);
	int withCustDiscount = productPrice * (15/100);
	productPrice = withSalesCommission - withCustDiscount;
	return productPrice;
	}
	}

When i run this it tells me that the endprice is equal to 0. I appreciate your help.

Sam

Member Avatar for coil

That is probably because (10/100) and (15/100) is rounded down to 0. You should probably return a double to avoid round-off error, or, at the very end, cast your double to an int.

commented: Good analysis and understanding of the limitations of integers vs. doubles in calculation(s) +1

how would i go about doing that? Im not very good with Java, i just started trying to learn it. Im better with Visual Basic.

Thanks,
Sam

Instead of hardcoding the integer literals: 12/100
define a double with the value 0.12 and use that in your computations.

or make the literals doubles by adding .0 at the end: 12.0

I tried to declare to double variables to the calculation method and it came up with a compiling error: Calculator.java:20: possible loss of precision
found : double
required: int
productPrice = withSalesCommission - withCustDiscount;
heres what i changed to:

int price = 20000;
	int commission = 10;
	int discount = 15;
	int endPrice;
	endPrice = calculation(price);
	System.out.println("The total price after a " + commission +
	"% commission and a " + discount + "% discount is " + endPrice); 
	}	
	public static int calculation(int finalPrice)
	{
	int productPrice = 20000;
	double commission = .10;
	double discount = .15;
	double withSalesCommission = productPrice * (commission/100);
	double withCustDiscount = productPrice * (discount/100);
	productPrice = withSalesCommission - withCustDiscount;
	return productPrice;
	}
	}

edit to the last one, i took out the /100 part. stupid mistake but still same error

Integer division discards remainders and returns the integer result, so for example 5/2 evaluates to 2 - there are two twos in five. Under integer division, x/y means "how many times can I subtract y from x and still get a positive result?" So in your calculation, 10/100 comes out to 0 (there are zero one hundreds in 10), and so does 15/100, for the same reason. Try doing the multiplication first, then the division.

You should look up the difference between integer arithmetic and floating-point arithmetic, as well, since it sounds like you haven't had any exposure to that.

I have do your correction of your wrong code.

public class Calculation
{
	public static void main(String[] args)
	{
	double price = 20000; // correction data type
	double commission = 10; //correction data type
	double discount = 15; //correction data type
	double endPrice; // correction data type
	endPrice = calculation(price,commission,discount); // use 3 parameter to pass the value to the function, so you no need to declare variable again inside the function
	System.out.println("The total price after a " + commission +
	"% commission and a " + discount + "% discount is " + endPrice); 
	}

/*all variable value in the main function will be pass to this function
 you can name your variable with any name,but it is good to follow the variable name is same as variable name in the main function.please note that you use function datatype is double not integer, so you need to declare a variable double datatype to return the value to this function
*/
	public static double calculation(double price, double commission,double discount){

		double productPrice; // use datatype double
		double withSalesCommission = price*(commission/100);//output:2000
		double withCustDiscount = price * (discount/100);//output:3000
		productPrice = withSalesCommission - withCustDiscount; //output -1000.0
		return productPrice;
		
	}
	
	}

Please note that the output will be -1000.0
if you want to the output like this -1000, please add String.format
for example

System.out.println("The total price after a " + commission +"% commission and a " + discount + "% discount is " + String.format("%.0f",endPrice)); //output : -1000

System.out.println("The total price after a " + commission +"% commission and a " + discount + "% discount is " + String.format("%.2f",endPrice)); //output : -1000.00

I hope this solution will help you to understand. Thank you very much

commented: Give help, not code. +0

Please change the class from Calculation to Calculator to working properly.
TQ

Hi all, im getting the same error. I changed to everything rushdan said, and i appreciate the visual to look at there. Im still getting the same error about the required int.
"Calculator.java:18: possible loss of precision
found : double
required: int
productPrice = withSalesCommission - withCustDiscount;
I changed some of my calculations, if my calculations are correct the answer im looking for should be 18,700 using the variables i have. Heres what i have now:

public class Calculator
{
	public static void main(String[] args)
	{
	double price = 20000.0;
	double commission = 10;
	double discount = 15;
	double endPrice;
	endPrice = calculator(price,commission,discount);
	System.out.println("The total price after a " + commission +
	"% commission and a " + discount + "% discount is " + endPrice); 
	}	
	public static int calculator(double price, double commission, double discount)
	{
	int productPrice;
	double withSalesCommission = price * (commission/100) + price;
	double withCustDiscount = withSalesCommission * (discount/100);
	productPrice = withSalesCommission - withCustDiscount;
	return productPrice;
	}
	}

Thanks much again for everyones help.
Sam

I've been working on this for a while now and i get it to run but it gives my price an answer of 20000 and it should be 18700. Is my productPrice variable not being passed back to the main method correctly?

Thanks again,
Sam

Okay, this is the problem with being given the rote code. You just try to change your stuff to match what you're given, and when you fail to change everything, you don't understand what's going on.

Go back to your own work and ignore rushdan's stuff. There's no use turning in work that you don't understand, it just means you'll have a harder time with the next assignment.

Try debugging your code by adding println()s at each step to show the intermediate values. Then you'll be able to see where your equations are wrong.
For example:
System.out.println("(commission/100)=" + (commission/100));
Add statements like the above for every sub equation you are using.

Member Avatar for coil

The error you are getting in calculation probably goes back to the "possible loss of precision" error you got.

Basically, you are subtracting two doubles and assigning the result to an int.

The problem is pretty clear:

5.9-4.1=1.8, but if you assign it to an int, you get 1.

The issue now stands that i cannot get the variable from the calculation method passed back to the main method. It shows that the price is 20000, which is the original price variable and the answer should be 18700. I worked out the whole int issue for now.

Did you change this:
public static int calculator(
to this:
public static double calculator(

You'll have to post your code if you expect any more help

I just tried changing that, it just give me 20000 still as my answer. Shouldnt it return an int value tho if the answer is 18700? Here is my code:

public class Calculator
{
	public static void main(String[] args)
	{
	int price = 20000;
	int commission = 10;
	int discount = 15;
	double endPrice;
	endPrice = calculator(price,commission,discount);
	System.out.println("The total price after a " + commission +
	"% commission and a " + discount + "% discount is " + endPrice); 
	}	
	public static double calculator(int price, int commission, int discount)
	{
	int productPrice;
	int withSalesCommission = price * (commission/100) + price;
	int withCustDiscount = withSalesCommission * (discount/100);
	productPrice = withSalesCommission - withCustDiscount;
	return productPrice;
	}
	}

I guess you missed my earlier post on how you can find your problem. Here it is again:

Try debugging your code by adding println()s at each step to show the intermediate values. Then you'll be able to see where your equations are wrong.
For example:
System.out.println("(commission/100)=" + (commission/100));
Add statements like the above for every sub equation you are using.

Norm, i seen your post and i tried it on one calculation i was doing and it gave me an error of value type and whats expected again so i took it back out.

it gave me an error

You need to try again. If you get errors, copy and paste the full text here, don't ignore them.

ok i finally got it, much with the help from everyone and hours of work. Thanks much everyone. Here is what i came up with:

public class Calculator
{
	public static void main(String[] args)
	{
	int price = 20000;
	int commission = 10;
	int discount = 15;
	double endPrice;
	endPrice = calculator(price,commission,discount);
	System.out.println("The total price after a " + commission +
	"% commission and a " + discount + "% discount is " + endPrice); 
	}	
	public static double calculator(int price, double commission, double discount)
	{
	double productPrice;
	double withSalesCommission = 0.10;
	commission = price * withSalesCommission + price;
	double withCustDiscount = 0.15;
	discount = commission * withCustDiscount;
	productPrice = commission - discount;
	return productPrice;
	}
	}
Member Avatar for coil

I doubt the problem lies with the value not being returned correctly. Try putting a System.out.println(productPrice) just before you return it, in your calculator method, and see if it is the right value.

Okay, this is the problem with being given the rote code. You just try to change your stuff to match what you're given, and when you fail to change everything, you don't understand what's going on.

Go back to your own work and ignore rushdan's stuff. There's no use turning in work that you don't understand, it just means you'll have a harder time with the next assignment.

Please don't underestimate the other people help. I'm just to help him as soon as possible. If wrong what I'm doing , just tell me.

TQ

Rushdan I do appreciate your help, even if it wasn't right, it was an experience for me to learn from and I much appreciate anyone's suggestions.

Sam

Rushdan - the problem is that a person who doesn't write programs never learns to write programs, and a person who is handed a program will not write that program.
So, when you "help" someone by handing them some code which returns about what they wanted, they never learn to write it themselves, so they never actually understand it, so what kind of help is that?

If you're actually the sort of person who will write programs - for a living or for satisfaction - here's something very satisfying about getting to the bottom of a puzzle like this with hints instead of handouts. If the guy's going to be any good at this, he'll figure it out. If he's not, better he find that out now, don't you think?

I've programmed a lot with visual basic before, so I did have a thought that rushdan's idea of changing them all to doubles was odd, becuase you have to do that in visual basic also, but it was worth a shot to try just in case, as soon as it didn't work I change my variable type back. No worries guys, its all a big learning process and I'm learning from even the wrong instructions given to me. I again thank you all for the help.

Sam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.