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

Objectives Practice basic I/O and decision structure You are hired by State Carr

ID: 3707070 • Letter: O

Question

Objectives Practice basic I/O and decision structure You are hired by State Carrier to write a Java program that computes the delivery charge. The company allows two types of packaging letter and box -and three types of service- Next Day Priority, Next Day Standard, and 2-Day. The following table shows the formula for computing the charge Package Ty Letter Next Day Priority Next Day Standard2-Dav $12.00, up to 8 oz $12.00, up to 8 oz Not available Box $7.00 for the first $15.75 for the first S13.75 for the first pound. Add $1.25 for pound. Add $1.00 for pound. Add $0.50 for each addition poundeach addition pound each addition pound over the first pound. over the first pound.over the first nd. Your program prompts users for three integers: (1) type of package (2) type of service (3) weight of the package. For type of package, ask users to enter 1 for Letter, and 2 for Box. For type of type of service, ask users to enter 1 for Next Day Priority, 2 for Next Day Standard, and 3 for 2-Day. Weight will be in oz if the package is Letter, and in pound if the package is Box. Your program will echo the entered data, and then display the computed delivery charge User interface specifications: . Input The program prompts for three values in three separate lines, making sure that the instructions are clear to users as described above. The three integers should be in the order of (1) type of package (2) type of service (3) weight of the package o Each data input line is immediately below the prompt message ine. In other words, after the prompt message is displayed, your program should force users to enter the corresponding integer two numbers on a new line immediately below the prompt message o You can assume users' entered data will be all integers Output Echo the entered data; i.e., type of package, type of service and weight of the package Display the computed delivery charge o o

Explanation / Answer

Java Code :

/**
* This class computes the delivery charges for the state carrier
* it prompts the user to enter the type of packaging, type of service and weight of the package and
* returns the delivery charges.  
* */
import java.util.Scanner;
public class MyClass {

static float calulate_delivery_charges(int t_o_p, int t_o_s, int weight )
{
float charges = 0;
//letter
if (t_o_p == 1)
{
//next day prime
if (t_o_s == 1)
{
charges = 12.0f + 12.0f *(weight / 8);
  
}
// next day
else if(t_o_s == 2)
{
charges = 12.0f + 12.0f * (weight / 8);
}
// 2 day
else{
return -1; //Not available
}
}   
// box
else
{
//next day prime
if (t_o_s == 1)
{
charges = 15.75f + (weight - 1) * 1.25f;
}
// next day
else if(t_o_s == 2)
{
charges = 13.75f + (weight - 1) * 1.0f;
}
// 2 day
else{
charges = 7.0f + (weight - 1) * 0.5f;
}
  
}
return charges;
}
  
public static void main(String args[]) {
float charges;
Scanner in = new Scanner(System.in);
System.out.println("Enter Type of Package ");
int t_o_p = in.nextInt(); //type of Packaging
  
System.out.println("Enter Type of service ");
int t_o_s = in.nextInt(); //type os serice
  
System.out.println("Enter Weight of Package ");
int weight = in.nextInt(); //weight of the parcel
  
// Data entered
System.out.println("Type of Package : " + t_o_p);
System.out.println("Type of service : " + t_o_s);
System.out.println("Weight : " + weight);
if (-1 == (charges = calulate_delivery_charges(t_o_p, t_o_s, weight)))
{
System.out.println("Delivery Not avaible");  
}
System.out.println("Delivery Charges : " + charges);
}
}

---------------------------------------------

Sample input:

2 2 3

output shown after running this code:

Enter Type of Package
Enter Type of service
Enter Weight of Package
Type of Package : 2
Type of service : 2
Weight : 3
Delivery Charges : 15.75