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.

Solutions

Expert Solution

Kindly upvote if this helped

OUTPUT:




CODE for indentation:




CODE as plain text:

import java.util.Scanner;

public class ConvertToMetric {
    double standard;

    public double getStandard() {
        return standard;
    }

    public ConvertToMetric(double standard) {
        this.standard = standard;
    }

    public double getMeters() {
        return this.standard * 0.3048; // 1 foot is 0.3048m
    }

    public double getCentiMeters() {
        return this.standard * 30.48; // 1 foot is 30.48cm
    }


}

class TestingMetric {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in); // creating object of scanner class to take the input from user from keyboard
        System.out.println("Enter standard value (in feet)");
        double input = sc.nextDouble();
        sc.close(); // close the Scanner object. No longer we want to take input from user now
        ConvertToMetric c = new ConvertToMetric(input);
        double met = c.getMeters();
        double cms = c.getCentiMeters();
        System.out.println(input + " feet in meters is " + met + " and in centimeters is " + cms);
    }
}

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...
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 &&...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT