Results 1 to 4 of 4

Thread: Elements 5th Java Assignment

  1. #1
    Join Date
    Jun 2007
    Posts
    532
    Mentioned
    1 Post(s)
    Quoted
    68 Post(s)

    Default Elements 5th Java Assignment

    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.

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

    }
    Finished B.S. Program in Radiology!!

    Projects: A big one! Total secret! hehe

  2. #2
    Join Date
    Jan 2012
    Posts
    2,568
    Mentioned
    35 Post(s)
    Quoted
    356 Post(s)

    Default

    Quote Originally Posted by Element17 View Post
    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.

    java Code:
    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
    Code:
            System.out.printf("How many hours did you use?\n");
             numberOfHoursUsed = keyboard.nextLine();
    why put them in the switch block?

  3. #3
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by riwu View Post
    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.
    I am Ggzz..
    Hackintosher

  4. #4
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    try to calculate in switch statement and print afterwards, would be my suggestion

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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