import java.util.*;
public class exercise6_6
{
public static int bumpMe (int price, int increase, boolean updown)
{
if (updown = true)
return price+((increase/100)*price);
else
return price-((increase/100)*price);
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.println("Enter a price in dollars: ");
int price = input.nextInt();
System.out.println("Enter a percentage for increase or decrease: ");
int increase = input.nextInt();
System.out.println("Enter price total going up = true or going down = false: ");
boolean updown = input.nextBoolean();
int total;
total = bumpMe(price, increase, updown);
System.out.println("The new total is $" + total + ".");
}
}
how to use boolean within my method
Page 1 of 18 Replies - 818 Views - Last Post: 14 October 2011 - 03:29 PM
#1
how to use boolean within my method
Posted 14 October 2011 - 02:00 PM
OK I believe the code is pretty much correct. The objective is to have the user input a price then a percentage for the method to either increase or decrease the total. This is done by the boolean updown. Im not sure how to implement the updown though. Currently the program asks for a price then percentage then either true or false and finally computes but always returns the original price input.
Replies To: how to use boolean within my method
#2
Re: how to use boolean within my method
Posted 14 October 2011 - 02:16 PM
Line 7 uses the assignment operator, '=', when you should be using the comparison operator, '=='.
Oh! And int types will not return the results you'd like from your bumpMe method.
Oh! And int types will not return the results you'd like from your bumpMe method.
This post has been edited by GregBrannon: 14 October 2011 - 02:22 PM
#3
Re: how to use boolean within my method
Posted 14 October 2011 - 02:42 PM
Well thanks for the operator help. I would like the end result to be a rounded price to the dollar would I have to switch the int type to double?
#4
Re: how to use boolean within my method
Posted 14 October 2011 - 02:45 PM
There are several ways it could be done. One would be to do the calculation in your bumpMe() method with doubles and then return the result as an int:
Another interesting experiment to try:
Change the signature of your bumpMe() method to use doubles:
but call it as you do now using ints.
One more thing. Give your variables better names. The boolean 'updown' could be changed to isIncreased or some other helpful name so that if it's true, you'll know the value should be increased, decreased if false. Why make your user decode his entry, "up = true or down = false"? Let the user enter "up" or "down" and then have the program set the boolean appropriately.
return (int)result;
Another interesting experiment to try:
Change the signature of your bumpMe() method to use doubles:
public static int bumpMe (double price, double increase, boolean updown)
but call it as you do now using ints.
One more thing. Give your variables better names. The boolean 'updown' could be changed to isIncreased or some other helpful name so that if it's true, you'll know the value should be increased, decreased if false. Why make your user decode his entry, "up = true or down = false"? Let the user enter "up" or "down" and then have the program set the boolean appropriately.
This post has been edited by GregBrannon: 14 October 2011 - 02:57 PM
#5
Re: how to use boolean within my method
Posted 14 October 2011 - 02:51 PM
You have to realize that as increase is a int
((increase/100)
will be 0 for all increase values from 0 to 99
will be 1 for all increase values from 100 to 199
...
better to make your bumpMe() method to return a double
in the method you can use (increase / 100.0) to force a double value in the calculation
in the main() method you can cast your double to int (with or without rounding)
double total = bumpMe(...
int totalToDisplay = (int) (total + 0.5);
System.out.println("Total: " + totalToDisplay + ".00");
Happy coding
((increase/100)
will be 0 for all increase values from 0 to 99
will be 1 for all increase values from 100 to 199
...
better to make your bumpMe() method to return a double
in the method you can use (increase / 100.0) to force a double value in the calculation
in the main() method you can cast your double to int (with or without rounding)
double total = bumpMe(...
int totalToDisplay = (int) (total + 0.5);
System.out.println("Total: " + totalToDisplay + ".00");
Happy coding
This post has been edited by pbl: 14 October 2011 - 02:52 PM
#6
Re: how to use boolean within my method
Posted 14 October 2011 - 03:17 PM
Thank you both, GregBrannon and pbl, I really appreciate your quick responses. The code works perfectly, I do have one more question though. In regards to the rounding part, what is the 0.5 doing when added to the total? I'm confused about how that rounds it to the nearest integer.
#7
Re: how to use boolean within my method
Posted 14 October 2011 - 03:25 PM
Just forcing a round up if > .5
int x = (int) (1.3 + 0.5) = (int) 1.8 = 1
int x = (int) (1.6 + 0.5) = (int) 2.1 = 2
int x = (int) (1.3 + 0.5) = (int) 1.8 = 1
int x = (int) (1.6 + 0.5) = (int) 2.1 = 2
#8
Re: how to use boolean within my method
Posted 14 October 2011 - 03:28 PM
Alright thanks that makes complete sense then. Thanks again.
#9
Re: how to use boolean within my method
Posted 14 October 2011 - 03:29 PM
Glad I could help
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|