pass an interger from one class to others class

Reply

Join Date: May 2008
Posts: 1
Reputation: dummbe is an unknown quantity at this point 
Solved Threads: 0
dummbe dummbe is offline Offline
Newbie Poster

pass an interger from one class to others class

 
0
  #1
May 24th, 2008
hi , i have this problem, which i cannot figure it out. This problem has been day now,and reached does not seem to help. How do u pass an integer value form one class to the other class. Here my code. The integer that summited by the user which is stored in the "amt" and amt is converted to integer, that is amount. Then the amount would be call be the class counter, so that is will be calculated until it reach zero. Also i am beginner in Java.ty



import javax.swing.*;

class input
{
int amount, num;
String amt;
public void userInput ()

{


JFrame frame = new JFrame();
amt = JOptionPane.showInputDialog(frame, "How much would u like?");
num= Integer.parseInt(amt);
amount = num;

counter obj = new counter();
obj.numberCounter();

}

}
class counter extends input
{

public void numberCounter ()
{
input count = new input();


int soda=10;
if (soda==0){
System.out.println("Out of soft drink");
}
else if (count.amount >1 && count.amount <10 ){
while (count.amount >1 && count.amount <10 )
{
input objj = new input();
objj.userInput();
}

}else {
soda=soda - count.amount;

}
}
}

class Test
{
public static void main (String args[]){
System.out.println("Hi");

input objj = new input();
objj.userInput();


}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: pass an interger from one class to others class

 
0
  #2
May 25th, 2008
I've no clue how "u" would do it. I do know how I would do it and that's described in every beginner's tutorial so go read one.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 1
Reputation: orangehead911 is an unknown quantity at this point 
Solved Threads: 0
orangehead911 orangehead911 is offline Offline
Newbie Poster

Re: pass an interger from one class to others class

 
0
  #3
May 26th, 2008
Your problem seems to be one of understanding how object oriented programming works and how conditionals work.

I found a few mistakes in your code as I was going through it. When picking up a new programming language I feel it's important to also pick up coding conventions such as class and method naming conventions.

This is the current flow of your program:
Lines 43-44: You create a new Input class instance, the you call userInput.
Lines 7-10: An input is requested from the user, then parsed.
Lines 12-13: A new instance of Counter is created, and numberCounter is invoked.
Line 20: Yet another Input class is instantiated with no connection to the first instance (this is where the problem lies).

  1. class Input {
  2.  
  3. int amount, num;
  4. String amt;
  5.  
  6. public void userInput() {
  7. final JFrame frame = new JFrame();
  8. amt = JOptionPane.showInputDialog(frame, "How much would u like?");
  9. num = Integer.parseInt(amt);
  10. amount = num;
  11.  
  12. final Counter obj = new Counter();
  13. obj.numberCounter();
  14. }
  15. }
  16.  
  17. class Counter extends Input {
  18.  
  19. public void numberCounter() {
  20. final Input count = new Input();
  21.  
  22. int soda = 10;
  23. if (soda == 0) {
  24. System.out.println("Out of soft drink");
  25. } else if (count.amount > 1 && count.amount < 10) {
  26.  
  27. while (count.amount > 1 && count.amount < 10) {
  28. final Input objj = new Input();
  29. objj.getUserInput();
  30. }
  31. } else {
  32. soda = soda - count.amount;
  33. }
  34. }
  35. }
  36.  
  37. class Test {
  38.  
  39. public static void main(final String... args) {
  40.  
  41. System.out.println("Hi");
  42.  
  43. final Input objj = new Input();
  44. objj.userInput();
  45. }
  46. }

I assume that this is for course work, so I won't provide a solution for you. I will say this, you have two classes too many. If you simplify the code using only one class, I am sure you will nail it right away.

Good luck!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC