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 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...
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...
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...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Write the definition for a generic class called Rectangle that has data members length and width....
Write the definition for a generic class called Rectangle that has data members length and width. The class has the following member functions: setlength to set the length data member setwidth to set the width data member perimeter to calculate and return the perimeter of the rectangle area to calculate and return the area of the rectangle show to return a text to display the length and width of the rectangle sameArea that has one parameter of type Rectangle. sameArea...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT