How does this Java program look? (Homework Help)

Status
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
 

-Triple-

Ars Tribunus Angusticlavius
8,554
Welcome to the world of programming. Glad to have another good brain.

Generally, we write slightly more descriptive headers for programs, but it's only important for release source. Also, some people only state the type of a variable once, and list all variables of that type in a comma-delimited list, like the following:

code:<HR>
Code:
double x,
       y,
       z;

Over the time you spend programming, however, you'll learn what sorts of things you think are good for comments and what are not. Comments are good for release code. You'll also develop your own systems for spacing and other such aesthetic things. Everyone has his own style, and yours will be unique.

And some may decry your use of Hungarian notation--I was tempted to--but remember, if you like it, use it.

Have fun!
 

Fluogen

Ars Tribunus Angusticlavius
7,268
Originally posted by PeterB:
Quote: "if you like it, use it."
Unless you expect other people to read your code, in which case, pick decent variable names to start off with, and remove the hungarian shit.

The notation might be a class requirement. That is the kind of stuff teachers like to have students do.

Congrats on the 98! Keep it up.
 
Isn't that what the 'class' keyword is for? Shouldn't the comments be things that explain functionality when the language can't do it concisely?
The purpose of the comment would not be to notify someone that a class was being defined; any idiot can see that. The class header comment would document what the class represented, what methods and properties it had, who wrote it, etc.
 

MrHat

Ars Scholae Palatinae
952
The purpose of the comment would not be to notify someone that a class was being defined; any idiot can see that. The class header comment would document what the class represented, what methods and properties it had, who wrote it, etc.

Ideally, yes. I just saw this:

code:<HR>
Code:
//start main method

... and assumed the 'class header' his teacher wanted to be similar.
 

gumbo

Ars Tribunus Angusticlavius
6,107
Sounds a lot like my intro to programming teacher not wanting me to do inline documenting. :rolleyes: I have to say what every variable in the program does at the beginning.

He can't tell where the code stops and my comment begins. :rolleyes:
Then again, he can't figure out how to multiply a few numbers and variables, divide it all, then print it. In Basic.

Good luck. :)

gubmo
 

-Triple-

Ars Tribunus Angusticlavius
8,554
Just out of curiousity, what is so distastefull about Hungarian notation?

A plethora of reasons exists to not use Hungarian notation--not the least of which are the problems with programmers changing the type without changing the Hungarian wart (which is an annoying task to remember), programmers do not use the warts consistently, the fact that the warts are generally ugly, and also that the warts do not contain the more important information about the variables (scope). Adding that information would only make the wart more disgusting, and give the source a greater chance of being broken. Obviously, there are no real benefits to using it (even if you can't remember the type), because the name, as Peter said, should be descriptive enough to remind you of the type.

But--there are some people who use it anyway. As long as they don't use it in release source, I am fine with it, because I don't want to see it.

Bah, Peter may attack Hungarian later on, but that's my take on the drawbacks.
 

Midlands

Ars Praetorian
535
Subscriptor
Originally posted by DaleDBeast:
Just out of curiousity, what is so distastefull about Hungarian notation?

Mostly, to me, that it's useless -- and therefore just gets in the way.

What does Hungarian notion do? Shows you the type of a variable. In a strongly-typed language like C, C++, or Java, the *declaration* shows you the type of a variable.

And if your method is long enough that you can't see the declaration at the same time that you read all the code, then your method is too long. Break it up and refactor it. Seriously.
 

Dandy Andy

Ars Tribunus Militum
2,458
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:
 
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
 

DaleDBeast

Wise, Aged Ars Veteran
172
...problems with programmers changing the type without changing the Hungarian wart (which is an annoying task to remember), programmers do not use the warts consistently, the fact that the warts are generally ugly, and also that the warts do not contain the more important information about the variables (scope). Adding that information would only make the wart more disgusting, and give the source a greater chance of being broken...

I think these issues are more pronounced in certain languages/environments than in others. For example, here is a snippet of VB 6 code from a class module, where Hungararian notation works well, IMO:

code:<HR>
Code:
Option Explicit<P>Private m_colFiles                      As New Collection
Private m_colExtensions                 As New Collection
Private m_sSourcePath                   As String
Private m_sDestinationPath              As String<P>'-----------------------------------------------------------------------------------------
'Collects files in the folder indicated.  Also can apply a filter when collecting files.
'-----------------------------------------------------------------------------------------
Public Function GetFiles(sFolderPath As String) As Long<P>    Dim sTmpFile            As String
    Dim bIsFile             As Boolean
    Dim bApplyFilter        As Boolean
    Dim vExtension          As Variant
        
    'make sure there is a trailing slash at the end of the path
    m_sSourcePath = sFolderPath
    If Right$(Trim$(m_sSourcePath), 1) <> "\" Then m_sSourcePath = m_sSourcePath & "\"
    
    'check if we should filter the files we find
    bApplyFilter = (m_colExtensions.Count > 0)
    
    'get the first file in the Folder
    sTmpFile = Dir$(m_sSourcePath)
    
...

Note that the variables that are scoped on the class-level are prefixed with m_. This to me does help in reading the code within procedures - much more than the typical Java code I come across that uses all lowercase non-prefixed variable names everywhere regardless of scope. On the occasions that I have had to convert Java code to VB (yes its happened several times), the "elegant" Java tends to lose some of its lustre if its just harder to read continuously.
 
Status
Not open for further replies.