Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java: Coding Issues

Multiple variables in one declaration

		int 	totalRainfall = 0,
			dailyRainfall = 0,
			maxDailyRainfall = 0,
			currentDay = 0,
			rainlessDays = 0,
			maxRainlessDays = 0;

Declaring many variables in one declaration is generally not the best style. Some code reformaters will try to fill out the line with all the declarations. Documenting each variable is more difficult.

Integer size

	public static final short NUMBER_OF_DAYS = 7;

Using short was nice, altho for single numbers the savings in memory is inconsequential. Shorts are expanded to do any computation. Why didn't you use "byte" type?

All fields of the for loop

		int 	totalRainfall = 0,
			dailyRainfall = 0,
			maxDailyRainfall = 0,
			currentDay = 0,
			rainlessDays = 0,
			maxRainlessDays = 0;

		
		for( ; currentDay < NUMBER_OF_DAYS ; currentDay++ )

Leaving out the for loop initialization saves an infinitesimal amount of CPU time, but causes big problems for the reader, who has to read the program backwards looking for the assignment to that variable. Easy in this case, but it creates a big interruption in reading speed.

For when counting

        day = 1;

        while (day <= 7)
        {
            totalRainfall += rainfall;
            System.out.println("Rainfall for day " +day + " is " +rainfall + " millimeters.");
            day++;
            rainfall = SavitchIn.readLineInt();
            System.out.println("Total rainfall so far is " +totalRainfall);
            if (rainfall > maxRainfall)
                maxRainfall = rainfall;
        }

Else clause not needed

    if (maxRain <= rain)
        maxRain = rain;
    else
        maxRain = maxRain;
    }

Reusing variables

int averageRainfall = 0;
static final int DAYS = 7;

for (int day=0; day < DAYS; day++) {
    String rainStr = JOptionPane.showInputDialog(null, "Enter rainfall");
    int rain = Integer.parseInt(rainStr);
    
    averageRainfall = averageRainfall + rain;
}

averageRainfall = averageRainfall / DAYS;
. . .

Weird braces

			{
			if (rain == 0)
				clearDay++;
			else
				clearDay = 0;//This resets clearDay when there is rain
				{
				if (clearDay > inARow)// This keeps track of rainless days in a row
					inARow = clearDay;
				}
			}

Psychological indentation

for (count = 1; count <= 7; count++ ){
    temp = JOptionPane.showInputDialog(null,"Please enter ...") ;
    rain = Integer.parseInt(temp);
    
        if (rain > maxRain){
            maxRain = rain;
        }
        
        if (rain<0){
            JOptionPane.showMessageDialog(null, "You can't ...");
            System.exit(0);
        }
        allRain = allRain + rain;
        rainTotal = allRain;
        
            if ((lastNumber==0) && (rain==0)){
                zeroDays++;}
            lastNumber = rain;
                }

Should be

for (count = 1; count <= 7; count++ ){
    temp = JOptionPane.showInputDialog(null,"Please enter ...") ;
    rain = Integer.parseInt(temp);
    
    if (rain > maxRain){
        maxRain = rain;
    }
    
    if (rain<0){
        JOptionPane.showMessageDialog(null, "You can't ...");
        System.exit(0);
    }
    allRain = allRain + rain;
    rainTotal = allRain;
    
    if ((lastNumber==0) && (rain==0)){
        zeroDays++;}
    lastNumber = rain;
}

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.