Question

In: Computer Science

I'm trying to write a feet to meters and centimeters program and I'm looking for a...

I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following:

  • 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.

Solutions

Expert Solution

Solution for the above java program is provided below. Please comment if you have any doubt.

ConvertToMetricMain.java

import java.util.Scanner;

public class ConvertToMetricMain {
    public static void main(String[] args) {
        // Create scanner object
        Scanner sc=new Scanner(System.in);
        // Create object of convertToMetrric class
        convertToMetric c=new convertToMetric();
        // Input value from keyboard
        System.out.println("Input a value for standared: ");
        double value=sc.nextDouble();
        // Set value
        c.setStandard(value);
        // Get value in meters
        double valueInMeter=c.getMeters();
        // Get value in centimeters
        double valueInCentimeter=c.getCentimeters();
        // Display the values
        System.out.println("The standard value in meter: "+c.getMeters());
        System.out.println("The standard value in centimeter: "+c.getCentimeters());
    }
  
}

convertToMetric.java

// Class declaaration
class convertToMetric {
    double standard;
  
    // Constructor
    public void convertToMetric(double length)
    {
        standard=length;
    }
  
    // Function to set the value
    public void setStandard(double length)
    {
        standard=length;
    }
  
    // Function to return the value
    public double getStandard()
    {
        return standard;
    }
  
    // Function to convert it it meter
    public double getMeters()
    {
        return standard*0.3048;
    }
  
    // Function to convert it to centimeter
    public double getCentimeters()
    {
        return standard*30.48;
    }
}

Output:


Related Solutions

I'm trying to write a program that reads 3 heights of kids and puts them in...
I'm trying to write a program that reads 3 heights of kids and puts them in ascending order using if and else statements but i'm having trouble #include<stdio.h> int main() { int k1,k2,k3; printf("Enter height of kid #1 >"); scanf("%d",&k1); printf("Enter height of kid #2 >"); scanf("%d",&k2); printf("Enter height of kid #3 >"); scanf("%d",&k3); if(k3>k2>k1) { printf("In ascending order %d %d %d",k3,k2,k1); } else if(k3>k1>k2) { printf("In ascending order %d %d %d",k3,k1,k2); } else if(k2>k3>k1) { printf("In ascending order %d...
I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
I'm currently looking at this figure, but I'm not sure how to interpret it. If I'm...
I'm currently looking at this figure, but I'm not sure how to interpret it. If I'm not mistaken, the energy loss of the muon (This is a muon that penetrates copper) is on the y-axis. But does that just mean, that if my muon has energy of around 0.5 GeV, then it has a stopping power around 2 MeV cm2/g (Minimum ionization), which means it loses that amount of energy pr. cm2/g? To me that just seems a bit "linear"....
Express your height in the units of inches, feet, and meters.
Express your height in the units of inches, feet, and meters.
This is just a very basic program/problem in Java I'm looking for some second opinions on....
This is just a very basic program/problem in Java I'm looking for some second opinions on. 1. Ask the user for a year input (positive integer - should be between 1500 and 2019, inclusive). 2. If the year input is not between 1500 and 2019, do not check for leap year, rather print that this year cannot be checked. 3. Take a year input from the user (should be between 1500 and 2019) 4. If the input year is a...
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
Java I'm trying to create a program that replicates a theater ticket reservation system. I have...
Java I'm trying to create a program that replicates a theater ticket reservation system. I have a Seat class with members row(integer), seat(character) and ticketType(character). Where a ticket type can be 'A' adult, 'C' child, 'S' senior, or '.' recorded as empty. I have a Node generic class that points to other nodes(members): up, Down. Left, Right. It also has a generic payload. Lastly, I have an Auditorium class which is also generic. its member is a First Node<T>. As...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed...
Hello! I'm trying to write a code that will either encrypt/decrypt a message from an inputed text. I'm having the absolute hardest time getting stared and figuring out how to identify in the code if the input needs to be encrypted/decrypted and identifying the string to encrypt/decrypt with. These are the instructions: Objectives Command line input File input and output Rethrowing exceptions Program Description Gaius Julius Caesar encoded his battle messages so that the opponent could not read them should...
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance...
Using Python Write a GUI program that converts a distance in Meters to the equivalent distance in Feet. The user should be able to enter a distance in Meters, click a button, and then see the equivalent distance in feet. Use the following formula to make the conversion: Meters = Feet x 0.304 For example, 1 Meter is 3.28 Feet.
C++ program, I'm a beginner so please make sure keep it simple Write a program to...
C++ program, I'm a beginner so please make sure keep it simple Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File.txt: Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT