Results 1 to 13 of 13
  1. #1
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default Need help with calling a static method please.

    Here is my code:

    import java.util.*;
    import java.text.NumberFormat;
    import java.lang.*;


    public class EnhancedInvoiceApp{


    public static void main(String[] args){

    double discountAmount=0;
    double total=0;
    double discountPercent;

    String customerType;

    //create a scanner object called sc
    Scanner sc = new Scanner(System.in);
    String choice = "y";//initialize choice



    while(!choice.equalsIgnoreCase("n")){

    //get input from the user
    System.out.println("Enter customer type (r/c/t): ");
    System.out.println();
    customerType = sc.next();



    //check for non r/t/c entry
    if(!(customerType.equalsIgnoreCase("R")||customerT ype.equalsIgnoreCase("T")||customerType.equalsIgno reCase("C"))){
    System.out.println(); //blank line
    System.out.println("Invalid Entry! Re-enter");// if not r, c, or t then print this and re-enter
    System.out.println(); //blank line
    }else{

    try{
    System.out.println("Enter subtotal: ");
    System.out.println();
    double subtotal = sc.nextDouble();


    //calculate the discount amount and total
    discountPercent = 0.0; //initialize
    if (customerType.equalsIgnoreCase("R"))
    if(subtotal<100)
    discountPercent = 0.0;
    else if(subtotal >=100 && subtotal < 250)
    discountPercent = .1;
    else if(subtotal >= 250 && subtotal <500)
    discountPercent = .25;
    else
    discountPercent = .30;
    else if(customerType.equalsIgnoreCase("C"))
    discountPercent = .2;
    else if(customerType.equalsIgnoreCase("T"))
    if(subtotal<500)
    discountPercent = 0.4;
    else
    discountPercent=.5;


    getDiscountPercent(discountPercent, discountAmount, total, subtotal);


    //format the results
    NumberFormat currency = NumberFormat.getCurrencyInstance();
    NumberFormat percent = NumberFormat.getPercentInstance();


    //display the discount and total amounts
    String message = "Discount percent: " + percent.format(discountPercent) + "\n" + "Discount amount: " + currency.format(discountAmount) + "\n"
    + "Invoice total: " + currency.format(total) + "\n";
    System.out.println(message);

    //see if the user wants to continue
    System.out.println("Continue? (y/n): ");
    choice = sc.next();
    System.out.println();
    }
    // catch a letter as input for subtotal and try again
    catch(InputMismatchException e){
    sc.next();//disregaurd the incorrect entry
    System.out.println(); //blank line
    System.out.println("Error! Invalid number entered. Try Again\n");
    System.out.println(); //blank line
    continue;//go to top of loop

    }
    }
    }
    }

    public static double getDiscountPercent(double discountPercent, double discountAmount, double total, double subtotal){

    discountAmount = subtotal*discountPercent;
    total = subtotal - discountAmount;
    return discountPercent;
    }
    }

    I need to call getDiscountPercent and my only parameters should be customerType and subtotal. I have the calcs happening in the getDiscountPercent() method and it is returning zero's. Can someone help me code this section right?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I'm not sure what you're trying to do, but this method doesn't make sense:

    Java Code:
    public static double getDiscountPercent(double discountPercent, double discountAmount, double total, double subtotal){
      discountAmount = subtotal*discountPercent;
      total = subtotal - discountAmount;
      return discountPercent;
    }
    Where is discountPercent acted on in this method? Why are you returning a variable that you don't do anything to?

    Before writing this method, think through what you want it to do and then write out the steps, in English, that are needed to achieve this goal, and then write out the code.

    Also, you've posted over 20 times to this forum, surely by now you know how to use code tags. If so, please edit your post so that they use them, and if not, please read the link in my signature on how to use them.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You say the method only takes two arguments, and then it receives more than 2. Thats a place to start, you also compute a value which is thrown away.

    Also, when you call the method you don't store the return value.

    Java Code:
    methodName(...);
    just calls the method and throws away the return value.

    You can save the value with a call similar to
    Java Code:
    double val = methodName(...);
    If you want to change the values in the method, I suggest you make a class which uses non static methods.

  4. #4
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    When you call methods with parameters, you put the values you want where the parameters are when you call the method.
    Here, You currently have this:
    Java Code:
    getDiscountPercent(discountPercent, discountAmount, total, subtotal);
    This is the same as where you defined the method but without the data type.
    You need to do this:
    Java Code:
    getDiscountPercent([B]ValueFordiscountPercent[/B],[B] ValueFordiscountAmount[/B], [B]ValueFortotal[/B], [B]ValueForsubtotal[/B]);
    You could also do this:
    Java Code:
    getDiscountPercent(2, 4, 5, 6)
    and put numbers in there.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm gonna partially disagree with you, the actual arguments can have the same name as the formal arguments. It may however be a little clearer what is meant by passing in different arguments. His way of calling the method will work fine as long as he has declared and initialized variables of the same name as the actual arguments passed to the method.

  6. #6
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default

    That's why i said to call with different arguments, i don't think i could ever handle variables named exactly like they were used in another function.

  7. #7
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    This is the last part of my assignment and I am up against the clock here. I will quote what the instructions say and maybe you can help me make sense out of it.

    "Code a static method named getDiscountPercent that has two parameters: customer type and subtotal. To do that efficiently, you can move the appropriate code from the main method of the application into the static method and make the required modifications." So from this I was first thinking that the customerType had to be passed, but by association, the discountPercent would have to be passed. right?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toppcon View Post
    This is the last part of my assignment and I am up against the clock here. I will quote what the instructions say and maybe you can help me make sense out of it.

    "Code a static method named getDiscountPercent that has two parameters: customer type and subtotal. To do that efficiently, you can move the appropriate code from the main method of the application into the static method and make the required modifications." So from this I was first thinking that the customerType had to be passed, but by association, the discountPercent would have to be passed. right?
    Wrong.

    • It tells you only two parameters. If I were you I'd follow the instructions, right?
    • The method is what you will use to calculate discountPercent, so there is no need to pass this into the method. Rather you will set the variable = to what the method returns when you call the method in your main program.

  9. #9
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Right. But the discount percent is determined by the customer type...there is no calculating. So do I put the entire if statements into this method then pass the customer type to it and have it return the right discount percent?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toppcon View Post
    Right. But the discount percent is determined by the customer type...there is no calculating.
    No, the discount percent is determined by both the customerType and the subTotal -- both are needed. There is no math equation, but there are decisions that must be made via "conditional" statements (if blocks).

    So do I put the entire if statements into this method then pass the customer type to it and have it return the right discount percent?
    I'm not sure what you mean, but the best thing to do is to try it and see what results. If its output makes sense, you're probably correct, and if not, you can always show us your attempt.

    By the way (and again), please edit your original post and add code tags. You really should be using them at this point; there's no excuse not to.

  11. #11
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    Ok, thanks, and I will try. Deadline is 11:59 so I don't think I'll make it. thanks

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by toppcon View Post
    Ok, thanks, and I will try. Deadline is 11:59 so I don't think I'll make it. thanks
    Best of luck.

  13. #13
    toppcon is offline Member
    Join Date
    May 2011
    Posts
    46
    Rep Power
    0

    Default

    You know, I just moved my if block into the new method and it worked! Sweeet. Thanks again!

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 7
    Last Post: 09-24-2010, 06:09 AM
  3. Replies: 3
    Last Post: 02-09-2010, 06:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 06:05 AM

Posting Permissions

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