Object-Oriented Program Help

Hi,

I'm having trouble writing this code. The program should convert Fahrenheit to Celsius. A given "MyInput" file reads the input and I have to create two files:
1. The implementation of Temperature class, and
2. The file containing the main method.

The temperature class may contain the variables, constructor, getter functions and the conversion method.

I know that I'm not doing it right because I get -17 for my Celsius every time. Also, I didn't know how to put in a getter function or how it should be used.

Can anyone help me out? Thanks!

Here's what I've got so far:

MAIN
------------------------------------------------------------
public class MainTemp
{
public static void main(String[] args)
{
double dFahrenheit;
double dCelsius;

System.out.println("Please enter the current temperature in degrees Fahrenheit: ");
dFahrenheit=MyInput.readDouble();


FahrToCelsius dF2C;
dF2C = new FahrToCelsius();

System.out.println(dFahrenheit + "degrees Fahrenheit = " + dF2C.dConvertTemp() "degrees Celsius.");

}

}
------------------------------------------------------------


IMPLEMENTATION
------------------------------------------------------------
public class FahrToCelsius{

private double dFahrenheit;
private double dCelsius;


public double dConvertTemp(){
dCelsius = (( dFahrenheit - 32 ) / 9 ) * 5;
return dCelsius;
}

}
------------------------------------------------------------


MyInput.java
------------------------------------------------------------
// MyInput.java: Contain the methods for reading int and double
// values from the keyboard
import java.io.*;
import java.util.*;

public class MyInput
{
static private StringTokenizer stok;
static private BufferedReader br
= new BufferedReader(new InputStreamReader(System.in), 1);

// Read an int value from the keyboard
public static int readInt()
{
int i = 0;
try
{
String str = br.readLine();
StringTokenizer stok = new StringTokenizer(str);
i = new Integer(stok.nextToken()).intValue();
}
catch (IOException ex)
{
System.out.println(ex);
}
return i;
}
------------------------------------------------------------

Comments

  • : Hi,
    :
    : I'm having trouble writing this code. The program should convert Fahrenheit to Celsius. A given "MyInput" file reads the input and I have to create two files:
    : 1. The implementation of Temperature class, and
    : 2. The file containing the main method.
    :
    : The temperature class may contain the variables, constructor, getter functions and the conversion method.
    :
    : I know that I'm not doing it right because I get -17 for my Celsius every time. Also, I didn't know how to put in a getter function or how it should be used.
    :
    : Can anyone help me out? Thanks!
    :
    : Here's what I've got so far:
    :
    : MAIN
    : ------------------------------------------------------------
    : public class MainTemp
    : {
    : public static void main(String[] args)
    : {
    : double dFahrenheit;
    : double dCelsius;
    :
    : System.out.println("Please enter the current temperature in degrees Fahrenheit: ");
    : dFahrenheit=MyInput.readDouble();
    :
    :
    : FahrToCelsius dF2C;
    : dF2C = new FahrToCelsius();
    :
    : System.out.println(dFahrenheit + "degrees Fahrenheit = " + dF2C.dConvertTemp() "degrees Celsius.");
    :
    : }
    :
    : }
    : ------------------------------------------------------------
    :
    :
    : IMPLEMENTATION
    : ------------------------------------------------------------
    : public class FahrToCelsius{
    :
    : private double dFahrenheit;
    : private double dCelsius;
    :
    :
    : public double dConvertTemp(){
    : dCelsius = (( dFahrenheit - 32 ) / 9 ) * 5;
    : return dCelsius;
    : }
    :
    : }
    : ------------------------------------------------------------
    :
    :
    : MyInput.java
    : ------------------------------------------------------------
    : // MyInput.java: Contain the methods for reading int and double
    : // values from the keyboard
    : import java.io.*;
    : import java.util.*;
    :
    : public class MyInput
    : {
    : static private StringTokenizer stok;
    : static private BufferedReader br
    : = new BufferedReader(new InputStreamReader(System.in), 1);
    :
    : // Read an int value from the keyboard
    : public static int readInt()
    : {
    : int i = 0;
    : try
    : {
    : String str = br.readLine();
    : StringTokenizer stok = new StringTokenizer(str);
    : i = new Integer(stok.nextToken()).intValue();
    : }
    : catch (IOException ex)
    : {
    : System.out.println(ex);
    : }
    : return i;
    : }
    : ------------------------------------------------------------
    :
    A getter function is a function to get a private variable. Nearly all Java classes use them. You can identify them by the prefix "get", such as String.getChars(), Object.getClass(), etc. A get function for your FahrToCelsius class could be getCelsius() and getFahrenheit().
    The error, which causes the result to remain -17, is that the value of the inputted value is not passed to the FahrToCelsius() class. Remember that variables declared in methods are only valid within the method itself, not in the rest of the program, even if the variable names are the same.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion