Solved

Tips to improve this program

Posted on 2009-07-05
2
166 Views
Last Modified: 2013-11-23
The output of this program is correct and as expected. It is actually a metric of multiplication.

Please advice me regarding code practices, maintability, readability, efficiency etc

package cattledrive;

public class Times {
      public static void main(String[] args) {
            for(int i = 0; i < 11; i++){
                  for(int j = 0; j < 11; j++){
                        if(i == 0){
                              if(j == 0){
                                    System.out.format("%4s" , " ");
                                    continue;
                              }
                              else{
                                    System.out.format("%4d" , j - 1);
                                    continue;
                              }
                        }
                        else{
                              if(j == 0){
                                    System.out.format("%4d", i - 1);
                                    continue;
                              }
                        }
                        
                     System.out.format("%4d" , (i - 1)*(j - 1));
                  }
                  System.out.println();
            }
      }

}
0
Comment
Question by:SunScreenCert
2 Comments
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 200 total points
ID: 24783045
With such a simple program, there is not much you can do to improve readability or maintainability except perhaps:

1) Add some comments to describe what you are doing
2) Use more descriptive variable names than i and j

0
 
LVL 20

Accepted Solution

by:
a_b earned 300 total points
ID: 24783113
At best you can remove the else clauses out of the program because they are redundant, and follows the guidelines provided by mrjoltcola
public static void main (String... args)

    {

        for (int i = 0; i < 11; i++)

        {

            for (int j = 0; j < 11; j++)

            {

                if (i == 0)

                {

                    if (j == 0)

                    {

                        System.out.format("%4s", " ");

                        continue;

                    }

                    System.out.format("%4d", j - 1);

                    continue;

                }

                if (j == 0)

                {

                    System.out.format("%4d", i - 1);

                    continue;

                }
 

                System.out.format("%4d", (i - 1) * (j - 1));

            }

            System.out.println();

        }

    }

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now