PDA

View Full Version : Elements 5th Java Assignment



Element17
10-02-2013, 02:39 AM
Hello guys!


Assignment!

C.
Exercise
An Internet Service provider has three different subscription packages for its customers:
Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
Package C: For $19.95 per unlimited access is provided.
Write a program that calculates a customer’s monthly bill. It should ask the user to enter the letter of the package the customer has purchases (A, B, C) and the number of hours that were used. It should then display the total charge. The program will also calculates and displays the amount of money Package A customers would save if they purchased Package B or C, and the amount of money
Package B customers would save if they purchases Package C. If there would be no savings, no message should be printed.
This assignment will require using a switch statement to check the different package type (A, B, or C). It requires to use System.out.printf( ) method to output the messages instead of System.out.println( ) or Systemout.print( ) or a dialog box. It requires formatting the input and output the same style as shown below in the sample runs.


What I have gotten so far. I just don't know how to use a if/else statement in a switch statement.



import java.util.Scanner;

/*Andrew Varnick
10/1/2013
CSCI 1015
Programming Assignment 5
A program to calculate a monthly bill and tell the customer how much they would save if any.
*/

public class VarnickPass5
{
//Constants
static final double PackA = 9.95;
static final double PackB = 13.95;
static final double PackC = 19.95;



public static void main(String[] args)
{
String input;
String numberOfHoursUsed;
int numberOfHours;
double finalPrice;
double PackA = 9.95;
double PackB = 13.95;
double PackC = 19.95;
double SavingB;
double SavingC;

Scanner keyboard = new Scanner(System.in);

System.out.printf("Choose a package: A, B or C\n");
input = keyboard.nextLine();

switch (input)
{
case "A":{
System.out.printf("How many hours did you use?\n");
numberOfHoursUsed = keyboard.nextLine();
numberOfHours = Integer.parseInt(numberOfHoursUsed);
if (numberOfHoursUsed.equals(10))
{
finalPrice = (PackA);
System.out.printf("The final price is $ %.2f\n", finalPrice);
else
}
finalPrice = ((numberOfHours - 10) * 2 + PackA);
SavingB = (finalPrice - (numberOfHours - 20) * 1 - PackB);
SavingC = (finalPrice - PackC);
System.out.printf("The final price is $ %.2f\n" , finalPrice);
System.out.printf("Savings with Package B $ %.2f\n", SavingB);
System.out.printf("Savings with Package C $ %.2f\n", SavingC);
break;
}
case "B":{
System.out.printf("How many hours did you use?\n");
numberOfHoursUsed = keyboard.nextLine();
numberOfHours = Integer.parseInt(numberOfHoursUsed);
finalPrice = ((numberOfHours - 20) * 1 + PackB);
SavingC = (finalPrice - PackC);
System.out.printf("The final price is $%.2f\n" + finalPrice);
System.out.printf("Savings with Package C $%.20f\n", SavingC);
break;
}
case "C":{
System.out.printf("How many hours did you use?\n"); //Not sure if this is need or the next line
numberOfHoursUsed = keyboard.nextLine(); //Any value inputed will return the same result, correct?
finalPrice = (PackC);
System.out.printf("The final price is $" + finalPrice);
break;
}
}
}

}

riwu
10-02-2013, 11:10 AM
Hello guys!


Assignment!

C.
Exercise
An Internet Service provider has three different subscription packages for its customers:
Package A: For $9.95 per month 10 hours of access are provided. Additional hours are $2.00 per hour.
Package B: For $13.95 per month 20 hours of access are provided. Additional hours are $1.00 per hour.
Package C: For $19.95 per unlimited access is provided.
Write a program that calculates a customer’s monthly bill. It should ask the user to enter the letter of the package the customer has purchases (A, B, C) and the number of hours that were used. It should then display the total charge. The program will also calculates and displays the amount of money Package A customers would save if they purchased Package B or C, and the amount of money
Package B customers would save if they purchases Package C. If there would be no savings, no message should be printed.
This assignment will require using a switch statement to check the different package type (A, B, or C). It requires to use System.out.printf( ) method to output the messages instead of System.out.println( ) or Systemout.print( ) or a dialog box. It requires formatting the input and output the same style as shown below in the sample runs.


What I have gotten so far. I just don't know how to use a if/else statement in a switch statement.



import java.util.Scanner;

/*Andrew Varnick
10/1/2013
CSCI 1015
Programming Assignment 5
A program to calculate a monthly bill and tell the customer how much they would save if any.
*/

public class VarnickPass5
{
//Constants
static final double PackA = 9.95;
static final double PackB = 13.95;
static final double PackC = 19.95;



public static void main(String[] args)
{
String input;
String numberOfHoursUsed;
int numberOfHours;
double finalPrice;
double PackA = 9.95;
double PackB = 13.95;
double PackC = 19.95;
double SavingB;
double SavingC;

Scanner keyboard = new Scanner(System.in);

System.out.printf("Choose a package: A, B or C\n");
input = keyboard.nextLine();

switch (input)
{
case "A":{
System.out.printf("How many hours did you use?\n");
numberOfHoursUsed = keyboard.nextLine();
numberOfHours = Integer.parseInt(numberOfHoursUsed);
if (numberOfHoursUsed.equals(10))
{
finalPrice = (PackA);
System.out.printf("The final price is $ %.2f\n", finalPrice);
else
}
finalPrice = ((numberOfHours - 10) * 2 + PackA);
SavingB = (finalPrice - (numberOfHours - 20) * 1 - PackB);
SavingC = (finalPrice - PackC);
System.out.printf("The final price is $ %.2f\n" , finalPrice);
System.out.printf("Savings with Package B $ %.2f\n", SavingB);
System.out.printf("Savings with Package C $ %.2f\n", SavingC);
break;
}
case "B":{
System.out.printf("How many hours did you use?\n");
numberOfHoursUsed = keyboard.nextLine();
numberOfHours = Integer.parseInt(numberOfHoursUsed);
finalPrice = ((numberOfHours - 20) * 1 + PackB);
SavingC = (finalPrice - PackC);
System.out.printf("The final price is $%.2f\n" + finalPrice);
System.out.printf("Savings with Package C $%.20f\n", SavingC);
break;
}
case "C":{
System.out.printf("How many hours did you use?\n"); //Not sure if this is need or the next line
numberOfHoursUsed = keyboard.nextLine(); //Any value inputed will return the same result, correct?
finalPrice = (PackC);
System.out.printf("The final price is $" + finalPrice);
break;
}
}
}

}

I didnt look at the question, but syntax wise '}' should come before, and not after 'else'.

If all your cases have


System.out.printf("How many hours did you use?\n");
numberOfHoursUsed = keyboard.nextLine();

why put them in the switch block?

Brandon
10-02-2013, 12:42 PM
why put them in the switch block?

Because of the default case. You don't want it to print when the wrong option is entered.

Le Jingle
10-02-2013, 03:20 PM
try to calculate in switch statement and print afterwards, would be my suggestion