Monday, August 15, 2011

java program to find largest of three numbers

java program to find largest of three numbers

This java program finds largest of three numbers and then prints it. If the entered numbers are unequal then "numbers are not distinct" is printed.

import java.util.Scanner;

class LargestOfThreeNumbers

{

public static void main(String args[])

{

int x, y, z;

System.out.println("Enter three integers ");

Scanner in = new Scanner(System.in);

x = in.nextInt();

y = in.nextInt();

z = in.nextInt();

if ( x > y && x > z )

System.out.println("First number is largest.");

else if ( y > x && y > z )

System.out.println("Second number is largest.");

else if ( z > x && z > y )

System.out.println("Third number is largest.");

else

System.out.println("Entered numbers are not distinct.");

}

}

Largest of three numbers program class file.Output:

· · *multilevel inheritance*/

· class A

· {

· int i=10,j=20;

· void xyz()

· {

· System.out.println(i+j);

· }

· void lmn()

· {

· System.out.println("lmn");

· }

· }

· class B extends A

· {

· int k=40,l=45;

· void abc()

· {

· System.out.println(i+j+k);

· lmn();

· }

· }

· class C extends B

· {

· }

· class Inherit

· {

· public static void main(String args[])

· {

· C a1= new C();

· a1.abc();

· }

· }

·

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

*/

public class calc extends Applet implements ActionListener{

String msg = "",flag = "";

int i=0,j=0;

Button one,two,three,four,five,six,seven,eight,nine,zero;

Button plus,minus,mul,div,equal,ce;

TextField text;

Button blist[] = new Button[10];

public void init(){

setLayout(null);

one = new Button("1");

two = new Button("2");

three = new Button("3");

four = new Button("4");

five = new Button("5");

six = new Button("6");

seven = new Button("7");

eight = new Button("8");

nine = new Button("9");

zero = new Button("0");

plus = new Button("+");

minus = new Button("-");

mul = new Button("*");

div = new Button("/");

equal = new Button("=");

ce = new Button("c");

one.setBounds(200,200,20,20);

two.setBounds(230,200,20,20);

three.setBounds(260,200,20,20);

four.setBounds(200,230,20,20);

five.setBounds(230,230,20,20);

six.setBounds(260,230,20,20);

seven.setBounds(200,260,20,20);

eight.setBounds(230,260,20,20);

nine.setBounds(260,260,20,20);

zero.setBounds(200,290,20,20);

plus.setBounds(290,200,20,20);

minus.setBounds(290,230,20,20);

mul.setBounds(290,260,20,20);

div.setBounds(290,290,20,20);

equal.setBounds(260,290,20,20);

ce.setBounds(230,290,20,20);

text = new TextField(10);

text.setBounds(200,170,110,20);

blist[0] = (Button) add(zero);

blist[1] = (Button) add(one);

blist[2] = (Button) add(two);

blist[3] = (Button) add(three);

blist[4] = (Button) add(four);

blist[5] = (Button) add(five);

blist[6] = (Button) add(six);

blist[7] = (Button) add(seven);

blist[8] = (Button) add(eight);

blist[9] = (Button) add(nine);

add(plus);

add(minus);

add(mul);

add(div);

add(equal);

add(ce);

add(text);

for(int i=0;i<10;i++)

blist[i].addActionListener(this);

plus.addActionListener(this);

minus.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

equal.addActionListener(this);

ce.addActionListener(this);

text.addActionListener(this);

}

public void actionPerformed(ActionEvent ae){

String str = ae.getActionCommand();

if(str.equals("1"))

i = i*10 + 1;

else if(str.equals("2"))

i = i*10 + 2;

else if(str.equals("3"))

i = i*10 + 3;

else if(str.equals("4"))

i = i*10 + 4;

else if(str.equals("5"))

i = i*10 + 5;

else if(str.equals("6"))

i = i*10 + 6;

else if(str.equals("7"))

i = i*10 + 7;

else if(str.equals("8"))

i = i*10 + 8;

else if(str.equals("9"))

i = i*10 + 9;

else if(str.equals("0"))

i = i*10;

if (str.equals("=")){

if (flag.equals("+"))

i += j;

if (flag.equals("-"))

i = j-i;

if (flag.equals("*"))

i *= j;

if (flag.equals("/"))

i = j/i;

}

if (str.equals("+")){

if (flag.equals("+"))

{i += j;j=i;}

else

{j = i;i=0;}

flag = "+";

}

if (str.equals("-")){

if (flag.equals("-"))

{ i = j-i;j=i;}

else

{j = i;i=0;}

flag = "-";

}

if (str.equals("*")){

if (flag.equals("*"))

{ i *= j;j=i;}

else

{j = i;i=0;}

flag = "*";

}

if (str.equals("/")){

if (flag.equals("/"))

{ i = j/i;j=i;}

else

{j = i;i=0;}

flag = "/";

}

if (str.equals("c"))

{ i = 0;j=0;flag = "";}

text.setText(i+"");

if (str.equals("=")) i=0;

repaint();

}

public void paint(Graphics g){

g.drawString(msg,100,100);

}

}

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/*

*/

public class window1 extends Applet

{

Label uname,pwd,lan,gen;

TextField t1,t2;

Checkbox c4,c5,c6,c7,c8;

CheckboxGroup cg;

public void init()

{

setLayout(null);

color c=new Color(23,120,230);

Font f=new Font("SERIF",Font.ITALIC,23);

setFont (f);

uname=new Label("uname",Label.CENTER);

Color c1=new Color(12,200,243);

uname.setBackground(c1);

uname.setBackground(50,50,150,245);

pwd=new Label("password");

pwd.setBounds(50,120,30,20);

lan=new Label("Lan");

gen=new Label("gen");

t1=new Textfield(100);

t1.setBounds(250,50,150,34);

t1.setBackgroud(Color.red);

t2=new TextField(25);

t2.setBounds(240,50,130,87);

t2.setBackgroud(Color.green);

c4=new Checkbox("hindi");

c4.setBounds(200,300,84,20);

c5=new Checkbox("english");

c5.setBounds(300,300,84,30);

c6=new Checkbox("tamil");

c6.setBounds(500,400,84,20);

cg=new CheckboxGroup();

c7=new Checkbox("male",false,cg);

c7.setBounds(400,400,84,20);

c8=new Checkbox("female",false,cg);

c8.setBounds(500,400,84,20);

add(c4);

add(c5);

add(c6);

add(c7);

add(c8);

add(uname);

add(pwd);

add(lan);

add(gen);

setBackground(c);

}

No comments:

Post a Comment