Link to home
Create AccountLog in
Programming

Programming

--

Questions

--

Followers

Top Experts

Avatar of Emms
Emms

Simple Java Program Exercise 2.1
Write a program that converts Fahrenheit to Celsius.  The fomula for the conversion is as follows:

Celsius=(5/9)*(fahrenheit-32)

Your program reads a Fahrenheit degree in double from the keyboard then converts it to Celsius and display the result.

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of mglxxxmglxxx

....and this is most likely homework.
Homework won't be done here. After reading the Java Tutorial at http://java.sun.com/docs/books/tutorial/index.html you should be able to do this problem yourself.

Avatar of EmmsEmms

ASKER

I was hoping to learn the java syntax by examples.  I am waiting for my Java for Dummies from Canada.

Thanks for the whip whip.

Sorry for the whip.
In the meantime, take a look at the Java Tutorial -- it's very good and will certainly get you going with java.

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of EmmsEmms

ASKER

Tried and tried.  Jumps too fast from hello world to applets, unix, etc.  
I know how to set the environment, compile using javac to produce class file, run the java program already.
How about a deal?  I give you the codes in C programming and you help me out in this.  (to make me feel less guilty).

Emms

Avatar of EmmsEmms

ASKER

#include<stdio.h>
#include<math.h>
main()
{
    float fahrenheit, celsius;
    clrscr();
   
    printf("Enter Fahrenheit to convert to Celsius: ");
    scanf("%f", &fahrenheit);

    celsius = (5/9)*(fahrenheit-32);
   
    printf("%f Fahrenheit = %f Celsius", fahrenheit, celsius);
}

Question revised:  Convert this c codings to java codings.

ASKER CERTIFIED SOLUTION
Avatar of mglxxxmglxxx

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Hi Emms, :-)

here are some FREE hints for you:



-

How to write a skeleton of Java application:



public class MyApplication
{
    public static void main( String[] args )
    {
        // your code goes here
    }
}



-

How to declare variables of types "int" and "double" (integer and floating-point numbers) ?



int radius;
int height;

double volume;



-

How to print something to the system output:



System.out.println( "Something" );
System.out.println( "Radius of the base is "+radius );



-

How to read an integer value from the system input (SHORT VERSION, terminate application on invalid input):



int inputAsIntegerValue;

System.out.print( "Please enter an integer value: " );

try
{
    inputAsIntegerValue = Integer.parseInt( System.in.readln() );
}
catch( NomberFormatException e )
{
    System.out.println( "Input "+inputAsString+" is not a valid int value!" );
    System.exit( 0 );
}

System.out.println( "The value is "+inputAsIntegerValue );



-

How to read an integer value from the system input (LONG VERSION, loop until you get a valid number):



String inputAsString = null;

int inputAsIntegerValue;

System.out.print( "Please enter an integer value: " );

while (inputAsString == null)
{
    try
    {
        inputAsString = System.in.readln();
        inputAsIntegerValue = Integer.parseInt( inputAsString );
    }
    catch( NomberFormatException e )
    {
        System.out.println( "Input "+inputAsString+" is not a valid int value! Again, please." );
        inputAsString = null;
    }
}

System.out.println( "The value is "+inputAsIntegerValue );



-

How to read a floating-point number (of the type "double") from the system input (SHORT VERSION):



int inputAsDoubleValue;

System.out.print( "Please enter a double value: " );

try
{
    inputAsDoubleValue = Double.parseDouble( System.in.readln() );
}
catch( NomberFormatException e )
{
    System.out.println( "Input "+inputAsString+" is not a valid double value!" );
    System.exit( 0 );
}

System.out.println( "The value is "+inputAsDoubleValue );



-

How to read a floating-point number (of the type "double") from the system input (LONG VERSION):



String inputAsString = null;

double inputAsDoubleValue;

System.out.print( "Please enter a double value: " );

while (inputAsString == null)
{
    try
    {
        inputAsString = System.in.readln();
        inputAsDoubleValue = Double.parseDouble( inputAsString );
    }
    catch( NomberFormatException e )
    {
        System.out.println( "Input "+inputAsString+" is not a valid double value! Again, please." );
        inputAsString = null;
    }
}

System.out.println( "The value is "+inputAsDoubleValue );



-

For your original problem here (top of the page), you should use the skeleton application, declare two variables of the type "double" (celsius and farenheit), then take farenheit from the system input, then caclulate calsius variable value using the given formula and then write the result to the system output.

If you have trouble understanding any of the given hints - ask :-)



Greetings,
    </ntr> :)

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


oops, short versions of number-inputhave a small copy/paste error (I'm sure you would fix that anyway), here are the fixed versions:



-

How to read an integer value from the system input (SHORT VERSION, terminate application on invalid input):



int inputAsIntegerValue;

System.out.print( "Please enter an integer value: " );

try
{
   inputAsIntegerValue = Integer.parseInt( System.in.readln() );
}
catch( NomberFormatException e )
{
   System.out.println( "That is not a valid int value!" );
   System.exit( 0 );
}

System.out.println( "The value is "+inputAsIntegerValue );


-

How to read a floating-point number (of the type "double") from the system input (SHORT VERSION):



int inputAsDoubleValue;

System.out.print( "Please enter a double value: " );

try
{
   inputAsDoubleValue = Double.parseDouble( System.in.readln() );
}
catch( NomberFormatException e )
{
   System.out.println( "That is not a valid double value!" );
   System.exit( 0 );
}

System.out.println( "The value is "+inputAsDoubleValue );



-

Greetings,
    </ntr> :)

Avatar of EmmsEmms

ASKER

Exception in thread "main" java.lang.NoClassDefFoundError:

Avatar of EmmsEmms

ASKER

Errors mglxxx

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


...also some FREE hints on compiling/running:

-

Let's say you know where is your JRE directory, and that you can see that there is a LIB directory inside it. If you look there, you'll see that there is a file RT.JAR in that LIB directory.

Before you can RUN/COMPILE your something.java source files you must set an environment variable:
(WinXX)
    SET CLASSPATH =full path to your JRE\LIB\RT.JAR file
    Example:
    SET CLASSPATH=c:\jdk131\jre\lib\rt.jar
(Linux*)
    export CLASSPATH=full path to your JRE/LIB/RT.JAR file
    Example:
    export CLASSPATH=~/jdk131/jre/lib/rt.jar

-

Now, let's say you've saved your Java application (just one class, like the skeleton given in the previous post) in the file MyApplication.java
(Note: class name must match filename)

-

first you compile the class like this:
    javac MyApplication.java

-

then you run the app like this:
    java MyApplication

-

'Hope this helps :-)

Greetings,
    </ntr> :)

Hmm, the sample works perfect for me.
Which class isn't found?

Go to the directory where the class file is and try a
java -classpath . FahrToCels

Avatar of EmmsEmms

ASKER

Thanks for the hints from Neutron, I added the LIB directory into dos path (environment)... and the error is gone.

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


...and another correction for my examples of setting the CLASSPATH
(you must include the current directory too)

Like this:

(WinXX)
   SET CLASSPATH=c:\jdk131\jre\lib\rt.jar;.
(Linux*)
   export CLASSPATH=~/jdk131/jre/lib/rt.jar:.


Greetings,
    </ntr> :)

oops, I think Neutron meant NumberFormatException, not Nomber(sic)FormatException, right? ;)

Avatar of EmmsEmms

ASKER

oh come on...

I believe it was an honest mistake... in fact Neutron's tips are very useful.  Many thanks, Neutron.


Cheers

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


to stitch2082: oops (many times here so far) :-)     correction: LumberFormatException ;)
That happens to me every time when I write some code, but when I pass the compiler syntax-check usually the rest works fine

to Emms: it was my pleasure :-)

Greetings,
    </ntr> :)
Programming

Programming

--

Questions

--

Followers

Top Experts

Programming includes both the specifics of the language you’re using, like Visual Basic, .NET, Java and others, but also the best practices in user experience and interfaces and the management of projects, version control and development. Other programming topics are related to web and cloud development and system and hardware programming.