Question

In: Computer Science

Write a convertToMetric class that has the following field: standard - holds a double, standard length...

  • Write a convertToMetric class that has the following field:
    • standard - holds a double, standard length value in feet.
  • The class should have the following methods:
    • Constructor - that accepts a length in feet (as a double) and stores it in the standard field.
    • setStandard - accepts a standard length in feet and stores it in standard.
    • getStandard - returns the value of the standard field, as a length in feet, no conversion required.
    • getMeters - returns the value of the standard field converted to meters.
    • getCentimeters - returns the value of the standard field converted to centimeters
  • A second class will be created and used to test the new object class. This, executable class, is to instantiate an object of 'convertToMetric' type. Then it will allow the user to use keyboard input to input a value for standard. That value will then be converted to meters and centimeters, and displayed.
  • Make sure that your output (or display) is user-friendly or easy to read and understand. You should display the length in feet, length in meters and length in centimeters.

Submit convertToMetric.java and testConvertToMetric.java as attachments under Assignment Submission.

if you could provide an idiot proof walkthrough i would appreciate it. im having problems with textpad compiling and running the programs. Thank you

Solutions

Expert Solution

/*
* Java program that prompts the user to enter the length value for feet
* and then display the meters and centimeters of the corresponding feet
* on the java console window.
* */
//testConvertToMetric.java
import java.util.Scanner;
public class testConvertToMetric
{
   public static void main(String[] args)
   {
       //Create a Scanner class object
       Scanner scan=new Scanner(System.in);
       System.out.print("Enter standard value(in feet) : ");
       //read feet value from console
       double feet=Double.parseDouble(scan.nextLine());
      
       //Create an object of convertToMetric class
       convertToMetric obj=new convertToMetric(feet);
       //call getCentimeters
       System.out.printf("%.2f Foot= %.2f Centimeter\n",feet,obj.getCentimeters());
       //call getMeters
       System.out.printf("%.2f feet= %.2f Meter\n",feet,obj.getMeters());
   }
}


-------------------------------------------------------------------------------------------------


//convertToMetric.java
public class convertToMetric
{
   //instance variable
   private double length;
   /*constructor that takes length as input
   * and set length to this class length*/
   public convertToMetric(double length)
   {
       setStandard(length);
   }
   /*Method to set the standard field, length */
   public void setStandard(double length)
   {
       this.length=length;
   }
   /*Method returns the standard field, length */
   public double getStandard()
   {
       return length;
   }
   /*Method that returns the converted feet to meters*/
   public double getMeters()
   {
       //conversion factor from feet to meter
       final double FEET_TO_MTR=0.3048;
       return length*FEET_TO_MTR;
   }
   /*Method that returns the converted cms of feet value*/
   public double getCentimeters()
   {
       //conversion factor from feet to cms
       final double FEET_TO_CM=30.48;
       return length*FEET_TO_CM;
   }
}//end of the class


-------------------------------------------------------------------------------------------

Sample program output screenshot :


Related Solutions

Write a convertToMetric class that has the following field: standard - holds a double, standard length...
Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard length in feet and stores it in standard. getStandard - returns the value of the standard field, as a length in feet, no conversion required. getMeters - returns the value of...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
Given the definition for a Point class that holds the coordinates of the point as double...
Given the definition for a Point class that holds the coordinates of the point as double values x and y, write a function called pt_dist that takes two points and returns the straight-line distance between them (as a double). Use two ways, pt_dist function version and the pt_dist method version. In main, include two if else tests for each, If passed "TEST PASSED, DIST IS " else "Test Failed, dist is ". Hint: Rhymes with Bythagorean Beorem. #include <iostream> #include...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length,...
Java- Write a class called Rectangle that inherits from Shape. Write a constructor that has length, width and colour as arguments. Define enough functions to make the class not abstract
using java Create a class Rectangle with attributes length and width both are of type double....
using java Create a class Rectangle with attributes length and width both are of type double. In your class you should provide the following: Provide a constructor that defaults length and width to 1. Provide member functions that calculate the area, perimeter and diagonal of the rectangle. Provide set and get functions for the length and width attributes. The set functions should verify that the length and width are larger than 0.0 and less that 50.0. Provide a member function...
Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double...
Write a class Battery that models a rechargeable battery. A battery has a constructor public Battery(double capacity) where capacity is a value measured in milliampere hours. A typical AA battery has a capacity of 2000 to 3000 mAh. The method public void drain(double amount) drains the capacity of the battery by the given amount. The method public void charge() charges the battery to its original capacity. The method public double getRemainingCapacity() gets the remaining capacity of the battery. Supply a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT