Jump to content

Basic java problem (greatest of 3 nos.)

- - - - -

  • Please log in to reply
7 replies to this topic

#1
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
Hello,
I have this small programm written in AWT in Java.
But it's not working properly.
Please. can anyone help me out?
It's not finding out the 1st no. as greatest, evrything else is fine.

//WAP TO FIND GREATEST & SMALLEST OF NOS.

//© DHARMiL

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code=Greatest height = 400 width = 500></applet>*/

public class Greatest extends Applet implements ActionListener

{

	TextField t1,t2,t3,t4;

	Button b1,b2;

	Label l1,l2,l3,l4;

	public void init ()

	{

		t1 = new TextField (5);

		t2 = new TextField (5);

		t3 = new TextField (5);

		t4 = new TextField (25);

		b1 = new Button ("SMALLEST");

		b2 = new Button ("GREATEST");

		l1 = new Label ("Enter no 1");

		l2 = new Label ("Enter no 2");

		l3 = new Label ("Enter no 3");

		l4 = new Label ("RESULT is :-");

		add(l1);

		add(t1);

		add(l2);

		add(t2);

		add(l3);

		add(t3);

		add(b1);

		add(b2);

		add(l4);

		add(t4);

		b2.addActionListener (this);

	}

	public void actionPerformed (ActionEvent e)	

	{

		if (e.getSource() == b2);

		{

			int a = Integer.parseInt (t1.getText());

			int b = Integer.parseInt (t2.getText());

			int c = Integer.parseInt (t3.getText());

			{

				if((a>b)&&(a>c))

				{

					t4.setText("no.1 is GREATER");

				}

				if ((b>c));

				{

				t4.setText ("n0.2 is GREATER");

				}

				else

				{	

					t4.setText ("n0.3 is GREATER");

				}

			}

		}

	}

}


#2
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
else if ((b>c));
{
t4.setText ("n0.2 is GREATER");
}


you forgot "else".


#3
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts

Sinipull said:

else if ((b>c));
{
t4.setText ("n0.2 is GREATER");
}


you forgot "else".

iTs giving error

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
You wrote something wrong, try again. Just add else, before if

#5
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You also have a semicolon after the if-statement:

else if ((b>c))[B][SIZE="6"][COLOR="red"];[/COLOR][/SIZE][/B] 

It should be removed.

#6
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
i'VE got the answer.
The right COde is :
//WAP TO FIND GREATEST & SMALLEST OF NOS.

//© DHARMiL

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code=Greatest1 height = 400 width = 500></applet>*/

public class Greatest1 extends Applet implements ActionListener

{

	TextField t1,t2,t3,t4;

	Button b1,b2;

	Label l1,l2,l3,l4;

	public void init ()

	{

		t1 = new TextField (5);

		t2 = new TextField (5);

		t3 = new TextField (5);

		t4 = new TextField (25);

		b1 = new Button ("SMALLEST");

		b2 = new Button ("GREATEST");

		l1 = new Label ("Enter no 1");

		l2 = new Label ("Enter no 2");

		l3 = new Label ("Enter no 3");

		l4 = new Label ("RESULT is :-");

		add(l1);

		add(t1);

		add(l2);

		add(t2);

		add(l3);

		add(t3);

		add(b1);

		add(b2);

		add(l4);

		add(t4);

		b2.addActionListener (this);

	}

	public void actionPerformed (ActionEvent e)	

	{

		if (e.getSource() == b2);

		{

			int a = Integer.parseInt (t1.getText());

			int b = Integer.parseInt (t2.getText());

			int c = Integer.parseInt (t3.getText());

			{

				if((a>b)&&(a>c))

				{

					t4.setText("no.1 is GREATER");

				}

				else

				{

					if ((b>c))

					{

					t4.setText ("n0.2 is GREATER");

					}

					else

					{

						t4.setText ("n0.3 is GREATER");

					}

				}

			}

		}

	}

}


#7
dharmil007

dharmil007

    Newbie

  • Members
  • PipPip
  • 16 posts
hey one more question.
i M trying to make work the smallest button also, but when i click on any button SMALLEST or GREATEST iT Returns on the result of the SMALLEST.
PlS. help

//WAP TO FIND GREATEST & SMALLEST OF NOS.

//© DHARMiL

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*<applet code=Greatest1 height = 400 width = 500></applet>*/

public class Greatest1 extends Applet implements ActionListener

{

	TextField t1,t2,t3,t4;

	Button b1,b2;

	Label l1,l2,l3,l4;

	public void init ()

	{

		t1 = new TextField (5);

		t2 = new TextField (5);

		t3 = new TextField (5);

		t4 = new TextField (25);

		b1 = new Button ("SMALLEST");

		b2 = new Button ("GREATEST");

		l1 = new Label ("Enter no 1");

		l2 = new Label ("Enter no 2");

		l3 = new Label ("Enter no 3");

		l4 = new Label ("RESULT is :-");

		add(l1);

		add(t1);

		add(l2);

		add(t2);

		add(l3);

		add(t3);

		add(b1);

		add(b2);

		add(l4);

		add(t4);

		b1.addActionListener (this);

		b2.addActionListener (this);

	}

	public void actionPerformed (ActionEvent e)	

	{

		//if (e.getSource() == b1);

		if (e.getSource() == b2);

		{

			int a = Integer.parseInt (t1.getText());

			int b = Integer.parseInt (t2.getText());

			int c = Integer.parseInt (t3.getText());

			{

				if((a>b)&&(a>c))

				{

					t4.setText("no.1 is GREATER");

				}

				else

				{

					if ((b>c))

					{

						t4.setText ("n0.2 is GREATER");

					}

					else

					{

						t4.setText ("n0.3 is GREATER");

					}

				}

			}

		}

		if (e.getSource() == b1);

			{

				int x = Integer.parseInt (t1.getText());

				int y = Integer.parseInt (t2.getText());

				int z = Integer.parseInt (t3.getText());

				if ((x<y)&&(x<z))

				{

					t4.setText ("no.1 is SMALLER");

				}

				else

				{

					if ((y<z))

					{

						t4.setText ("no.2 is SMALLER");

					}

					else

					{

						t4.setText ("no. 3 is SMALLER");

					}

				}

			}

		}

	}


#8
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
yeah, it still looks ugly as hell. I would write the method over like this:

public void actionPerformed (ActionEvent e)    
    {
        
        int a = Integer.parseInt (t1.getText());
        int b = Integer.parseInt (t2.getText());
        int c = Integer.parseInt (t3.getText());
            
        if(e.getSource().equals(b2))t4.setText(Math.max(Math.max(a, b),c)+" is the greatest number");                
        else t4.setText(Math.min(Math.min(a, b),c)+" is the smallest number");       
    }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users