Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-29-2007, 04:12 PM
Member
 
Join Date: Nov 2007
Posts: 4
ad0m is on a distinguished road
Maths table.. what am I doing wrong (probably everything!! :p )
is this type of switch right ?

switch(type){
case 'a': for(i = 1; i <= lines; i++){
total = maths * i;
System.out.println(maths + "*" + i + "=" + total);
}

Last edited by ad0m : 12-02-2007 at 03:29 AM. Reason: spelling
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-29-2007, 05:21 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
At a quick glance it looks like you have it pretty much figured out. I would add a switch statement in your for loop to account for the different operations.

What specific problems are you having?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-29-2007, 07:43 PM
Member
 
Join Date: Nov 2007
Posts: 4
ad0m is on a distinguished road
IM not getting it to compile - not even that what I have posted above.


As to Switch Statments do they some what go like this

Code:
static void calculation() { int total = 1; int number; int lines; int i; switch() { case a: System.out.println("Multiplication Table"); for(i = 1; i < lines; i++) total = number * i; System.out.println(number + "*" + i + "=" + total); } }
?? man im just lost from here
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-29-2007, 08:08 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Maybe this will help a little more. Make sure to import the Keyboard class at the top. I wasn't able to test it since I don't have to Keyboard class.

I put the for loops inside the switch cases because I thought it would be a little more efficient since it won't be evaluating the switch statement every time. I could be wrong though.

Code:
//Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ public static void main(String[]args) { input(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { int type, lines; char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What maths Table would you like? : "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); calculation(type, lines, table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int maths, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(type){ case 'a': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-29-2007, 09:09 PM
Member
 
Join Date: Nov 2007
Posts: 4
ad0m is on a distinguished road
I have a

Keyboard.class in the same directory

but still not getting an noutput for that method

I run the java and I can Input 1st line Input 2nd line and input 'a' 'b' 'c' or 'd'

but i just get a return of

Multiplication Table
press any key to continue......

Last edited by ad0m : 12-02-2007 at 03:31 AM.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 11-29-2007, 09:12 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
I didn't put the calculate method in the main class because it needs the input from the user to work. Take a look at the code.
Code:
//Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ public static void main(String[]args) { input(); calculation(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { int type, lines; char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What maths Table would you like? : "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int maths, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(type){ case 'a': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
I moved the calculation method back out to main. So imagine that the input() method just ran and you are about to run the calculation() method. The main method doesn't know anything about the what the user entered because nothing was returned from the input method. There is no way to let the calculation know how many lines, what kind of operation to perform and what not.

To fix this, you have 2 options. First would be to return the user input from the input() method back to main. This is possible but complicated due to the fact that a) you have multiple variables to return and b) the variables are of different types. Java only allows one return value. To return multiple values, you would need to return an Array. But arrays can only handle data of on type. To handle the two types that you have (ints and a char) you would need to use a List, Vector.....something along those lines. Now these collections store objects as exactly that....generic Objects. So when you pull them out, you have to type cast them which can cause exceptions and blah blah blah....see what I mean about complicated?

Your second option would be to call the calculation method from inside the input method like I wrote. Look at the code again.

Code:
//Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ public static void main(String[]args) { input(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { int type, lines; char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What maths Table would you like? : "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); calculation(type, lines, table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int maths, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(type){ case 'a': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
Look at the end of the input() method just before calculation is called(). You just obtained all the input from the user and it is right there in that method. So why not just call the calculation() method and pass it that data directly? This saves a bunch of work.

Ok, now that I think about it there is a third option that is also easier than the first. You could set up local fields in the Mathtab class that correspond to your input. The input class would then set these and then the calculation method could use them. That would look something like this:

Code:
//Need to import Keyboard class here. //Did it come from your book or something? import.... class Mathtab{ int type; int lines; char table; //Need a constructor public Mathtab(){ } public static void main(String[]args) { Mathtabl foo = new Mathtab(); foo.input(); foo.calculation(); } //////////////////////////////////////// // Gathering Data from user /////////////////////////////////////// static void input() { Don't need these anymore. //int type, lines; //char table; //Enter the table eg 5 times table 9 Addition table etc System.out.print("What this.type Table would you like? : "); this.type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); this.lines = Keyboard.readInt(); //this sets the filed declared above for this particular object. System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); this.table = Keyboard.readChar(); this.table = Character.toUpperCase(table); } //////////////////////////////////////// // Calculation of Data Gathered /////////////////////////////////////// static void calculation(int this.type, int lines, char type) { int total; System.out.println("Multiplication Table"); int i; switch(this.table){ case 'a': for(i = 1; i <= this.lines; i++){ total = this.type * i; System.out.println(this.type + "*" + i + "=" + total); } break; case 'b': for(i = 1; i <= this.lines; i++){ total = this.type / i; System.out.println(this.type + "/" + i + "=" + total); } break; case 'c': for(i = 1; i <= this.lines; i++){ total = this.type + i; System.out.println(this.type + "+" + i + "=" + total); } break; case 'd': for(i = 1; i <= this.lines; i++){ total = this.type - i; System.out.println(this.type + "-" + i + "=" + total); } } } }
Again, I didn't test any of these but the concepts are accurate.

As far as not getting output, are you sure you moved the calculation method? I think it might be because you are changing the char to upper case. I didn't notice that before. Try changing to case('x') statements to case('X').

Good luck dude.

-Shoe

Quote:
Originally Posted by ad0m
Hi ShoeNinja

Thank you so much was very helpful

I hope it is ok if I can geta few more bits of info off you.

IE

The Method 'input();' is in the "public static void manin(String[] args)"

how come I don't put "calculation" there aswell.. ?



I have the Keyboard.class file int he same directory hence why I dont have to import it.

But I am getting No out put after I select the table i want eg Multiplication or Division "a for *" or "b for /" ect ??


Thanks again so much !
Kindest regards
Adam
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 11-29-2007, 09:12 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Oh yeah. Get rid of that System.out.println("Multiplication Table");. You don't need it.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 11-29-2007, 09:34 PM
Member
 
Join Date: Nov 2007
Posts: 4
ad0m is on a distinguished road
Dude your a tru champion!!!!

I I get stuck again I will update this thred and keep u posted!!

Fantastic stuff

!!!
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 11-29-2007, 09:45 PM
Member
 
Join Date: Nov 2007
Location: Dublin, Ireland
Posts: 12
Mark1989 is on a distinguished road
Send a message via MSN to Mark1989
AWH Shoe,

SAVIOR, I was trying to get the same program to work. True legend.

Cheers!



Mark
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 11-29-2007, 09:47 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
lol, are you guys in the same class or something?

I hope you learn something from it.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 11-29-2007, 10:30 PM
Member
 
Join Date: Nov 2007
Location: Dublin, Ireland
Posts: 12
Mark1989 is on a distinguished road
Send a message via MSN to Mark1989
Quote:
Originally Posted by ShoeNinja View Post
lol, are you guys in the same class or something?

I hope you learn something from it.
Now what makes you think that . Crazy talk.

Yeh learned loads thanks so much.

Extra kind regards,

Mark
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 11-30-2007, 03:14 AM
Member
 
Join Date: Nov 2007
Posts: 3
eljarrah is on a distinguished road
nice
Take this code from me

i change "keyboard" method to "Scanner method"

i use "switch" and "for" loops
i didn't use a method but a direct code

my regards


