Results 1 to 4 of 4
  1. #1
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default 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

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    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.

  3. #3
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default 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.

  4. #4
    BillyB is offline Member
    Join Date
    Nov 2010
    Posts
    22
    Rep Power
    0

    Default 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

  1. Public static method error
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 11:10 PM
  2. public instance method
    By steve123 in forum New To Java
    Replies: 5
    Last Post: 06-20-2008, 08:45 PM
  3. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 4
    Last Post: 05-23-2008, 01:05 PM
  4. calling a public void method from a class button
    By supa_kali_frajilistik in forum AWT / Swing
    Replies: 1
    Last Post: 05-21-2008, 05:40 AM
  5. public method
    By dirtycash in forum New To Java
    Replies: 4
    Last Post: 11-21-2007, 07:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •