user3529827 user3529827 - 7 months ago 21
Java Question

JAVA Need help for OOP

I need to create an object oriented C-F converter program. I need to have 2 methods for converted c to f and f to c. The method should have a return type and one parameter. This is what I have and I'm getting a bunch of errors when compiling. Can anybody help me find out what I'm doing wrong? Would be greatly appreciated :).

import java.io.*;

class Celsius {

private int value, convertedValue;

int setValue(int c) {
value = c;
convertedValue = (int) ((9.0 / 5.0) * value + 32);
}

int getValue() {
return value;
}

int getConvertedValue() {
return convertedValue;
}
}

class Fahrenheit {

private int value, convertedValue;

int setValue(int f) {
value = f;
convertedValue = (int) ((9.0 / 5.0) * value - 32);
}

int getValue() {
return value;
}

int getConvertedValue() {
return convertedValue;
}
}

class FCconvert {

public static void main(String [] args) throws IOException {
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader stdin = new BufferedReader(inStream);
String inData, choice1, choice2;
int input, c, f;
choice1 = "cel";
choice2 = "fah";

System.out.println("Please choose Celsius or Fahrenheit (c for celcius and f for fahrenheit)");
inData = stdin.readLine();

if (/*choice1 equals inData*/) {
System.out.println("Enter a value.");
inData = stdin.readLine();
c = Integer.parseInt(inData);
Celsius cel = new Celsius();
cel.setValue(c);
System.out.println("The converted value is: " + cel.getConvertedValue());
}

if (/*choice2 equals inData*/) {
System.out.println("Enter a value.");
inData = stdin.readLine();
f = Integer.parseInt(inData);
Fahrenheit fah = new Fahrenheit();
fah.setvalue(f);
System.out.println("The converted value is: " + fah.getConvertedValue());
}
}
}

Answer
if (inData.equals("c"))
{
    System.out.println("Enter a value.");
    inData = stdin.readLine ( );
    c = Integer.parseInt (inData);
    Celsius cel = new Celsius();
    cel.setValue(c);
    System.out.println("The converted value is: " + c.getConvertedValue());
}

You need to actually create an object of Celsius class before using the methods. Do similarly for fahrenheit class. Having said that, please reconsider your class design.

Also notice change in the way strings are compared.

Comments