aspose file tools*
The moose likes Beginning Java and the fly likes Multiplication Table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Multiplication Table" Watch "Multiplication Table" New topic
Author

Multiplication Table

David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hello, I am a new java developer and am struggling with the following assignment:


Purpose: To learn how to convert integers to strings, how to format text, how to use nested loops.
Write a program that (algorithmically) shows a times table.
In other words, I want to type
java Times
and see
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7 8 9
2 0 2 4 6 8 10 12 14 16 18
3 0 3 6 9 12 15 18 21 24 27
4 0 4 8 12 16 20 24 28 32 36
5 0 5 10 15 20 25 30 35 40 45
6 0 6 12 18 24 30 36 42 48 54
7 0 7 14 21 28 35 42 49 56 63
8 0 8 16 24 32 40 48 56 64 72
9 0 9 18 27 36 45 54 63 72 81

So far this is the code I have:

public class Times
{
public static void main (String[] args)
{
System.out.print( " " );
{
for (int row = 0; row<=9; row++)
System.out.print(row + " ")
System.out.println();
for (int row = 0; row<=9; row++)
{
System.out.print(row + " ");
for (int col = 0; col<=9; col++)
{
System.out.print((col*row)) + " ")
}
System.out.println();
}
}
}
}

and this is the result:

0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81

I am almost there, and know I need to add a header row and column, but am lost on how to achieve this, please help. Thank you.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

You might want to quote your source...especially since this comes from this site's cattle drive.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hello Fred, this exercise is in the cattle drive. Am I posting in the right place?

Thank you.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

I assume you have not signed up for the cattle drive. That's fine - the problems are posted there for anyone to look at and try. We simply ask that WHEREVER you get your questions from, you give due credit to the author.

If i'm reading your post correctly, you have two issues:

how to get the additional row across the top
how to get the indices down the side.

First, approach these one at a time. They are really independent of each other.

So, tackle the row across the top first. You want to print out a row of data (the headers) before you print anything else. So, do just that. before you generate your table, print something.

Give that a whirl, and let us know how that goes.
David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hi fred,

This is what I have so far:

class Times
{
static
{
System.out.println(" ");
for (int i = 0; i <=9; i++)
System.out.println(" " + i + " ");
System.out.println();
}
static
{
for (int i = 0; i <=9; i++)
System.out.print(" " + i + " ");
System.out.println();
}
public static void main (String[] args)
{
for (int row = 0; row<=9; row++)
{
for (int col = 0; col<=9; col++)
{
int product = row * col;
if (product < 10 )
System.out.print(" " + product + " ");
else
System.out.print(product + " ");
}
System.out.println(" ");
}
}
}

Looks like:

0
1
2
3
4
5
6
7
8
9

0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9
0 2 4 6 8 10 12 14 16 18
0 3 6 9 12 15 18 21 24 27
0 4 8 12 16 20 24 28 32 36
0 5 10 15 20 25 30 35 40 45
0 6 12 18 24 30 36 42 48 54
0 7 14 21 28 35 42 49 56 63
0 8 16 24 32 40 48 56 64 72
0 9 18 27 36 45 54 63 72 81

I attached an image of the actual cmd. Have worked with the program, changing println vs. print; I believe the print lines are taking up the space of the actual table.

I appreciate your assistance as to how I could present this information as to the assignment specs. Thank you. David


[Thumbnail for Table.png]

David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hello, I made some changes and now this is the updated code:

class Times
{
static
{
System.out.print(" ");
for (int i = 0; i <=9; i++)
System.out.print(" " + i + " ");
System.out.println();
}
static
{
int i = 0;
while (i < 10)
{
System.out.println(i++);

}
}
public static void main (String[] args)
{
for (int row = 0; row<=9; row++)
{
System.out.print(" ");
for (int col = 0; col<=9; col++)
{
int product = row * col;
if (product < 10 )
System.out.print(" " + product + " ");
else
System.out.print(product + " ");
}
System.out.println(" ");
}
}

}

It´s looking better but still not how it needs to look. Is there someway I can move the main table up?

Thank you.

David


[Thumbnail for Table2.png]

Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
Hi David,

You're on the right track, but I found it to be easier to start with the first horizontal row, then your left column, then work on the inner rows and columns. Here, I'll get you started:

I know you can figure out the rest.

Edit: Well, never mind, you figured out that part before I was able to post. Good job.
David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hi Buenny, I finally figured it out by formatting a header for each row. Thanks so much for replying to my post.

My final code looks like this:

class Times
{
static
{
System.out.print(" ");
for (int i = 0; i <=9; i++)
System.out.print(" " + i + " ");
System.out.println();
}
public static void main (String[] args)
{
for (int row = 0; row<=9; row++)
{
System.out.print(" " + row);
System.out.print(" ");
for (int col = 0; col<=9; col++)
{
int product = row * col;
if (product < 10 )
System.out.print(" " + product + " ");
else
System.out.print(product + " ");
}
System.out.println(" ");
}
}

}



[Thumbnail for Tablefinal.png]

Greg Ferguson
Ranch Hand

Joined: Jun 04, 2012
Posts: 34
David Coello wrote:
int i = 0;
while (i < 10)
{
System.out.println(i++);

}
It's looking better but still not how it needs to look. Is there someway I can move the main table up?

