-
Having trouble returning max
-
Re: Having trouble returning max
Oops, lets try again.
Trying to write this program with 3 text fields. User enters two integers,
and max number outputs into third text field. I'm a beginner, so be kind

Code:
import java.awt.*;
import java.applet.*;
public class Subclass extends Applet
{
TextField textField1;
TextField textField2;
TextField textFieldMax;
int num1;
int num2;
int max;
public void init()
{
textField1 = new TextField(3);
add(textField1);
textField1.setText("0");
textField2 = new TextField(3);
add(textField2);
textField2.setText("0");
textFieldMax = new TextField(3);
add(textFieldMax);
textFieldMax.setText("0");
}
public int max()
{
String s = textField1.getText();
int num1 = Integer.parseInt(s);
String t = textField2.getText();
int num2 = Integer.parseInt(t);
if (num1 > num2)
max=num1;
else
max=num2;
return max;
}
public void paint (Graphics g)
{
g.drawString("Enter two numbers." , 50, 80);
String m = "Max number is: ";
m+= max;
g.drawString(m, 50, 100);
}
public boolean action (Event event, Object arg)
{
repaint();
return true;
}
}
-
Re: Having trouble returning max
U r doing a small mistake..
u r missing function call () for max.
It should be m+=max(); instead of m+=max;
Cheers
Sagar
<java..@127.0.0.1> wrote:
>
>Oops, lets try again.
>
>Trying to write this program with 3 text fields. User enters two integers,
>and max number outputs into third text field. I'm a beginner, so be kind
>
>
>
Code:
>import java.awt.*;
>import java.applet.*;
>
>public class Subclass extends Applet
>{
> TextField textField1;
> TextField textField2;
> TextField textFieldMax;
> int num1;
> int num2;
> int max;
>
> public void init()
> {
> textField1 = new TextField(3);
> add(textField1);
> textField1.setText("0");
>
> textField2 = new TextField(3);
> add(textField2);
> textField2.setText("0");
>
> textFieldMax = new TextField(3);
> add(textFieldMax);
> textFieldMax.setText("0");
>
> }
>
> public int max()
> {
> String s = textField1.getText();
> int num1 = Integer.parseInt(s);
>
> String t = textField2.getText();
> int num2 = Integer.parseInt(t);
>
> if (num1 > num2)
> max=num1;
> else
> max=num2;
>
> return max;
> }
>
> public void paint (Graphics g)
> {
> g.drawString("Enter two numbers." , 50, 80);
>
> String m = "Max number is: ";
> m+= max;
> g.drawString(m, 50, 100);
>
> }
>
> public boolean action (Event event, Object arg)
> {
> repaint();
> return true;
> }
>}
>
>
>
-
Re: Having trouble returning max
As the previous poster wrote, you should call your function using brackets:
max();
But there are other problems in the code as well - which makes me think that
you may have a hard time distiguishing between functions and variables. You
can remove a lot of your code - which is a nice thing. See comments below
(in the code).
best wishes
Jesper
<java..@127.0.0.1> wrote:
>
>Oops, lets try again.
>
>Trying to write this program with 3 text fields. User enters two integers,
>and max number outputs into third text field. I'm a beginner, so be kind
>
>
>
Code:
>import java.awt.*;
>import java.applet.*;
>
>public class Subclass extends Applet
>{
> TextField textField1;
> TextField textField2;
> TextField textFieldMax;
Code:
You don't need these three attributes:
> int num1;
> int num2;
> int max;
>
> public void init()
> {
> textField1 = new TextField(3);
> add(textField1);
> textField1.setText("0");
>
> textField2 = new TextField(3);
> add(textField2);
> textField2.setText("0");
>
> textFieldMax = new TextField(3);
> add(textFieldMax);
> textFieldMax.setText("0");
>
> }
>
> public int max()
> {
put the max variable in here:
int max;
> String s = textField1.getText();
> int num1 = Integer.parseInt(s);
>
> String t = textField2.getText();
> int num2 = Integer.parseInt(t);
>
> if (num1 > num2)
> max=num1;
> else
> max=num2;
>
> return max;
> }
>
> public void paint (Graphics g)
> {
> g.drawString("Enter two numbers." , 50, 80);
>
> String m = "Max number is: ";
> m+= max;
This should be:
m+= max();
> g.drawString(m, 50, 100);
>
> }
>
> public boolean action (Event event, Object arg)
> {
> repaint();
> return true;
> }
>}
>
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks