How does this Java program look? (Homework Help)

Status
You're currently viewing only Xynie's posts. Click here to go back to viewing the entire thread.
Not open for further replies.
I have an assignment due in my Java class in a few days, it is an intro to programming class and this is our first programming assignment :)

Anyways, what we had to do was create a program that we could enter a temperature in Celcius and comvert it to Fahrenheit. I wrote this, and it runs fine. Please understand we have not gotten into the whole negative and invalid number thing yet, just the basics... Does this look okay for my first assignment? Don't mind all the commenting, it is required in the class...

code:<HR>
Code:
//Program to convert from Celcius to Fahrenheit. <P>
//Java extension packages
import javax.swing.JOptionPane;<P>
public class temperature{<P>	//start main method
	public static void main (String args[])
	{
		
		double dblTemp_C; 			//variable for Temperature in Celcius
		double dblTemp_F; 			//variable for Temperature in Fahrenheit
		String strTemp_C;			//string entered by user showing Temperature in Celcius
				
		//read in the temperature in celcius from the user as a string
		strTemp_C = JOptionPane.showInputDialog ("Enter the current Celcius Temperature");
               
		//convert number from type string to double
                     dblTemp_C= Double.parseDouble(strTemp_C);
                
		//Calculate Fahrenheit Temperature
		dblTemp_F = (9/5) * dblTemp_C + 32;
		
		//Display the results
		JOptionPane.showMessageDialog(null, "Your Temperature in Fahrenheit is " +dblTemp_F, "Results",
		JOptionPane.PLAIN_MESSAGE);
		System.exit(0);<P>        }//ends the method main<P>}//ends the class temperature

Anything I can do to clean this up, or should I turn it in as is? :D

I am under the impression that I can ask for opinions on stuff like this for my homework... just not ask you guys to write it for me :)

Thanks
Anthony
 
Originally posted by Dandy Andy:
Here's a concept you might come across at some point - unit testing:

Input temperature = 37.5C (human body temperature)

Output temperature = 69.5F (Tribble body temperature)

This is caused by using '9/5' instead of '9.0/5.0'

I can't get my brain around the fact that your teacher marks you down for omitting a comment of dubious value, but fails to spot a fairly obvious integer-instead-of-float division operation.

:rolleyes:

Thanks for the advice, for the next program we have to intigrate the temperature conversion the other way (F to C).. kind of like this...

1. Which way you want to convert is your first set of choices.. (I was told i could add Kelvin in for extra credit, which i will probably do) Choose 1 for C to F, 2 for F to C, etc...

2. After the selection is made, input the temperature and output the conversion...

Nothing major, but it isn't due for about 2 weeks, so when i get around to writing it, i will post it here :)

Thanks Guys
Anthony
 
Status
You're currently viewing only Xynie's posts. Click here to go back to viewing the entire thread.
Not open for further replies.