Upload
sainath89
View
221
Download
0
Embed Size (px)
Citation preview
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 1/68
4
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 External
Examiner
Head of Department
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 2/68
4
TABLE OF CONTENTS
S.NO. PARTICULARS OF THE RECORD WORK DONE PAGE NO. SIGN
1 Program to illustrate Type conversions
2Program to demonstrate the use of
methods of the Scanner class
3Program illustrating use of input dialog
boxes
4Program using the nested if control
structure
5 Program to illustrate use of switch case
6Program illustrating looping with
sentinel value
7Program to print the multiplication
table of the first ten natural numberusing nested loops.
8Check whether a number is perfect or
not using for loop
9Print the primes from 1 to 100 using
while loop
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 3/68
4
10Use do while loop to find sum of squares of first hundred natural
numbers
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 4/68
4
11
a. Check if a number is palindromeor not
b. Check if a number is Amstrongnumber or not
c. To generate the first n terms of aGeometric progression, also thefirst n terms of an Arithmeticprogression
12Program to illustrate Passing of arguments to methods by value
13Program illustrating the use of Math
class
14
Recursion-a. To calculate the factorial of anumber
b. To generate the first n terms of the Fibonacci series
15
Single dimensional arrays:a. Sum of elements in a listb. Smallest and largest values in a
listc. Copying arraysd. Passing arrays to methods
e. Returning arrays from methodsf. Variable length argument lists
16Searching in an array using Linear
Search
17Sorting a list of values using Bubble
Sort
18 Program illustrating use of Array class
19
Double Dimensional Arrays:a. Accept and display elements in a
matrix formb. Addition, Subtraction and
Multiplication of matrices usingmethods
c. Transpose of a matrixd. Display the sum of elements of
each row and each column in amatrix
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 5/68
4
20Program illustrating the use of default
and parameterised constructors
21Program using the Date, Random, GUI
components classes from the Java
library
22Program using static variables and
static methods
23 Passing objects to methods
24 Program using array of objects
25Overloading:
a. Method overloadingb. Constructor overloading
26Program illustrating Single
inheritance
27Program to illustrate multiple
inheritance
28 Method overriding using super keyword
29 Program using the JFrame class
S.NO. PARTICULARS OF THE RECORD WORK DONE PAGE NO. SIGN
30 Program on polymorphism
31 Program using the ArrayList class
32 Program illustrating the use of Abstractclasses
33 Program based on interfaces
34 Program demonstrating exceptionhandling
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 6/68
4
1. 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;
int j=i+4; //the value of i is implicitly converted to
integer value(widening)
System.out.println( i +”\t”+j);
//the output is 124 128
float k=12.3;
j=(int)k;
System.out.println( k +”\t”+j);
//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.3 12
k=i; //widening the data type
System.out.println( k +”\t”+i);
//the output is 124.0 124
// after type casting, the value and data type of i has notbeen altered.
// i is still a byte value
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 7/68
4
2. Write a program to illustrate the use of methods
of the Scanner class
// Program to demonstrate the use of methods of theScanner class
import java.util.Scanner;
public class Scannermethods{
public static void main(String args[]){
//creating an object of the Scanner class
Scanner input=new Scanner(System.in);
int a,b;
System.out.println(“enter two integer values”):
a=input.nextInt(); //using the nextInt() method to accept
integer values
b=input.nextInt();
System.out.println(“enter a byte value”):
byte c=nextByte();
System.out.println(“enter a short value”):
short d= input.nextShort();
System.out.println(“enter a long integer value”):
long x=input.nextLong();
System.out.println(“enter a float value”):
float f=input.nextFloat();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 8/68
4
System.out.println(“enter a double value”):
double y=input.nextDouble();
System.out.println(“enter a character and a word”):
char ch=input.next();
String str=input.next();
System.out.println(“enter a line of text/sentence”):
String str1=input.nextLine();
System.out.println(“the integers entered are :”
a+”\t”+b);
System.out.println(“the other numeric values entered
are :” c+”\t”+d+”\t”+x+”\t”+f+”\t”+y);
System.out.println(“the character, word and sentence
entered are :” ch+”\n”+str+”\n”+str1);
}}
3. Program to illustrate use of input dialog boxes
//computation of simple interest using input dialog boxes
import javax.swing.JOptionPane;
public class InputDialog{
public static void main(String args[]) {
String prompt=JOptionPane.showInputDialog("enter the
principal amount");
double p= Double.parseDouble(prompt);
prompt=JOptionPane.showInputDialog("enter the time in
years");
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 9/68
4
float t= Float.parseFloat(prompt);
prompt=JOptionPane.showInputDialog("enter the rate
%");
float r= Float.parseFloat(prompt);
double si=p*t*r/100;
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.showMessageDialog(null,out);
}}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 10/68
4
4. Program to determine grades of a student using
nested if control structure
//grades based on marks
import java.util.Scanner;
public class Grades{
public static void main(String args[]){
Scanner get=new Scanner(System.in);
char grade;
System.out.println(“enter the marks”);
int marks=get.nextInt();
if(marks>0)
{
if(marks>=75)
grade=’A’;
else if((marks>=60) &&(marks<75))
grade=’B’;
else if((marks>=40) &&(marks<60))
grade=’C’;
else if(marks>=27)
grade=’D’;
else
grade=’F’;
System.out.println(“Student’s marks:”+marks+”\n
Grade:”+grade);
}
else
System.out.println(“Invalid marks”);
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 12/68
4
5. Program using Switch case
import java.util.Scanner;
class Daysnum {
public static void main(String[] args) {
Scanner ip=new Scanner(System.in);
System.out.println(“enter the month and year of your
choice example 2 2000 for February 2000”);
int month=ip.nextInt();
int year = ip.nextInt();
countDays(year,month);
}
public static void countDays(int year,int month ){
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
numDays = 30;
break;
case 2:
if ( ((year % 4 == 0) && !(year % 100 == 0))
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 13/68
4
|| (year % 400 == 0) )
numDays = 29;
else
numDays = 28;
break;
default:
System.out.println("Invalid month.");
break;
}
System.out.println("Number of Days = " + numDays);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 14/68
4
6. Program illustrating looping with Sentinel value
//sum of values till the user enter the value 0
import java.util.Scanner;
public class SentinelValue{
public static void main(String args[]){
int sum=0;
Scanner in=new Scanner(System.in);
int value=in.nextInt();
while(value!=0){
sum+=value;
value=in.nextInt();
}
System.out.println(“the sum of the values entered is”+”sum);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 15/68
4
7. Program to print multiplication tables of numbers
from 1 to n using nested loops (first ten multiples).
//Program using nested for loop
import java.util.Scanner;
public class MultTable{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println(“enter the limit:”);
int n=in.nextInt();
System.out.println(“Multiplication tables
of 1 to” +n +”:\n”);
for(int i=1;i<=n;i++)
{
System.out.println(“/t”);
for(j=1;j<=10;j++)
System.out.println(i+”\t x \t”+
j+”\t=\t”+(i*j)+”\n”);
}
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 16/68
4
8. Program using for loop to find if a number is
perfect or not
//check if a number is perfect number or not
import java.util.Scanner;
public class Perfect {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.println("enter the number:");
int n=in.nextInt();
perf(n);
}
public static void perf(int n)
{
int sum=0;
for(int p=0;p<n;p++){
if(n%p==0)
sum+=p;
}
if (sum==n)
System.out.println(n+"is a perfect no.");
else
System.out.println(n+"is not a perfect no.");
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 17/68
4
9. Program to print primes from 1 to 100 using while
loop
//To generate Prime numbers between 1 and 100
import java.util.Scanner;
public class Prime {
public static void generate(){
int flag=0;
int i=1;
while(i<=100)
{
for ( int j = 2; j <= i; j++)
{
if ( i!=j && i % j == 0 )
{
flag=1;
break;
}
}
if (flag==1)
System.out.println(i+ "\t");
flag=0;
i++;
}
}
public static void main(String args[]) {
Scanner input=new Scanner(System.in);
generate();
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 19/68
4
10. Sum of squares of first hundred natural numbers
using do...while loop
//program to find 1^2+2^2+..........+n^2
import java.util.Scanner;
public class SumSquare{
public static int squaresum(int n){
int sum=0;
int i=1;
do{
sum+=i*i;
i++;
if(i>n)
break;
}while(i<=n);
return sum;
}
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.println(“enter the n value”);
int n=input.nextInt();
int sumsq=squaresum(n);
System.out.println(“the sum of the 1st
“+n+”squares=”+sumsq);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 20/68
4
11a. Palindrome
import java.util.Scanner;
public class Palindrome{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
System.out.println("Enter the number");
int a=in.nextInt();
pal(a);
}
public static void pal(int b){
int d=b;
int d1=0;
int r=0;
while(d>0)
{
r=d%10;d1=r+(10*d1);
d=d/10;
}
if(d1==b)
System.out.println(b+"is a palindrome");
else
System.out.println(b+"is not a palindrome");
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 21/68
4
11b. Amstrong number
//Amstrong number
import java.util.Scanner;
public class Amstrong {
{
public static void main(String args[])
{
Scanner get=new Scanner(System.in);
Amstrong a=new Amstrong();
System.out.println("enter the number");
int n=get.nextInt();
if(a.sumcube (n)==n)
System.out.println(n + " is Armstrong Number");
else
System.out.println(n + " is not Armstrong Number");
}
}
public static int sumcube(int x)
{
int dig,sum = 0;
while(x >= 1)
{dig = x % 10;
sum = sum + dig * dig * dig;
x = x / 10;
}
return(sum);
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 22/68
4
}
11c. GP and AP first n terms
import java.util.Scanner;
public class Gpap{
int i;
public static void gp(int a,int r,int n){
System.out.println(“the GP series is:\n”);
for(i=1;i<=n;i++)
System.out.println(a*Math.pow(r,i-1)+”\t”);
}
public static void ap(int a,int d,int n){
System.out.println(“the AP series is:\n”);
for(i=1;i<=n;i++)
System.out.println(a+(i-1)*d+”\t”);
}
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println(“enter the first term of both the series:”)
int a=sc.nextInt();
System.out.println(“enter the last term of both the series:”)
int n=sc.nextInt();
System.out.println(“enter the common difference (which willalso be the common ratio for the GP):”);
int r=sc.nextInt();
gp(a,n,r);
ap(a,n,r);
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 24/68
4
12. Pass by value
//passing arguments by value
import java.util.Scanner;
public class PassByVal{
public static void main(){
Scanner input= new Scanner(System.in);
int a, b;
System.out.println(“enter the values of a and b”);
a=input.nextInt();
b= input.nextInt();
compute(a,b); //passing a and b values to the user defined
function
}
public static void compute(int a,int b){
System.out.println(“(a+b)^2=”+
(Math.pow(a,2)+2*a*b+Math.pow(b,2));
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 25/68
4
13 Math class
//using Math class functions
import java.util.Scanner;
public class MathFns{
public static void main(){
Scanner in=new Scanner(System.in);
System.out.println(“enter the angle in degrees”);
int deg=input.nextInt();
System.out.println(“the value of
sin”+deg+”is”+Math.sin(math.toRadians(deg)));
System.out.println(“enter the base and exponent”);
float base= input.nextFloat();
float exp= input.nextFloat();
System.out.println(base+”to the
power”+exp+”=”+Math.pow(base,exp));
System.out.println(“enter the value for square root andexponent”);
int value= input.nextInt();
System.out.println(“square root=”+Math.sqrt(value)+”\n e
raised to the power”+value+”=”+Math.exp(value));
System.out.println(“the absolute value
of”+value+”is”+Math.abs(value));
System.out.println(“minimum value
of”+base+”and”+exp+”is”+Math.min(base,exp));
if(value<deg)
System.out.println(“a random value
between”+value+”and”+deg+”is”+
value+Math.random()*deg);
else
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 26/68
4
System.out.println(“a random value
between”+value+”and”+deg+”is”+
deg+Math.random()*value);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 27/68
4
14a. Factorial of a number using recursion
// Factorial of a number using recursion
import java.util.Scanner;
public class Factorial {
public static int fact(int integer)
{
if(integer>0)
{
if( (integer == 0)||(integer==1))
return 1;
else
return(integer*(fact(integer-1)));
}
}
public static void main(String args[]) {
Scanner i=new Scanner(System.in);System.out.println("enter the number for which
factorial is to be found");
int n=i.nextInt();
int f=fact(n);
System.out.println("Factorial of" +n +"is"+f);
}
}
14b. // First n terms of Fibonacci series usingrecursion
import java.util.Scanner;
public class Fibo {
int f1,f2,f3;
public static void fib(int integer)
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 28/68
4
{
if(integer>0){
f3=f1+f2;
System.out.println(f3+”\t”);
f1=f2;
f2=f3;
fib(integer-3);
}
else
System.out.println(“\n this is the end of the series”);
}
public static void main(String args[]) {
Scanner i=new Scanner(System.in);
System.out.println("enter the number of terms inthe Fibonacci series");
int n=i.nextInt();
f1=0;
f2=1;
System.out.println(“the first n terms of theFibonacci series are”+f1+”\t”+f2);
fib(n);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 29/68
4
15. a Sum of elements in a list
//Sum of elements in a list
import java.util.Scanner;
class SumMat{
int n,i,sum;
int a[]=new int[10];
Scanner ip=new Scanner(System.in);
void accept(){
System.out.println(“how many values”);
int n=ip.nextInt();
for( i=0;i<n;i++)
a[i]=ip.nextInt();
}
int sum(){
sum=0;
for(i=0;i<n;i++)sum+=a[i];
}
public class M{
public static void main(String args[]){
SumMat m1=new SumMat();
m1.accept();
System.out.println(“the sum of the elements entered is”+(m1.sum());
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 30/68
4
15b. Smallest and largest values in a list
//minimum and maximum elements in a list of numbers
import java.util.Scanner;
class Minmax{
int n;
int a[]=new int[10];
Scanner ip=new Scanner(System.in);
void accept(){
System.out.println(“how many values”);
n=ip.nextInt();
for(int i=0;i<n;i++)
a[i]=ip.nextInt();
}
void findminmax(){
int min=a[0],max=a[0];
for(int p=1;p<n;p++){if(a[p]>max)
max=a[p];
if(a[p]<min)
min=a[p];
}
System.out.println(“min value=”+min+”max value=”+max);
}
}
public class M{
public static void main(String args[]){
Minmax m1=new Minmax();
m1.accept();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 31/68
4
m1.findminmax();
}
}
15c. Copying arrays
import java.util.Scanner;
class ArrayCopy{
int n,i;
Scanner ip=new Scanner(System.in);
void accept(){
int a[]=new int[10];
System.out.println(“how many values”);
int n=ip.nextInt();
for(int i=0;i<n;i++)
a[i]=ip.nextInt();
}
void copyarray(){
int copya[]=new int[10];
for(i=0;i<a.length;i++)
copya[i]=a[i];
}
void display()
{
System.out.println(“the source array is”)
for(i=0;i<n;i++)
System.out.println(a[i]+”\t”);
System.out.println(“the target array is”)
for(i=0;i<n;i++)
System.out.println(copya[i]+”\t”);
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 32/68
4
}
public class M{
public static void main(String args[]){
ArrayCopy a=new ArrayCopy();
a.accept();
a.copyarray();
a.display();
}
}
15d. Passing arrays to methods
import java.util.Scanner;class Passarray{
public static void main(String args[])
{
int [] a=new int[10];
int i,n;
Scanner in=new Scanner(System.in);
System.out.println("\nhow many values");n=in.nextInt();
for(i=0;i<n;i++)
a[i]=in.nextInt();
System.out.println("\nthe elements of the array are\n");
parray(a,n);
}
public static void parray(int [] x,int n){
for(int i=0;i<n;i++)
System.out.println(x[i] + "\n");
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 33/68
4
15e. Returning arrays from methods
import java.util.Scanner;
class Revarray{
public static void main(String args[])
{
int [] a=new int[10];
int [] b=new int[10];
int i,n;
Scanner in=new Scanner(System.in);
System.out.println("\nhow many values");
n=in.nextInt();
for(i=0;i<n;i++)
a[i]=in.nextInt();
System.out.println("\nthe elements of the array A\n");
parray(a,n);
b=reverse(a,n);
System.out.println("\nthe elements of the array B\n");
parray(b,n);
}
public static void parray(int [] x,int x1)
{
for(int i=0;i<x1;i++)
System.out.println(x[i] + "\n");
}
public static int [] reverse(int [] list,int x)
{
int [] r=new int[x];
for(int i=0,j=x-1;i<x;i++,j--)
{
r[j]=list[i];
}
return r; }}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 34/68
4
15f. Variable length argument list
import java.util.Scanner;
class varargs{
public static void main(String args[])
{
printarray(2,4,6,8,10);//passing variable-length arg listprintarray(new int[] {1,2,4});//pass an array arg
}
public static void printarray(int...n)
{
if (n.length==0)
{ System.out.println("no arguments passed");
return;
}
int r=n[0];
for(int i=1;i<n.length;i++)
{
if (n[i]>r)
r=n[i];
}
System.out.println("\nthe max value is "+ r);}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 35/68
4
16. Linear search
import java.util.Scanner;
public class LinSearch{
int n,search;
int a[]=new int[10];
Scanner i=new Scanner(System.in);
void accept(){
System.out.println(“how many elements”);
int n=i.nextInt();
for(int s=0;s<n;s++)
a[s]=i.nextInt();
}
void lsearch(){
int flag=0;
System.out.println(“enter the element to be searched”);
int search=i.nextInt();for(int j=1;j<n;j++){
if(a[j]= =search)
{
flag=1;
break;
}
}
if(flag==1)
Sytem.out.println(search+” is found at”+ (j+1) +”location”);
else
Sytem.out.println(search+” not found”);
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 36/68
4
}
public class LiSrch{
public static void main(String args[]){
LinSearch l=new LinSearch();
l.accept();
l.lsearch();
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 37/68
4
17. Bubble sort
import java.util.Scanner;
public class BubbleSort {
int intArray[] = new int[10];
public static void main(String[] args) {
System.out.println(“enter number of elements”);
Scanner in=new Scanner(System.in);
int n=in.nextInt();
System.out.println(“enter the”+n+”elements”);
for(int t=0;t<n;t++)
intArray[t]=in.nextInt();
System.out.println("Array Before Bubble Sort");
display(intArray);
bubbleSort(intArray);
System.out.println("Array After Bubble Sort");
display(intArray);}public static void display(int []intArray){
for(int j=0; j < intArray.length; j++)
System.out.print(intArray[j] + " ");
}
public static void bubbleSort(int[] intArray) {
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(intArray[j-1] > intArray[j]){
temp = intArray[j-1];
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 38/68
4
intArray[j-1] = intArray[j];
intArray[j] = temp; }}}}
18. Using the Array class
import java.util.Arrays;
import java.util.Scanner;
public class ArrayClass {
public static void main(String args[] ) {
Scanner sc=new Scanner(System.in);
int [] n=new int[10];
System.out.println("enter number of elements:");
int num=sc.nextInt();
for(int i=0;i<num;i++)
n[i]=sc.nextInt();
java.util.Arrays.sort(n);
System.out.println("\the sorted list is");
display(n);java.util.Arrays.sort(n,1,3);//sorts from location 1 to 3-1
display(n);
int [] a1={2,4,6,8};
int [] a2={2,4,6,8};
System.out.println(java.util.Arrays.equals(a1,a2));
java.util.Arrays.fill(a1,7);//fills 7 in the entire array
display(a1);
java.util.Arrays.fill(a2,1,3,9);//fills 9 in the partial array
display(a2);
}
public static void display(int[] a){
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 39/68
4
for(int i=0;i<a.length;i++)
System.out.println(a[i]);}}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 40/68
4
19.a. Accept and display elements in the matrix form
import java.util.Scanner;
class Matrix{
int [][]a=new int[10][10];
Scanner input=new Scanner(System.in);
int i,j;
void accept(int r,int c){
System.out.println(“enter elements of the matrix”);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
a[i][j]=input.nextInt();
}
void display(int r,int c){
for(i=0;i<r;i++)
{
System.out.println(“\n”);
for(j=0;j<c;j++)
System.out.println(a[i][j]+”\t”);
}
}
}
public class Call{
public static void main(String args[]){
Scanner inp=new Scanner(System.in);
Matrix m=new Matrix();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 41/68
4
System.out.println(“enter the order of the matrix”);
row=inp.nextInt();
col=inp.nextInt();
m.accept(row,col);
m.display(row,col);
}
}
19b. Addition subtraction multiplication using
methods
import java.util.Scanner;
class MatrixOpns{
int [][]a=new int[5][5];
int[][]b=new int[5][5];
int [][]c=new int[5][5];
int i,j,k;
Scanner in=new Scanner(System.in);
int[][] accept(int r,int co){
System.out.println(“enter the elements of the matrix”);
for(i=0;i<r;i++)
{
for(j=0;j<co;j++)
a[i][j]=in.nextInt();
}
return a;
}
int[][] addmat(int [][]a,int[][]b,int r,int c){
for(i=0;i<r;i++)
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 42/68
4
{
for(j=0;j<c;j++)
c[i][j]=a[i][j]+b[i][j];
}
}
int[][] submat(int [][]a,int[][]b,int r,int co){
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
c[i][j]=a[i][j]-b[i][j];
}
}
int[][] multmat(int[][]a1,int[][]a2,int row1,int col1,int col2){
for(i=0;i<row1;i++)
for(j = 0; j < col2; j++) {
array3[i][j]=0;
for(k = 0; k < col1; k++){
array3[i][j] += array1[i][k]*array2[k][j];
}
}
}
}
void display(int[][]array,int r,int c){
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
System.out.println(array[i][j]);
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 43/68
4
}
}
}
public class Call{
Scanner input=new Scanner(System.in);
public static void main(String args[]){
System.out.println(“enter the order of the matrix1”);
int row1=input.nextInt();
int col1=input.nextInt();
System.out.println(“enter the order of the matrix2”);
int row2=input.nextInt();
int col2=input.nextInt();
int [][]array1=new int[5][5];
int [][]array2=new int[5][5];
int [][]array3=new int[5][5];
System.out.println(“the first matrix:\n);
MatrixOpns mo=new MatrixOpns();
array1[][]=mo.accept(row1,col1);
MatrixOpns mo1=new MatrixOpns();
System.out.println(“the second matrix:\n”);
array2[][]=mo1.accept(row2,col2);
if((row1==row2)&&(col1=col2))
{
array3[][]=mo.addmat(array1,array2,row1,col1);
display(array3,row1,col1);
}
else
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 44/68
4
System.out.println(“addition not possible\n”);
if((row1==row2)&&(col1=col2))
{
array3[][]=mo.submat(array1,array2,row1,col1);
display(array3,row1,col1);
}
else
System.out.println(“subtraction not possible\n”);
if(col1==row2){
array3[][]=mo.multmat(array1,array2,row1,col1,col2);
display(array3,row1,col2);
}
else
System.out.println(“multiplication not possible\n”);
}
}
19c. Transpose of a matrix
import java.util.Scanner;
class MatrixOpns{
int [][]a=new int[5][5];
int i,j;
Scanner in=new Scanner(System.in);
void accept(int r,int co){
System.out.println(“enter the elements of the matrix”);
for(i=0;i<r;i++)
{
for(j=0;j<co;j++)
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 45/68
4
a[i][j]=in.nextInt();
}
}
void transpose(int r,int co){
int [][]trans=new int[5][5];
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
trans[i[j]=a[j][i];
}
}
void display(int r,int c){
for(i=0;i<r;i++)
{
System.out.println(“\n”);
for(j=0;j<c;j++)
System.out.println(a[i][j])+”\t”);
}
}
}
public class Call{
Scanner input=new Scanner(System.in);
public static void main(String args[]){
System.out.println(“enter the order of the matrix”);
int row=input.nextInt();
int col=input.nextInt();
MatrixOpns mo=new MatrixOpns();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 46/68
4
mo.accept(row,col);
mo.transpose(row,col);
mo.display(row,col);
}
}
19d. Sum of each row and column elements
import java.util.Scanner;
class MatrixOpns{
int [][]a=new int[5][5];
int i,j;
Scanner in=new Scanner(System.in);
void accept(int r,int co){
System.out.println(“enter the elements of the matrix”);
for(i=0;i<r;i++)
{
for(j=0;j<co;j++)
a[i][j]=in.nextInt();
}
}
void SumRowCol(int r,int co){
int sumrow=0;
int sumcol=0;
for(i=0;i<r;i++)
{
sumrow=sumcol=0;
System.out.println(“\n Sum of”+(i+1)+”th row elements and
+(j+1)+”th column are respectively: \t ”);
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 47/68
4
for(j=0;j<c;j++){
sumrow+=a[i][j];
sumcol+=a[j][i];
}
System.out.println(sumrow+”\t”+sumcol);
}
}
void display(int r,int c){
for(i=0;i<r;i++)
{
System.out.println(“\n”);
for(j=0;j<c;j++)
System.out.println(a[i][j])+”\t”);
}
}
}
public class Call{
Scanner input=new Scanner(System.in);
public static void main(String args[]){
System.out.println(“enter the order of the matrix”);
int row=input.nextInt();
int col=input.nextInt();
MatrixOpns mo=new MatrixOpns();
mo.accept(row,col);
mo.display(row,col);
mo.SumRowCol(row,col);
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 49/68
4
20. Default and parameterised constructors with ternary
operators
//ternary operator with constructors example
import java.util.Scanner;
public class Check{
int n;
Check()
{
n=0;}
Check(int p)
{
n=p;
}
public static void main(String args[])
{
Check c1-new Check();
Check c2=new Check(5);
int min;
min=((c1.n)<(c2.n))? (c1.n):(c2.n);
System.out.println(“minimum value is”+min);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 50/68
4
21. Date, Random, GUI classes
import java.util.Scanner;
import javax.swing.JFrame;
public class LibraryClasses{
public static void main(String args[]){
java.util.Date d=new java.util.Date();
System.out.println(“Time elapsed since Jan
1,1970:”+d.getTime()+”milliseconds”);
String s=d.toString();
System.out.println(s);
java.util.Random r= new java.util.Random();
Scanner in=new Scanner(System.in);
System.out.println(“enter the limit:”);
int n=in.nextInt();
System.out.println(“ten random values between 0
and”+n+”are\n”);
for(int i=0;i<10;i++)
System.out.println(r.nextInt(n)+”\t”);
Jframe f1=new JFrame();
JButton jok=new JButton(“OK!”);
JLabel jlb=new JLabel(“Time elapsed since 1 Jan,1970:”);
JTextField tf=new JTextField(s);
f1.add(jlb);
f1.add(tf);
f1.add(jok);
f1.setTitle(“Current Time”);
f1.setSize(200,150);
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 51/68
4
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true); }}
22. Static methods and variables
class Circle{
double radius;
static int nobj=0;
final static double pi=3.14159;
Circle(){
radius=0.0;
nobj++;
}
Circle(double r){
radius=r;
nobj++;
}
static int getobj(){
return nobj;
}
void displayarea(){
System.out.println(“area=”+(radius*radius*pi);
}
}
public class Call{
public static void main(String args[]){
Circle c1=new Circle();
System.out.println(“the number of objects
created=”+c1.getobj());
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 52/68
4
Circle c2=new Circle(5);
System.out.println(“the number of objects
created=”+c2.getobj() +”\n the area is”+c2.displayarea() );
}}
23. Passing objects to methods
import java.util.Scanner;
class obj
{
private int r=1;
private static int nobj=0;
public obj(){nobj++;}
public obj(int i){r=i;nobj++;}
public int get(){return r;}
public void set(int i){r=(i>0) ? i : 0; }
public static int getnobj() { return nobj;}
public int calculate(){return r*r*2; }
}
public class Passobj{
public static void main(String args[])
{
obj o=new obj(3);
disp(o);
}
public static void disp(obj o1)
{
System.out.println("\nthe value of r"+o1.get() +"calculated
value=" + o1.calculate());
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 53/68
4
24. Array of objects
import java.util.Scanner;
class Sample{
Scanner input=new Scanner(System.in);
int a,b;
void accept(){
a=input.nextInt();
b=input.nextInt();
}
void calculate(){
System.out.println(“Sum of the values=”+(a+b));
}
}
public class Call{
public static void main(String args[]){
Sample []s=new Sample[5];
for(int j=0;j<5;j++)
{
s[j]=new Sample();
s[j].accept();
s[j].calculate();
}
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 54/68
4
25. a. Method overloading
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);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 55/68
4
25 b. Constructor overloading
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”);
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 56/68
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();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 57/68
4
b1.display1();
b1.display();}}
27. Multiple inheritance
class A {
int x;
int y;
int get(int p, int q){
x=p;
y=q;
return(0);
}
void Show(){
System.out.println(x+”\t”+y); }}
class B extends A{
void Showb(){
System.out.println("the values for object of class
B:"+get(5,6));
Show();
}}
class C extends A{
void display(){
System.out.println("the values for object of class
C:"+get(15,16));
Show();}}
public class Call{
public static void main(String args[]){
C c = new C();
c.display();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 58/68
4
B b=new B();
b.Showb();
c.get(7,8);
c.Show(); }}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 59/68
4
28. Method overriding using super keyword
// Method overriding
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
} }
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
} }
class override1 {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 60/68
4
subOb.show(); // this calls show() in B
} }
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 61/68
4
29. Program using the JFrame class
import javax.swing.*;
public class kframe{
public static void main(String args[]) {
JFrame f1=new CustomFrame();
f1.setTitle("Official Student Information");
f1.setLocation(200,100);
f1.setVisible(true);
JFrame f2=new CustomFrame();
f2.setTitle("Duplicate record of student information");
f2.setLocation(200,300);
f2.setVisible(true);
}}
class CustomFrame extends JFrame {
public CustomFrame() {
JButton jok=new JButton("OK");
JLabel label2=new JLabel("Select your combination");
JLabel label3=new JLabel("Gender");
JLabel label1=new JLabel("Enter your name");
JTextField tf1=new JTextField("Type name here");
JCheckBox jck=new JCheckBox("I am above 18 years of
age");
JRadioButton jrd=new JRadioButton("Male");
JRadioButton jrd1=new JRadioButton("Female");
JComboBox jcb=new JComboBox(new String[]
{"MECs","MSCs","MPCs","BZC"});
JPanel p=new JPanel();
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 62/68
4
p.add(label1);
p.add(tf1);
p.add(jck);
p.add(label3);
p.add(jrd);
p.add(jrd1);
p.add(label2);
p.add(jcb);
add(p);
p.add(jok);
setSize(450,270);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 63/68
4
30.Polymorphism
class Shape {
void Area() {
System.out.println("No dimensions hence area cannot be
calculated");
}
}
class Circle extends Shape {
void Area() {
float r=4.5;
System.out.println("Area of circle="+(Math.PI*r*r));
}
}
class Square extends Shape {
void Area() {
int s=5;
System.out.println("Area of square:"+(s*s));
}
}
class Traingle extends Shape{
void Area() {
float base=4.5;
float ht=4.3;
System.out.println("Area of right angled
triangle:"+(0.5*base*ht));
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 64/68
4
class poly {
public static void main(String[] args) {
Shape obj1 = new Shape();
Shape obj2 = new Circle);
Shape obj3 = new Square();
Shape obj4 = new Triangle();
obj1.Area();
obj2.Area();
obj3.Area();
obj4.Area();
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 65/68
4
31.Array list class
import java.util.ArrayList;
public class alist
{
public static void main(String args[])
{
ArrayList<String> subjects=new ArrayList<String>();
subjects.add("Mathematics");
subjects.add("Electronics");
subjects.add("Computer Science");
subjects.add("Chemistry");
subjects.add("Botany");
System.out.println("list size? " + subjects.size());
System.out.println("is Physics in the list " +
subjects.contains("Physics"));
System.out.println("the location of Chemistry in the list " +
subjects.indexOf("Chemistry"));
System.out.println("is the list subjects empty" +
subjects.isEmpty());
subjects.add(2,"Physics");
subjects.remove("Chemistry");
subjects.remove(1);
System.out.println(subjects.toString());
for(int i=subjects.size()-1;i>=0;i--)
System.out.println(subjects.get(i) + " " );
}}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 66/68
4
32.Abstract classes
abstract class A {
abstract void callme();
abstract void call();
// non-abstract method
void callmetoo() {
System.out.println("This is a concrete method.");
}
}
class B extends A {
void callme() {
System.out.println("B's implementation of callme.");
}
void call(){
System.out.println(“B’s implementation of call”);
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
b.callme();b.callmetoo();
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 67/68
4
33. Interfaces
public class Sample {
public static void main(String[] args) {
shape circleshape=new circle();circleshape.Draw();
}
}
interface shape
{
public String baseclass="shape";
public void Draw();}
class circle implements shape
{
public void Draw() {
System.out.println("Drawing Circle here");
}
}
8/3/2019 Table of Contents 13
http://slidepdf.com/reader/full/table-of-contents-13 68/68
34. Exception handling
import java.util.Scanner;
public class exceptionHandle{
public static void main(String[] args) throws Exception{
try{
int a,b;
Scanner in =
new Scanner(System.in));
a = in.nextInt();
b = in.nextInt();
}
catch(NumberFormatException ex){
System.out.println("This is not a numeric value.");
}
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}