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

Write in Java public Weight() : The default constructor for the class, which ini

ID: 3694273 • Letter: W

Question

Write in Java

public Weight(): The default constructor for the class, which initializes the object to INIT_VAL kilograms. Both variables must be set using explicit assignment statements.

public Weight(double initW, char initScale): Another definition of the constructor. It takes as a parameter a weight measurement and a symbol ('k' or 'K' for kilograms or 'p' or 'P' for pounds) representing the scale and initializes the object using those values. If the values provided as parameters are not valid, meaning that the scale is not either kilograms or pounds or the weight is below the INIT_VAL, the constructor should use a default value of INIT_VAL kilograms for the measurement. In either case, do not prompt the user for another value. Simply use the default specified. Both variables must be set using explicit assignment statements.

public void set(): This method prompts the user for a decimal value representing the weight measurement and a character representing the weight scale. If an invalid scale (something other than 'k', 'K', 'p', or 'P') is entered, repeatedly ask for the scale again. The measurement entered must be greater than or equal to INIT_VAL. If an invalid measurement is entered, repeatedly prompt for another value. Make sure to not ask the user to re-enter valid information. For example, if the user enters a valid character for the scale and then incorrectly enters a weight measurement less than INIT_VAL, only re-prompt for the weight measurement, not the scale.

public double getKilos(): This method returns the weight measurement in kilograms. If the weight is stored in kilos already, it just returns it. Otherwise it uses the KILO_TO_LB constant to convert the weight measurement to kilos (without saving that value in the object) and returns it.

public double getPounds(): This method returns the weight measurement in pounds. If the weight is stored in pounds already, it just returns it. Otherwise it uses the LB_TO_KILO constant to convert the weight measurement to pounds (without saving that value in the object) and returns it.

The following is some output from my completed solution using the Hw4.java driver program:

0.0 kilograms is 0.0 pounds.
0.0 kilograms is 0.0 pounds.

Entering first weight ...
Please enter the weight scale (p/k):$
pounds and kilograms are the only valid scales
Please enter the weight scale (p/k):*
pounds and kilograms are the only valid scales
Please enter the weight scale (p/k):#
pounds and kilograms are the only valid scales
Please enter the weight scale (p/k):P
Please enter a weight measurement (a non-negative value): -3
The weight measurement must be greater than or equal to 0.0.
Please enter a weight measurement (a non-negative value): -2
The weight measurement must be greater than or equal to 0.0.
Please enter a weight measurement (a non-negative value): -1
The weight measurement must be greater than or equal to 0.0.
Please enter a weight measurement (a non-negative value): 0.5
Entering second weight ...
Please enter the weight scale (p/k):K
Please enter a weight measurement (a non-negative value): 23.4
0.2268 kilograms is 0.5 pounds.
23.4 kilograms is 51.480000000000004 pounds.
Do you wish to continue (y/n)?y

Entering first weight ...
Please enter the weight scale (p/k):K
Please enter a weight measurement (a non-negative value): 5
Entering second weight ...
Please enter the weight scale (p/k):P
Please enter a weight measurement (a non-negative value): 10
5.0 kilograms is 11.0 pounds.
4.536 kilograms is 10.0 pounds.
Do you wish to continue (y/n)?n

Goodbye!

Explanation / Answer

package com.dq.thematrix.main.java.test;

import java.util.Scanner;

public class Weight {


   private static final double INIT_VAL = 0.0;   //Lets say 0.0
   double initW;
   char initScale;

   public Weight()
   {
       initW=INIT_VAL;
   }

   public Weight(double initW, char initScale)
   {
       if((initScale=='K'||initScale=='p'||initScale=='P'||initScale=='p')&&(initW>=INIT_VAL))
       {
           this.initScale=initScale;
           this.initW=initW;
           //System.out.println("All good");
       }
       else if(initW>=INIT_VAL)
       {
           this.initScale=initScale;
           this.initW=INIT_VAL;
       }
       else
       {
           this.initScale='K';
           this.initW=INIT_VAL;
       }
   }

   public void set()
   {

       Scanner sc=new Scanner(System.in);
       System.out.println("Please enter a weight measurement (a non-negative value):");
       initW=sc.nextDouble();
       while(initW<INIT_VAL)
       {
           System.out.println("The weight measurement must be greater than or equal to 0.0.");
           initW=sc.nextDouble();
       }
       System.out.println("Please enter the weight scale (p/k):$");
       initScale=sc.next().charAt(0);
       while(!(initScale=='K'||initScale=='p'||initScale=='P'||initScale=='p'))
       {
           System.out.println("pounds and kilograms are the only valid scales");
           initScale=sc.next().charAt(0);
       }
   }

   public double getKilos()
   {
       if(initScale=='K'||initScale=='k')
           return initW;
       else
       {
           return initW*2.20462;
       }
   }

   public double getPounds()
   {
       if(initScale=='p'||initScale=='P')
           return initW;
       else
       {
           return initW*0.453592;
       }
   }

   public static void main(String[] args) {
       // Use your Menu Driver Program here
   }

}

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