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

Need help to code this please ASPS. I will rate you. Part 1) Write a Java progra

ID: 3749106 • Letter: N

Question

Need help to code this please ASPS. I will rate you.

Part 1) Write a Java program to calculate the weights of the objects. The program does the following (a) Read an object's name from the user (b) Get the object's mass from the user. Display the object's name when asking for the mass (See Part 2 for sample output) (c) Calculate the weight with the following formula: weight-mass* 9.8 (d) 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 1. 2. 3. (e) Verify that your code works for all three cases before moving on. Part 2) This part extends the code from Part 1 into a loop (10 points) (a) 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 object's name and first mass outside of the loop for things to work. (b) The input that ends the loop should be the object's mass, not the object's name, to prevent the need to convert data types.

Explanation / Answer

import java.util.Scanner; public class ObjectWeight { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter name of the object: "); String name = in.nextLine(); System.out.print("Enter mass of " + name + ": "); double mass = in.nextDouble(); double weight; while (mass != -5) { weight = mass * 9.8; if (weight > 1000) { System.out.println(name + " is too heavy!"); } else if (weight < 10) { System.out.println(name + " is too light!"); } else { System.out.println(name + " weights " + weight); } in.nextLine(); // reading newline after mass System.out.print("Enter name of the object: "); name = in.nextLine(); System.out.print("Enter mass of " + name + ": "); mass = in.nextDouble(); } } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote