# ST.

FRANCIS COLLEGE FOR WOMEN BEGUMPET, HYDERABAD-500016 CERTIFICATE DEPARTMENT OF COMPUTER SCIENCE

This is to certify that this is a bonafide project work done by Meghana B Rao of class B.Sc III D bearing Roll no.095426 in the semester V of the academic year 2011-2012.

Internal Examiner Examiner

External

4

S.NO. 1 PARTICULARS OF THE RECORD WORK DONE Program to illustrate Type conversions PAGE NO. SIGN

2

Program to demonstrate the use of methods of the Scanner class

3

Program illustrating use of input dialog boxes

4

Program using the nested if control structure

5

Program to illustrate use of switch case

6

Program illustrating looping with sentinel value

7

Program to print the multiplication table of the first ten natural number using nested loops.

8

Check whether a number is perfect or not using for loop

9

Print the primes from 1 to 100 using while loop

4

10

Use do while loop to find sum of squares of first hundred natural numbers

4

Sum of elements in a list b. Check if a number is Amstrong number or not c. Smallest and largest values in a list c. Transpose of a matrix d.11 a. Variable length argument lists Searching in an array using Linear Search 14 15 16 17 Sorting a list of values using Bubble Sort 18 Program illustrating use of Array class Double Dimensional Arrays: a. To generate the first n terms of a Geometric progression. To generate the first n terms of the Fibonacci series Single dimensional arrays: a. Copying arrays d. Addition. Passing arrays to methods e. also the first n terms of an Arithmetic progression Program to illustrate Passing of arguments to methods by value 12 13 Program illustrating the use of Math class Recursiona. Accept and display elements in a matrix form b. Display the sum of elements of each row and each column in a matrix 4 19 . To calculate the factorial of a number b. Check if a number is palindrome or not b. Subtraction and Multiplication of matrices using methods c. Returning arrays from methods f.

SIGN 25 a. Random. b. Overloading: Method overloading Constructor overloading 26 Program illustrating Single inheritance 27 Program to illustrate multiple inheritance 28 Method overriding using super keyword 29 Program using the JFrame class 4 .S. GUI components classes from the Java library Program illustrating the use of Abstract classes Program using static variables and static methods Program based on interfaces Passing objects to methods Program demonstrating exception handling Program using array of objects PAGE NO. 20 30 31 21 32 22 33 23 34 24 PARTICULARS OF THE RECORD WORK DONE Program illustrating the use of default Program on polymorphism and parameterised constructors Program using the ArrayList class Program using the Date.NO.

j=(int)k.0 124 // after type casting. Program to illustrate Type Conversions //Program to illustrate Type conversions-widening and narrowing of a type public class Conversions{ public static void main{String args[]){ byte i=124. //widening the data type System. System. //here the value of k is explicitly converted to integer value(narrowing a type) //this leads to loss of data at times //the output is 12. //the output is 124.3 12 k=i.1. // i is still a byte value } } 4 . int j=i+4.out.println( k +”\t”+j). //the output is 124 128 float k=12.println( k +”\t”+i).out.println( i +”\t”+j).out. the value and data type of i has not been altered. //the value of i is implicitly converted to integer value(widening) System.3.

System. System.println(“enter a short value”): short d= input.nextInt().util.out.println(“enter a long integer value”): long x=input.nextShort(). System. System.nextInt(). int a. Write a program to illustrate the use of methods of the Scanner class // Program to demonstrate the use of methods of the Scanner class import java.b.println(“enter two integer values”): a=input.out.out. public class Scannermethods{ public static void main(String args[]){ //creating an object of the Scanner class Scanner input=new Scanner(System.in). //using the nextInt() method to accept integer values b=input. 4 .out. System.println(“enter a float value”): float f=input.nextLong().println(“enter a byte value”): byte c=nextByte().nextFloat().Scanner.out.2.

System.out. System. double p= Double.System. String str=input. prompt=JOptionPane.nextLine().out.showInputDialog("enter the time in years").JOptionPane.println(“the other numeric values entered are :” c+”\t”+d+”\t”+x+”\t”+f+”\t”+y). Program to illustrate use of input dialog boxes //computation of simple interest using input dialog boxes import javax.showInputDialog("enter the principal amount").println(“the integers entered are :” a+”\t”+b).println(“enter a character and a word”): char ch=input.nextDouble().parseDouble(prompt).out.println(“the character. System.out. 4 .out.swing. System.println(“enter a double value”): double y=input.next().out.next(). public class InputDialog{ public static void main(String args[]) { String prompt=JOptionPane. word and sentence entered are :” ch+”\n”+str+”\n”+str1).println(“enter a line of text/sentence”): String str1=input. }} 3. System.

float t= Float.parseFloat(prompt).showInputDialog("enter the rate %"). String out="the Simple interest is" +si + "\n the amount is"+(p+si)+"\n integral parts of the principal and amount are"+(int)si+"and"+(int)(p+si). JOptionPane.parseFloat(prompt). }} 4 .out). float r= Float. prompt=JOptionPane. double si=p*t*r/100.showMessageDialog(null.

} } 4 .

class Daysnum { public static void main(String[] args) { Scanner ip=new Scanner(System.out.Scanner.util. } public static void countDays(int year. switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31. Program using Switch case import java. case 4: case 6: case 9: case 11: numDays = 30. break. countDays(year.in). int year = ip.5.nextInt().int month ){ int numDays = 0. int month=ip. System.nextInt(). case 2: if ( ((year % 4 == 0) && !(year % 100 == 0)) 4 .println(“enter the month and year of your choice example 2 2000 for February 2000”). break.month).

out. default: System. else numDays = 28."). } } 4 . } System. break. break.println("Invalid month.|| (year % 400 == 0) ) numDays = 29.println("Number of Days = " + numDays).out.

util.println(“the sum of the values entered is”+”sum).6.nextInt().in). } } 4 . value=in. Program illustrating looping with Sentinel value //sum of values till the user enter the value 0 import java. public class SentinelValue{ public static void main(String args[]){ int sum=0. while(value!=0){ sum+=value.nextInt().Scanner. Scanner in=new Scanner(System.out. } System. int value=in.

Scanner. public class MultTable{ public static void main(String args[]){ Scanner in=new Scanner(System. System. int n=in.out.out.7.i<=n.println(“/t”). System.j++) System.util. Program to print multiplication tables of numbers from 1 to n using nested loops (first ten multiples).j<=10.in).println(“enter the limit:”).out.println(“Multiplication tables of 1 to” +n +”:\n”). for(int i=1. //Program using nested for loop import java.nextInt(). for(j=1.out.println(i+”\t x \t”+ j+”\t=\t”+(i*j)+”\n”). } } } 4 .i++) { System.

nextInt().p++){ if(n%p==0) sum+=p. perf(n).p<n.in).8.").").out.println(n+"is not a perfect no.util. for(int p=0. } public static void perf(int n) { int sum=0.out. } if (sum==n) System.println("enter the number:"). } } 4 . System. else System. int n=in. Program using for loop to find if a number is perfect or not //check if a number is perfect number or not import java.Scanner.println(n+"is a perfect no.out. public class Perfect { public static void main(String[] args) { Scanner in=new Scanner(System.

int i=1. i++.9. } } if (flag==1) System. while(i<=100) { for ( int j = 2. j++) { if ( i!=j && i % j == 0 ) { flag=1. break. } 4 .in).util. generate().Scanner. } } public static void main(String args[]) { Scanner input=new Scanner(System.println(i+ "\t"). flag=0. j <= i. public class Prime { public static void generate(){ int flag=0. Program to print primes from 1 to 100 using while loop //To generate Prime numbers between 1 and 100 import java.out.

} 4 .

in). Sum of squares of first hundred natural numbers using do.out.+n^2 import java..println(“enter the n value”)... System.Scanner. if(i>n) break. public class SumSquare{ public static int squaresum(int n){ int sum=0.. return sum.. int sumsq=squaresum(n).. } public static void main(String args[]){ Scanner input=new Scanner(System.while loop //program to find 1^2+2^2+. } } 4 . int i=1.. int n=input.nextInt().util.println(“the sum of the 1st “+n+”squares=”+sumsq).10.. System....out. i++. do{ sum+=i*i. }while(i<=n).

} public static void pal(int b) { int d=b.11a. else System. public class Palindrome{ public static void main(String args[]) { Scanner in=new Scanner(System. } if(d1==b) System.out.out.println(b+"is not a palindrome").println(b+"is a palindrome"). int r=0. pal(a). int a=in.Scanner. int d1=0.println("Enter the number").util.in). while(d>0) { r=d%10.out.nextInt(). d=d/10. d1=r+(10*d1). } } 4 . Palindrome import java. System.

Amstrong a=new Amstrong().out.out. Amstrong number //Amstrong number import java. } 4 .in).println("enter the number").println(n + " is Armstrong Number"). System.11b. } } public static int sumcube(int x) { int dig. if(a. public class Amstrong { { public static void main(String args[]) { Scanner get=new Scanner(System. } return(sum). else System.sum = 0. int n=get.out. sum = sum + dig * dig * dig.util.println(n + " is not Armstrong Number").Scanner. while(x >= 1) { dig = x % 10. x = x / 10.sumcube (n)==n) System.nextInt().

println(“enter the common difference (which will also be the common ratio for the GP):”). System. public class Gpap{ int i. for(i=1.Scanner.println(a+(i-1)*d+”\t”).out.r).i<=n. System.println(a*Math.println(“the AP series is:\n”).int n){ System.println(“the GP series is:\n”). ap(a.i++) System.out.pow(r.int d.int r. GP and AP first n terms import java.out. for(i=1.r). int r=sc.i<=n.println(“enter the last term of both the series:”) int n=sc.out. gp(a.i++) System.nextInt().println(“enter the first term of both the series:”) int a=sc. } 4 .util.} 11c.out. } public static void ap(int a.out.int n){ System.i-1)+”\t”).in). public static void gp(int a.nextInt().n.n. } public static void main(String args[]){ Scanner sc=new Scanner(System. System.nextInt().out.

} 4 .

util. public class PassByVal{ public static void main(){ Scanner input= new Scanner(System.b). System.println(“enter the values of a and b”). int a. //passing a and b values to the user defined function } public static void compute(int a.12.out. a=input.in). Pass by value //passing arguments by value import java. b.pow(a.int b){ System.nextInt(). b= input. compute(a.pow(b.println(“(a+b)^2=”+ (Math. } } 4 .2)).2)+2*a*b+Math.Scanner.out.nextInt().

public class MathFns{ public static void main(){ Scanner in=new Scanner(System.out.out.toRadians(deg))). System. int deg=input.exp)).abs(value)).13 Math class //using Math class functions import java.pow(base. float base= input.out.nextInt(). System. if(value<deg) System.out.println(“enter the base and exponent”). System.out.nextInt().sqrt(value)+”\n e raised to the power”+value+”=”+Math. int value= input.println(“the absolute value of”+value+”is”+Math.println(“enter the angle in degrees”).out.nextFloat().nextFloat().out.random()*deg).println(“square root=”+Math.exp(value)).Scanner.println(“a random value between”+value+”and”+deg+”is”+ value+Math.println(“minimum value of”+base+”and”+exp+”is”+Math. System. System. System.println(base+”to the power”+exp+”=”+Math.min(base.util.sin(math.out. float exp= input. else 4 .exp)).println(“the value of sin”+deg+”is”+Math. System.in).println(“enter the value for square root and exponent”).out. System.

System.println(“a random value between”+value+”and”+deg+”is”+ deg+Math. } } 4 .random()*value).out.

int f=fact(n).nextInt().f3. public class Fibo { int f1.Scanner. System. int n=i.util. Factorial of a number using recursion // Factorial of a number using recursion import java. } } 14b. else return(integer*(fact(integer-1))).Scanner.println("enter the number for which factorial is to be found"). // First n terms of Fibonacci series using recursion import java.14a. public static void fib(int integer) 4 .out. } } public static void main(String args[]) { Scanner i=new Scanner(System.util.println("Factorial of" +n +"is"+f).f2.out. System.in). public class Factorial { public static int fact(int integer) { if(integer>0) { if( (integer == 0)||(integer==1)) return 1.

} public static void main(String args[]) { Scanner i=new Scanner(System. f2=f3. f2=1. } } 4 .out. System.{ if(integer>0){ f3=f1+f2. System. fib(integer-3).println("enter the number of terms in the Fibonacci series").println(f3+”\t”).in).out. int n=i.println(“the first n terms of the Fibonacci series are”+f1+”\t”+f2). f1=f2. System.out. f1=0. } else System.println(“\n this is the end of the series”).out.nextInt(). fib(n).

sum. a Sum of elements in a list //Sum of elements in a list import java. class SumMat{ int n.15.out.nextInt().sum()). int n=ip. m1.i++) a[i]=ip.util. int a[]=new int[10].i.println(“how many values”). for( i=0. Scanner ip=new Scanner(System.out. for(i=0. System.i++) sum+=a[i]. void accept(){ System.Scanner.i<n.i<n. } int sum(){ sum=0.accept().println(“the sum of the elements entered is”+ (m1.in). } } 4 .nextInt(). } public class M{ public static void main(String args[]){ SumMat m1=new SumMat().

} System. for(int i=0.out.i<n. 4 .accept(). m1.max=a[0].out. Smallest and largest values in a list //minimum and maximum elements in a list of numbers import java. void accept(){ System.util. int a[]=new int[10]. } } public class M{ public static void main(String args[]){ Minmax m1=new Minmax(). } void findminmax(){ int min=a[0]. n=ip.println(“how many values”).nextInt(). for(int p=1.p<n.Scanner.in).println(“min value=”+min+”max value=”+max).i++) a[i]=ip.15b. Scanner ip=new Scanner(System.p++){ if(a[p]>max) max=a[p].nextInt(). class Minmax{ int n. if(a[p]<min) min=a[p].

out.i++) System. System.i++) a[i]=ip.i<a.nextInt().m1.out.i++) copya[i]=a[i]. Copying arrays import java.util.println(“the source array is”) for(i=0.println(“how many values”). int n=ip. for(i=0.out. } void copyarray(){ int copya[]=new int[10].i<n. } void display() { System.in). } } 15c.i++) System.out. for(int i=0.findminmax().i<n. System.println(“the target array is”) for(i=0.out. void accept(){ int a[]=new int[10]. class ArrayCopy{ int n. Scanner ip=new Scanner(System.println(a[i]+”\t”).i.Scanner.i<n. } 4 .nextInt().length.println(copya[i]+”\t”).

a. a.out. class Passarray{ public static void main(String args[]) { int [] a=new int[10].i<n.println("\nhow many values"). parray(a.i++) System. int i.copyarray().nextInt(). a. Passing arrays to methods import java.out.i++) a[i]=in.int n) { for(int i=0.util.in).out. n=in.n. System. } public static void parray(int [] x.display().nextInt().println("\nthe elements of the array are\n").Scanner. } } 15d. } } 4 . for(i=0.i<n.n).println(x[i] + "\n").accept().} public class M{ public static void main(String args[]){ ArrayCopy a=new ArrayCopy(). Scanner in=new Scanner(System. System.

n=in.n). System.nextInt().out.in).i++.j=x-1.println("\nthe elements of the array A\n").util. parray(b.println(x[i] + "\n"). Scanner in=new Scanner(System. Returning arrays from methods import java.int x) { int [] r=new int[x].n).i<n. parray(a. int i.println("\nthe elements of the array B\n").out.nextInt(). } return r.i<x1.n.i<x.i++) System. } public static void parray(int [] x. }} 4 .i++) a[i]=in.out. System.int x1) { for(int i=0.n). System.println("\nhow many values").Scanner. for(int i=0.j--) { r[j]=list[i]. for(i=0. class Revarray{ public static void main(String args[]) { int [] a=new int[10]. b=reverse(a.out. int [] b=new int[10].15e. } public static int [] reverse(int [] list.

2.4}).n) { if (n.//pass an array arg } public static void printarray(int. for(int i=1. Variable length argument list import java.4.out. return.length.println("no arguments passed").//passing variable-length arg list printarray(new int[] {1.. } int r=n[0].Scanner. } } 4 ..15f.i++) { if (n[i]>r) r=n[i].8.length==0) { System. } System.10). class varargs{ public static void main(String args[]) { printarray(2.println("\nthe max value is "+ r).i<n.util.6.out.

else Sytem. Scanner i=new Scanner(System. } 4 . public class LinSearch{ int n.nextInt().println(search+” is found at”+ (j+1) +”location”).nextInt(). for(int s=0.Scanner.out.j++){ if(a[j]= =search) { flag=1.s++) a[s]=i.j<n.s<n.out.nextInt(). for(int j=1.out.out. int a[]=new int[10]. Linear search import java. break.println(search+” not found”). System. int search=i.println(“how many elements”).in). } } if(flag==1) Sytem.println(“enter the element to be searched”). } void lsearch(){ int flag=0.16.search.util. void accept(){ System. int n=i.

} public class LiSrch{ public static void main(String args[]){ LinSearch l=new LinSearch(). l.accept(). } } 4 . l.lsearch().

util.17. } public static void bubbleSort(int[] intArray) { int n = intArray. int n=in.} public static void display(int []intArray){ for(int j=0.println("Array Before Bubble Sort"). i++){ for(int j=1. public class BubbleSort { int intArray[] = new int[10]. j++) System. j < (n-i).out.length. for(int t=0. Bubble sort import java. Scanner in=new Scanner(System.in). public static void main(String[] args) { System. display(intArray). 4 . bubbleSort(intArray).Scanner.print(intArray[j] + " "). System.out. for(int i=0.println(“enter number of elements”).length.println("Array After Bubble Sort"). display(intArray). i < n.t++) intArray[t]=in.out. j++){ if(intArray[j-1] > intArray[j]){ temp = intArray[j-1].nextInt().t<n.println(“enter the”+n+”elements”).out. int temp = 0.out. System. j < intArray. System.nextInt().

java.println("enter number of elements:").out.util.Scanner.Arrays. for(int i=0.1.util.3). System.6. System. java.//sorts from location 1 to 3-1 display(n). } public static void display(int[] a){ 4 .1. int [] n=new int[10].a2)).Arrays.4. intArray[j] = temp. java.println("\the sorted list is"). Using the Array class import java.util. public class ArrayClass { public static void main(String args[] ) { Scanner sc=new Scanner(System. System.8}.equals(a1.9).4.nextInt().i++) n[i]=sc.sort(n).3.fill(a2.out.out. display(n).//fills 7 in the entire array display(a1).sort(n.intArray[j-1] = intArray[j].Arrays.util.6. java.fill(a1.8}. int [] a2={2.println(java.util. import java.Arrays.in). int [] a1={2.Arrays.util.//fills 9 in the partial array display(a2).i<num. int num=sc.nextInt(). }}}} 18.util.7).Arrays.

}} 4 .length.i<a.i++) System.println(a[i]).for(int i=0.out.

in). Scanner input=new Scanner(System.nextInt(). Matrix m=new Matrix().j++) System.j<c. int i. 4 .println(a[i][j]+”\t”). for(j=0.in).19.int c){ System.println(“enter elements of the matrix”). Accept and display elements in the matrix form import java.i<r.out. } void display(int r. for(i=0. void accept(int r.i++) { for(j=0.j.Scanner.i<r.int c){ for(i=0. class Matrix{ int [][]a=new int[10][10].out.j++) a[i][j]=input.util.j<c.a.println(“\n”).out. } } } public class Call{ public static void main(String args[]){ Scanner inp=new Scanner(System.i++) { System.

col).Scanner.i++) 4 . class MatrixOpns{ int [][]a=new int[5][5]. int [][]c=new int[5][5].i++) { for(j=0.nextInt(). int[][]b=new int[5][5].i<r. } return a.j<co. int[][] accept(int r.int[][]b.k.println(“enter the elements of the matrix”). Addition subtraction multiplication using methods import java.display(row. } } 19b.in).int co){ System. m.out. m.int c){ for(i=0.nextInt(). for(i=0.accept(row.println(“enter the order of the matrix”).int r. col=inp.nextInt().System.j. } int[][] addmat(int [][]a.j++) a[i][j]=in. row=inp. Scanner in=new Scanner(System.util.i<r.col).out. int i.

int r.int r.j<c.j<c.j++) c[i][j]=a[i][j]+b[i][j].int[][]a2. j < col2.println(array[i][j]).i++) { for(j=0.out.j<c.int co){ for(i=0.int[][]b. } } int[][] submat(int [][]a. } } } } void display(int[][]array.{ for(j=0.i<row1.j++) System. k < col1. for(k = 0.i<r. j++) { array3[i][j]=0.i++) { for(j=0. k++){ array3[i][j] += array1[i][k]*array2[k][j]. } } int[][] multmat(int[][]a1.j++) c[i][j]=a[i][j]-b[i][j]. 4 .i++) for(j = 0.i<r.int c){ for(i=0.int col2){ for(i=0.int row1.int col1.

display(array3. MatrixOpns mo=new MatrixOpns(). MatrixOpns mo1=new MatrixOpns().in).} } } public class Call{ Scanner input=new Scanner(System.addmat(array1. int [][]array1=new int[5][5].println(“enter the order of the matrix2”). if((row1==row2)&&(col1=col2)) { array3[][]=mo.col1). int [][]array2=new int[5][5].col2). int [][]array3=new int[5][5].out. int row2=input.nextInt().col1).accept(row2.nextInt().println(“enter the order of the matrix1”).out. int col1=input. int col2=input. public static void main(String args[]){ System.array2.println(“the second matrix:\n”).row1.nextInt().out.out. } else 4 . System. System. array2[][]=mo1. System.accept(row1.col1).nextInt(). array1[][]=mo.println(“the first matrix:\n).row1. int row1=input.

row1. class MatrixOpns{ int [][]a=new int[5][5]. Scanner in=new Scanner(System.Scanner.int co){ System. } } 19c.out.println(“subtraction not possible\n”).row1.System. int i.in).util. display(array3.out. void accept(int r.col1.out.array2.row1. } else System.j<co.out.col1).multmat(array1.i<r.col1). Transpose of a matrix import java.i++) { for(j=0.println(“enter the elements of the matrix”). if((row1==row2)&&(col1=col2)) { array3[][]=mo. } else System.col2). if(col1==row2){ array3[][]=mo. display(array3.println(“multiplication not possible\n”). for(i=0.println(“addition not possible\n”).col2).array2.row1.submat(array1.j++) 4 .j.

} } } public class Call{ Scanner input=new Scanner(System.i++) { for(j=0. 4 .out.i++) { System.nextInt().in). int col=input.nextInt().i<r.out.int co){ int [][]trans=new int[5][5].j++) System.println(a[i][j])+”\t”).j++) trans[i[j]=a[j][i].j<c.j<c. public static void main(String args[]){ System. int row=input.a[i][j]=in.out.println(“\n”).nextInt().int c){ for(i=0.println(“enter the order of the matrix”). for(j=0. MatrixOpns mo=new MatrixOpns().i<r. } } void transpose(int r. for(i=0. } } void display(int r.

col). } } 19d. mo.display(row.println(“\n Sum of”+(i+1)+”th row elements and +(j+1)+”th column are respectively: \t ”).col).out.util. 4 .j.int co){ System. int sumcol=0.println(“enter the elements of the matrix”). mo. void accept(int r.transpose(row.in). } } void SumRowCol(int r.out. for(i=0.j++) a[i][j]=in. for(i=0.j<co.col).mo.int co){ int sumrow=0.i++) { for(j=0. class MatrixOpns{ int [][]a=new int[5][5].Scanner. int i. Scanner in=new Scanner(System.i<r. System.accept(row.i++) { sumrow=sumcol=0.i<r. Sum of each row and column elements import java.nextInt().

j++) System.println(“enter the order of the matrix”).i<r. } } void display(int r.col). public static void main(String args[]){ System.println(a[i][j])+”\t”). } 4 . for(j=0.for(j=0. } System. mo.nextInt().println(“\n”). int col=input.nextInt().j++){ sumrow+=a[i][j].col).out. mo.out.out.col). int row=input.SumRowCol(row. MatrixOpns mo=new MatrixOpns(). sumcol+=a[j][i].display(row. } } } public class Call{ Scanner input=new Scanner(System.i++) { System.int c){ for(i=0.j<c.in).out.println(sumrow+”\t”+sumcol).j<c. mo.accept(row.

} 4 .

Check() { n=0.n)<(c2. int min. public class Check{ int n.n):(c2.n))? (c1.n).out. Default and parameterised constructors with ternary operators //ternary operator with constructors example import java. } } 4 .Scanner.println(“minimum value is”+min). Check c2=new Check(5).util. min=((c1.} Check(int p) { n=p. } public static void main(String args[]) { Check c1-new Check().20. System.

println(“Time elapsed since Jan 1.util.150). Scanner in=new Scanner(System.out.21. JTextField tf=new JTextField(s). public class LibraryClasses{ public static void main(String args[]){ java.1970:”).JFrame.util. Random. JButton jok=new JButton(“OK!”).println(“enter the limit:”). JLabel jlb=new JLabel(“Time elapsed since 1 Jan. import javax. f1.println(“ten random values between 0 and”+n+”are\n”).toString().i++) System. f1. System. Jframe f1=new JFrame(). GUI classes import java.println(s). System.add(jlb).Random(). f1.add(tf).Scanner.Random r= new java. String s=d.nextInt().nextInt(n)+”\t”).println(r.setSize(200.util.Date().setTitle(“Current Time”). java. System.out. for(int i=0.1970:”+d.util.i<10.out. System. Date.getTime()+”milliseconds”).util.out. f1.Date d=new java.in). 4 . int n=in.add(jok).swing.out. f1.

nobj++. Circle(){ radius=0. } } public class Call{ public static void main(String args[]){ Circle c1=new Circle().println(“area=”+(radius*radius*pi). 4 .out.f1. f1.0. } static int getobj(){ return nobj. } Circle(double r){ radius=r. System. } void displayarea(){ System. final static double pi=3.14159.setDefaultCloseOperation(JFrame. }} 22.getobj()).EXIT_ON_CLOSE).println(“the number of objects created=”+c1.out. Static methods and variables class Circle{ double radius. nobj++.setVisible(true). static int nobj=0.

disp(o). } } 4 . } } public class Passobj{ public static void main(String args[]) { obj o=new obj(3).} public obj(int i){r=i. System.} public void set(int i){r=(i>0) ? i : 0.} public int get(){return r.displayarea() ).getobj() +”\n the area is”+c2.println("\nthe value of r"+o1. } public static int getnobj() { return nobj.calculate()).Scanner.Circle c2=new Circle(5).println(“the number of objects created=”+c2. Passing objects to methods import java.nobj++. }} 23. private static int nobj=0. class obj { private int r=1.get() +"calculated value=" + o1.} public int calculate(){return r*r*2.util. public obj(){nobj++.out.out. } public static void disp(obj o1) { System.

} void calculate(){ System. int a. } } public class Call{ public static void main(String args[]){ Sample []s=new Sample[5]. b=input.24.b. s[j]. for(int j=0.nextInt().println(“Sum of the values=”+(a+b)).Scanner.calculate(). Array of objects import java.j++) { s[j]=new Sample().util. s[j].j<5. } } } 4 .in). void accept(){ a=input.out. class Sample{ Scanner input=new Scanner(System.accept().nextInt().

import java.util.Scanner; class Overload{ public static void main(String args[]) { int a,b; float c,d; Scanner in=new Scanner(System.in); System.out.println("\naccept 2 integer values"); a=in.nextInt(); b=in.nextInt(); System.out.println("\naccept 2 float values"); c=in.nextFloat(); d=in.nextFloat(); System.out.println("\ncalling function int\n"); pcall(a,b); System.out.println("\nalling function float\n"); pcall(c,d); } public static void pcall(int x, int y) { System.out.print(x + "\t" + y); } public static void pcall(float x,float y) { System.out.print(x + "\t" + y); } }

4

public class COverload{ int i; COverload(){ i=0; System.out.println(“the default constructor has been invoked\n the value of i is”+i); } COverload(int p){ i=p; System.out.println(“the parameterised constructor taking an integer parameter has been invoked\n the value of i is”+i); } COverload(String str){ System.out.println(“the parameterised constructor taking a String parameter has been invoked\n the message is”+str); } } public class Call{ public static void main(String args[]){ COverload c1=new COverload(); COverload c2=new COverload(5); COverload c3=new COverload(“Java Programming”); } }

4

26 Single inheritance
import java.util.Scanner; class A{ int x,y; Scanner in=new Scanner(System.in); void gdata() { System.out.println("accept 2 values"); x=in.nextInt(); y=in.nextInt(); } void display(){ System.out.println("\nthe sum of the values is"+ (x+y)); }} class B extends A{ int c,d; void display1(){ c=10; d=20; System.out.println("\nthe product of”+x+”and”+y+ is"+ (x*y)); System.out.println("\tand the difference is"+ (x-y)); System.out.println("\nthe product of”+c+”and”+d+ is"+ (c*d)); System.out.println("\tand the difference is"+ (c-d)); }} class Inherit{ public static void main(String args[]){ B b1=new B(); b1.gdata();

4

} void Show(){ System. b1.display().out.println("the values for object of class B:"+get(5.6)). return(0). Show().out. c.}} 27.}} public class Call{ public static void main(String args[]){ C c = new C().println("the values for object of class C:"+get(15.16)). Show().b1. }} class B extends A{ void Showb(){ System.out. int q){ x=p. int get(int p.display().println(x+”\t”+y). 4 . Multiple inheritance class A { int x. }} class C extends A{ void display(){ System.display1(). y=q. int y.

c.Show().Showb().get(7. c. b. }} 4 .B b=new B().8).

println("i and j: " + i + " " + j). j.println("k: " + k). Method overriding using super keyword // Method overriding class A { int i. 3). } // display i and j void show() { System. }} class override1 { public static void main(String args[]) { B subOb = new B(1.out. } void show() { super. b). j = b. k = c.out. int b) { i = a.28. 4 .show(). }} class B extends A { int k. // this calls A's show() System. int b. int c) { super(a. 2. B(int a. A(int a.

// this calls show() in B }} 4 .subOb.show().

JComboBox jcb=new JComboBox(new String[] {"MECs". 4 .setVisible(true). JCheckBox jck=new JCheckBox("I am above 18 years of age").swing. JTextField tf1=new JTextField("Type name here"). }} class CustomFrame extends JFrame { public CustomFrame() { JButton jok=new JButton("OK").300).setVisible(true).29. JFrame f2=new CustomFrame(). f2. JPanel p=new JPanel(). JLabel label2=new JLabel("Select your combination")."BZC"}).setLocation(200.*."MPCs". f2.setTitle("Duplicate record of student information").setLocation(200. JLabel label3=new JLabel("Gender"). JRadioButton jrd1=new JRadioButton("Female")."MSCs". f1. Program using the JFrame class import javax. public class kframe{ public static void main(String args[]) { JFrame f1=new CustomFrame(). f1. JRadioButton jrd=new JRadioButton("Male").100). f2. f1.setTitle("Official Student Information"). JLabel label1=new JLabel("Enter your name").

} } class Square extends Shape { void Area() { int s=5.println("Area of square:"+(s*s)).println("Area of right angled triangle:"+(0.out. System.out.5. } } class Circle extends Shape { void Area() { float r=4. System.5*base*ht)).30.out. float ht=4.out.Polymorphism class Shape { void Area() { System. System.3.5. } } 4 .println("No dimensions hence area cannot be calculated").PI*r*r)). } } class Traingle extends Shape{ void Area() { float base=4.println("Area of circle="+(Math.

obj4. obj2.Area(). Shape obj2 = new Circle).Area(). } } 4 .class poly { public static void main(String[] args) { Shape obj1 = new Shape().Area(). obj3. obj1. Shape obj4 = new Triangle(). Shape obj3 = new Square().Area().

System.add("Electronics").out. subjects.remove(1). subjects. subjects.indexOf("Chemistry")). subjects.Array list class import java.out. System.get(i) + " " ).add(2. }} 4 .println(subjects. subjects.out.println("list size? " + subjects.remove("Chemistry").add("Mathematics").out.println("is Physics in the list " + subjects.contains("Physics")).i>=0.println(subjects.31. subjects. subjects.add("Chemistry").add("Computer Science").add("Botany").ArrayList. System.toString()). System."Physics").println("is the list subjects empty" + subjects.size()).size()-1.util.println("the location of Chemistry in the list " + subjects. public class alist { public static void main(String args[]) { ArrayList<String> subjects=new ArrayList<String>(). System. for(int i=subjects.i--) System.out.out. subjects.isEmpty()).

32. b. abstract void call(). } } 4 .Abstract classes abstract class A { abstract void callme().callmetoo().callme().println("B's implementation of callme."). } } class AbstractDemo { public static void main(String args[]) { B b = new B().out. } void call(){ System. } } class B extends A { void callme() { System.println(“B’s implementation of call”).out. // non-abstract method void callmetoo() { System.println("This is a concrete method.").out. b.

Interfaces public class Sample { public static void main(String[] args) { shape circleshape=new circle(). circleshape.33. 4 . } } String baseclass="shape". public void Draw().println("Drawing Circle here").out. } } interface shape { public } class circle implements shape { public void Draw() { System.Draw().

in)). b = in.out.out. System. }catch(ArrayIndexOutOfBoundsException e){ System. } System.println("This is not a numeric value.out.34. public class exceptionHandle{ public static void main(String[] args) throws Exception{ try{ int a.").out. } catch(NumberFormatException ex){ System. a = in.nextInt(). } try{ int a[] = new int[2]. Scanner in = new Scanner(System.nextInt(). Exception handling import java.Scanner.println("Out of the block").b.util.println("Access element three :" + a[3]).println("Exception thrown :" + e). } 4 .