answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
mart [117]
1 year ago
6

A shipping company uses the following function to calculate the cost in dollars of shipping based on the weight of the package (

in pounds): Weight Cost Greater than 0 but less than or equal to 1 pound $3.50 Greater than 1 but less than or equal to 3 pounds $5.50 Greater than 3 but less than or equal to 10 pounds $8.50 Greater than 10 but less than or equal to 20 pounds $10.50 Write a program that prompts the user to enter the weight of the package and displays the shipping cost. If the weight is negative or zero, display a message "Invalid input". If the weight is greater than 20 pounds, display a message "The package can not be shipped". Test your program with the following inputs: -10, 0, 0.5, 1, 7.25, 20, 21
Computers and Technology
1 answer:
Gwar [14]1 year ago
8 0

Answer:

I am writing the code in JAVA and C++. Let me know if you want the code in some other programming language. Both the programs works the same but the only difference is that i have used logical operator AND (&&) with C++ code to check each condition.

JAVA code:

import java.util.Scanner;    // to take input from the user

public class ShippingCost {  // ShippingCost class

public static void main(String[] args) {   // main method entry to the program

Scanner input = new Scanner(System.in);  //allows to take input from user

System.out.print("please enter the weight of the package: "); /*prompts the user to enter the weight of package.*/

double weight = input.nextDouble();  /*reads and stores the input values(weight) entered by the user*/

       double cost=0;  //holds the value of cost

 if (weight<=0)  /* checks if the user enters weight value less than or equals to 0 */

 { System.out.println("invalid input."); //prints invalid input

     System.exit(0); //exits the program

 }

    else if (weight > 0 && weight <= 1)  /*if weight is greater than 0 and less than or equal to 1*/

   cost = 3.5;  /*if the above condition is true then it stores the value 3.5 in cost variable */

 else if (weight <= 3)  // if value of weight is less than or equal to 3

   cost = 5.5;  /*if the above condition is true then it stores the value 5.5 in cost variable */

 else if (weight <= 10)  // if value of weight is less than or equal to 10

   cost = 8.5;  /*if the above condition is true then it stores the value 8.5 in cost variable */

 else if (weight <= 20)  // if value of weight is less than or equal to 20

   cost = 10.5;/*if the above condition is true then it stores the value 10.5 in cost variable */

 else  // if the value of weight is greater than 20

 {  System.out.println("The package cannot be shipped");

// displays package cannot be shipped

      System.exit(0);  //exits the program if weight>20}

 System.out.println("The shipping cost is: $" + cost);     /*prints the value stored in the cost from any of the above conditions */

} }

C++ Code

#include <iostream>//to use input output functions

using namespace std;

int main() // start of main() function body

{ double weight; //stores weight value

   double cost= 0; //stores cost value

cout << "please enter the weight of the package:" << endl;

//prompts user to enter the weight

cin >> weight;    

//reads the value of weight entered by the user

  if (weight<=0) //if value of weight entered by user is less than or equal to 0

 { cout<<"invalid input."; //displays invalid input

     exit(0); //exits the program

 }

/*the below else if conditions are checked, if any one of them is true then the cost displayed in the output will be the which is assigned to cost variable of that specific if condition's body which evaluates to true. */

    else if (weight > 0 && weight <= 1)

   cost = 3.5;

 else if (weight > 1 && weight <=3)

   cost = 5.5;

 else if (weight > 3 && weight <= 10)

   cost = 8.5;

 else if (weight> 10 && weight <= 20)

   cost = 10.5;

 else //if weight>20

//displays the message below and exits the program

 {  cout<<"The package can not be shipped";

      exit(0); }

 cout<<"The shipping cost is: $"<<cost;  /*displays the value stored in cost variable of that else-if condition which evaluates to true */

}

Explanation:

Everything is well explained in the comments above. I will summarize it all.

The program takes the input weight from the user and checks the value of weight.

If and else-if conditions are used to check the value of weight.

if the value of weight is less than 0 or equal to 0 then invalid input is displayed.

Else the weight value is checked for the given ranges. && operator is used in each else if statement which is used to specify the range of the weight values the input weight should be in order to display the shipping cost.

For example take this statement: else if (weight > 0 && weight <= 1)

This statement checks the value entered by the user that if it lies in the range mentioned in this statement which is that the weight value should be greater than 0 AND less than or equal to 1. The value cannot lie above or below the given range in order for this condition to be true so && operator is used here.

If the weight entered by user exceeds 20 then the message is displayed:The package can not be shipped and the program exits. In JAVA System.exit(0) and in c++ exit(0) function is used to exit the program.

The output of both the programs is as following:

please enter the weight of the package: 3

The shipping cost is: $ 5.5

You might be interested in
Please help!! Caleb is working on a simple logic-based program to simulate a game of tic-tac-toe. Which programming language wou
irga5000 [103]

Basic language should do it

4 0
2 years ago
A restaurant manager wants to advertise that his lunch special offers enough choices to eat different meals every day of the yea
riadik2000 [5.3K]
2 to 12 options for each customer
6 0
1 year ago
Find a 95% confidence interval for the mean failure pressure for this type of roof panel.The article "Wind-Uplift Capacity of Re
aliya0001 [1]

Answer:

The 95% confidence interval would be given by (2.351;3.525)    

Explanation:

1) Previous concepts

