Question
Preliminaries Create a project submission folder, in the form Lab#FirstNameLastName. For example, mine would be Lab4DavidLiu Create an Eclipse Java Project. Add a Java class
Preliminaries
Create a project submission folder, in the form Lab#FirstNameLastName. For example, mine would be Lab4DavidLiu
Create an Eclipse Java Project.
Add a Java class named MassHandler to your project
Exercises
Part 1) Simple Input 4 points
This part is based on Chapter 3 Exercise 5, Mass and Weight (page 185). You should write code that does the following
Create a scanner object and get an objects name from the user.
Get the objects mass from the user. Display the objects name when asking for the mass (See Part 2 for sample output). Note: Directly after calling nextDouble(), also call nextLine(). Java console input can sometimes leave residual newlines, which mess with future inputs
Calculate the weight with weight = mass * 9.8
Write an if statement that checks the weight. If weight > 1000 Newtons, display objectName is too heavy! (Where object name is the name input at step a). If the object weighs less than 10, display objectName is too light! Otherwise, display objectName weighs X, where X is the weight from part c. Use String.format (page 175) to have it display only one decimal point.
As always, output can be slightly different so long as the weight and objectName are displayed.
Verify that your code works for all three cases before moving on
Part 2) Basic Loop 6 points
This part extends the code from Part 1 into a loop.
Take the code from part 1 and put it into a while loop. The while loop should run until the user inputs a -5, at which point the while loop should end. In this case, the input is a sentinel value. Note: you will have to get the first name and first mass outside of the loop for things to work how I intend it to.
The input that ends the loop should be the objects mass, not the objects name, to prevent the need to convert data types
Sample Output after Part 2:
Enter the object's name> Honda Accord
Enter Honda Accord's mass> 240
Honda Accord is too heavy.
Enter the object's name> Feather
Enter Feather's mass> 0.001
Feather is too light.
Enter the object's name> Running Shoes
Enter Running Shoes's mass> 2.5
Running Shoes's weight is 24.5 Newtons.
Enter the object's name> Panda
Enter Pandas mass>-5
Part 3) Some basic stats 10 points
The final part of the lab is concerned with the collection of a few basic stats, using counters.
Modify the program to keep track of all objects input into the system into a counter (an int variable). Increment the counter by one each time an objects name and mass is input.
Modify the program to display the Total Input Count at the end, after the user inputs -5 and before ending the program
Modify the program again to have another counter, countValid (or something similar), which is only incremented when the objects weight is accepted (between 1000 and 10). Display this counter at the end as well
Finally, modify the program to keep a running total of the weight in Newtons of all valid objects. Objects that are too heavy or too light should not be included in this count.
At the end, display the total valid weight as well as the average valid weight (total/countValid)
Sample Output at the end:
Enter the object's name>Honda Accord
Enter Honda Accord's mass>240
Honda Accord is too heavy.
Enter the object's name>Feather
Enter Feather's mass>0.001
Feather is too light.
Enter the object's name>Running Shoes
Enter Running Shoes's mass>2.5
Running Shoes weight is 24.5 Newtons
Enter the object's name>Kiwi
Enter Kiwi's mass>5.5
Kiwi weight is 53.9 Newtons
Enter the object's name>Panda
Enter Panda's mass>-5
-----
Total input count: 4
Total valid count: 2
Total valid weight input: 78.4
Average valid weight: 39.2
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started