Personally, I would replace that while loop with a for loop. You can accomplish your goal with only three simple for loops and since you already have the math done, you only need to figure out where to put that code and use your print() and println() statements to achieve your spacing.

Edit: David, stop reading my mind and posting before me! But seriously, no problem, I like to help when I can. Awesome job man! The next project is bit more of a challenge. I suggest creating methods in a Money class to save you a ton of code -- just a suggestion. Good luck!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

David,

It's time you learned to use "code tags" here. I took your java and pasted it into my post. Then, I hi-lighted it all, and clicked the button labeled "code" - it works just like you want to make something bold or italics. See how much easier your source is to read? Please start doing this in the future. The easier you can make it for folks to help you, the more help you will get.

Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

There's no rule that says you have to put everything in main. Actually, I follow a rule that says "Put every little task in its own method." Here's kind of what I would expect the main method to look like:

I'll leave it to you to figure out what the appropriate parameters and implementation of each of those methods would be.


Junilu - [How to Ask Questions] [How to Answer Questions]
David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hi Fred, should I be using Dreamweaver to achieve the code tags with their corresponding color? They definitely stand out and look like a big help while creating the program.

Thanks!!

David
David Coello
Greenhorn

Joined: Jun 05, 2012
Posts: 22
Hi Junilu, so to understand, basically what you are saying is define what tasks need to be completed, then create individual methods for each task? I also need to practice adding comments while creating the code, do you recommend that?

Thank you,

David
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

Hi David,

Here's my general approach to comments:

If I find myself wanting to write a comment about WHAT code does, that tells me the code is not as clear as it should be. I will refactor for clarity. Sometimes all it takes is changing the name of a method or variable. More often it makes sense to refactor some code into a method by itself and give that method a very descriptive name.

Listing 1 below is an example of poorly written code because it's cryptic.

Listing 1. POOR - cryptic code, takes effort to understand
The next listing below shows a comment added. IMO, this is still lame and LAZY because the code is still cryptic. Comments can be very unreliable, especially with complex code, because they tend to become out-of-date and out of synch as changes are made to the code over time and the comments are not updated to reflect those changes.

Listing 2. Still POOR - and LAZY, too. Adding a comment does not make the code any easier to read
The best thing to do is to refactor and give GOOD names. Refactoring makes the code a lot easier to read and the code documents itself so there is actually little or no need for comments.

Listing 3. BETTER - Refactored into self-documenting, intent-revealing code
Notice how, on Line 1 of this last example, understanding the code requires only as much effort as it takes to read it. It does what it says it does: build a header for as many number of columns as defined by maxColumns. This is what is meant by "readable code". There is virtually no cognitive weight* to it as compared to the unrefactored code, which takes some effort for someone reading it to understand its intent.

Other Notes:
1. The "h" variable from the previous examples has been given a better name that clearly documents its reason for existence: it's a "header".
2. The loop has been extracted to a separate method that has also been given a name that clearly documents what the PURPOSE or INTENT of the code is: to build a header.
3. JavaDocs are fine as long as they help clarify the INTENT and do not go into implementation details.
4. In the buildHeader() method, the parameter is given a descriptive name and the JavaDocs give more details as to its purpose.
5. The local variable name "sb" in the buildHeader() method is cryptic but because its scope is very limited, it's easy to discern its purpose from the surrounding code.

---
Sometimes, it makes sense to document how you came to a particular design decision. Clarifying the motivating factors behind a design can help others coming in later to understand why the code is written in a certain way. It can also help them avoid making new design decisions that could compromise the program if they were unaware of certain things that you have already taken into account and took the time to document. In cases like this, I will put a comment like the one below near the relevant code:

Listing 4. Example of an implementation note to help developers coming in later
Notice that it is a block comment rather than a JavaDoc comment. You can also put this type of note in the JavaDocs if it can help other developers understand how to avoid certain pitfalls when using the class.

Hope this helps.

* cognitive weight - just a fancy term I picked up somewhere that can be used to refer to the effort it takes to understand something. The greater the cognitive weight, the more effort it takes to understand. I've been using "conceptual weight" lately in some other posts but just realized it wasn't the right term.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

David Coello wrote:... define what tasks need to be completed, then create individual methods for each task?

Yes, it's called "Divide and Conquer" or also Functional Decomposition. The refactoring example in my previous post is called Extract Method but when you do a lot of that to make code clearer and easier to understand as in the example I gave for the multiplication table problem, it's called Compose Method.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4714
    
    7

David Coello wrote:Hi Fred, should I be using Dreamweaver to achieve the code tags with their corresponding color?

David, it's much simpler than that. When you are creating or editing a post, you will see a row of buttons above the text area where you are typing. Two buttons are useful for code: the "tt" button and the "Code" button. To use them, highlight some text that you want to format as code, then click on the button. This will put the appropriate tags around the highlighted text. To see how they would display when posted, hit the "Preview" button that's to the left of the "Submit" button.

code tags - for full code formatting, including different colors for language elements and preserving spacing and indentation

tt tags - displays the enclosed text using fixed-width font. Example: It's useful when you are referencing class names like MultiplicationTable but you don't want a full code listing.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

you can also manually type in the tags. they look like this:

[ code]

[ /code]

The only difference is that you don't really want a space between the opening bracket and the next character. I put the space in so you can see what they looks like (and then I nested another set of real tags so my example would look formatted)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiplication Table