Question

In: Computer Science

Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much...

Create JAVA PROGRAM, and write comment for codes also.

4) Prompt the user for how much stuff a truck can carry, in pounds. Then ask them for the weight of each thing they add until they stop. Don't forget to be safe.

Solutions

Expert Solution

import java.util.Scanner;
class Main {
public static void main(String args[]) {
// taking user input of capacity of truck and declaring variables
Scanner sc = new Scanner(System.in);
System.out.print("Enter how much stuff a truck can carry(in pounds): ");
int total = 0, curr, capacity = sc.nextInt();
  
while(true) // looping for infinite times
{
// taking the weight of a product
System.out.print("Enter weight of a product(-1 to stop): ");
curr = sc.nextInt();

if(total + curr > capacity ) // stopping user to enter
{
System.out.print("This thing can't fit in truck.");
break;
}
  
total += curr;
if(total== capacity) // exactly filled case
{
System.out.print("Truck is filled.");
break;
}
}
// printing the filled amount
System.out.println("\nTruck has products of weight " + total );
}
}

/*OUTPUT
Enter how much stuff a truck can carry(in pounds): 100
Enter weight of a product(-1 to stop): 10
Enter weight of a product(-1 to stop): 20
Enter weight of a product(-1 to stop): 30
Enter weight of a product(-1 to stop): 25
Enter weight of a product(-1 to stop): 10
Enter weight of a product(-1 to stop): 20
This thing can't fit in truck.
Truck has products of weight 95

Enter how much stuff a truck can carry(in pounds): 100
Enter weight of a product(-1 to stop): 50
Enter weight of a product(-1 to stop): 50
Truck is filled.
Truck has products of weight 100
*/

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate...
Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate names for a ballot. Then imagine the program is moved to a voting booth. Each voter enters their unique name and who they vote for, and when there are no more voters display who won. (Imagine something outside your program is telling it there are no more voters.)
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of...
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining...
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining a pound. Falling short by 3500 means losing a pound. Get the base amount of calories the person needs in a day (BMR). Then have the user enter in calorie totals for each day and display their weight every week. They might eat more than once in a day.
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
Write a Java program to randomly create an array of 50 double values. Prompt the user...
Write a Java program to randomly create an array of 50 double values. Prompt the user to enter an index and prints the corresponding array value. Include exception handling that prevents the program from terminating if an out of range index is entered by the user. (HINT: The exception thrown will be ArrayIndexOutOfBounds) *Please do it in eclipse and this is java language*
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
Write by hand a full java program that will prompt the user for the length and...
Write by hand a full java program that will prompt the user for the length and width of a square, calculate and display the area of that square rounded to the nearest tenth with an appropriate message.
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user...
IN JAVA PROGRAMMING Write a complete Java program to do the following: a) Prompt the user to enter the name of the month he/she was born in (example: September). b) Prompt the user to enter his/her weight in pounds (example: 145.75). c) Prompt the user to enter his/her height in feet (example: 6.5). d) Display (print) a line of message on the screen that reads as follows: You were born in the month of September and weigh 145.75 lbs. and...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter...
6) Create the following in a Java Program: Create payroll system Prompt the user to enter payroll information Employee name and create variable for scanner Hours worked Hourly pay rate Federal tax rate State tax rate Declare variable doubles for gross pay, federal, state and total deduction Display payroll statement example: Name of employee Hours worked Hourly pay rate Gross pay which is equal hours worked multiply hourly pay rate Federal withholding which is equal of gross pay multiply federal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT