Click to See Complete Forum and Search --> : Multiplication table in Java
Nez2003
11-24-2005, 06:13 PM
HI all ok here I need a quick help :( I am trying to Design and develop an application that displays a multiplication table based on the number to be multiplied, and how many lines are to be displayed.
I will be expected to create a new project and build a class from scratch that will implement a solution.
The user might enter 9 as the table to be shown and 12 as the number of lines. In that case, the output would look like this:
1 x 9 = 9
2 x 9 = 18 and so on up to 12 x 9 any help will be great thank you bye.
Cytael
11-25-2005, 02:06 AM
Sounds like you've got a pretty good idea of what you need to do. I suggest you give it a shot first and see what you can come up with.
If you're looking for help getting started, I find that oftentimes the first step in writing a program is to turn off the computer. After that, sit down with a pencil and some paper and write out what elements you know you're gonna need, and consider how you can make them interact. Turn the computer back on, start coding what you've written down, and see where that takes you -- often you'll find by that time that you've got a firmer grasp of exactly what you're up against, and the rest should be, as they say, easy as pie (or cake...or cheese...maybe pizza? better stop before I make myself hungry).
Anywho, give it a whirl, and if you get stuck, drop back in here for a nudge or two in the (hopefully) right direction! :)
Nez2003
11-25-2005, 07:46 PM
Cytael Thx after you told me to start something here what I did :) OK I created 2 class's and I called them The Factorial Calculator and The Displays a Factorial link them up and here how I started first code for The Displays a Factorial class is:
public class FactorialDisplay
{
/**
* Constructor for objects of class FactorialDisplay
*/
public FactorialDisplay()
{
calculator = new FactorialCalculator();
}
/**
* Displays the factorial of a positive number less than 10 calculated by three different methods.
* Should print three identical lines
* @param the integer whose factorial is to be calculated
*/
public void display(int n)
{
if(n > 0 && n < 10)
{
System.out.println("The factorial of " + n + " using a for loop is: " + calculator.factorialByForLoop(n));
System.out.println("The factorial of " + n + " using a while loop is: " + calculator.factorialByWhileLoop(n));
System.out.println("The factorial of " + n + " using a do..while loop is: " + calculator.factorialByDoWhileLoop(n));
}
else
{
System.out.println("" + n + " is out of scope");
}
}
}
that was for The Displays a Factorial next one was The Factorial Calculator thats where I am stock now :( don't know what to do next :(
public class FactorialCalculator
{
}
I need a code there to calculate the factorial of 1x9 up to 12x9 and also to print a print out.
pyroclasm
11-25-2005, 09:00 PM
class Test {
public static void main(String[] args) {
int n = 2, upper = 10;
String output = "";
for (int i = 1; i <= upper; i++) {
output += String.valueOf(i * n) + "\n";
}
System.out.println(output);
output = "";
int i = 1;
while (i <= upper) {
output += String.valueOf(i * n);
}
System.out.println(output);
output = "";
i = 1;
do {
output += String.valueOf(i * n);
i++;
} while (i <= upper);
System.out.println(output);
}
}
By print, do you mean print on the command prompt? Use System.out.println(). If you mean actually print on paper, use java.io.PrintWriter.
BTW, you probably need to change your method definitions. You need to have 2 arguments for each: the number to be multiplied and the upper limit of the multiplication.
Nez2003
11-26-2005, 09:24 AM
Thx for the code pyroclasm ok after I install it on my program I still can't do 1 x 9 :( it just don't let me do it I don't know why :confused:
About the print out yes I mean using system.out.println so I enter 9 it should give me all 9 x 9s up to 9 x 12 = ?
Khalid Ali
11-26-2005, 01:16 PM
Well eventhough in ur messageu clearly posted that its "your task", I still am willing to write a short program for you..take a look below and let me know if thats what u wanted...
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
/**
* Created by IntelliJ IDEA.
* User: Khalid Ali
* Date: Nov 26, 2005
* Time: 10:52:00 AM
*/
public class TableGenerator {
public TableGenerator() {
BufferedReader keyIn = new BufferedReader(new InputStreamReader(System.in));
String tableNum = "", tableLines = "";
while (true) {//always look for new input unless user exits the program
//Display menu
getMenu(keyIn);
}
}
/**
*
* @param keyIn
*/
private void getMenu(BufferedReader keyIn) {
String message = "Please select a number from the options below and hit Enter key";
message += "\n1: Create a new table";
message += "\n2: Quit";
System.out.println(new StringBuffer().append("\n********** Menu ***********\n").append(message).toString());
try {
String cmd = keyIn.readLine();
if (cmd.endsWith("2")) {//exit program if user enters 2
System.exit(0);
}
getTableData(keyIn); //continue on wit table generation
} catch (IOException e) {
e.printStackTrace();
}
}
public void getTableData(BufferedReader keyIn) {
String tableNumStr = "";
String tableLinesStr = "";
int tableNum = 0;
int tableLines = 0;
try { //get user input for table to be shown
System.out.println("Enter number for which table to be created and hit Enter key:");
tableNumStr = keyIn.readLine();
System.out.println("Enter number of lines 2 be displayed for the table and hit Enter key:");
tableLinesStr = keyIn.readLine();
tableNum = Integer.parseInt(tableNumStr);
tableLines = Integer.parseInt(tableLinesStr);
generateTable(tableNum, tableLines);
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void generateTable(int tableNum, int tableLines) {
System.out.println("\n ***** Table ******\n");
for(int n=0;n<tableLines;n++){
int multipliedBy;
multipliedBy = n+1;
System.out.println(multipliedBy +" X "+ tableNum + " = "+(multipliedBy * tableNum));
}
}
public static void main(String[] args) {
new TableGenerator(); //constructor to initiate the whole process
}
}
Edit:
Make sure you remove autognerated comments at the top of the class displaying my name and date etc by Intellij before using this class..:-)
It will not reflect too good if u claimed it to be written by u with those comments at the top...hehehe
Nez2003
11-26-2005, 06:49 PM
Thx for the program Khalid Ali but thats not what I want :(
OK what I want is for a program to print.out a table of 1 x 9 = 9 up to 12 x 9 = 108 just that and the print.out should look like this
1 x 9 = 9
2 x 9 = 18
3 x 9 =
4 x 9 =
5 x 9 =
6 x 9 =
7 x 9 =
8 x 9 =
9 x 9 =
10 x 9 =
11 x 9 =
12 x 9 = 108
thats all so for example when I enter 9 it should give me that table.
BTW this is just a practice on java for me its not a Homework or something but it would be great if I know how I can do this :(
Cytael
11-26-2005, 07:08 PM
You shouldn't need anything much more complicated than this
public PrintTable(int base, int rows)
{
for(int i=1; i<=rows; i++)
{
System.out.println(i + " x " + base + " = " (i*base));
}
}
I'll leave the exact method used for passing in the parameters up to you. :)
Cytael
11-26-2005, 07:16 PM
by the way, what you're asking for in this table is very different from the factorials you alluded to in your second post.
a factorial is the product of a positive integer times all positive integers less than it, such that 9 factorial = 9! = 9x8x7x6x5x4x3x2x1 = 362880
one way to calculate a factorial using a for loop would be:
int factorial = 9; //hardcoding 9 for demo purposes
int product = 1;
for(int i=factorial; i>0; i--)
product *= i; //does that work in java? if not, use product = product*i
System.out.println(factorial + "! = " + product);
Khalid Ali
11-26-2005, 10:49 PM
Thx for the program Khalid Ali but thats not what I want :(
OK what I want is for a program to print.out a table of 1 x 9 = 9 up to 12 x 9 = 108 just that and the print.out should look like this
1 x 9 = 9
2 x 9 = 18
3 x 9 =
4 x 9 =
5 x 9 =
6 x 9 =
7 x 9 =
8 x 9 =
9 x 9 =
10 x 9 =
11 x 9 =
12 x 9 = 108
thats all so .... :(
I wonder if you even tried to run the example program I posted above...take a look at the example out put above which u posted and compare it with the out put of my program below...
********** Menu ***********
Please select a number from the options below and hit Enter key
1: Create a new table
2: Quit
1
Enter number for which table to be created and hit Enter key:
9
Enter number of lines 2 be displayed for the table and hit Enter key:
12
***** Table ******
1 X 9 = 9
2 X 9 = 18
3 X 9 = 27
4 X 9 = 36
5 X 9 = 45
6 X 9 = 54
7 X 9 = 63
8 X 9 = 72
9 X 9 = 81
10 X 9 = 90
11 X 9 = 99
12 X 9 = 108
********** Menu ***********
Please select a number from the options below and hit Enter key
1: Create a new table
2: Quit
2
Process finished with exit code 0
And this is not complex by any means, logic is very simple only a bit modular...
Nez2003
11-27-2005, 05:28 PM
Khalid Ali I tried your program and I saw what it did user enters a number and it calculates it for them but thats not what I am after I know your program dose 1 x 9 = 9 and so on but the program I am after is I create a class then click on it create lets say calculate 1 x 9 then I enter 9 and I get a print.out that show me all 9x 9's from 1 up 12 :rolleyes:
pyroclasm
11-27-2005, 05:58 PM
I don't completely understand what you just posted. Do you want to separate the logic from the view (model-view) like a GUI and have user input from that? Then you would use the same algorithm Khalid and I posted but have the GUI code in another class.
If you want to use just an instance of a class, then it would look something like this:
class Test {
int number;
public Test(int n) {
this.number = n;
}
private void doPrint() {
int product;
for (int i = 1; i <= 12; i++) {
product = this.number * i;
System.out.println(this.number + " x " + i + " = " + product);
}
}
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
Test test = new Test(n);
test.doPrint();
}
}
Khalid Ali
11-27-2005, 06:30 PM
..... I am after is I create a class then click on it create lets say calculate 1 x 9 then I enter 9 and I get a print.out that show me all 9x 9's from 1 up 12 :rolleyes:
What...???? :confused:
you have say it in plain english so that its understandable for poor software developers as welll...:-)
Seems like you want a GUI stand alone app where you have a "button" which upon a click show 2 fields where user can enter the values and then you show the table in that GUI somwhere? is that what you are after?
Nez2003
11-28-2005, 05:49 PM
OPs sorry Khalid Ali you right I was very tired and I came from work so just posted that without thinking :D
OK any away finally after working on the java I get what I wanted to get here is the code for it :)
public class Multiplication
{
// a multiplication table
public void show ( int facter)
{
int Multiplication;
for ( int i = 1; i < 13 ; i++)
{
Multiplication = i * facter; // "i" is been muliplyed by facter which was entered by the user.
System.out.println("" + i + " x " + facter + " = " + Multiplication );
}
}
}
This is was what I was after :) any away thank you guys so much for the all help :D thank you.
Nez2003
11-28-2005, 05:51 PM
Deam another mistake :( for get about ''is'' I don't know whats wrong with me these days this stupid java programing makes me feel ill :)
Hi:
Well I do not blame you Nez2003, I am taking Intro to Computer Sience and I am currently learning arrays and its a bit confusing at the beginning but if you do some of them you will catch it. I get confused as well with many of the problems the book has. Like word problems and stuff oh my God lol... But still after braking my head i get somewhere and what i thought difficult resulted to be a small thing.. I should bother you guys often in the future :P since i am still learning..
Regards
Ed
Nez2003
11-29-2005, 09:32 PM
Man welcome to my world I am in same postion right now as you were :( I am just totaly lost in java programming I just don't understand it and thats why I keep doing these practice examples to learn it :(
Khalid Ali
11-30-2005, 07:04 AM
...I am in same postion right now as you were :( I am just totaly lost in java programming I just don't understand it .....:(
Hey now.....Java is one of the easiest to learn with the help of a variety of documentation that is available online. Just wondering, if you are complaining for this language, then what would be your reaction if you were to learn C/C++, where its almost impossible to get your hands on any API information as you can for Java....:-)
Anyways...just give it some time and read the API's as much as you can. Your best friend should be the Typical 3 framed webapges for JAVA API.. :D
Nez2003
12-02-2005, 08:47 PM
Khalid Ali I am trying but its just to hard to understand it for me :( I know it sux but nothing I can do about my brain :confused: My plan is to just pass Java programming course and just go through it thats all :D
Waylander
12-03-2005, 12:58 AM
There is plenty you can do actually, what I would suggest doing is starting a project in your own time, something resonably fun. Then whenever you learn something new add it to your own project, even if its completely pointless... my friends and I each had various projects during tafe we even wrote a ridiculus pie generator lol meat, not maths, that allowed you to mince people for fun...
It pretty much doubles what you learn, there is research about learning and memory rates that indicate that if you use something that you have learned in the 24 hour or so period after learning it, you will be considerably more likely to remember it in the long term.
Im actually just finishing my poker game now a 2-3 year work in progress, which of course I will put on my portfolio site for free download in non comercial use as a demonstration of my skills, which is very very good when you start looking for jobs, its also good if you have the freedom with in the assignment to make meaningfull assignments that you can use too especially if you can demonstrate some skill above the standard requirements.
The other thing about java is that every class and method is documented online. You can actually search the classes and look at them while your lecturer is speaking, lol if you are skilled in the art of soft typing that is...
If your more active in your learning and using the skills that you gain your graded work will become alot easier and you should be able to get higher grades as a result too...
Good Luck :)
Waylander.
Nez2003
12-03-2005, 08:38 AM
Thanks Waylander :) OK what you suggest thats what I am doing now :) practicing on my own time and I just done few practice which was 9 X 1 table in java which I already done it and also two more practice's which are alarm clock and clock display :) and I want to do them to understand a bit about java :D