Question
package hw4; import java.util.Scanner; public class Weight { // Please do not declare any additional members of the class // Feel free to use local
package hw4;
import java.util.Scanner;
public class Weight { // Please do not declare any additional members of the class // Feel free to use local variables as necessary in each method private double wValue; // If you like, you can change the type of scale to Character private char scale; private final double FACTOR = 2.2046; private Scanner vScan = new Scanner(System.in);
// The default constructor for the class public Weight() { }
// The parameterized constructor for the class public Weight(double initW, char initS) { }
// Input values for the instance variables using the Scanner vScan public void set() { System.out.println("Entering the set method."); // Put your code here System.out.println("Leaving the set method."); }
// Return the weight in pounds public double getP() { // A stub -- replace by the correct code return 0.0; }
// Return the weight in kilos public double getK() { // A stub -- replace by the correct code return 0.0; }
}
Hw4 java
import java.util.Scanner;
public class Hw4 {
public static void main(String[] args) { Weight firstW = new Weight(); Weight secondW = new Weight(-3.0, 'p'); Weight thirdW = new Weight(3, '%'); System.out.println(firstW.getP() + " pounds is " + firstW.getK() + " kilos."); System.out.println(secondW.getP() + " pounds is " + secondW.getK() + " kilos."); System.out.println(thirdW.getP() + " pounds is " + thirdW.getK() + " kilos."); System.out.println(); String rText; char reply; boolean done = false; Scanner wScan = new Scanner(System.in);
while (!done) {
System.out.println("Entering first temp ..."); firstW.set(); System.out.println("Entering second temp ..."); secondW.set();
System.out.println(firstW.getP() + " pounds is " + firstW.getK() + " kilos."); System.out.println(secondW.getP() + " pounds is " + secondW.getK() + " kilos.");
System.out.print("Do you wish to continue (y)? "); rText = wScan.next(); reply = rText.charAt(0);
System.out.println(); if (reply == 'n' || reply == 'N') done = true;
}
System.out.println("Goodbye!"); wScan.close(); } }
Assignment in downloading that file. You should submit ONLY the Weight.java file. You will complete the class by writing the following methods: default values given to variables by Java representing the scale and initializes the object using those values. If the scale provided as a parameter is not valid, meaning the scale is not either pounds or kilos, the constructor should use a default of pounds for the measurement. If the weight provided is not at least 0 (eg. >= 0) then a default value of 0.0 should be used for the measurement. In either case do not prompt the user for another value. Simply use the defaults specified. Both variables must be set using explicit assignment statements. You cannot rely on default values given to (something other than 'p', 'P,'k', or'K') is entered, the method repeatedly asks for the scale again. The method also checks that the weight entered is at least 0.0 and re-prompts the user repcatedly if the weight is negative. You may assume that when prompted the user will provide a number (either floating point or integer) for the weight measurement. 4 class. You must use the constant for full credit 5. public double getK0: This method returns the weight in kilos. If the weight is stored in kilos already, it just returns it. Otherwise it computes the equivalent weight in pounds class. You must use the constant for full credit test
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