Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; import java.util.Date; import java.util.Calendar; impo

ID: 3594978 • Letter: I

Question

import java.util.Scanner;
import java.util.Date;
import java.util.Calendar;
import java.text.*;

class PA1
{
public static void main(String args[])
{
int cablePkg,noOfMovies,totalMovieCharges;
int cableSrv=0;
  
while(true)
{
  
System.out.println("WELCOME TO SA CABLE ");
System.out.println("Please enter your name:");
Scanner in=new Scanner(System.in);
String customerName=in.nextLine();
System.out.println("SA CABLE - SUBSCRIPTION PACKAGES ");
System.out.println("1.Basic: Local & major TV network channels $35");
System.out.println("2.Deluxe:Local, major TV, cable & 100 other channels $75");
System.out.println("3.Premium: Deluxe package plus HBO, on-demand & 300 other channels $110");
System.out.println(" Select your cable subscription package:");
  
cablePkg=in.nextInt();
System.out.println(cablePkg);
  
if(cablePkg !=1 && cablePkg !=2 && cablePkg !=3)
{
  
  
System.out.println("Invalid selection!");
System.out.print("Please try again:");
cablePkg=in.nextInt();
  
  
}
System.out.println("SA CABLE-MOVIES ");
System.out.println("Enter the number of Movies-On-Demand-HD purchases:");
noOfMovies=in.nextInt();
totalMovieCharges=noOfMovies*7;
  
  
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");
  
if(cablePkg == 1)
{ cableSrv = 35;
System.out.printf("SA CABLE CHARGES AS OF "+sdf.format(date));
System.out.println("Customer:"+customerName.toUpperCase()+" ");
System.out.println("Cable Service:"+" "+"$"+cableSrv);
System.out.println("Movie-On-Demand-HD: "+" "+"$"+totalMovieCharges);
System.out.println("TOTAL DUE:"+" "+"$"+(cableSrv+totalMovieCharges));
  
  
}
else if(cablePkg == 2)
{ cableSrv = 75;
System.out.println("SA CABLE CHARGES AS OF "+sdf.format(date));
System.out.println("Customer:"+customerName.toUpperCase()+" ");
System.out.println("Cable Service:"+" "+"$"+cableSrv);
System.out.println("Movie-On-Demand-HD: "+" "+"$"+totalMovieCharges);
System.out.println("TOTAL DUE:"+" "+"$"+(cableSrv+totalMovieCharges));
  
  
}
  
else if(cablePkg == 3)
{
cableSrv = 110;  
System.out.println("SA CABLE CHARGES AS OF "+sdf.format(date));
System.out.println("Customer:"+customerName.toUpperCase()+" ");
System.out.println("Cable Service:"+" "+"$"+cableSrv);
System.out.println("Movie-On-Demand-HD: "+" "+"$"+totalMovieCharges);
System.out.println("TOTAL DUE:"+" "+"$"+(cableSrv+totalMovieCharges));
  
  
}
else
{  
System.out.println("SA CABLE CHARGES AS OF "+sdf.format(date));
System.out.println("Customer:"+customerName.toUpperCase()+" ");
System.out.println("Cable Service:"+" "+"$"+cableSrv);
System.out.println("Movie-On-Demand-HD: "+" "+"$"+totalMovieCharges);
System.out.println("TOTAL DUE:"+" "+"$"+(cableSrv+totalMovieCharges));  
}
  
System.out.println("Enter 'Y' to continue or 'N' to exit:");  
char toContinue=in.next().charAt(0);
in.close();
if(toContinue =='y')
{
continue;
}
else
{
break;
}   
  
}
System.exit(0);
}
  
}

