# passing percent to public int method

• 02-04-2011, 06:33 AM
BillyB
passing percent to public int method
I believe the author of the text I am using made an error in the problem he has presented. The problem asks to write a main method that includes a percentage of increase and pass that to an int(parameters) method and recalculate. Below is the entire code I have so far. Is there a solution? The problem seems confusing to me. Thank you all for your help.

import java.util.Scanner;

/**Write a method
* int bumpMe(int price, int increase, boolean updown)
* that accepts a price in dollars and returns a new
* price rounded to the nearest dollar, after
* increasing/decreasing price by increase percent. If
* updown is true then you should increase the price;
* otherwise, decrease the price. Write an appropriate
* main(...() method to test bumpMe(...).
*
* @author: Billyb
*/

public static int bumpMe(int price, int increase, boolean updown)
{
//convert increase to decimal
(double) increase = increase /100; //convert to decimal

double newPrice = price * (1 + increase);
newPrice = Math.round(newPrice);

return newPrice;

}//end bumpMe

public static void main(String[] args) {
//declare variables
int dollar; //original price
int incr; //the increase
boolean priceIncrease = true;

//initialize Scanner
Scanner input = new Scanner(System.in);
System.out.println("Enter the original price: ");
dollar = input.nextInt();

System.out.print("Enter the increased % ");
System.out.println(": (enter - for negative numbers");
incr = input.nextInt();

if (incr <=0)
priceIncrease = false;

System.out.println("Value of dollar = " + dollar);
System.out.println("Value of % is = " + incr);
System.out.println("Value of priceIncrease is = " + priceIncrease);

System.out.println("The new price is: " + adjustedPrice);
}//end main
}//end class
• 02-04-2011, 06:47 AM
Junky
I think the intent is to increase by 5 percent not 5.75 percent. Therefore using an int for percent is fine.

One problem I see is that you do not use the updown parameter in the bumpMe method.
• 02-04-2011, 06:53 AM
BillyB
I'm not sure I get the logic of the problem then
If the original price is \$5 and the increase is 5% then the new price should be %.25 but because the returned value is an int, the returned value is \$5. But by what the author asked for, that is probably the correct answer. Right?

And thanks. You are right about the uptown parameter. Thank you.
• 02-04-2011, 07:08 AM
BillyB
A can't convert double to int error
Junky,

I re-wrote the bumpMe method as follows and get an the error that I
can't cast double to int: Thanks again

public static int bumpMe(int price, int increase, boolean updown)
{
if(updown = true){
//convert increase to decimal
increase = increase /100;
double newPrice = price * (1 + increase);
newPrice = Math.round(newPrice);
}
else {
increase = increase /100;
double newPrice = price * (1 - increase);
newPrice = Math.round(newPrice);
}
//also tried return (int)newPrice;
return newPrice;