Solved

Tips to improve this program

Posted on 2009-07-05
2
171 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
factorial example 4 39
Configure a Bean in an XML file 4 31
Running JavaFX on JDeveloper 12C 1 32
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

770 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