Reputation: 1
I have to make an object-oriented program for an assignment, I get the same error on both line 9 and 30. I know that I'm trying to create the Celsius and Fahrenheit objects wrong, but I am not sure how to do it correctly.
import java.io.*;
class Celsius
{
String inData;
int celsius;
int temperature;
Celsius();
{
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (inStream);
System.out.println("Enter a temperature in degres fahrenheit.");
inData = stdin.readLine();
temperature = Integer.parseInt(inData);
celsius = (5 / 9) * (temperature - 32);
System.out.println("Your temperature in degrees celsius is: " + celsius);
}
}
class Fahrenheit
{
String inData;
int fahrenheit;
int temperature;
Fahrenheit();
{
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (inStream);
System.out.println("Enter a temperature in degrees celsius.");
inData = stdin.readLine();
temperature = Integer.parseInt(inData);
fahrenheit = (9 / 5) * temperature + 32;
System.out.println("Your temperature in degrees fahrenheit is: " + fahrenheit);
}
}
class TemperatureTest
{
public static void main(String[] args) throws IOException
{
InputStreamReader inStream = new InputStreamReader (System.in);
BufferedReader stdin = new BufferedReader (inStream);
String inData;
int selection;
System.out.println("Input 1 to convert fahrenheit to celsius");
System.out.println("Input 2 to convert celsius to fahrenheit");
inData = stdin.readLine();
selection = Integer.parseInt(inData);
if (selection == 1)
{
Celsius c1 = new Celsius();
}
if (selection == 2)
{
Fahrenheit f1 = new Fahrenheit();
}
if (selection != 1 & selection != 2)
{
System.out.println("Invalid selection.");
}
}
}
Upvotes: 0
Views: 345
Reputation: 178243
The errors are on your constructors:
Celsius();
{
and
Fahrenheit();
{
There should be no semicolon between a constructor/method and its block. Remove those semicolons:
Celsius()
{
Fahrenheit()
{
Additionally, in Java, integer division occurs when two int
s are divided, which must yield an int
. Consequently, (9 / 5)
will yield 1
, and (5 / 9)
will yield 0
.
Make your variables double
, and use a double
literal for your constants (or cast one of them as a double
), to use floating-point division:
(9.0 / 5.0)
or
( (double) 9 / 5)
Upvotes: 2