Help with Java code

Associate
Joined
25 May 2006
Posts
13
Hi there,

Firstly, I'm new to Java and have very little experience - so apologies if this seems rather basic. I'm trying to write a programme in BlueJ that will allow me to output a table of multiplication values, the parameters of which can be entered when the method is called. Below is a (rather poor) example of what I mean.

1 2 ... 10
1 1 2 ... 10
2 2 4 ... 20
...
5 5 10 50

It's important that the table lines up correctly, which unfortunately these forums won't allow me to illustrate (the top line of numbers should be one space to the right).

I'm trying to teach myself Java from a book called 'Objects First with Java' with a few supplementary tuition lessons from a friend of a friend, but am finding it extremely difficult.

So far, this is the code I've written:

"public class MultiplyTables

{
private int accross;
private int down;

public void maketable(int accrossx, int downy)

{

int across = 1;
int down = 1;


while (accross <= 12)

{

}"

I think I need to use if/else statements but am having real trouble grasping all these different types of statement. Could anyone give me a hand with this?

Many thanks for any help.
 
Code:
public class timestable{
	int lowerBound, higherBound;
	public timestable(int lowerBound, int higherBound){
		this.lowerBound = lowerBound;
		this.higherBound = higherBound;
	}

	public void makeHeader(){
		System.out.print("      ");
		for(int i = lowerBound;i<=higherBound; i++){
			PrintNumAndSpace(i);
		}
		System.out.println("\n");
	}

	public void makeBody(){
		for(int i = lowerBound;i<=higherBound; i++){
			PrintNumAndSpace(i);
			for(int j =lowerBound;j<=higherBound;j++){
				PrintNumAndSpace(i*j);
			}
			System.out.println("");
		}
	}

	public void PrintNumAndSpace(int num){
		System.out.print(num);
		if(num<10){
			System.out.print("     ");
		} else if (num<100) {
			System.out.print("    ");
		} else if (num< 1000) {
			System.out.print("   ");
		}
	}
	public static void main(String[] args) {
		timestable tt = new timestable(1,12);
		tt.makeHeader();
		tt.makeBody();
	}

}

hi i had a little go, i can explain it through if you want. here is the output of the file, ive used the range 1-12 but this fully customisable

Code:
      1     2     3     4     5     6     7     8     9     10    11    12

1     1     2     3     4     5     6     7     8     9     10    11    12
2     2     4     6     8     10    12    14    16    18    20    22    24
3     3     6     9     12    15    18    21    24    27    30    33    36
4     4     8     12    16    20    24    28    32    36    40    44    48
5     5     10    15    20    25    30    35    40    45    50    55    60
6     6     12    18    24    30    36    42    48    54    60    66    72
7     7     14    21    28    35    42    49    56    63    70    77    84
8     8     16    24    32    40    48    56    64    72    80    88    96
9     9     18    27    36    45    54    63    72    81    90    99    108
10    10    20    30    40    50    60    70    80    90    100   110   120
11    11    22    33    44    55    66    77    88    99    110   121   132
12    12    24    36    48    60    72    84    96    108   120   132   144
 
Last edited:
Hi Protocol,

Thankyou very much for taking the time to do that - it is hugely appreciated. Rather than wasting more of your time, I will go through it myself (with the help of my trusty textbook) and will try to understand each section of your code. I may post back here if I run into trouble though - I'm finding it much like learning an entirely new language at the moment!

Thanks again.
 
hey i dont mind explaining it through with you, just ask whihc methods you dont understand or which operations or loops you dont understand and i will explain no probs
 
Btw: You might want to think about getting a proper IDE (e.g: Eclipse) rather than BlueJ. BlueJ is awful !
 
Lagz said:
Btw: You might want to think about getting a proper IDE (e.g: Eclipse) rather than BlueJ. BlueJ is awful !

I second that, if you can get IntelliJ try that, otherwise go with eclipse. Blue-j is terrible.
 
Back
Top Bottom