Having trouble returning max


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Having trouble returning max

  1. #1
    Lou Guest

    Having trouble returning max




  2. #2
    Guest

    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;
    }
    }

  3. #3
    sagar Guest

    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;
    >	}
    >}
    >
    >
    >



  4. #4
    Jesper Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center