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
Code :
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.");
}
}
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:
Code java:
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:
Code java:
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:
Code java:
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.
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