0% found this document useful (0 votes)
307 views

Java Assigment

The document contains 9 Java programming assignments. The first assignment asks the student to print their name and address. The second asks them to write a program to multiply two matrices. The third asks them to write a program to calculate the area and perimeter of a rectangle. The remaining assignments involve writing programs to perform various tasks like checking data types, working with classes and inheritance, sorting arrays, and more. The document provides sample code and explanations for each assignment.

Uploaded by

liamkho
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
307 views

Java Assigment

The document contains 9 Java programming assignments. The first assignment asks the student to print their name and address. The second asks them to write a program to multiply two matrices. The third asks them to write a program to calculate the area and perimeter of a rectangle. The remaining assignments involve writing programs to perform various tasks like checking data types, working with classes and inheritance, sorting arrays, and more. The document provides sample code and explanations for each assignment.

Uploaded by

liamkho
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 139

Java Assignment

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.
Bhavin Modi
Page 1
Java Assignment
import java.io.*;
class arr2mul { public static void main(String args[]) { int i=0,j=0; DataInputS
tream in = new DataInputStream(System.in);
try { int n[][] = new int[3][3]; int m[][] = new int[3][3]; int mul[][] = new in
t[3][3];
for(i=0;i<3;i++) { for(j=0;j<3;j++) { System.out.print("Enter n[" + i + "][" + j
+ "]: ");
Bhavin Modi
Page 2
Java Assignment
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]; } }
Bhavin Modi
Page 3
Java Assignment
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
/*
Bhavin Modi
Page 4
Java Assignment
Write an Application program to find area and perimeter of a rectangle. Note: de
fine static methods. Area Return l*b; Perimeter Return 2*(l+b); */
import java.io.*; import java.lang.*; class fun { static float area(float x,floa
t y) { return (x * y); }
static float perimeter(float x,float y) { return (2 * (x + y)); } }
Bhavin Modi
Page 5
Java Assignment
class rect { public static void main(String args[]) { DataInputStream in = new D
ataInputStream(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) {
Bhavin Modi
Page 6
Java Assignment
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 m
anufactured with the chosen wood.
Bhavin Modi
Page 7
Java Assignment
Floppy price is 15 Rs., CD price is 20 Rs and Pen drive price is 1250 Rs. The cl
ass name is Hardware. */
class Hardware { public static void main(String args[]) { char ch; System.out.pr
intln("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()) {
Bhavin Modi
Page 8
Java Assignment
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': Sy
stem.out.print("Pen Drive price is 1250 Rs."); break; default: System.out.print(
"Wrong choice"); }
}
catch (Exception e) {
Bhavin Modi
Page 9
Java Assignment
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 Pal
indrome 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. */
Bhavin Modi
Page 10
Java Assignment
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); abstr
act 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.Chec
k for positive or negative number ");
Bhavin Modi
Page 11
Java Assignment
System.out.println("2.Check for odd or even number"); System.out.println("3.Chec
k for primary number"); System.out.println("4.Check for Palindrome number"); Sys
tem.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"); S
ystem.out.println(); }
void pn(int x) { n = x; if ( n > 0) System.out.print("Number is Positive"); else
System.out.print("Number is Negative"); }
Bhavin Modi
Page 12
Java Assignment
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)
Bhavin Modi
Page 13
Java Assignment
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"); }
Bhavin Modi
Page 14
Java Assignment
void fibo(int x) { n = x; int n1=1,n2=0,sum=0; System.out.print("Fibonacci serie
s 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"); }
Bhavin Modi
Page 15
Java Assignment
}
class Menu1 { public static void main(String args[]) throws IOException { DataIn
putStream 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.parse
Int(in.readLine());
try {
Bhavin Modi
Page 16
Java Assignment
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:
Bhavin Modi
Page 17
Java Assignment
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')); } } ASSIGM
ENT-6
Bhavin Modi
Page 18
Java Assignment
/** Create an abstract class Auto with fields for the car maker and price. Inclu
de get and set methods for these fields; the setPrice() method is an abstract. C
reate two subclasses for individual automobile makers and include appropriate se
tPrice() methods in each subclass. Finally, write a program that uses the Auto c
lass 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()
Bhavin Modi
Page 19
Java Assignment
{ 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); } }
Bhavin Modi
Page 20
Java Assignment
class car { public static void main(String args[]) throws Exception { DataInputS
tream 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
Bhavin Modi
Page 21
Java Assignment
/** 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() m
ethod that overrides the parent method. Write a program that instantiate a Squar
e 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;
Bhavin Modi
Page 22
Java Assignment
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); d
epth=z; } int sfa() { return (height * width * depth); }
Bhavin Modi
Page 23
Java Assignment
}
class surfacearea { public static void main(String args[]) { int h=0,w=0,d=0; Da
taInputStream 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){}
Bhavin Modi
Page 24
Java Assignment
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[])
Bhavin Modi
Page 25
Java Assignment
{ int invalid=0,valid=0,n;
for(int i=0;i<args.length;i++) { try { n = Integer.parseInt(args[i]); } catch(Ex
ception e) { invalid++; continue; } valid++; } System.out.println("Valid Number:
" + valid); System.out.println("Invalid Number: " + invalid); } } ASSIGNMENT-9
Bhavin Modi
Page 26
Java Assignment
/** 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 mai
n(String args[]) { int i=0,j=0,swap=0; DataInputStream in = new DataInputStream(
System.in);
try { int n[] = new int[10];
Bhavin Modi
Page 27
Java Assignment
for(i=0;i<10;i++) { System.out.print("Enter n[" + i + "]: "); n[i] = Integer.par
seInt(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 ");
Bhavin Modi
Page 28
Java Assignment
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)
Bhavin Modi
Page 29
Java Assignment
*/
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]); }
Bhavin Modi
Page 30
Java Assignment
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++) {
Bhavin Modi
Page 31
Java Assignment
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 vi
ce-versa. Exit. */ import java.io.*; import java.lang.*;
class fun
Bhavin Modi
Page 32
Java Assignment
{ String s,s1 = new String(); int i,j; char temp; void rev(String s) { System.ou
t.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++) {
Bhavin Modi
Page 33
Java Assignment
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) {
Bhavin Modi
Page 34
Java Assignment
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 {
Bhavin Modi
Page 35
Java Assignment
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());
Bhavin Modi
Page 36
Java Assignment
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:
Bhavin Modi
Page 37
Java Assignment
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 str
ing.
Bhavin Modi
Page 38
Java Assignment
*/ 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()
Bhavin Modi
Page 39
Java Assignment
{ 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 {
Bhavin Modi
Page 40
Java Assignment
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. conve
rt 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())
Bhavin Modi
Page 41
Java Assignment
{ case 'a': f.len(); break; case 'b': f.rep(); break; case 'c': f.uc(); break; c
ase 'd': f.ext(); break; case 'e': f.exit(); break; default: System.out.print("W
rong choice"); } }
Bhavin Modi
Page 42
Java Assignment
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;
Bhavin Modi
Page 43
Java Assignment
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"); } }
Bhavin Modi
Page 44
Java Assignment
} 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 overloa
ding. */ 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) {
Bhavin Modi
Page 45
Java Assignment
if(a>b) System.out.println(a+ " is greater"); else System.out.println(b+ " is gr
eater"); }
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();
Bhavin Modi
Page 46
Java Assignment
try { if(args[0].indexOf('.')>=0 && args[0].indexOf('.')<=32000) { double t=Doub
le.parseDouble(args[0]); double t1=Double.parseDouble(args[1]); f.max(t,t1); } e
lse { 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=arg
s[1].charAt(0); f.max(c,c2);
Bhavin Modi
Page 47
Java Assignment
}
} } ASSIGNMENT-15
/** Write an Application program and create one class that accepts an array of t
en numbers create one subclass which has following, - Display numbers entered -
Sum of the numbers - Average of numbers - Maximum of numbers - Minimum of number
s - Exit Create appropriate methods in the subclass to execute operation as per
our choice.
Bhavin Modi
Page 48
Java Assignment
Note: use super keyword. */
import java.io.*; class operation { public static void main(String args[]) throw
s IOException { DataInputStream in = new DataInputStream(System.in);
System.out.println(); System.out.println("1. Display numbers entered"); System.o
ut.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();
Bhavin Modi
Page 49
Java Assignment
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(); b
reak; case 4: f.max(); break;
Bhavin Modi
Page 50
Java Assignment
case 5: f.min(); break; case 6: f.exit(); break; default: System.out.print("Wron
g choice"); } } }
class read { DataInputStream in = new DataInputStream(System.in);
int a[] = new int[10]; read() { try
Bhavin Modi
Page 51
Java Assignment
{ for(int i=0;i<10;i++) { System.out.print("Enter a[" + (i+1) + "]: "); a[i] = I
nteger.parseInt(in.readLine()); } } catch(Exception e){} }
}
class fun extends read { fun() { super(); } int s=0;
void disp()
Bhavin Modi
Page 52
Java Assignment
{ 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 Arra
y is " + s); }
void avg() { for (int i=0;i<10;i++)
Bhavin Modi
Page 53
Java Assignment
{ 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]; } Sy
stem.out.print("Maximum Value of Array is " + m); }
void min() { int m = a[9];
Bhavin Modi
Page 54
Java Assignment
for (int i=0;i<10;i++) { if (m > a[i]) m = a[i]; } System.out.print("Minimum Val
ue 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 mak
ing the candidate number on the ballot paper does the voting.
Bhavin Modi
Page 55
Java Assignment
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 ba
llot 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 = Int
eger.parseInt(in.readLine());
Bhavin Modi
Page 56
Java Assignment
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); }
Bhavin Modi
Page 57
Java Assignment
} 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 { DataIn
putStream in = new DataInputStream(System.in); String s = new String(); char ch;
Bhavin Modi
Page 58
Java Assignment
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.*;
Bhavin Modi
Page 59
Java Assignment
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("S
tudent Name: " + name); } } class stud {
Bhavin Modi
Page 60
Java Assignment
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
();
Bhavin Modi
Page 61
Java Assignment
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
Bhavin Modi
Page 62
Java Assignment
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 ob
jects and read appropriate value on choice. */ import java.io.*; import java.lan
g.*;
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;
Bhavin Modi
Page 63
Java Assignment
sub1 = s1; sub2 = s2; sub3 = s3; System.out.print("New Entry"); } void result(in
t n) { if(no == n) { total = sub1 + sub2 + sub3; per = total / 3;
System.out.print("Result calculated"); } else { System.out.print("std_no not exi
st"); } } void display(int n) {
Bhavin Modi
Page 64
Java Assignment
if ( no == n) { System.out.println("Student No: " + no); System.out.println("Stu
dent 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) {
Bhavin Modi
Page 65
Java Assignment
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 { Data
InputStream in = new DataInputStream(System.in);
int std_no,std_sub1,std_sub2,std_sub3; String std_name;
Bhavin Modi
Page 66
Java Assignment
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.ou
t.println("2. Calculate student result"); System.out.println("3. Display specifi
ed student's formatted Mark sheet"); System.out.println("4. Delete Student Entry
"); System.out.println("5. Modify the Student info"); System.out.println("6. Exi
t"); System.out.println();
System.out.print("Enter Choice: "); int ch = Integer.parseInt(in.readLine());
System.out.println();
Bhavin Modi
Page 67
Java Assignment
switch(ch) { case 1: { System.out.print("Student No: "); std_no = Integer.parseI
nt(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();
Bhavin Modi
Page 68
Java Assignment
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()); Sys
tem.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());
Bhavin Modi
Page 69
Java Assignment
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"); }
Bhavin Modi
Page 70
Java Assignment
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;
Bhavin Modi
Page 71
Java Assignment
}
void display() { System.out.println("Student No: " + no ); System.out.println("S
tudent 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
Bhavin Modi
Page 72
Java Assignment
{ for(int i=0;i<2;i++) { System.out.print("Student No: "); std_no = Integer.pars
eInt(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(); } }
Bhavin Modi
Page 73
Java Assignment
} */ 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 c
onstructor 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 {
Bhavin Modi
Page 74
Java Assignment
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 = Intege
r.parseInt(in.readLine());
System.out.print("Enter Month: "); m = Integer.parseInt(in.readLine());
System.out.print("Enter Year: ");
Bhavin Modi
Page 75
Java Assignment
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 = b
eg - 28; polio = polio - 28; m = m + 1; } else { beg = beg - 30; polio = polio -
30; }
Bhavin Modi
Page 76
Java Assignment
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
Bhavin Modi
Page 77
Java Assignment
{ 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.
Bhavin Modi
Page 78
Java Assignment
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 t
he formatted pay slip.
Note: emp_no should be generated automatically with prefix EMP, use array of obj
ects. */
import java.io.*;
class employee { public static void main(String args[]) throws IOException {
Bhavin Modi
Page 79
Java Assignment
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: ");
Bhavin Modi
Page 80
Java Assignment
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: ");
Bhavin Modi
Page 81
Java Assignment
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(" bas
ic: " + basic[i]); p.netpay(basic[i]); System.out.println("Des: " + Des[i]); Sys
tem.out.println(); } else { System.out.println("emp_no not found"); } }
Bhavin Modi
Page 82
Java Assignment
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; }
Bhavin Modi
Page 83
Java Assignment
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)); //return (basic + hra + da); } } ASSIGNMENT-21
da: " + da);
System.out.println("netpay: " + (basic + hra +
/*
Bhavin Modi
Page 84
Java Assignment
Write a simple java program that will catch exception using multiple catch. */ i
mport 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 (ArithmeticException e) { c
atch (Exception e) { System.out.print("IOException");
Bhavin Modi
Page 85
Java Assignment
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 an
d item cost.
Bhavin Modi
Page 86
Java Assignment
Use two classes. The first class contains the item data and methods to get an se
t the item name, quantity and price. The other class creates objects for the ite
ms and uses the objects to call the set and get methods. */
import java.io.*; import java.lang.*; class item { public static void main(Strin
g args[]) throws IOException { DataInputStream in = new DataInputStream(System.i
n); String name = new String(); int quantity = 0; float price = 0.0f; invoice i
= new invoice();
Bhavin Modi
Page 87
Java Assignment
System.out.print("Item Name : "); name = in.readLine(); System.out.print("Quanti
ty : "); 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;
Bhavin Modi
Page 88
Java Assignment
} void set() { System.out.println();
System.out.println("Item Name : " + n); System.out.println("Quantity : " + q); S
ystem.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
Bhavin Modi
Page 89
Java Assignment
Methods: To assign an initial values. To display total no of issue books and ret
urned books. To display the name and date of issue of the book. */ import java.i
o.*; 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() {
Bhavin Modi
Page 90
Java Assignment
System.out.println(); System.out.println("Book Number: " + no); System.out.print
ln("Book Name : " + s); // } } class library { public static void main(String ar
gs[]) throws IOException { DataInputStream in = new DataInputStream(System.in);
String bname = new String(); String bno = new String(); int btno; book b = new b
ook(); System.out.print("Book Number: "); bno = in.readLine(); System.out.print(
"Book Name : "); bname = in.readLine(); System.out.println("Book date : " +
Bhavin Modi
Page 91
Java Assignment
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 f
orm accepts a double value that represents radius of circle. This constructor as
sumes that circle is centered at the origin. The second form accepts three doubl
e values. The first two arguments define co-ordinates of center. And third value
defines radius. */
Bhavin Modi
Page 92
Java Assignment
class circle { public static void main(String args[]) //throws IOException { sec
ond 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);
Bhavin Modi
Page 93
Java Assignment
} }
class second extends first { double z; second(double a,double b,double c) { supe
r(a,b); z = c; }
void disp() { System.out.print(z); } } ASSIGNMENT-25
/**
Bhavin Modi
Page 94
Java Assignment
Create a class called Time having data member hh, mm,ss. Write an Application pr
ogram 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: ");
Bhavin Modi
Page 95
Java Assignment
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++;
Bhavin Modi
Page 96
Java Assignment
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 news
hape { void draw(); }
Bhavin Modi
Page 97
Java Assignment
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
Bhavin Modi
Page 98
Java Assignment
it to the other file. (DataInputStream and DataOutputStream class). */
import java.io.*; class DIOS { public static void main(String args[]) throws IOE
xception { 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.writeC
har('X');
Bhavin Modi
Page 99
Java Assignment
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.*;
Bhavin Modi
Page 100
Java Assignment
/* <applet code="display" width=200 height=200> </applet> */
public class display extends Applet { public void paint(Graphics g) { g.drawStri
ng("Hello World",10,100); } } ASSIGNMENT-30
import java.applet.*; import java.awt.*; import java.awt.event.*;
/* <applet code="Mouse" width=400 height=400> </applet>
Bhavin Modi
Page 101
Java Assignment
*/
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";
Bhavin Modi
Page 102
Java Assignment
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.*;
Bhavin Modi
Page 103
Java Assignment
/* <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:");
Bhavin Modi
Page 104
Java Assignment
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);
Bhavin Modi
Page 105
Java Assignment
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.g
etSelectedItem();
if (str.equals("OK")) if (m == "+") ans = a + b; else if (m == "-") ans = a - b;
Bhavin Modi
Page 106
Java Assignment
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[]) {
Bhavin Modi
Page 107
Java Assignment
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(Ca
lendar.HOUR));
// System.out.print("Date:" + months[calendar.get(Calendar.MONTH)] + " " + calen
dar.get(Calendar.DATE) + " " + calendar.get(Calendar.YEAR));
// System.out.println("Time:" + calendar.get(Calendar.HOUR) + ":" + calendar.get
(Calendar.MINUTE) + ":" + calendar.get(Calendar.SECOND)); } }
Bhavin Modi
Page 108
Java Assignment
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:");
Bhavin Modi
Page 109
Java Assignment
t1 = new TextField(10); add(l1); add(t1); t1.addActionListener(this); } public v
oid 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>
Bhavin Modi
Page 110
Java Assignment
</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.*;
Bhavin Modi
Page 111
Java Assignment
/* <applet code="email" width=400 height=400> </applet> */
public class email extends Applet implements ActionListener { Label l1,l2; TextF
ield t1,t2;
public void init() { l1 = new Label("Email ID:"); t1 = new TextField(10); l2 = n
ew Label("Password:"); t2 = new TextField(10); t2.setEchoChar('*');
add(l1); add(t1); add(l2); add(t2);
Bhavin Modi
Page 112
Java Assignment
t2.addActionListener(this); } public void actionPerformed(ActionEvent ae) { repa
int(); } 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 Cou
ntry) into U.S. Dollar.*/
import java.awt.*; import java.awt.event.*;
Bhavin Modi
Page 113
Java Assignment
import java.applet.*;
/* <applet code="money" width=400 height=400> </applet> */
public class money extends Applet implements ActionListener, ItemListener { Labe
l l1,l2; TextField t1,t2; Choice cty; String msg= ""; int m1,m2;
public void init() { l1 = new Label("Enter money to convert:"); t1 = new TextFie
ld(10); l2 = new Label("Currency in Dollar:"); t2 = new TextField(10);
Bhavin Modi
Page 114
Java Assignment
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) { repai
nt(); } public void itemStateChanged(ItemEvent ie) {
Bhavin Modi
Page 115
Java Assignment
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(ms
g == "Ausralia") m2 = m1 * 35; else m2 = 0; repaint(); } public void paint(Graph
ics g) { // g.drawString("money : " + t1.getText(),10,100); // g.drawString("dol
lar: " + t2.getText(),10,150); t2.setText(String.valueOf(m2)); } } ASSIGNMENT-38
Bhavin Modi
Page 116
Java Assignment
/* Develop an applet that performs the following operations: a.when button is pr
essed, the word "down" is displays the current coordinates of the mouse b.when b
utton is released, the word "up" is displays the current coordinates of the mous
e c.when button is clicked, the message is "mouse is clicked" is shown. */ impor
t java.applet.*; import java.awt.*; import java.awt.event.*;
/* <applet code="buttonevent" width=200 height=200> </applet> */
Bhavin Modi
Page 117
Java Assignment
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";
Bhavin Modi
Page 118
Java Assignment
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); } }
Bhavin Modi
Page 119
Java Assignment
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=" "; Ch
oice c;
public void init() { c = new Choice();
c.add("Red"); c.add("Green");
Bhavin Modi
Page 120
Java Assignment
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(Co
lor.green);
Bhavin Modi
Page 121
Java Assignment
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 {
Bhavin Modi
Page 122
Java Assignment
String msg =""; public void init() { addKeyListener(this); } public void keyPres
sed(KeyEvent ke) { showStatus("Key Down"); } public void keyReleased(KeyEvent ke
) { showStatus("Key Up"); } public void keyTyped(KeyEvent ke) { msg += ke.getKey
Char(); repaint(); } public void paint(Graphics g) { g.drawString(msg,10,100);
Bhavin Modi
Page 123
Java Assignment
} } 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 { Strin
g msg=" "; Checkbox m,l,p; TextField ch;
public void init() { // setBackground(Color.green);
Bhavin Modi
Page 124
Java Assignment
// 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); }
Bhavin Modi
Page 125
Java Assignment
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); }
Bhavin Modi
Page 126
Java Assignment
} ASSIGNMENT-42
import java.applet.*; import java.awt.*; import java.awt.event.*; /* <applet cod
e="select" width=200 height=200> </applet> */
public class select extends Applet implements ItemListener { String msg=" "; Che
ckbox m,f;
public void init() { setBackground(Color.cyan); setForeground(Color.red);
CheckboxGroup cbg = new CheckboxGroup();
Bhavin Modi
Page 127
Java Assignment
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);
Bhavin Modi
Page 128
Java Assignment
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 { S
tring msg=" ";
TextField no = new TextField(10); TextField nm = new TextField(10); TextField ad
= new TextField(10);
Bhavin Modi
Page 129
Java Assignment
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 La
bel("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");
Bhavin Modi
Page 130
Java Assignment
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,tr
ue); 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);
Bhavin Modi
Page 131
Java Assignment
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)
Bhavin Modi
Page 132
Java Assignment
{ String str = ae.getActionCommand(); if (str.equals("Store")) msg = "You presse
d"; 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); } }
Bhavin Modi
Page 133
Java Assignment
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 { S
tring 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);
Bhavin Modi
Page 134
Java Assignment
public void init() { // setBackground(Color.yellow); // setForeground(Color.red)
; Label sno = new Label("Sno"); Label snm = new Label("Snm"); Label add = new La
bel("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");
Bhavin Modi
Page 135
Java Assignment
s.add("Dehli"); s.add("MP");
CheckboxGroup cbg = new CheckboxGroup(); Checkbox m = new Checkbox("Male",cbg,tr
ue); 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);
Bhavin Modi
Page 136
Java Assignment
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";
Bhavin Modi
Page 137
Java Assignment
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); } }
Bhavin Modi
Page 138

You might also like