3. Logical Control Structures a. Modify the while loop from PA1 to process new customers. All the code dealing with customers will be in this loop. Set the new loop-control variable, called another, to a value that automatically enters the loop the first time b. Use a do-while loop to process multiple cable package subscriptions per customer This means that the do-while is embedded in the while. All the code dealing with subscriptions will be in this loop. Its loop-control variable is the pre-existing toContinue. 1) Within this do-while, code a nested do-while for the cablePkg prompt. If the cablePkg is below 1 OR above 3, print the "Invalid! Choose 1, 2 or 3." error message. 1) The loop-control variable for the nested do-while is cablePkg. 2) The test expression for the ifand the nested do-while is a) cablePkg1 cablePkg > 3 b) The Il are two vertical lines representing the OR logical operator that joins the 2 relational expressions. Press the "shift" key and hit the " key on your keyboard twice c. Use a switch to figure out the charge for the cable service and the subscription type d. There will be NO if-else structures. The only decision structures are the if mentioned in e. The output is to be created in segments. To do this a cableBill variable of type String (basic, deluxe, premium) based on whether the customer enters 1, 2, or 3. The new variable cableSub stores the subscription type 3.b and the switch mentioned above in 3.c. has to be declared and then populated as the program progresses 1) Right after the customer name is captured assign the header, date and customer name to that variable: cableBill-String.format("%n%n%s %tD + "%nCustomer: %S", "SA CABLE CHARGES AS OF". dateTime, customerName): 2) Right after the calculations add the individual subscription and the number of movies purchased to the cable bill (the format specifiers contain field lengths for left and right justification). String.format("%n%nCable Service: +"%nMovies-On-Demand-HD: %,-9d%14c%,12.2t. cableSub, "$', cableSrv, noOfMovies, , movieCharges) cableBill %-7s%22c%, 12.2f" 3) When there are no more cable subscriptions for a customer, add the total lines to the cableBill, then print the cableBill with the "Thank you for boing a valued SA Cable customer!" message. cableBill += String.format("%n%nTotal Cable Services: %22c%,12.2f" +"%nTotal Movies-On-Demand-HD: %17C%,12.2f" +"%n%nTOTAL DUE: %33c%,12.2f" + "%n%nThank you for being a valued SA Cable customer!%n" "$, totalCableSrvs,, totalMovieCharges, '$', total);

Explanation / Answer

import java.util.Scanner;

import java.util.Date;

import java.util.Calendar;

import java.text.*;

class PA1

{

public static void main(String args[])

{

int cablePkg,noOfMovies,totalMovieCharges;

int cableSrv=0;

String cableSub="";

char another='Y';

while(another=='Y')

{

System.out.println("WELCOME TO SA CABLE ");

System.out.println("Please enter your name:");

Scanner in=new Scanner(System.in);

String customerName=in.nextLine();

char toContinue='Y';

do{

System.out.println("SA CABLE - SUBSCRIPTION PACKAGES ");

System.out.println("1.Basic: Local & major TV network channels $35");

System.out.println("2.Deluxe:Local, major TV, cable & 100 other channels $75");

System.out.println("3.Premium: Deluxe package plus HBO, on-demand & 300 other channels $110");

System.out.println(" Select your cable subscription package:");

cablePkg=in.nextInt();

System.out.println(cablePkg);

if(cablePkg < 1 || cablePkg > 3){

do

{

System.out.println("Invalid selection!");

System.out.print("Please try again:");

cablePkg=in.nextInt();

}

while(cablePkg < 1 || cablePkg > 3);

}

System.out.println("SA CABLE-MOVIES ");

System.out.println("Enter the number of Movies-On-Demand-HD purchases:");

noOfMovies=in.nextInt();

totalMovieCharges=noOfMovies*7;

Date date = Calendar.getInstance().getTime();

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy");

switch (cablePkg) {

case 1:

cableSrv = 35;

cableSub="basic";

break;

case 2:

cableSrv = 75;

cableSub="deluxe";

break;

case 3:

cableSrv = 110;

cableSub="premium";

break;

}

System.out.println("SA CABLE CHARGES AS OF "+sdf.format(date));

System.out.println("Customer:"+customerName.toUpperCase()+" ");

System.out.println("Cable Service:"+" "+"$"+cableSrv);

System.out.println("Cable Subscription type:"+" "+cableSub);

System.out.println("Movie-On-Demand-HD: "+" "+"$"+totalMovieCharges);

System.out.println("TOTAL DUE:"+" "+"$"+(cableSrv+totalMovieCharges));  

System.out.println("Enter 'Y' to add more subscriptions for a customer or 'N' to exit:");

toContinue=in.next().charAt(0);

}

while(toContinue=='Y');

System.out.println("Enter 'Y' to add another customer or 'N' to exit:");

another=in.next().charAt(0);

}

}

}