Results 1 to 4 of 4
- 02-04-2011, 05:33 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
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;
public class PriceAdjustment {
/**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;
double adjustedPrice; // new price after adjustment
//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);
adjustedPrice = bumpMe(dollar, incr, priceIncrease);
System.out.println("The new price is: " + adjustedPrice);
}//end main
}//end class
- 02-04-2011, 05:47 AM #2
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, 05:53 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
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, 06:08 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 22
- Rep Power
- 0
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;
Similar Threads
-
Public static method error
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-29-2009, 11:10 PM -
public instance method
By steve123 in forum New To JavaReplies: 5Last Post: 06-20-2008, 08:45 PM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 4Last Post: 05-23-2008, 01:05 PM -
calling a public void method from a class button
By supa_kali_frajilistik in forum AWT / SwingReplies: 1Last Post: 05-21-2008, 05:40 AM -
public method
By dirtycash in forum New To JavaReplies: 4Last Post: 11-21-2007, 07:29 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks