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

(Java/Chapter 3) Using the decision control structure. Have 2 unrelated tasks pe

ID: 3748518 • Letter: #

Question

(Java/Chapter 3) Using the decision control structure.

Have 2 unrelated tasks performed in one Java file.

Part 1 ) Prompt the user to enter his or her name and an amount of money in cents. Calculate and
display the user’s name and the number of half-dollars, quarters, dimes, nickels, and pennies. The
modulus operator will come in handy here.


Part 2 )
Write a program that calculates the shipping cost for packages based on the weight of the package.

Use the following set of decisions for calculating the shipping cost based on the weight of a
package:

_ if the package weighs more than 0 pounds and less than 2 lbs, then the shipping cost is
$0.75/lb
_ if the package weighs between 2 and 10 lbs, then the shipping cost is $1.12/lb
_ if the package weighs more than 10 lbs. and 25 lbs or less, then the shipping cost is $1.87/lb
_ if the package weights over 25 lbs, then there is a at rate shipping cost of $50
_ if the package weights 0 or less, issue an error message and do not compute anything.

Part 1) SAMPLE RUN:

Enter your name: Billy
Enter the amount of money in cents: 41
Number of half-dollars = 0
Number of quarters = 1
Number of dimes = 1
Number of nickels = 1
Number of pennies = 1

Part 2) SAMPLE RUN

Enter the package weight in lbs: 0
ERROR: Cannot process this data

Enter the package weight in lbs: 13
The shipping cost for a package weighing 13 lbs is $24.31

Explanation / Answer

import java.util.Scanner;
//We have to import Scanner for user input
class Chegg{
public static void main (String args[]){
Scanner in = new Scanner(System.in);
//Declearing Scanner object for our use as in
//op cents and weight are variables of type int
int op,cents,weight;
//Happens forever until user want to exit
while(true){
//User option selection
System.out.print("1.Cents conversion 2.Shipping cost Calculation 0.Exit Select option : ");
//next.Int() for taking input of type int
op = in.nextInt();
//Switch programm as per user option
switch(op){
//Cents convertion
case 1:
String str;
System.out.print("Enter your name: ");
str = in.next();
//in.next will take string one word from user
System.out.print("Enter the amount of money in cents: ");
cents = in.nextInt();
//50 cents = 1 half-dollar
//We use modulus operator to get reamaing cents
System.out.println("Number of half-dollars = "+ cents/50);
cents %= 50;
//25 cents = 1 quarter
System.out.println("Number of quarters = "+cents/25);
cents %= 25;
//10 cents = 1 dime
System.out.println("Number of dimes = "+cents/10);
cents %= 10;
//5 cents = 1 nickel
System.out.println("Number of nickels = "+cents/5);
cents %= 5;
//1 cent = 1 pennie
System.out.println("Number of pennies = "+ cents/1);
break;
case 2:
//Shipping cost taking weight input from user
System.out.print("Enter the package weight in lbs: ");
weight = in.nextInt();
//Changing rate as per user input
if(weight<=0)
System.out.println("ERROR: Cannot process this data");
//Error handling
else if(weight<3)
System.out.printf("The shipping cost for a package weighing 13 lbs is $%.02f",((float)weight)*0.75);
//Cost is in float so we have to fist convert our weight to float and then multiply to get cost
else if(weight<26)
System.out.printf("The shipping cost for a package weighing 13 lbs is $%.02f",((float)weight)*1.87);
else
System.out.printf("The shipping cost for a package weighing 13 lbs is $%.02f",((float)weight)*50);
break;
case 0:
//If user wants to exit the program
System.exit(0);
break;
default:
//If user selection not valid
System.out.println("Select valid option");
}
}
}
}