944,093 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 892
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 6th, 2009
-3

mutlipication table in java program

Expand Post »
how to create a mutlipication table in java program for example:
enter x=5
enter y =6

output is:
1*6=6
2*6=12
3*6=18
4*6=24
5*6=30
Similar Threads
LKH
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LKH is offline Offline
5 posts
since Sep 2009
Oct 6th, 2009
0
Re: mutlipication table in java program
1) Create a array, which represents a table.
2) use a for loop;

Java Syntax (Toggle Plain Text)
  1. for(int i = 1; i != 5; i++)
  2. myArrayTable[i] = 6*i; //say, myArrayTable is already declared.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,864 posts
since Dec 2008
Oct 6th, 2009
0
Re: mutlipication table in java program
1) Create a array, which represents a table.
2) use a for loop;

Java Syntax (Toggle Plain Text)
  1. for(int i = 1; i != 5; i++)
  2. myArrayTable[i] = 6*i; //say, myArrayTable is already declared.
I have a question:
Why didn't you use this at the for loop:
Java Syntax (Toggle Plain Text)
  1. for(int i = 1; i < 5; i++)
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 447
Nearly a Senior Poster
javaAddict is offline Offline
3,260 posts
since Dec 2007
Oct 6th, 2009
0

its easier using 2 classes.

1)
Class: Multiply
Attributes: int x
int y
Methods: getters&setters
Constructors
toString

in Main
create array or arraylist of the class Multiply
take in the 2 numbers into each
to print:
iterate through the array and print the "Multiply.toString()" and the "toString()" will include the "x*y = z" to form each line

I would recomend using the IDE's BlueJ or Netbeans (Netbeans does most of it for you hence is my favourite)

any errors in my repllies then pm me please as I am also a learner
Reputation Points: 10
Solved Threads: 0
Newbie Poster
0805638 is offline Offline
14 posts
since Oct 2009
Oct 13th, 2009
0
Re: mutlipication table in java program
sorry! I am new learner for java program, I still confuse about your answer. can you give me some example for this program, thanks...
LKH
Reputation Points: 10
Solved Threads: 0
Newbie Poster
LKH is offline Offline
5 posts
since Sep 2009
Oct 13th, 2009
0
Re: mutlipication table in java program
They just gave you a more than sufficient reply. If you don't understand the reply either go study about for loops and arrays, or ask a specific question if there is something you don't understand. We can't read your mind.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 3rd, 2009
0
Re: mutlipication table in java program
Click to Expand / Collapse  Quote originally posted by javaAddict ...
I have a question:
Why didn't you use this at the for loop:
Java Syntax (Toggle Plain Text)
  1. for(int i = 1; i < 5; i++)
late reply but its just preference at times.

A thing to note ( at least in c++) is that != is more generic than
'<' because '!=' works with containers like vectors, string map
while '<' is not compatible with all of them. I imagine that
java is similar. It also depends on context btw.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,864 posts
since Dec 2008
Nov 3rd, 2009
0
Re: mutlipication table in java program
interesting point. I haven't seen the non-equals operator used as a loop conditional before. I have to admit that what you said makes sense. I would have never thought of it in that way before. But . . . what guarantee do you have that you are actually comparing values in c++? wouldn't a statement like objecta != objectb be interpreted as: The address where objecta resides is not equal to the address where objectb resides?
Besides, c++ allows you to overload your operators so there is no ambiguity. The classes you are talking about may have just implemented that.
But as far as comparing primitives? Sure != 5 looks hunky dory. I've been using comparison operators for the last 15 years, so I don't thnink I'll all of the sudden switch though.
Reputation Points: 31
Solved Threads: 4
Junior Poster in Training
eggmatters is offline Offline
67 posts
since Nov 2008
Nov 3rd, 2009
0
Re: mutlipication table in java program
>>
"what guarantee do you have that you are actually comparing values in c++?"
<<

In c++ the "!=" operator when used with container from std does not compare address like the "==" in java ( at times), it only compares
the values. The standard C++ library was implemented with generic in
mind.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is online now Online
3,864 posts
since Dec 2008
Nov 3rd, 2009
0
Re: mutlipication table in java program
In c++ the "!=" operator when used with container from std does not compare address like the "==" in java ( at times), it only compares
the values. The standard C++ library was implemented with generic in
mind.
Well, if that is the case, those operators would have had to have been overloaded for those operations, otherwise the comparitors would compare addresses. I would have to go and look at the library code, (I use DevC++) to check and see if that's the case. I'm pretty sure that for any object regardless if it's in a collection or not will have to have the comparitors overloded in addition to providing a deep copy constructor. Come to think of it, if you call something to the effect of:
c++ Syntax (Toggle Plain Text)
  1. someCollection<someClass> someObject[] = new someCollection<someClass>()[2];
  2. someObject[0] = someData;
  3. someObject[1] = otherData;
  4.  
  5. if (someObject[0] != someObject[1])
  6. {
  7. std::cout << "AHHHHHH!!!! Run for your lives!!! ";
  8. }
that operator will be called from the overloaded operator defined in someClass, not someCollection. What I was saying, without knowing if someObject overloads !=, you have no guarantee that you are comparing someData and otherData. If not you will be comparing the dereferenced addresses of each object. So in java, this is one of the reasons that all classes derive from Object. That way, they can implement the equals() method to ensure that you are comparing by value and not by reference.
Also, c++ calls generic types template types. But you knew that.
So . . . should this thread be moved to c++ or did LKH get his answer? Sorry for the rambling.
Last edited by eggmatters; Nov 3rd, 2009 at 3:18 pm.
Reputation Points: 31
Solved Threads: 4
Junior Poster in Training
eggmatters is offline Offline
67 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java ActionListener Question
Next Thread in Java Forum Timeline: Merging two files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC