Solved

Tips to improve this program

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 80
word0 challenge 3 77
JList custom Cell Renderer refresh 15 53
How to convert String matching to regex in java 4 16
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

930 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

16 Experts available now in Live!

Get 1:1 Help Now