A confidence interval is "a range of values that’s likely to include a population value with a certain degree of confidence. It is often expressed a % whereby a population means lies between an upper and lower interval".

The margin of error is the range of values below and above the sample statistic in a confidence interval.

Normal distribution, is a "probability distribution that is symmetric about the mean, showing that data near the mean are more frequent in occurrence than data far from the mean".

\bar X represent the sample mean  

\mu population mean (variable of interest)

s represent the sample standard deviation

n represent the sample size  

2) Confidence interval

The confidence interval for the mean is given by the following formula:

\bar X \pm t_{\alpha/2}\frac{s}{\sqrt{n}}   (1)

In order to calculate the mean and the sample deviation we can use the following formulas:  

\bar X= \sum_{i=1}^n \frac{x_i}{n} (2)  

s=\sqrt{\frac{\sum_{i=1}^n (x_i-\bar X)}{n-1}} (3)  

The mean calculated for this case is \bar X=2.938

The sample deviation calculated s=0.472

In order to calculate the critical value t_{\alpha/2} we need to find first the degrees of freedom, given by:

df=n-1=5-1=4

Since the Confidence is 0.95 or 95%, the value of \alpha=0.05 and \alpha/2 =0.025, and we can use excel, a calculator or a table to find the critical value. The excel command would be: "=-T.INV(0.025,4)".And we see that t_{\alpha/2}=2.78

Now we have everything in order to replace into formula (1):

2.938-2.78\frac{0.472}{\sqrt{5}}=2.351    

2.938+2.78\frac{0.472}{\sqrt{5}}=3.525    

So on this case the 95% confidence interval would be given by (2.351;3.525)    

5 0
1 year ago
More Loops: All versions of foods.py in this section have avoided using for loops when printing to save space. Choose a version
Nata [24]

Answer:

Following are the program in the Python Programming Language.

#set list type variable and initialize the elements

myPizza = ['Margarita', 'Capsicum and onion', 'Chicken']

#set variable and initialize the elements

frndPizzas = myPizza[:]

#append value in list variable

myPizza.append('Corn')

#append value in list variable

frndPizzas.append('paperica')

#print message

print("My pizzas are:")

#set the for loop and print elements of 1st list

for pizza in myPizza:

   print(pizza)

#print message

print("\nFriend's pizzas are:")

#set the for loop and print elements of 2st list

for frndsPizza in frndPizzas:

   print(frndsPizza)

<u>Output</u>:

My pizzas are:                                                                                                                  

Margarita                                                                                                                       Capsicum and onion                                                                                                               Chicken                                                                                                                          Corn                                                                                                                                                                                                                                                        

Friend's pizzas are:                                                                                                            

Margarita                                                                                                                      Capsicum and onion                                                                                                              Chicken                                                                                                                         paperica    

Explanation:

<u>Following are the description of the program</u>:

  • Set a list data type variable that is 'myPizza' and initializes elements in it.
  • Set another list data type variable that is 'frndPizzas' and initializes elements in it.
  • Append one-one element in both of the list data type variables.
  • Set the for loops to print the elements of both list data type variables.
5 0
1 year ago
In this exercise we look at memory locality properties of matrix computation. Th e following code is written in C, where element
Sonja [21]

Answer:

Explanation:

temporal locality can be defined as: when a particular memory  is referenced or accessed several times  within a specific period of time. In the question, i think the variable that exhibit temporal locality are I, J and 0(all the variable). This is because the variable J and 0 are accessed several times within the loop. I would not have been part of it, but in the A[I][J]=B[I][0]+A[J][I], the variable "I" is also accessed in the addition. this is why it is part of the temporal locality.

4 0
1 year ago
Other questions:
  • U.S. industries like steel, computers, and energy need to be protected from foreign competition to ensure which of the following
    6·2 answers
  • Use a VLOOKUP function in cell I5 to identify and calculate the federal withholding tax. Use the tax rates from the range D21:E2
    15·1 answer
  • In cell B16, enter a function to calculate the total attendance for the years 2014 through 2018 using the totals in the range B1
    10·1 answer
  • A spreadsheet keeps track of student scores on all the exams in a course. Each row of the spreadsheet corresponds to one student
    14·1 answer
  • 14. Emelia is very concerned about safety and has conducted a study to determine how many bike helmets were replaced at each loc
    13·2 answers
  • What is the Gain (dB) of a transmission if the Maximum Data Rate is 1 Gbps and the Bandwidth =7000 MHz? Group of answer choices
    6·1 answer
  • JAVA Write a program that first asks the user to type a letter grade and then translates the letter grade into a number grade. L
    9·1 answer
  • What measures are needed to trace the source of various types of packets used in a DoS attack? Are some types of packets easier
    14·1 answer
  • You are a security consultant and have been hired to evaluate an organization's physical security practices. All employees must
    6·1 answer
  • Which of the following is an advantage of inserting a page number field in your document rather than inserting each page number
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!