Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Need help with simple code

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help with simple code

    Here is what I have for my code, I am trying to use multiple methods to calculate a best plan for the user based on the hours he inputs.

    basically, plan A is $9.95 a month and $2.00 per extra hour and the first 10 hours free.

    plan B is $13.95 and $1.00 per extra hour and first 20 hours free.

    plan C is $19.95 for unlimited usage.

    I am extremely new to programming so I don't know where to begin, the books I've read haven't been very clear either. Thanks for any help

       import java.util.Scanner;
       public class ISProvider2
       {
     
          public static void main(String [] args)
          {
             Scanner input = new Scanner(System.in); //scanner
     
          	//declare variables
             int hours = getHours(input);
     
             double planA = (((hours - 10) *2) + 9.95);
             double planB = (((hours - 20) *1) + 13.95);
             double planC = 19.95;
     
             char plan = bestPrice(planA, planB, planC);
     
             System.out.println("The best plan for you is " + plan);
     
     
          }
     
          public static int getHours(Scanner input)
          {
             System.out.println("Enter your hours: ");
             hours = input.nextInt();
             return hours;
          }
     
          public static double bestPrice(double planA, double planB, double planC)
          {
     
     
             if (planA < planB && planC)
     
                System.out.println("Plan A is best for you.");
     
             else 
                if (planB < planC)
                   System.out.println("Plan B is best for you.");
     
                else 
                   System.out.println("Plan C is best for you.");
     
          }
     
     
     
     
       }


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Need help with simple code

    What is wrong with your code right now?

    I can see one potential logistic mistake and 2 errors that your compiler should catch:
    Logistic Mistakes
    In the following calculations in your main:
    double planA = (((hours - 10) *2) + 9.95);
             double planB = (((hours - 20) *1) + 13.95);
             double planC = 19.95;
    You are assuming that the number of hours inputted will be at least 20. What if the user enters just 9 hours? That would cause a reimbursement of hours from the calculations since the hours, multiplied by the cost, will result in a negative number. To have a "complete" calculation, you should check if the number of hours is less than 10 before calculating Plan A and you should check if the number of hours is less than 20 before calculating Plan B.

    Compiler Error(1)
    Compare the following two lines:
    char plan = bestPrice(planA, planB, planC);
    public static double bestPrice(double planA, double planB, double planC)
    You are declaring the bestPrice method to return a double, but when you retrieve that value in your main you are setting the value to a char. There should (theoretically) be a compiler error. What you should do is change either the call in your main or your method declaration to be consistent with what you are trying to do.

    Compiler Error(2)
    Look at the following method:
    public static double bestPrice(double planA, double planB, double planC)
          {
     
     
             if (planA < planB && planC)
     
                System.out.println("Plan A is best for you.");
     
             else 
                if (planB < planC)
                   System.out.println("Plan B is best for you.");
     
                else 
                   System.out.println("Plan C is best for you.");
     
          }
    You have declared this method to return a double but no where in the method do you have a return statement that returns a double. This will cause a compiler error as a double is expected to be returned.



    Since you have said you are new to programming, I'll help you as much as you can without actually doing it for you. Try to fix the above (compiler errors specifically, the logistical error can be dealt with later). If you still are having trouble with it after you attempt to fix it with what you know, post the new code and we will go from there. If you are unsure how to do something, feel free to ask specifically what you are unsure of.
    Last edited by aussiemcgr; February 21st, 2011 at 07:40 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    19
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with simple code

    Hi there, thanks for the help. I will try it out and see what happens. Unfortunately this is due in a few hours so I don't think I'll be able to solve it out by then. But your help is appreciated still. It has cleared some things up. I'll let you know how it turns out. Thanks

Similar Threads

  1. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM
  2. Simple code error
    By robin28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2010, 03:25 PM
  3. im dying here..on a simple code
    By Jason in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 28th, 2010, 10:33 PM
  4. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  5. Help! how would you code these simple games?
    By makarov in forum Java Applets
    Replies: 1
    Last Post: November 14th, 2009, 12:31 PM