8 Replies - 818 Views - Last Post: 14 October 2011 - 03:29 PM Rate Topic: -----

#1 iggyman11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-October 11

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.
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 + ".");                        
        
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: how to use boolean within my method

#2 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

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.

This post has been edited by GregBrannon: 14 October 2011 - 02:22 PM

Was This Post Helpful? 2
  • +
  • -

#3 iggyman11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-October 11

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?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

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:
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

Was This Post Helpful? 1
  • +
  • -

#5 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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

This post has been edited by pbl: 14 October 2011 - 02:52 PM

Was This Post Helpful? 1
  • +
  • -

#6 iggyman11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-October 11

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.
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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
Was This Post Helpful? 1
  • +
  • -

#8 iggyman11   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 05-October 11

Re: how to use boolean within my method

Posted 14 October 2011 - 03:28 PM

Alright thanks that makes complete sense then. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#9 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: how to use boolean within my method

Posted 14 October 2011 - 03:29 PM

Glad I could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1