| | |
pass an interger from one class to others class
![]() |
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
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();
}
}
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();
}
}
•
•
Join Date: May 2008
Posts: 1
Reputation:
Solved Threads: 0
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).
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!
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).
Java Syntax (Toggle Plain Text)
class Input { int amount, num; String amt; public void userInput() { final JFrame frame = new JFrame(); amt = JOptionPane.showInputDialog(frame, "How much would u like?"); num = Integer.parseInt(amt); amount = num; final Counter obj = new Counter(); obj.numberCounter(); } } class Counter extends Input { public void numberCounter() { final 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) { final Input objj = new Input(); objj.getUserInput(); } } else { soda = soda - count.amount; } } } class Test { public static void main(final String... args) { System.out.println("Hi"); final Input objj = new Input(); objj.userInput(); } }
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!
![]() |
Other Threads in the Java Forum
- Previous Thread: Graphe implementation
- Next Thread: Tic Tac Toe - Efficiency
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle physics plazmic print problem program programming project recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree trolltech unlimited utility webservices windows






