java assigment

Post on 03-Apr-2015

382 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

Java Program's

TRANSCRIPT

Java Assignment

Bhavin Modi Page 1

ASSIGMENT-1

//Write an Application program to print your name and

address.

class Ex1

{

public static void main(String args[])

{

System.out.println("Bhavin Modi");

System.out.print("Ankleshwar");

}

}

ASSIGMENT-2

/**

Write an Application program to find two matrix

multiplication.

1 2 3 1 2 3 = 1 4 9

4 5 0 * 1 2 3 = 4 10 0

6 7 8 1 2 3 = 6 14 24.

*/

Java Assignment

Bhavin Modi Page 2

import java.io.*;

class arr2mul

{

public static void main(String args[])

{

int i=0,j=0;

DataInputStream in = new

DataInputStream(System.in);

try

{

int n[][] = new int[3][3];

int m[][] = new int[3][3];

int mul[][] = new int[3][3];

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

System.out.print("Enter n[" + i + "][" + j + "]: ");

Java Assignment

Bhavin Modi Page 3

n[i][j] = Integer.parseInt(in.readLine());

}

}

System.out.println();

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

System.out.print("Enter m[" + i + "][" + j + "]: ");

m[i][j] = Integer.parseInt(in.readLine());

}

}

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

mul[i][j] = n[i][j] * m[i][j];

}

}

Java Assignment

Bhavin Modi Page 4

System.out.println();

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

System.out.println(n[i][j] + " * " + m[i][j] + " = " +

mul[i][j]);

}

System.out.println();

}

}

catch (Exception e){}

}

}

ASSIGMENT-3

/*

Java Assignment

Bhavin Modi Page 5

Write an Application program to find area and

perimeter of a rectangle.

Note: define static methods.

Area Return l*b;

Perimeter Return 2*(l+b);

*/

import java.io.*;

import java.lang.*;

class fun

{

static float area(float x,float y)

{

return (x * y);

}

static float perimeter(float x,float y)

{

return (2 * (x + y));

}

}

Java Assignment

Bhavin Modi Page 6

class rect

{

public static void main(String args[])

{

DataInputStream in = new

DataInputStream(System.in);

float l=0,b=0;

try

{

System.out.flush();

System.out.print("Enter Length: ");

l = Float.valueOf(in.readLine());

System.out.print("Enter Breadth: ");

b = Float.valueOf(in.readLine());

}

catch (Exception e)

{

Java Assignment

Bhavin Modi Page 7

System.out.print("IO Error");

}

System.out.println();

System.out.println("Area of a rectangle = " +

fun.area(l,b));

System.out.println("Perimeter of a rectangle = " +

fun.perimeter(l,b));

}

}

ASSIGMENT-4

/*

Write a program for Hardware company. Ask the user

to choose F for Floppy, C for CD, P for Pen drive.

Show the price of each item.

Show price of a hardware manufactured with the

chosen wood.

Java Assignment

Bhavin Modi Page 8

Floppy price is 15 Rs., CD price is 20 Rs and Pen drive

price is 1250 Rs.

The class name is Hardware.

*/

class Hardware

{

public static void main(String args[])

{

char ch;

System.out.println("F for Floppy");

System.out.println("C for CD");

System.out.println("P for Pen Drive");

System.out.flush();

System.out.print("Select your choice: ");

try

{

switch(ch =(char)System.in.read())

{

Java Assignment

Bhavin Modi Page 9

case 'F':

case 'f':

System.out.print("Floppy price is 15 Rs.");

break;

case 'C':

case 'c':

System.out.print("CD price is 20 Rs.");

break;

case 'P':

case 'p':

System.out.print("Pen Drive price is 1250

Rs.");

break;

default:

System.out.print("Wrong choice");

}

}

catch (Exception e)

{

Java Assignment

Bhavin Modi Page 10

System.out.print("I/O Error");

}

}

}

ASSIGMENT-5

/*

Write a menu driven an Application program.

1.Check for positive or negative number

2.Check for odd or even number

3.Check for primary number

4.Check for Palindrome number

5.Check for Armstrong number

6.Check for number whether a member of fibonacci

series

7.Exit

Create abstract methods for every check into abstract

Proto1 class and

define into Defi1 class and call from Menu1 class.

*/

Java Assignment

Bhavin Modi Page 11

import java.io.*;

abstract class Proto1

{

abstract void disp();

abstract void pn(int x);

abstract void oe(int x);

abstract void prime(int x);

// abstract void palin(int x);

abstract void arms(int x);

abstract void fibo(int x);

abstract void exit();

}

class Defi1 extends Proto1

{

int n = 0;

void disp()

{

System.out.println("1.Check for positive or

negative number ");

Java Assignment

Bhavin Modi Page 12

System.out.println("2.Check for odd or even

number");

System.out.println("3.Check for primary number");

System.out.println("4.Check for Palindrome

number");

System.out.println("5.Check for Armstrong

number");

System.out.println("6.Check for number whether a

member of fibonacci series ");

System.out.println("7.Exit");

System.out.println();

}

void pn(int x)

{

n = x;

if ( n > 0)

System.out.print("Number is Positive");

else

System.out.print("Number is Negative");

}

Java Assignment

Bhavin Modi Page 13

void oe(int x)

{

n = x;

if ((n%2) == 1)

System.out.print("Number is Odd");

else

System.out.print("Number is Even");

}

void prime(int x)

{

int i=0,c=0;

n = x;

for(i=1;i<=n;i++)

{

if (n%i == 0)

c++;

}

if (c==0)

Java Assignment

Bhavin Modi Page 14

System.out.print("primary no is " + n);

else

System.out.print("Not primary no");

}

void arms(int x)

{

n = x;

int a,b,c,sum=0;

a = n/100;

b = (n-(a*100))/10;

c = (n-(a*100))-(b*10);

sum = (a*a*a) + (b*b*b) + (c*c*c);

if (sum == n)

System.out.print("Armstrong no is " + sum);

else

System.out.print("Not Armstrong no");

}

Java Assignment

Bhavin Modi Page 15

void fibo(int x)

{

n = x;

int n1=1,n2=0,sum=0;

System.out.print("Fibonacci series is ");

while(n>0)

{

sum = n1+n2;

if (x==sum)

System.out.print("fibo");

n1 = n2;

n2 = sum;

n--;

System.out.print(sum + " ");

}

}

void exit()

{

System.out.print("Exit");

}

Java Assignment

Bhavin Modi Page 16

}

class Menu1

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

Defi1 d1=new Defi1();

String s = new String();

do

{

d1.disp();

System.out.print("Select your choice: ");

int ch = Integer.parseInt(in.readLine());

try

{

Java Assignment

Bhavin Modi Page 17

System.out.print("Enter Number: ");

int n = Integer.parseInt(in.readLine());

switch(ch)

{

case 1:

d1.pn(n);

break;

case 2:

d1.oe(n);

break;

case 3:

d1.prime(n);

break;

case 5:

d1.arms(n);

break;

case 6:

d1.fibo(n);

break;

case 7:

Java Assignment

Bhavin Modi Page 18

d1.exit();

break;

default:

System.out.print("Wrong choice");

}

}

catch (Exception e)

{

System.out.print("I/O Error");

}

System.out.println();

System.out.print("\t\tDo want to Continue (y/n)?:

");

s = in.readLine();

}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));

}

}

ASSIGMENT-6

Java Assignment

Bhavin Modi Page 19

/**

Create an abstract class Auto with fields for the car

maker and price.

Include get and set methods for these fields;

the setPrice() method is an abstract.

Create two subclasses for individual automobile

makers and

include appropriate setPrice() methods in each

subclass.

Finally, write a program that uses the Auto class and

subclasses

to display information about different cars.

*/

import java.io.*;

abstract class Auto

{

abstract void setprice(int x);

}

class maruti extends Auto

{

void get()

Java Assignment

Bhavin Modi Page 20

{

System.out.println("Car Maker is Maruti");

}

void setprice(int x)

{

System.out.println("Car Price is " + x);

}

}

class honda extends Auto

{

void get()

{

System.out.println("Car Maker is Honda");

}

void setprice(int x)

{

System.out.println("Car Price is " + x);

}

}

Java Assignment

Bhavin Modi Page 21

class car

{

public static void main(String args[]) throws

Exception

{

DataInputStream in = new

DataInputStream(System.in);

System.out.print("Enter Car Price: ");

int cp = Integer.parseInt(in.readLine());

maruti m = new maruti();

m.get();

m.setprice(cp);

honda h = new honda();

h.get();

h.setprice(cp);

}

}

ASSIGNMENT-7

Java Assignment

Bhavin Modi Page 22

/**

Create a class named Square that contains data fields

for height, width,

and surfaceArea, and a method named

computeSurfaceArea().

Create child class Cube.

Cube contains an additional data field named depth,

and a computeSurfaceArea() method that overrides

the parent method.

Write a program that instantiate a Square object and

a Cube object and

display the surface areas of the objects.

*/

import java.io.*;

class square

{

int height=0,width=0,surfaceArea=0;

square(int x,int y)

{

height=x;

Java Assignment

Bhavin Modi Page 23

width=y;

}

int sfa()

{

surfaceArea=height * width;

return surfaceArea;

}

}

class Cube extends square

{

int depth=0;

Cube(int x,int y,int z)

{

super(x,y);

depth=z;

}

int sfa()

{

return (height * width * depth);

}

Java Assignment

Bhavin Modi Page 24

}

class surfacearea

{

public static void main(String args[])

{

int h=0,w=0,d=0;

DataInputStream in = new

DataInputStream(System.in);

try

{

System.out.print("Enter height: ");

h = Integer.parseInt(in.readLine());

System.out.print("Enter width: ");

w = Integer.parseInt(in.readLine());

System.out.print("Enter depth: ");

d = Integer.parseInt(in.readLine());

}

catch (Exception e){}

Java Assignment

Bhavin Modi Page 25

square s = new square(h,w);

Cube c = new Cube(h,w,d);

System.out.println("Square SurfaceArea is " +

s.sfa());

System.out.println("Cube SurfaceArea is " + c.sfa());

}

}

ASSIGNMENT-8

/**

Write a simple java program that give the list of valid

and invalid numbers

using command line arguments.

Input : 112 java 23.2 3434

Output: valid numbers:2 invalid numbers :2

*/

class cmdvi

{

public static void main(String args[])

Java Assignment

Bhavin Modi Page 26

{

int invalid=0,valid=0,n;

for(int i=0;i<args.length;i++)

{

try

{

n = Integer.parseInt(args[i]);

}

catch(Exception e)

{

invalid++;

continue;

}

valid++;

}

System.out.println("Valid Number: " + valid);

System.out.println("Invalid Number: " + invalid);

}

}

ASSIGNMENT-9

Java Assignment

Bhavin Modi Page 27

/**

Create 1integer array of size 10 and initialize the

array with some values.

Write a program to arrange the entire array into its

ascending order and

display the sorted array.

*/

import java.io.*;

class ascending

{

public static void main(String args[])

{

int i=0,j=0,swap=0;

DataInputStream in = new

DataInputStream(System.in);

try

{

int n[] = new int[10];

Java Assignment

Bhavin Modi Page 28

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

{

System.out.print("Enter n[" + i + "]: ");

n[i] = Integer.parseInt(in.readLine());

}

System.out.println();

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

{

for(j=0;j<10;j++)

{

if (n[i] < n[j])

{

swap = n[i];

n[i] = n[j];

n[j] = swap;

}

}

}

System.out.print("Ascending order is ");

Java Assignment

Bhavin Modi Page 29

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

{

System.out.print(n[i] + " ");

}

}

catch (Exception e){}

}

}

ASSIGNMENT-10

/**

Write an Application program to find matrix

multiplication table

for example.

1 2 3

------------

1 | 1 2 3

2 | 2 4 6

3 | 3 6 9

Note: use command-line arguments(R C)

Java Assignment

Bhavin Modi Page 30

*/

class multitable

{

public static void main(String args[])

{

int i=0,j=0;

int r[] = new int[3];

int c[] = new int[3];

int m[][] = new int[3][3];

for(i=0;i<3;i++)

{

r[i]=Integer.parseInt(args[i]);

}

for(j=0;i<6;i++,j++)

{

c[j]=Integer.parseInt(args[i]);

}

Java Assignment

Bhavin Modi Page 31

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

m[i][j] = r[i] * c[j];

}

}

System.out.print(" ");

for(i=0;i<3;i++)

{

System.out.print(r[i] + " ");

}

System.out.println();

System.out.print(" ------");

System.out.println();

for(i=0;i<3;i++)

{

System.out.print(c[i] + "| ");

for(j=0;j<3;j++)

{

Java Assignment

Bhavin Modi Page 32

System.out.print(m[i][j] + " ");

}

System.out.println();

}

}

}

ASSIGNMENT-11

/**

Write menu driven an Application program for

following string manipulation.

Reverse a string.

Sorting (using a string)

Conversion from upper to lower and vice-versa.

Exit.

*/

import java.io.*;

import java.lang.*;

class fun

Java Assignment

Bhavin Modi Page 33

{

String s,s1 = new String();

int i,j;

char temp;

void rev(String s)

{

System.out.print("Reverse String is ");

for(i=s.length()-1;i>=0;i--)

{

System.out.print(s.charAt(i));

}

}

void sort(String s)

{

char s2[] = new char[s.length()];

System.out.print("Sorted String is ");

for(i=0;i<s.length();i++)

{

Java Assignment

Bhavin Modi Page 34

s2[i] = s.charAt(i);

}

for(i=0;i<s.length();i++)

{

for(j=i+1;j<s.length();j++)

{

if(s2[j].compareTo(s2[i]) < 0)

{

temp = s2[i];

s2[i] = s2[j];

s2[j] = temp;

}

}

}

System.out.print(s2);

}

void uc(String s)

{

Java Assignment

Bhavin Modi Page 35

System.out.println("Upper String is " +

s.toUpperCase());

}

void lc(String s)

{

System.out.println("Lower String is " +

s.toLowerCase());

}

void exit()

{

System.out.println("Exit");

}

}

class strfun

{

public static void main(String args[]) throws

IOException

{

Java Assignment

Bhavin Modi Page 36

DataInputStream in = new

DataInputStream(System.in);

int ch;

fun f = new fun();

String s = new String();

System.out.println("1.Reverse a string");

System.out.println("2.Sorting a string");

System.out.println("3.Conversion from upper to

lower");

System.out.println("4.Conversion from lower to

upper");

System.out.println("5.Exit");

System.out.println();

System.out.print("Select your choice: ");

try

{

System.out.flush();

ch = Integer.parseInt(in.readLine());

Java Assignment

Bhavin Modi Page 37

System.out.print("Enter String: ");

s = in.readLine();

switch(ch)

{

case 1:

f.rev(s);

break;

case 2:

f.sort(s);

break;

case 3:

f.lc(s);

break;

case 4:

f.uc(s);

break;

case 5:

f.exit();

break;

default:

Java Assignment

Bhavin Modi Page 38

System.out.print("Wrong choice");

}

}

catch (Exception e)

{

System.out.print("I/O Error");

}

}

}

ASSIGNMENT-12

/**

You are given a sting str ="sardar patel university".

Perform the following operation on it.

a. find the length of string

b. replace the character p' by 'r'

c. convert all character in uppercase

extract and print "sardar" from given string.

Java Assignment

Bhavin Modi Page 39

*/

import java.io.*;

import java.lang.*;

class fun

{

String s = new String("sardar patel university");

void len()

{

System.out.print("Length of String is " +

s.length());

}

void rep()

{

System.out.print("Replaced String is " +

s.replace('p','r'));

}

void uc()

Java Assignment

Bhavin Modi Page 40

{

System.out.println("Upper String is " +

s.toUpperCase());

}

void ext()

{

System.out.println("Extract String is " +

s.substring(0,6));

}

void exit()

{

System.out.println("Exit");

}

}

class strfun

{

public static void main(String args[]) throws

IOException

{

Java Assignment

Bhavin Modi Page 41

DataInputStream in = new

DataInputStream(System.in);

fun f = new fun();

System.out.println();

System.out.println("a. find the length of string");

System.out.println("b. replace the character p' by

'r'");

System.out.println("c. convert all character in

uppercase");

System.out.println("d. extract and print " + '\"' +

"sardar" + '\"' + " from given string.");

System.out.println("e. Exit");

System.out.println();

System.out.print("Select your choice: ");

try

{

char ch;

switch(ch = (char) System.in.read())

Java Assignment

Bhavin Modi Page 42

{

case 'a':

f.len();

break;

case 'b':

f.rep();

break;

case 'c':

f.uc();

break;

case 'd':

f.ext();

break;

case 'e':

f.exit();

break;

default:

System.out.print("Wrong choice");

}

}

Java Assignment

Bhavin Modi Page 43

catch (Exception e)

{

System.out.print("I/O Error");

}

}

}

ASSIGNMENT-13

/**

Write an Application program that search through its

command-line arguments

if an argument is found that does not begin with an

Uppercase letter

display an error message and terminate.

*/

class search

{

public static void main(String args[])

{

int c = args[0].length(),n;

Java Assignment

Bhavin Modi Page 44

String str = args[0];

char ch;

try

{

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

{

ch = str.charAt(i);

if (ch >= 65 && ch <= 90)

break;

else

System.out.print (ch);

}

}

catch(Exception e)

{

System.out.print("IO Error");

}

}

Java Assignment

Bhavin Modi Page 45

}

ASSIGNMENT-14

/**

Write an Application program to find maximum

number from two numbers of

any data type using command-line arguments.Note:

Using the concept of method overloading.

*/

class fun

{

void max(int a,int b)

{

if(a>b)

System.out.println(a+ " is greater");

else

System.out.println(b+ " is greater");

}

void max(double a,double b)

{

Java Assignment

Bhavin Modi Page 46

if(a>b)

System.out.println(a+ " is greater");

else

System.out.println(b+ " is greater");

}

void max(char a,char b)

{

if(a>b)

System.out.println("\'"+a+"\'"+ " is greater");

else

System.out.println("\'"+b+"\'"+ " is greater");

}

}

class cmdmax

{

public static void main(String args[])

{

fun f = new fun();

Java Assignment

Bhavin Modi Page 47

try

{

if(args[0].indexOf('.')>=0 &&

args[0].indexOf('.')<=32000)

{

double t=Double.parseDouble(args[0]);

double t1=Double.parseDouble(args[1]);

f.max(t,t1);

}

else

{

int s=Integer.parseInt(args[0]);

int s1=Integer.parseInt(args[1]);

f.max(s,s1);

}

}

catch(NumberFormatException e)

{

char c=args[0].charAt(0);

char c2=args[1].charAt(0);

f.max(c,c2);

Java Assignment

Bhavin Modi Page 48

}

}

}

ASSIGNMENT-15

/**

Write an Application program and create one class

that accepts

an array of ten numbers

create one subclass which has following,

- Display numbers entered

- Sum of the numbers

- Average of numbers

- Maximum of numbers

- Minimum of numbers

- Exit

Create appropriate methods in the subclass to execute

operation

as per our choice.

Java Assignment

Bhavin Modi Page 49

Note: use super keyword.

*/

import java.io.*;

class operation

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

System.out.println();

System.out.println("1. Display numbers entered");

System.out.println("2. Sum of the numbers");

System.out.println("3. Average of numbers");

System.out.println("4. Maximum of numbers");

System.out.println("5. Minimum of numbers");

System.out.println("6. Exit");

System.out.println();

Java Assignment

Bhavin Modi Page 50

System.out.print("Enter Choice: ");

int ch = Integer.parseInt(in.readLine());

fun f = new fun();

System.out.println();

switch(ch)

{

case 1:

f.disp();

break;

case 2:

f.sum();

break;

case 3:

f.avg();

break;

case 4:

f.max();

break;

Java Assignment

Bhavin Modi Page 51

case 5:

f.min();

break;

case 6:

f.exit();

break;

default:

System.out.print("Wrong choice");

}

}

}

class read

{

DataInputStream in = new

DataInputStream(System.in);

int a[] = new int[10];

read()

{

try

Java Assignment

Bhavin Modi Page 52

{

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

{

System.out.print("Enter a[" + (i+1) + "]: ");

a[i] = Integer.parseInt(in.readLine());

}

}

catch(Exception e){}

}

}

class fun extends read

{

fun()

{

super();

}

int s=0;

void disp()

Java Assignment

Bhavin Modi Page 53

{

System.out.print("Array is ");

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

{

System.out.print(a[i] + " ");

}

}

void sum()

{

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

{

s += a[i];

}

System.out.print("Sum of Array is " + s);

}

void avg()

{

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

Java Assignment

Bhavin Modi Page 54

{

s += a[i];

}

System.out.print("Average of Array is " + (s/10));

}

void max()

{

int m = a[0];

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

{

if (m < a[i])

m = a[i];

}

System.out.print("Maximum Value of Array is " +

m);

}

void min()

{

int m = a[9];

Java Assignment

Bhavin Modi Page 55

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

{

if (m > a[i])

m = a[i];

}

System.out.print("Minimum Value of Array is " +

m);

}

void exit()

{

System.out.print("Exit");

}

}

ASSIGNMENT-16

/**

5 candidates contest an election.

The candidates are numbered 1 to 5 and making the

candidate number on

the ballot paper does the voting.

Java Assignment

Bhavin Modi Page 56

Write a program to read the ballots and count the

votes cast candidate

using an array variable count.

In case, a number read is outside the range 1 to 5,

the ballot should be considered as a 'spoilt ballot' and

the program should also count the number of spoilt

ballots.

*/

import java.io.*;

class election

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

int i,n,count=0,spoilt=0;

System.out.print("Enter number of ballots: ");

n = Integer.parseInt(in.readLine());

Java Assignment

Bhavin Modi Page 57

System.out.println();

int a[] = new int[n];

for(i=0;i<n;i++)

{

System.out.print("Enter Vote: ");

a[i] = Integer.parseInt(in.readLine());

if( a[i] == 1 || a[i] == 2 || a[i] == 3 || a[i] == 4

|| a[i] == 5)

count++;

else

{

System.out.println("\t\tOutside the range");

spoilt++;

}

}

System.out.println("bollte paper: " + count);

System.out.println("spoilt parep: " + spoilt);

}

Java Assignment

Bhavin Modi Page 58

}

ASSIENMENT-17

/**

Write an application program to count occurrence of

particular

character in entered String.

*/

import java.io.*;

import java.lang.*;

class count

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

String s = new String();

char ch;

Java Assignment

Bhavin Modi Page 59

int c=0;

System.out.print("Enter String: ");

s = in.readLine();

System.out.print("Enter Character: ");

ch = (char) System.in.read();

for(int i=0;i<s.length();i++)

{

if (ch == s.charAt(i))

c++;

}

System.out.println();

System.out.println("occurrence of character is " +

c);

}

}

ASSIGNMENT-18(1)

import java.io.*;

Java Assignment

Bhavin Modi Page 60

import java.lang.*;

class info

{

int no;

String name;

void getdata(int n,String nm)

{

no = n;

name = nm;

}

void display()

{

System.out.println("Student No: " + no );

System.out.println("Student Name: " + name);

}

}

class stud

{

Java Assignment

Bhavin Modi Page 61

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

int std_no;

String std_name;

info st[] = new info[10];

try

{

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

{

System.out.print("Student No: ");

std_no = Integer.parseInt(in.readLine());

System.out.print("Student Name: ");

std_name = in.readLine();

System.out.println();

Java Assignment

Bhavin Modi Page 62

st[i] = new info();

st[i].getdata(std_no,std_name);

}

}

catch (Exception e){}

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

{

st[i].display();

}

}

}

ASSIGNMENT-18(2)

/**

Write a menu driven an Application program to do

following.

Member variable:

std_no,std_name,std_sub1,std_sub2,std_sub3,total,per

1. New Student Entry

Java Assignment

Bhavin Modi Page 63

2. Calculate student result

3. Display specified student's formatted Mark sheet

4. Delete Student Entry

5. Modify the Student info

6. Exit

Note: use array of objects and read appropriate value

on choice.

*/

import java.io.*;

import java.lang.*;

class info

{

int no,sub1,sub2,sub3;

float total,per;

String name;

void data(int n,String s,int s1,int s2,int s3)

{

no = n;

name = s;

Java Assignment

Bhavin Modi Page 64

sub1 = s1;

sub2 = s2;

sub3 = s3;

System.out.print("New Entry");

}

void result(int n)

{

if(no == n)

{

total = sub1 + sub2 + sub3;

per = total / 3;

System.out.print("Result calculated");

}

else

{

System.out.print("std_no not exist");

}

}

void display(int n)

{

Java Assignment

Bhavin Modi Page 65

if ( no == n)

{

System.out.println("Student No: " + no);

System.out.println("Student Name: " + name);

System.out.println("---------------------------");

System.out.println("Student Subject1: " + sub1);

System.out.println("Student Subject2: " + sub2);

System.out.println("Student Subject3: " + sub3);

System.out.println("---------------------------");

System.out.println("total: " + total);

System.out.println("per: " + per);

}

else

{

System.out.print("std_no not exist");

}

}

void del(int n)

{

if(no == n)

{

Java Assignment

Bhavin Modi Page 66

no = '\0';

System.out.print("Entry Deleted");

}

else

{

System.out.print("std_no not exist");

}

}

}

class student

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

int std_no,std_sub1,std_sub2,std_sub3;

String std_name;

Java Assignment

Bhavin Modi Page 67

String s = new String();

int c = 0,i,j;

info st[] = new info[10];

do

{

System.out.println();

System.out.println("1. New Student Entry");

System.out.println("2. Calculate student result");

System.out.println("3. Display specified student's

formatted Mark sheet");

System.out.println("4. Delete Student Entry");

System.out.println("5. Modify the Student info");

System.out.println("6. Exit");

System.out.println();

System.out.print("Enter Choice: ");

int ch = Integer.parseInt(in.readLine());

System.out.println();

Java Assignment

Bhavin Modi Page 68

switch(ch)

{

case 1:

{

System.out.print("Student No: ");

std_no = Integer.parseInt(in.readLine());

System.out.print("Student Name: ");

std_name = in.readLine();

System.out.print("Student Subject1: ");

std_sub1 = Integer.parseInt(in.readLine());

System.out.print("Student Subject2: ");

std_sub2 = Integer.parseInt(in.readLine());

System.out.print("Student Subject3: ");

std_sub3 = Integer.parseInt(in.readLine());

st[c] = new info();

Java Assignment

Bhavin Modi Page 69

st[c].data(std_no,std_name,std_sub1,std_sub2,std_sub3)

;

c++;

break;

}

case 2:

{

System.out.print("Student No: ");

std_no = Integer.parseInt(in.readLine());

System.out.println();

for(i=0;i<c;i++)

{

st[i].result(std_no);

}

break;

}

case 3:

{

System.out.print("Student No: ");

std_no = Integer.parseInt(in.readLine());

Java Assignment

Bhavin Modi Page 70

System.out.println();

for(i=0;i<c;i++)

{

st[i].display(std_no);

}

break;

}

case 4:

{

System.out.print("Student No: ");

std_no = Integer.parseInt(in.readLine());

System.out.println();

for(i=0;i<c;i++)

{

st[i].del(std_no);

}

break;

}

default:

System.out.print("Wrong choice");

}

Java Assignment

Bhavin Modi Page 71

System.out.println("\n");

System.out.print("\t\tDo want to Continue

(y/n)?: ");

s = in.readLine();

}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));

}

}

/*

class info

{

int no;

String name;

void getdata(int n,String nm)

{

no = n;

name = nm;

Java Assignment

Bhavin Modi Page 72

}

void display()

{

System.out.println("Student No: " + no );

System.out.println("Student Name: " + name);

}

}

class stud

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

int std_no;

String std_name;

info st[] = new info[10];

try

Java Assignment

Bhavin Modi Page 73

{

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

{

System.out.print("Student No: ");

std_no = Integer.parseInt(in.readLine());

System.out.print("Student Name: ");

std_name = in.readLine();

System.out.println();

st[i] = new info();

st[i].getdata(std_no,std_name);

}

}

catch (Exception e){}

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

{

st[i].display();

}

}

Java Assignment

Bhavin Modi Page 74

}

*/

ASSIGNMENT-19

/**

Define a class baby with the following attributes.

1. xname

2. date of birth

3. date on which beg injection has to be given (60 days

from date of birth)

4. xdate on which polio drops is to be given (45 days

from date of birth)

Write a constructor to construct the babyobject y.

The constructor must find out bcg and polio drops

dates from the date of birth.

In the main program define a baby and display its

details.

*/

import java.io.*;

class babyobject

{

Java Assignment

Bhavin Modi Page 75

int d,m,y,beg,polio;

String xname = new String();

babyobject()

{

DataInputStream in = new

DataInputStream(System.in);

try

{

System.out.print("Enter Name: ");

xname = in.readLine();

System.out.println("Date of birth");

System.out.print("Enter Day: ");

d = Integer.parseInt(in.readLine());

System.out.print("Enter Month: ");

m = Integer.parseInt(in.readLine());

System.out.print("Enter Year: ");

Java Assignment

Bhavin Modi Page 76

y = Integer.parseInt(in.readLine());

}

catch (Exception e){}

beg = d + 60 ;

polio = d + 45;

if ( (beg >= 30) || (polio >= 30))

{

m = m + 1;

if(m == 2 && y%4 == 0)

{

beg = beg - 28;

polio = polio - 28;

m = m + 1;

}

else

{

beg = beg - 30;

polio = polio - 30;

}

Java Assignment

Bhavin Modi Page 77

if ( (beg >= 30) || (polio >= 30))

{

beg = (beg - d) - 30;

polio = polio - d;

}

}

if (m >= 12)

{

m = 1;

y = y + 1;

}

System.out.println();

System.out.println("date on " + beg + "/" + m + "/" +

y + " beg injection has to be given");

System.out.println("date on " + polio + "/"+ m + "/"

+ y + " polio drops is to be given");

}

}

class baby

Java Assignment

Bhavin Modi Page 78

{

public static void main(String args[])

{

babyobject y = new babyobject();

}

}

ASSIGNMENT-20

/**

Write an Application program to generate Employee

Payslip.

Create following classes.

Employee - emp_no,emp_name,basic ,Des

InvalidBasicException - Class for user defined

Exception

(if basic is negative or non numeric).

Payslip - do all additions (da, hra) And

deductions (loan_ins , gpr)

If basic<=5000 Then

hra=5% of basic and da=3% of basic.

Java Assignment

Bhavin Modi Page 79

If basic>=15000 Then

hra=7.5% of basic and da=5% of basic

Else

hra=10% of basic and da=8% of basic.

Based on that calculate netpay for each employee in

Payslip class and

generate the formatted pay slip.

Note: emp_no should be generated automatically

with prefix EMP,

use array of objects.

*/

import java.io.*;

class employee

{

public static void main(String args[]) throws

IOException

{

Java Assignment

Bhavin Modi Page 80

DataInputStream in = new

DataInputStream(System.in);

int emp_no[] = new int[100];

String emp_name[] = new String[100];

float basic[] = new float[100];

String Des[] = new String[100];

int c = 0,i,j;

String s = new String();

Payslip p = new Payslip();

do

{

System.out.println();

System.out.print("emp_no: ");

emp_no[c] = Integer.parseInt(in.readLine());

System.out.print("emp_name: ");

Java Assignment

Bhavin Modi Page 81

emp_name[c] = in.readLine();

System.out.print("basic: ");

basic[c] = Integer.parseInt(in.readLine());

System.out.print("Des: ");

Des[c] = in.readLine();

c++;

System.out.println();

System.out.print("\t\tDo want to Continue (y/n)?:

");

s = in.readLine();

}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));

do

{

System.out.println();

System.out.print("emp_no: ");

Java Assignment

Bhavin Modi Page 82

j = Integer.parseInt(in.readLine());

for(i=0;i<c;i++)

{

if( emp_no[i] == j)

{

System.out.println("emp_no: EMP" +

emp_no[i]);

System.out.println("emp_name: " +

emp_name[i]);

System.out.println(" basic: " + basic[i]);

p.netpay(basic[i]);

System.out.println("Des: " + Des[i]);

System.out.println();

}

else

{

System.out.println("emp_no not found");

}

}

Java Assignment

Bhavin Modi Page 83

System.out.println();

System.out.print("\t\tDo want to Continue (y/n)?:

");

s = in.readLine();

}while ((s.charAt(0) == 'Y') || (s.charAt(0) == 'y'));

}

}

class Payslip

{

void netpay(float x)

{

float basic,da,hra,net;

basic = x;

if (basic <= 5000)

{

hra = (basic * 5)/100;

da = (basic * 3)/100;

}

Java Assignment

Bhavin Modi Page 84

else if (basic >= 15000)

{

hra = (basic * 75)/1000;

da = (basic * 5)/100;

}

else

{

hra = (basic * 10)/100;

da= (basic * 8)/100;

}

System.out.println(" hra: " + hra);

System.out.println(" da: " + da);

System.out.println("netpay: " + (basic + hra +

da));

//return (basic + hra + da);

}

}

ASSIGNMENT-21

/*

Java Assignment

Bhavin Modi Page 85

Write a simple java program that will catch exception

using multiple catch.

*/

import java.lang.*;

class mulce

{

public static void main(String args[])

{

int a[] = {5,10};

int b = 5;

try

{

int x = a[2] / b - a[1];

}

// catch (Exception e)

// {

// System.out.print("IOException");

// }

catch (ArithmeticException e)

{

Java Assignment

Bhavin Modi Page 86

System.out.print("Division by Zero");

}

catch (ArrayIndexOutOfBoundsException e)

{

System.out.print("Array index error");

}

int y = a[1] /a[0];

System.out.print("y = " + y);

}

}

ASSIGNMENT-22

/**

Write a program that displays an invoice of several

items.

It should contain the item name, quantity, price, and

total cost on each

line for the quantity and item cost.

Java Assignment

Bhavin Modi Page 87

Use two classes. The first class contains the item data

and methods

to get an set the item name, quantity and price.

The other class creates objects for the items and

uses the objects

to call the set and get methods.

*/

import java.io.*;

import java.lang.*;

class item

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

String name = new String();

int quantity = 0;

float price = 0.0f;

invoice i = new invoice();

Java Assignment

Bhavin Modi Page 88

System.out.print("Item Name : ");

name = in.readLine();

System.out.print("Quantity : ");

quantity = Integer.parseInt(in.readLine());

System.out.print("Price : ");

price = Float.valueOf(in.readLine()).floatValue();

i.get(name,quantity,price);

i.set();

}

}

class invoice

{

String n = new String();

int q;

float p,tc;

void get(String x, int y, float z)

{

n = x;

q = y;

p = z;

Java Assignment

Bhavin Modi Page 89

}

void set()

{

System.out.println();

System.out.println("Item Name : " + n);

System.out.println("Quantity : " + q);

System.out.println("Price : " + p);

System.out.println("Total Cost: " + (q*p));

}

}

ASSIGNMENT-23

/**

Design a class to represent a library account.

Include the following members :

Data members:

name, number, total no of books

Java Assignment

Bhavin Modi Page 90

Methods:

To assign an initial values.

To display total no of issue books and returned

books.

To display the name and date of issue of the book.

*/

import java.io.*;

class book

{

String s = new String();

String no = new String();

int tno;

void get(String x, String y, int z)

{

no = x;

s = y;

tno = z;

}

void display()

{

Java Assignment

Bhavin Modi Page 91

System.out.println();

System.out.println("Book Number: " + no);

System.out.println("Book Name : " + s);

// System.out.println("Book date : " +

}

}

class library

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

String bname = new String();

String bno = new String();

int btno;

book b = new book();

System.out.print("Book Number: ");

bno = in.readLine();

System.out.print("Book Name : ");

bname = in.readLine();

Java Assignment

Bhavin Modi Page 92

System.out.print("Total no of Books: ");

btno = Integer.parseInt(in.readLine());

b.get(bno,bname,btno);

b.display();

}

}

ASSIGNMENT-24

/**

Write a program that defines circle class with two

constructors.

The first form accepts a double value that represents

radius of circle.

This constructor assumes that circle is centered at the

origin.

The second form accepts three double values.

The first two arguments define co-ordinates of center.

And third value defines radius.

*/

Java Assignment

Bhavin Modi Page 93

class circle

{

public static void main(String args[]) //throws

IOException

{

second s = new second(1.5,2.5,3.5);

s.disp();

}

}

class first

{

double x,y;

first(double a, double b)

{

x = a;

y = b;

}

void disp()

{

System.out.print(x + y);

Java Assignment

Bhavin Modi Page 94

}

}

class second extends first

{

double z;

second(double a,double b,double c)

{

super(a,b);

z = c;

}

void disp()

{

System.out.print(z);

}

}

ASSIGNMENT-25

/**

Java Assignment

Bhavin Modi Page 95

Create a class called Time having data member hh,

mm,ss.

Write an Application program that will add the data of

two objects and

store the answer in third object and display .

*/

import java.io.*;

class Time

{

public static void main(String args[]) throws

IOException

{

DataInputStream in = new

DataInputStream(System.in);

int hh[] = new int[2];

int mm[] = new int[2];

int ss[] = new int[2];

int i,h=0,m=0,s=0;

for(i=0;i<2;i++)

{

System.out.print("Enter Hour: ");

Java Assignment

Bhavin Modi Page 96

hh[i] = Integer.parseInt(in.readLine());

System.out.print("Enter Minite: ");

mm[i] = Integer.parseInt(in.readLine());

System.out.print("Enter Second: ");

ss[i] = Integer.parseInt(in.readLine());

}

for(i=0;i<2;i++)

{

h += hh[i];

m += mm[i];

s += ss[i];

}

if (m>60)

h++;

if (s>60)

m++;

Java Assignment

Bhavin Modi Page 97

if (h>24)

{

h=0;

m=0;

s=0;

}

System.out.print("Date: " + h + ":" + m + ":" + s);

}

}

ASSIGNMENT-26

/**

Write an application program to show the use of

interface.

*/

interface newshape

{

void draw();

}

Java Assignment

Bhavin Modi Page 98

class circle implements newshape

{

public void draw()

{

int radius = 12;

System.out.print("Radius of circle is " + radius);

}

}

class shape

{

public static void main(String args[])

{

newshape nc = new circle();

nc.draw();

}

}

ASSIGNMENT-27

/**

Write an application program that read the contents of

one file and copy

Java Assignment

Bhavin Modi Page 99

it to the other file. (DataInputStream and

DataOutputStream class).

*/

import java.io.*;

class DIOS

{

public static void main(String args[]) throws

IOException

{

File primitive = new File ("prim.dat");

FileOutputStream fos = new FileOutputStream

(primitive);

DataOutputStream dos = new DataOutputStream

(fos);

dos.writeInt(1999);

dos.writeDouble(375.85);

dos.writeBoolean(false);

dos.writeChar('X');

Java Assignment

Bhavin Modi Page 100

dos.close();

fos.close();

FileInputStream fis = new FileInputStream

(primitive);

DataInputStream dis = new DataInputStream (fis);

System.out.println(dis.readInt());

System.out.println(dis.readDouble());

System.out.println(dis.readBoolean());

System.out.println(dis.readChar());

dis.close();

fis.close();

}

}

ASSIGNMENT-29

import java.applet.*;

import java.awt.*;

Java Assignment

Bhavin Modi Page 101

/*

<applet code="display" width=200 height=200>

</applet>

*/

public class display extends Applet

{

public void paint(Graphics g)

{

g.drawString("Hello World",10,100);

}

}

ASSIGNMENT-30

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/*

<applet code="Mouse" width=400 height=400>

</applet>

Java Assignment

Bhavin Modi Page 102

*/

public class Mouse extends Applet implements

MouseMotionListener

{

String msg=" ";

int mouseX = 0 , mouseY = 0;

public void init()

{

setBackground(Color.yellow);

setForeground(Color.blue);

addMouseMotionListener(this);

}

public void mouseDragged(MouseEvent me)

{

mouseX = me.getX();

mouseY = me.getY();

msg = "JAVA";

Java Assignment

Bhavin Modi Page 103

showStatus("Dragging mouse at " + mouseX + ", "

+ mouseY);

repaint();

}

public void mouseMoved(MouseEvent me)

{

showStatus("Moving mouse at " + me.getX() + ", " +

me.getY());

}

public void paint (Graphics g)

{

g.drawString(msg,mouseX,mouseY);

}

}

ASSIGNMENT-31

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

Java Assignment

Bhavin Modi Page 104

/* <applet code="operator" width=400 height=200>

</applet> */

public class operator extends Applet implements

ActionListener

{

String msg=" ",m;

int a,b,ans;

TextField n1,n2,r;

Choice op;

Button ok;

public void init()

{

setBackground(Color.yellow);

setForeground(Color.red);

Label fno = new Label(" First No:");

Label sno = new Label("Second No:");

Java Assignment

Bhavin Modi Page 105

Label res = new Label("Result:");

n1 = new TextField(10);

n2 = new TextField(10);

r = new TextField(10);

op = new Choice();

add(fno);

add(n1);

add(sno);

add(n2);

op.add("+");

op.add("-");

op.add("*");

op.add("/");

add(op);

add(res);

add(r);

Java Assignment

Bhavin Modi Page 106

ok = new Button("OK");

add(ok);

ok.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

String str = ae.getActionCommand();

a = Integer.parseInt(n1.getText());

b = Integer.parseInt(n2.getText());

m = op.getSelectedItem();

if (str.equals("OK"))

if (m == "+")

ans = a + b;

else if (m == "-")

ans = a - b;

Java Assignment

Bhavin Modi Page 107

else if (m == "*")

ans = a * b;

else if (m == "/")

ans = a / b;

repaint();

}

public void paint (Graphics g)

{

r.setText(ans+"");

}

}

ASSIGNMENT-32(1)

import java.util.*;

class calendardemo

{

public static void main(String args[])

{

Java Assignment

Bhavin Modi Page 108

String months[] =

{"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",

"Oct","Nov","Dec"};

Calendar calendar = Calendar.getInstance();

Date date =new Date();

System.out.print("Date:" + date);

System.out.println("\nTime:" +

calendar.get(Calendar.HOUR));

// System.out.print("Date:" +

months[calendar.get(Calendar.MONTH)] + " " +

calendar.get(Calendar.DATE) + " " +

calendar.get(Calendar.YEAR));

// System.out.println("Time:" +

calendar.get(Calendar.HOUR) + ":" +

calendar.get(Calendar.MINUTE) + ":" +

calendar.get(Calendar.SECOND));

}

}

Java Assignment

Bhavin Modi Page 109

ASSIGNMENT-32(2)

/* Write An Applet Program That Accept Date And

Display It. */

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/* <applet code="date" width = 200 height = 200>

</applet> */

public class date extends Applet implements

ActionListener

{

Label l1;

TextField t1;

public void init()

{

l1 = new Label("Enter Date:");

Java Assignment

Bhavin Modi Page 110

t1 = new TextField(10);

add(l1);

add(t1);

t1.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

repaint();

}

public void paint(Graphics g)

{

g.drawString("Date:" + t1.getText(),10,100);

}

}

ASSIGNMENT-33

import java.applet.*;

import java.awt.*;

/*

<applet code="good" width=200 height=200>

Java Assignment

Bhavin Modi Page 111

</applet>

*/

public class good extends Applet

{

public void paint(Graphics g)

{

g.drawString('Time()',10,100);

}

}

ASSIGNMENT-34

/* Write An Applet Program To Create Registration

Form For

Your Mail Account And Register It. */

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

Java Assignment

Bhavin Modi Page 112

/* <applet code="email" width=400 height=400>

</applet> */

public class email extends Applet implements

ActionListener

{

Label l1,l2;

TextField t1,t2;

public void init()

{

l1 = new Label("Email ID:");

t1 = new TextField(10);

l2 = new Label("Password:");

t2 = new TextField(10);

t2.setEchoChar('*');

add(l1);

add(t1);

add(l2);

add(t2);

Java Assignment

Bhavin Modi Page 113

t2.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

repaint();

}

public void paint(Graphics g)

{

g.drawString("Email ID: " + t1.getText(),10,100);

g.drawString("Password: " + t2.getText(),10,150);

}

}

ASSIGNMENT-37

/* Write an Applet program to convert money for the

local currency

(selected Country) into U.S. Dollar.*/

import java.awt.*;

import java.awt.event.*;

Java Assignment

Bhavin Modi Page 114

import java.applet.*;

/* <applet code="money" width=400 height=400>

</applet> */

public class money extends Applet implements

ActionListener, ItemListener

{

Label l1,l2;

TextField t1,t2;

Choice cty;

String msg= "";

int m1,m2;

public void init()

{

l1 = new Label("Enter money to convert:");

t1 = new TextField(10);

l2 = new Label("Currency in Dollar:");

t2 = new TextField(10);

Java Assignment

Bhavin Modi Page 115

cty = new Choice();

cty.add("U.S.");

cty.add("U.K.");

cty.add("Ausralia");

add(l1);

add(t1);

add(cty);

add(l2);

add(t2);

cty.addItemListener(this);

}

public void actionPerformed(ActionEvent ae)

{

repaint();

}

public void itemStateChanged(ItemEvent ie)

{

Java Assignment

Bhavin Modi Page 116

msg = cty.getSelectedItem();

m1 = Integer.parseInt(t1.getText());

if (msg == "U.S.")

m2 = m1 * 37;

else if(msg == "U.K.")

m2 = m1 * 36;

else if(msg == "Ausralia")

m2 = m1 * 35;

else

m2 = 0;

repaint();

}

public void paint(Graphics g)

{

// g.drawString("money : " + t1.getText(),10,100);

// g.drawString("dollar: " + t2.getText(),10,150);

t2.setText(String.valueOf(m2));

}

}

ASSIGNMENT-38

Java Assignment

Bhavin Modi Page 117

/*

Develop an applet that performs the following

operations:

a.when button is pressed, the word "down" is

displays the

current coordinates of the mouse

b.when button is released, the word "up" is

displays the

current coordinates of the mouse

c.when button is clicked, the message is "mouse is

clicked" is shown.

*/

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/*

<applet code="buttonevent" width=200 height=200>

</applet>

*/

Java Assignment

Bhavin Modi Page 118

public class buttonevent extends Applet implements

MouseListener

{

String msg=" ";

Button click;

public void init()

{

// setBackground(Color.blue);

// setForeground(Color.red);

click = new Button("CLICK");

add(click);

addMouseListener(this);

}

/* public void mouseClicked(MouseEvent me)

{

msg = "mouse clicked";

Java Assignment

Bhavin Modi Page 119

repaint();

}

public void mousePressed(MouseEvent me)

{

msg = "mouse down";

repaint();

}

public void mouseReleased(MouseEvent me)

{

msg = "mouse up";

repaint();

}

*/

public void paint (Graphics g)

{

g.drawString(msg,10,100);

}

}

Java Assignment

Bhavin Modi Page 120

ASSIGNMENT-39

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="bgcolor" width=200 height=200>

</applet> */

public class bgcolor extends Applet implements

ItemListener

{

String msg=" ";

Choice c;

public void init()

{

c = new Choice();

c.add("Red");

c.add("Green");

Java Assignment

Bhavin Modi Page 121

c.add("Blue");

add(c);

c.addItemListener(this);

}

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

public void paint (Graphics g)

{

msg = c.getSelectedItem();

g.drawString(msg,10,100);

if (msg == "Red")

setBackground(Color.red);

if (msg == "Green")

setBackground(Color.green);

Java Assignment

Bhavin Modi Page 122

if (msg == "Blue")

setBackground(Color.blue);

}

}

ASSIGNMENT-40

/* Write An Applet Program To Write User Note On

Canvas. */

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/* <applet code="note" width=400 height=400>

</applet> */

public class note extends Applet implements

KeyListener

{

Java Assignment

Bhavin Modi Page 123

String msg ="";

public void init()

{

addKeyListener(this);

}

public void keyPressed(KeyEvent ke)

{

showStatus("Key Down");

}

public void keyReleased(KeyEvent ke)

{

showStatus("Key Up");

}

public void keyTyped(KeyEvent ke)

{

msg += ke.getKeyChar();

repaint();

}

public void paint(Graphics g)

{

g.drawString(msg,10,100);

Java Assignment

Bhavin Modi Page 124

}

}

ASSIGNMENT-41

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="copy" width=200 height=200>

</applet> */

public class copy extends Applet implements

ItemListener, ActionListener

{

String msg=" ";

Checkbox m,l,p;

TextField ch;

public void init()

{

// setBackground(Color.green);

Java Assignment

Bhavin Modi Page 125

// setForeground(Color.red);

CheckboxGroup cbg = new CheckboxGroup();

m = new Checkbox("Music",cbg,true);

l = new Checkbox("Listening",cbg,false);

p = new Checkbox("Painting",cbg,false);

add(m);

add(l);

add(p);

m.addItemListener(this);

l.addItemListener(this);

p.addItemListener(this);

ch = new TextField(12);

add(ch);

ch.addActionListener(this);

}

Java Assignment

Bhavin Modi Page 126

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

public void actionPerformed(ActionEvent ae)

{

repaint();

}

public void paint (Graphics g)

{

// msg = "" + m.getState();

// g.drawString(msg,10,100);

ch.setText(cbg.getSelectedCheckboxGroup().getLabel());

// g.drawString("Name: " + ch.getText(),10,100);

}

Java Assignment

Bhavin Modi Page 127

}

ASSIGNMENT-42

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="select" width=200 height=200>

</applet> */

public class select extends Applet implements

ItemListener

{

String msg=" ";

Checkbox m,f;

public void init()

{

setBackground(Color.cyan);

setForeground(Color.red);

CheckboxGroup cbg = new CheckboxGroup();

Java Assignment

Bhavin Modi Page 128

m = new Checkbox("Male",cbg,true);

f = new Checkbox("Female",cbg,false);

add(m);

add(f);

m.addItemListener(this);

f.addItemListener(this);

}

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

public void paint (Graphics g)

{

msg = "Male " + m.getState();

g.drawString(msg,10,100);

Java Assignment

Bhavin Modi Page 129

msg = "Female " + f.getState();

g.drawString(msg,10,150);

}

}

ASSIGNMENT-43

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="controls" width=300 height=400>

</applet> */

public class controls extends Applet implements

ActionListener, ItemListener

{

String msg=" ";

TextField no = new TextField(10);

TextField nm = new TextField(10);

TextField ad = new TextField(10);

Java Assignment

Bhavin Modi Page 130

TextField pc = new TextField(10);

TextArea text = new TextArea(msg,10,30);

public void init()

{

// setBackground(Color.yellow);

// setForeground(Color.red);

Label sno = new Label("Sno");

Label snm = new Label("Snm");

Label add = new Label("Address");

Label pin = new Label("PinCode");

Label city = new Label("City");

Label state = new Label("State");

Label gen = new Label("Gender");

Label area = new Label("Area of int.");

Choice c = new Choice();

c.add("Anand");

c.add("Nadiad");

Java Assignment

Bhavin Modi Page 131

c.add("Baroda");

Choice s = new Choice();

s.add("Gujarat");

s.add("Dehli");

s.add("MP");

CheckboxGroup cbg = new CheckboxGroup();

Checkbox m = new Checkbox("Male",cbg,true);

Checkbox f = new Checkbox("Female",cbg,false);

Checkbox com = new Checkbox("Comput");

Checkbox eng = new Checkbox("Enginee");

Button store = new Button("Store");

add(sno);

add(no);

add(snm);

add(nm);

Java Assignment

Bhavin Modi Page 132

add(add);

add(ad);

add(pin);

add(pc);

add(city);

add(c);

add(state);

add(s);

add(gen);

add(m);

add(f);

add(area);

add(com);

add(eng);

add(store);

add(text);

store.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

Java Assignment

Bhavin Modi Page 133

{

String str = ae.getActionCommand();

if (str.equals("Store"))

msg = "You pressed";

repaint();

}

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

public void paint (Graphics g)

{

text.setText("Sno: " + no.getText() + "\nSnm: " +

nm.getText() + "\nAddress: " + ad.getText() +

"\nPincode: " + pc.getText());

g.drawString(msg,300,100);

}

}

Java Assignment

Bhavin Modi Page 134

ASSIGNMENT-44

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

/* <applet code="controls" width=300 height=400>

</applet> */

public class controls extends Applet implements

ActionListener, ItemListener

{

String msg=" ";

TextField no = new TextField(10);

TextField nm = new TextField(10);

TextField ad = new TextField(10);

TextField pc = new TextField(10);

TextArea text = new TextArea(msg,10,30);

Java Assignment

Bhavin Modi Page 135

public void init()

{

// setBackground(Color.yellow);

// setForeground(Color.red);

Label sno = new Label("Sno");

Label snm = new Label("Snm");

Label add = new Label("Address");

Label pin = new Label("PinCode");

Label city = new Label("City");

Label state = new Label("State");

Label gen = new Label("Gender");

Label area = new Label("Area of int.");

Choice c = new Choice();

c.add("Anand");

c.add("Nadiad");

c.add("Baroda");

Choice s = new Choice();

s.add("Gujarat");

Java Assignment

Bhavin Modi Page 136

s.add("Dehli");

s.add("MP");

CheckboxGroup cbg = new CheckboxGroup();

Checkbox m = new Checkbox("Male",cbg,true);

Checkbox f = new Checkbox("Female",cbg,false);

Checkbox com = new Checkbox("Comput");

Checkbox eng = new Checkbox("Enginee");

Button store = new Button("Store");

add(sno);

add(no);

add(snm);

add(nm);

add(add);

add(ad);

add(pin);

add(pc);

Java Assignment

Bhavin Modi Page 137

add(city);

add(c);

add(state);

add(s);

add(gen);

add(m);

add(f);

add(area);

add(com);

add(eng);

add(store);

add(text);

store.addActionListener(this);

}

public void actionPerformed(ActionEvent ae)

{

String str = ae.getActionCommand();

if (str.equals("Store"))

msg = "You pressed";

Java Assignment

Bhavin Modi Page 138

repaint();

}

public void itemStateChanged(ItemEvent ie)

{

repaint();

}

public void paint (Graphics g)

{

text.setText("Sno: " + no.getText() + "\nSnm: " +

nm.getText() + "\nAddress: " + ad.getText() +

"\nPincode: " + pc.getText());

g.drawString(msg,300,100);

}

}

top related