Code:
import java.util.Scanner; public class Mathtab { public static void main(String[]args) { ///////////// // gather the data from user ///////////////////////////////////////////////// Scanner input = new Scanner(System.in); System.out.print("What maths Table would you like? : "); int table = input.nextInt(); System.out.print("\nHow many lines of the table would you like? :"); int line = input.nextInt(); System.out.print("\nSelect a Table Type (1)Multiplication (2)Division (3)Addition (4)Subtraction: "); int type = input.nextInt(); System.out.print("\n-------------------\n"); /////////////////////////////////////////////////// ///////////////// // calculate the data //////////////////////// switch (type) { case 1: for (int i=1;i<=line;i++) System.out.println(table+"*"+i+"="+(table*i)); break; case 2: for (int i=1;i<=line;i++) System.out.println(table+"/"+i+"="+(table/i)); break; case 3: for (int i=1;i<=line;i++) System.out.println(table+"+"+i+"="+(table+i)); break; case 4: for (int i=1;i<=line;i++) System.out.println(table+"-"+i+"="+(table-i)); break; } System.out.print("\n-------------------"); } }

Last edited by eljarrah : 11-30-2007 at 03:16 AM.
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 11-30-2007, 03:20 AM
Member
 
Join Date: Nov 2007
Posts: 3
eljarrah is on a distinguished road
keyboard ??

Scanner is the best .
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 11-30-2007, 01:45 PM
Member
 
Join Date: Nov 2007
Location: Dublin, Ireland
Posts: 12
Mark1989 is on a distinguished road
Send a message via MSN to Mark1989
Hey,

Haven't used scanner before. Only have a Keyboard.class file.

What i want to know though is how would you get the program to run in a do/while loop, so that at the end the user would be prompted with

Process another (y/n):

so y would repeat and n be the termination of the Program



Code:
class maths{ public static void main(String[]args) { input(); } /* Data Gathering */ static void input() do{ { int type, lines; char table; char again; System.out.print("Which maths Table would you like(1-12)?: "); type = Keyboard.readInt(); System.out.print("\nHow many lines of the table would you like? :"); lines = Keyboard.readInt(); System.out.print("\nSelect a Table Type (a)Multiplication (b)Division (c)Addition (d)Subtraction: "); Keyboard.skipLine(); table = Keyboard.readChar(); table = Character.toUpperCase(table); calculation(type, lines, table); System.out.print("\nProcess Another ('y'/'n'): "); again = Keyboard.readChar(); again = Character.toUpperCase(again); } }while(again == 'Y'); /* Calculation of Data Gathered */ static void calculation(int maths, int lines, char type) { int total; int i; switch(type){ case 'A': for(i = 1; i <= lines; i++){ total = maths * i; System.out.println(maths + "*" + i + "=" + total); } break; case 'B': for(i = 1; i <= lines; i++){ total = maths / i; System.out.println(maths + "/" + i + "=" + total); } break; case 'C': for(i = 1; i <= lines; i++){ total = maths + i; System.out.println(maths + "+" + i + "=" + total); } break; case 'D': for(i = 1; i <= lines; i++){ total = maths - i; System.out.println(maths + "-" + i + "=" + total); } } } }
I have tried the loop in this and several other positions..

HELP PLEASE

Thanks
Mark
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 11-30-2007, 03:41 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 123
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
It looks like you have it except that you need use the .equals method of the char class instead of the boolean operator '=='.

Code:
(while again.equals('Y'))
Aren't you using the Keyboard class because your book is making you? I agree that Scanner would be better but I was under the impression that it was not an option.

Last edited by ShoeNinja : 11-30-2007 at 08:51 PM.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 11-30-2007, 07:19 PM
Member
 
Join Date: Nov 2007
Location: Dublin, Ireland
Posts: 12
Mark1989 is on a distinguished road
Send a message via MSN to Mark1989
Na scanners not an option.

Thanks that helped, I also had the loop in the wrong spot .

But really thanks for the help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I am Doing Something Wrong But Don't Know What? BHCluster New To Java 3 04-16-2008 03:16 PM
what is wrong with this code masaka New To Java 5 04-16-2008 10:27 AM
Cannot understand whats wrong Lehane_9 New To Java 1 03-06-2008 09:57 PM
What's wrong with this code? Wizard wusa New To Java 14 01-23-2008 01:55 AM
Generates random maths questions cachi New To Java 4 08-01-2007 06:48 AM


All times are GMT +3. The time now is 03:04 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org