oops1(1) (1)
oops1(1) (1)
Exp.No. 1:
Date :
AIM: Implementation of a java program that prints all real solutions to the quadratic
equation ax2 +bx+c=0. Read in a, b, c and use the quadratic formula.
Algorithms
1. Start the program
2. Create a class with the name „roots‟.
3. Declare the double variables r1, r2.
4. Read a, b, c values from keyboard using a Scanner class.
5. Find the discriminate value using dynamic initialization int d=b*b-4*a*c;
6. Check the discriminate value using if condition.
Program :
import java.util.*;
class roots
{
public static void main(String args[])
{
double r1,r2;
System.out.println("Enter a,b,c of the equation ax2+bx+c=0");
Scanner as=new Scanner (System.in);
int a=as.nextInt();
int b=as.nextInt();
int c=as.nextInt();
int d=b*b-4*a*c;
if(d<0)
{
System.out.println("There are no solutions");
}
if(d==0)
{
System.out.println("The roots are real and equal"); r1=r2=-b/(2.0*a);
System.out.println("The roots are "+r1+" and "+r2);
}
if(d>0)
{
System.out.println("The roots are real");
}
if(d>0)
{
System.out.println("The roots are real");
r1=(-b+Math.sqrt(d))/(2.0*a);
r2=(-b-Math.sqrt(d))/(2.0*a);
System.out.println("The roots are "+r1+" and "+r2);
}
}
}
Output
E:\java>java roots
Enter a,b,c of the equation ax2+bx+c=0
1
4
4
The roots are real and equal
The roots are -2.0 and -2.0
The roots are real
E:\java>java roots
Enter a,b,c of the equation ax2+bx+c=0
5
5
3
There are no solutions
Result
Thus the above sample programs were written and the output were executed successfully.
FIBONACCI SERIES
Exp.No.2:
Date :
AIM :The Fibonacci sequence is defined by the following rule: The first two values in the
sequence are 1 and 1. Every subsequent value is the sum of the two values preceding it. Write
a Java program that uses both recursive and non recursive functions to print the nth value in
the Fibonacci sequence.
Algorithms
1. Start the program
2. Create a class with the name Fib.
3. Declare the int variables n, n1.
4. Create an object for predefined class Random and generate a random using nextInt()
5. In main() create an object for fib1 class and call the method fibc() using the object.
Program
import java.util.Scanner;
import java.io.*;
class Fibonacci
{
int a1,a2,a3,num,i;
int fibRec(int x)
{
if(x==0)
return 0;
else if(x==1)
return 1;
else
return (fibRec(x-1)+fibRec(x-2));
}
int fibNonRec(int y)
{
a1=1; a2=1;
num=y; for(i=3;i<=num;i++)
{
a3=a1+a2; System.out.println(a3);
a1=a2; a2=a3;
}
return a3;
}
}
class Fib
{
public static void main(String args[]) throws IOException
{
System.out.println("enter n value");
Scanner s = new Scanner(System.in);
int n = s.nextInt();
Fibonacci f=new Fibonacci(); for (int i = 0; i <= n; i++) {
System.out.print(f.fibRec(i) + " ");
}
System.out.println("The nth value with recursive:"+f.fibRec(n));
System.out.println("The nth value with non-recursive:"+f.fibNonRec(n));
}
}
OUTPUT:
E:\java>java Fib
enter n value
6
0112358
The nth value with recursive:8
2
3
5
8
The nth value with non-recursive:8
Result
Thus the above sample programs were written and the output were executed successfully.
SORT IN ASCENDING ORDER
Exp.No.3:
Date :
AIM: Read array of numbers through scanner class and sort in ascending order.
Algorithms
Program
import java.util.*;
class Sorting
{
void sortStrings()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = s.nextInt();
String[] str = new String[n];
System.out.println("Enter strings: ");
for(int i = 0; i < n; i++)
{
str[i] = new String(s.next());
}
for(int i = 0; i < n; i++)
{
for(int j = i+1; j < n; j++)
{
if(str[i].compareTo(str[j])>0)
{
String temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}
System.out.println("Sorted list of strings is:");
for(int i = 0; i < n ; i++)
{
System.out.println(str[i]);
}
}
}
class sortnames
{
public static void main(String[] args)
{
Sorting obj = new Sorting();
obj.sortStrings();
}
}
Output
E:\java>java sortnames
Enter the value of n:
5
Enter strings:
sri
mani
banu
kala
anu
Sorted list of strings is:
anu
banu
kala
mani
sri
Result
Thus the above sample programs were written and the output were executed successfully.
Prime Numbers
Exp.No.4:
Date :
Aim: Implementation of a program that prompts the user for an integer and then prints out
all prime numbers up to that integer.
Algorithms
Program
import java.util.Scanner;
import java.util.*;
public class Testp
{
public static void main(String[] args)
{
System.out.println("--------Prime Number ");
Scanner sc = new scanner(System.in);
System.out.println("Enter valid Number");
int n = sc.nextInt();
int c = 0;
for (int i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2)
{
System.out.println(n + "is Prime Number");
}
else
System.out.println(n + "is not Prime Number");
}
}
Output
E:\java>javac PrimeNumbers.java
E:\java>java PrimeNumbers
Enter a number:
30
2
3
5
7
11
13
17
19
23
29
Result
Thus the above sample programs were written and the output were executed successfully.
MatixMultiplication
Exp.No. 5
Date
Algorithm
1. Start
2. Declare variables for matrix size.
3. Initialize the number of rows and columns for the first matrix.
4. Initialize the number of rows and columns for the second matrix.
5. Declare two matrices.
6. Ask the user to initialize the matrices.
7. Call a method to multiply the two matrices.
8. Print the two matrices.
9. Check if matrix multiplication is possible or not.
10. If possible, then create a new Matrix to store the product of the two matrices.
11. Traverse each element of the two matrices and multiply them.
12. Store this product in the new matrix at the corresponding index.
13. Print the final product matrix.
14. If matrix multiplication is not possible, then display the same.
15. Stop.
Program
import java.util.Scanner;
public class MatixMultiplication
{
public static void main(String args[])
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Enter the base of squared matrices");
n = input.nextInt();
int[][] a = new int[n][n];
int[][] b = new int[n][n];
int[][] c = new int[n][n];
System.out.println("Enter the elements of 1st martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
a[i][j] = input.nextInt();
}
}
System.out.println("Enter the elements of 2nd martix row wise \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
b[i][j] = input.nextInt();
}
}
System.out.println("Multiplying the matrices...");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product is:");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j] + " ");
}
System.out.println();
}
input.close();
}
}
Output
E:\java>javac MatixMultiplication.java
E:\java>java MatixMultiplication
Enter the base of squared matrices
3
Enter the elements of 1st martix row wise
3
1
2
4
5
6
7
8
9
Enter the elements of 2nd martix row wise
2
3
4
5
6
7
8
9
1
Multiplying the matrices...
The product is:
27 33 21
81 96 57
126 150 93
Result
Thus the above sample programs were written and the output were executed successfully.
Palindrome Or Not
Exp.No.: 6
Date :
Aim : Implementation of a Java program that checks whether a given string is a palindrome
or not
Algorithm:
Program
import java.io.*;
class PalindromeExample{
public static void main(String args[]){
int r,sum=0,temp;
int n=454;//It is the number variable to be checked for palindrome
temp=n;
while(n>0){
r=n%10; //getting remainder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
Output
E:\java>javac PalindromeExample.java
E:\java>java PalindromeExample
palindrome number
Result
Thus the above sample programs were written and the output were executed successfully.
DEVELOP A JAVA APPLICATION TO GENERATE ELECTRICITY BILL
Exp.No.7
Date :
AIM:
To Develop a java application to generate electricity bill for the domestic and commercial
reading type of eb connection and print the bill amount.
ALGORITHM
1. Start
2. Declare Consumer no ,consumer name,previous month reading, current month
3. reading type of eb connection(ie domestic or commercial)
4. Compute the bill amount using if else statement to calculate the bill amount for
domestic and commercial.
5. If the type of EB connection is domestic and commercial.
6. Print the bill amount.
7. Stop the process.
PROGRAMS
import java.io.*;
import java.util.*;
class Bill
{
public static void main(String args[])
{
String name;
Scanner scr=new Scanner(System.in);
System.out.println("enter the name :");
name=scr.next();
int consno;
System.out.println("enter the consno :");
consno=scr.nextInt();
int pread,cread;
System.out.println("enter the prev month reading :");
pread=scr.nextInt();
System.out.println("enter the current month reading :");
cread=scr.nextInt();
int units,choice;
double amount=0.0;
System.out.println("enter the choice:1.domestic 2.commercial ");
choice=scr.nextInt();
units=pread-cread;
switch(choice)
{
case 1:
if(units<=100)
amount=units*1;
else if(units>100 & units<=200)
{
units=units-100;
amount=100*1+(2.50*units);
}
else if(units>200 & units<=500)
{
units=units-200;
amount=100+(100*2.50)+(4*units);
}
else if(units>500)
{
units=units-500;
amount=100+(100*2.50)+(4*300)+(6*units);
}
System.out.println("electric bill="+amount);
break;
case 2:
if(units<=100)
amount=units*2;
else if(units>100 & units<=200)
{
units=units-100;
amount=100*1+(4.50*units);
}
else if(units>200 & units<=500)
{
units=units-200;
amount=100+(100*4.50)+(6*units);
}
else if(units>500)
{
units=units-500;
amount=100+(100*4.50)+(6*300)+(7*units);
}
System.out.println("electric bill="+amount);
break;
}
}
}
OUTPUT
RESULT:
Thus the java program to develop electricity bill was written and the output was executed
successfully.
DEVELOP A JAVA APPLICATION TO IMPLEMENT CURRENCY CONVERTER
EX.NO:8
DATE:
Aim
Algorithm
1. Start
2. Declare dollar, INR, euro and yen for currency converter
3. Declare meter, kilometre for distance converter.
4. Declare hours, minutes, seconds for time converter
5. Compute the currency conversion, time conversion and distance in separate package
using the formula .
6. Compile the packages and run the main program
7. Print the value
8. Stop the process.
Programs
CURPACK:
package Curpack;
import java.util.Scanner;
import java.io.*;
public class Currency
{
public void curconverter()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Dollar To Be Converted:");
float USD=S.nextFloat();
float INR=USD*72;
System.out.println("The Converted Amount From USD to INR="+INR);
}
}
DISTPACK:
package Distpack;
import java.util.Scanner;
import java.io.*;
public class Distance
{
public void distconverter()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Distance In Meter:");
float distm=S.nextFloat();
float distcm=distm*100;
System.out.println("Distance In Centimeter="+distcm);
}
}
TIMEPACK:
package Timepack;
import java.util.Scanner;
import java.io.*;
public class Time
{
public void timeconverter()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Time In Hours:");
float hrs=S.nextFloat();
float mins=hrs*60;
float seconds=mins*60;
System.out.println("Time In Minutes="+mins);
System.out.println("Time In Seconds="+seconds);
}
}
MAIN PROGRAM:
import Curpack.*;
import Distpack.*;
import Timepack.*;
class Convert
{
public static void main(String args[])
{
Currency c=new Currency();
c.curconverter();
Distance d=new Distance();
d.distconverter();
Time t=new Time();
t.timeconverter();
}
}
OUTPUT
Result
Thus the java program to implement currency converter, distance converter and time
converter using package was written and the output was executed successfully.
DEVELOP A JAVA APPLICATION FOR EMPLOYEE DETAILS USING
INHERITANCE
EX.NO.:9
DATE:
Aim
To Develop a java application with employee class and print gross and net salary of
employee using inheritance.
Algorithm
1. Start
2. Declare emp_name, emp_id,address, mail id ,mobile no as members
3. Inherit the classes, programmer, assistant professor,associate professor and professor
4. from the employee class..
5. Add basic pay (BP) as the member of all inherited classes with 97% of BP as
6. DA,10% of BP as HRA,12 % of BP as PF,0.1%of BP for staff club fund.
7. Generate pay slip for the employees with their gross and net salary.
8. Stop the process.
Programs
import java.io.*;
import java.util.*;
class Employee
{
int empid;
String empname,mailid,add;
long mobno;
}
class Programmer extends Employee
{
double BP;
public void getm()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Employee ID:");
empid=S.nextInt();
System.out.println("Enter Employee Name:");
empname=S.next();
System.out.println("Enter Mobile No:");
mobno=S.nextLong();
System.out.println("Enter Basic Pay:");
BP=S.nextDouble();
System.out.println("Enter Address:");
add=S.next();
System.out.println("Enter Mail Id:");
mailid=S.next();
}
public void calc()
{
double DA=(77/100)*BP;
double HRA=(10/100)*BP;
double PF=(12/100)*BP;
double fund=(.1/100);
double GS=BP+HRA+DA;
double NS=GS-PF-fund;
System.out.println("Employee ID="+empid);
System.out.println("Employee NAME="+empname);
System.out.println("Employee ADDRESS="+add);
System.out.println("Employee MAIL ID="+mailid);
System.out.println("Employee MOBILE NO="+mobno);
System.out.println("BASIC PAY="+BP);
System.out.println("GROSS SALARY="+GS);
System.out.println("NET SALARY="+NS);
}
}
class Asstproffessor extends Employee
{
double BP;
public void getm()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Employee ID:");
empid=S.nextInt();
System.out.println("Enter Employee Name:");
empname=S.next();
System.out.println("Enter Mobile No:");
mobno=S.nextLong();
System.out.println("Enter Basic Pay:");
BP=S.nextDouble();
System.out.println("Enter Address:");
add=S.next();
System.out.println("Enter Mail Id:");
mailid=S.next();
}
public void calc()
{
double DA=(77/100)*BP;
double HRA=(10/100)*BP;
double PF=(12/100)*BP;
double fund=(.1/100);
double GS=BP+HRA+DA;
double NS=GS-PF-fund;
System.out.println("Employee ID="+empid);
System.out.println("Employee NAME="+empname);
System.out.println("Employee ADDRESS="+add);
System.out.println("Employee MAIL ID="+mailid);
System.out.println("Employee MOBILE NO="+mobno);
System.out.println("BASIC PAY="+BP);
System.out.println("GROSS SALARY="+GS);
System.out.println("NET SALARY="+NS);
}
}
class Associateproffessor extends Employee
{
double BP;
public void getm()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Employee ID:");
empid=S.nextInt();
System.out.println("Enter Employee Name:");
empname=S.next();
System.out.println("Enter Mobile No:");
mobno=S.nextLong();
System.out.println("Enter Basic Pay:");
BP=S.nextDouble();
System.out.println("Enter Address:");
add=S.next();
System.out.println("Enter Mail Id:");
mailid=S.next();
}
public void calc()
{
double DA=(77/100)*BP;
double HRA=(10/100)*BP;
double PF=(12/100)*BP;
double fund=(.1/100);
double GS=BP+HRA+DA;
double NS=GS-PF-fund;
System.out.println("Employee ID="+empid);
System.out.println("Employee NAME="+empname);
System.out.println("Employee ADDRESS="+add);
System.out.println("Employee MAIL ID="+mailid);
System.out.println("Employee MOBILE NO="+mobno);
System.out.println("BASIC PAY="+BP);
System.out.println("GROSS SALARY="+GS);
System.out.println("NET SALARY="+NS);
}
}
class Proffessor extends Employee
{
double BP;
public void getm()
{
Scanner S=new Scanner(System.in);
System.out.println("Enter Employee ID:");
empid=S.nextInt();
System.out.println("Enter Employee Name:");
empname=S.next();
System.out.println("Enter Mobile No:");
mobno=S.nextLong();
System.out.println("Enter Basic Pay:");
BP=S.nextDouble();
System.out.println("Enter Address:");
add=S.next();
System.out.println("Enter Mail Id:");
mailid=S.next();
}
public void calc()
{
double DA=(77/100)*BP;
double HRA=(10/100)*BP;
double PF=(12/100)*BP;
double fund=(.1/100);
double GS=BP+HRA+DA;
double NS=GS-PF-fund;
System.out.println("Employee ID="+empid);
System.out.println("Employee NAME="+empname);
System.out.println("Employee ADDRESS="+add);
System.out.println("Employee MAIL ID="+mailid);
System.out.println("Employee MOBILE NO="+mobno);
System.out.println("BASIC PAY="+BP);
System.out.println("GROSS SALARY="+GS);
System.out.println("NET SALARY="+NS);
}
}
class Employeemain
{
public static void main(String args[])
{
int ch;
System.out.print("****EMPLOYEE DETAILS****");
System.out.print("\n 1.PROGRAMMER \n 2.ASSISTANT PROFFESSOR \n
3.ASSOCIATE PROFFESSOR \n 4.PROFFESSOR");
Scanner S=new Scanner(System.in);
System.out.println("\n Enter Choice:");
ch=S.nextInt();
switch(ch)
{
case 1:
Programmer p=new Programmer();
p.getm();
p.calc();
break;
case 2:
Asstproffessor a=new Asstproffessor();
a.getm();
a.calc();
break;
case 3:
Associateproffessor ap=new Associateproffessor();
ap.getm();
ap.calc();
break;
case 4:
Proffessor pr=new Proffessor();
pr.getm();
pr.calc();
break;
}
}
}
OUTPUT
Result
Thus the java program to implement employee details and calculation of gross and net salary
using inheritance was written and the output was executed successfully.
DESIGN A JAVA INTERFACE FOR STACK ADT USING ARRAY
IMPLEMENTATION
EX.NO:10
DATE:
Aim
To Design a java interface for ADT stack. Implement this interface using array. Provide
necessary exception handling in both the implementations.
ALGORITHM:
Programs
import java.io.*;
import java.util.*;
public class StackDemo
{
private static final int capacity=3;
int arr[]=new int[capacity];
int top=-1;
public void push(int pushedElement)
{
if(top<capacity-1)
{
top++;
arr[top]=pushedElement;
System.out.println("Element"+pushedElement+"is pushed to stack!");
printElement();
}
else
{
System.out.println("Stack Overflow");
}
}
public void pop()
{
if(top>=0)
{
top--;
System.out.println("Pop Operation Done");
}
else
{
System.out.println("Stack Underflow");
}
}
public void printElement()
{
if(top<=0)
{
System.out.println("Elements in Stack:");
for(int i=0;i<=top;i++)
{
System.out.println(arr[i]);
}
}
}
public static void main(String args[])
{
StackDemo s = new StackDemo();
s.pop();
s.push(23);
s.push(2);
s.push(73);
s.push(21);
s.pop();
s.pop();
s.pop();
s.pop();
}
}
OUTPUT:
Stack Underflow
Element23is pushed to stack!
Elements in Stack:
23
Element2is pushed to stack!
Element73is pushed to stack!
Stack Overflow
Pop Operation Done
Pop Operation Done
Pop Operation Done
Stack Underflow
Result:
Thus the java program to develop stack ADT using interface was written and the output was
executed successfully.
PROGRAM TO PERFORM STRING OPERATIONS USING ARRAY LIST
EX.NO.:11
DATE:
Aim:
To write a program to perform string operations using array list. Write functions for the
following.
a.append – add at end
b.insert – add at particular index.
c. search
d. list all strings starts with given letter.
Algorithm
Step1: create an Array List
Step 2. perform the following string operations in the created Array List
toString( ) , equals( ) , charAt( ). insert( ) , append( ) ,search( ) ,searchlist( )
Programs
import java.util.*;
class list
{
public void insert( List<String> al)
{
int pos;
String n;
Scanner s = new Scanner(System.in);
System.out.print("Enter elements to insert :");
n = s.next();
System.out.print("Enter the position to insert :");
pos=s.nextInt();
al.add(pos-1,n);
System.out.println(al);
}
public void search(List<String> al)
{
int pos=al.size();
int o=-1;
String n,d;
Scanner s= new Scanner(System.in);
System.out.print("Enter the element to search:");
n=s.next();
for (int i = 0; i <pos; i++)
{
d=al.get(i).toString();
if(n.equals(d))
{
o=i+1;
System.out.println("Element found at:"+o);
break;
}
}
if(o==-1)
{
System.out.println("Element not found.");
}
}
public void searchlist(List<String> al)
{
int pos=al.size();
String d;
Scanner s= new Scanner(System.in);
System.out.print("Enter the starting character to search:");
char n=s.next().charAt(0);
for (int i = 0; i <pos; i++)
{
d=al.get(i).toString();
if(n==d.charAt(0))
{
System.out.println(d);
}
}
}
public void append( List<String> al)
{
int pos;
String x,n;
Scanner s = new Scanner(System.in);
System.out.print("Enter elements to insert :");
n = s.next();
al.add(n);
System.out.println(al);
}
OUTPUT
Enter no. of elements you want in array:3
Enter all the elements:
Result
Thus a program in java language to perform string operations using array list was written and
the output was executed successfully.
PROGRAM TO CREATE AND IMPLEMENT AN ABSTRACT CLASS
EX.NO: 12
DATE:
AIM:
To Write a java program to create an abstract class named shape that contains two integers
and an empty method named printarea(). Provide three classes named rectangle, triangle and
circle such that each one of the classes extends the class shape. Each one of the classes
contains only the method printarea() that prints the area of the given shape.
ALGORITHM:
Programs:
import java.util.*;
import java.io.*;
abstract class shape
{
int a=3,b=4;
abstract public void print_area();
}
class rectangle extends shape
{
public int area_rect;
public void print_area()
{
area_rect=a*b;
System.out.println("The Area Of Rectangle Is:"+area_rect);
}
}
class triangle extends shape
{
int area_tri;
public void print_area()
{
area_tri=(int)(0.5*a*b);
System.out.println("The Area Of Triangle Is:"+area_tri);
}
}
class circle extends shape
{
int area_circle;
public void print_area()
{
area_circle=(int)(3.14*a*a);
System.out.println("The Area Of Circle Is:"+area_circle);
}
}
public class JavaApplication
{
public static void main(String args[])
{
rectangle r=new rectangle();
r.print_area();
triangle t=new triangle();
t.print_area();
circle ra=new circle();
ra.print_area();
}
}
OUTPUT
Result
Thus a program in java language to Create and implement an abstract class was written and
the output was executed successfully.
PROGRAM TO IMPLEMENT USER DEFINED EXCEPTION HANDLING
EX.NO: 13
DATE:
AIM:
ALGORITHM
Programs
class Division {
public static void main(String[] args) {
int a, b, result;
a = input.nextInt();
b = input.nextInt();
// try block
try {
result = a / b;
System.out.println("Result = " + result);
}
// catch block
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}
//Example of custom Exception
E:\sampleprograms>javac Division.java
E:\sampleprograms>java Division
Input two integers
70
Exception caught
Division by zero
E:\sampleprograms>javac Example1.java
E:\sampleprograms>java Example1
Caught the exception
Product Invalid
Result
Thus a program in java language to Create User defined exception handling was written and
the output was executed successfully.
PROGRAM TO IMPLEMENT FILE OPERATIONS
EX.NO: 14
DATE:
AIM:
To Write a java program that reads a filename from the user, displays information about
whether the file exists, whether the file is readable, or writable , the type of file and the length
of the file in bytes.
Algorithm:
Programs
package ooplabs;
import java.io.*;
import java.util.*;
class AboutFile{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter the name of the file:");
String file_name = input.nextLine();
File f = new File(file_name);
if(f.exists())
System.out.println("The file " +file_name+ " exists");
else
System.out.println("The file " +file_name+ " does not exist");
if(f.exists()){
if(f.canRead())
System.out.println("The file " +file_name+ " is readable");
else
System.out.println("The file " +file_name+ " is not readable");
if(f.canWrite())
System.out.println("The file " +file_name+ " is writeable");
else
System.out.println("The file " +file_name+ " is not writeable");
System.out.println("The file type is: " +file_name.substring(file_name.indexOf('.')+1));
System.out.println("The Length of the file:" +f.length());
}
}}
Output:
E:\sampleprograms>java AboutFile
Enter the name of the file:
PrimeExample.java
The filePrimeExample.java exists
The file PrimeExample.java is readable
The file PrimeExample.java is writeable
The file type is: java
The Length of the file:543
Result
Thus a program in java language to Create and implement file operations was written and the
output was executed successfully.
PROGRAM TO IMPLEMENT MULTITHREADED APPLICATION
EX.NO: 15
DATE:
AIM
To Write Java Program that implements a multithread application that has three threads. First
thread generates Random integer for every second and if the value is even, second thread
computes the square of number and prints. If the value is odd, the third thread will print the
value of cube of number.
ALGORITHM:
1. Create a Thread.
2. Let the Thread creates random numbers randomly.
3. Check whether the number generated is odd or even
4. If even create a new thread that computes the square of that number and prints it.
5. If odd create a new thread that computes the cube of that number and prints it.
6. Stop the process
Programs
import java.util.*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
}
}
class odd implements Runnable
{ public int x;
public odd(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}
class A extends Thread
{
public void run()
{
int num = 0;
Random r = new Random();
try
{
for (int i = 0; i < 5; i++)
{
num = r.nextInt(100);
System.out.println("Main Thread and Generated Number is " + num);
if (num % 2 == 0)
{
Thread t1 = new Thread(new even(num));
t1.start();
}
Else
{
Thread t2 = new Thread(new odd(num));
t2.start();
}
Thread.sleep(1000);
System.out.println("--------------------------------------");
}
} catch (Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
public class JavaProgram
{
public static void main(String[] args)
{ A a = new A();
a.start();
}
}
OUTPUT:
E:\sampleprograms>java JavaProgram
Main Thread and Generated Number is 45
New Thread 45 is ODD and Cube of 45 is: 91125
--------------------------------------
Main Thread and Generated Number is 49
New Thread 49 is ODD and Cube of 49 is: 117649
--------------------------------------
Main Thread and Generated Number is 76
New Thread 76 is EVEN and Square of 76 is: 5776
--------------------------------------
Main Thread and Generated Number is 51
New Thread 51 is ODD and Cube of 51 is: 132651
--------------------------------------
Main Thread and Generated Number is 6
New Thread 6 is EVEN and Square of 6 is: 36
-----------------------------------
Result
Thus a program in java language to implement multithreaded application was written
and the output was executed successfully.
Producer Consumer Program Using Inter Thread Communication
EX.NO: 16
DATE:
Aim : Implementation of producer consumer program using the concept of inter thread
communication
Algorithm
Program:
class Q
{
int n;
boolean valueSet=false;
synchronized int get()
{
if(!valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception caught");
}
System.out.println("Got:"+n);
valueSet=false;
notify();
return n;
}
synchronized void put(int n)
{
if(valueSet)
try
{
wait();
}
catch(InterruptedException e)
{
System.out.println("Interrupted Exception caught");
}
this.n=n;
valueSet=true;
System.out.println("Put:"+n);
notify();
}
}
class Producer implements Runnable
{
Q q;
Producer(Q q)
{
this.q=q;
new Thread(this,"Producer").start();
}
public void run()
{
int i=0;
while(true)
{
q.put(i++);
}
}
}
class Consumer implements Runnable
{
Q q;
Consumer(Q q)
{
this.q=q;
new Thread(this,"Consumer").start();
}
public void run()
{
while(true)
{
q.get();
}
}
}
class ProdCons
{
public static void main(String[] args)
{
Q q=new Q();
new Producer(q);
new Consumer(q);
System.out.println("Press Control-c to stop");
}}
Output
E:\java>java ProdCons
E:\java>java ProdCons
Put:0
Press Control-c to stop
Got:0
Put:1
Got:1
Put:2
Got:2
Put:3
Got:3
Put:4
Got:4
Put:5
Got:5
Put:6
Got:6
Put:7
Got:7
Put:8
Got:8
Put:9
Got:9
Put:10
Got:10
Result
Thus a program in java language to implement producer consumer program using the
concept of inter thread communication was written and the output was executed successfully.
PROGRAM TO IMPLEMENT GENERIC FUNCTION
Exp.No. 17
Date :
AIM
To Write a java program to find the maximum value from the given type of elements using a
generic function.
ALGORITHM:
Programs
class generic
{
public static <T extends Comparable<T>>
T max(T... elements) {
T max = elements[0];
for (T element : elements) {
if (element.compareTo(max) > 0) {
max = element;
}
}
return max;
}
public static void main(String[] args) {
System.out.println("Integer Max: " + max(Integer.valueOf(32), Integer.valueOf(56),
Integer.valueOf(89), Integer.valueOf(3), Integer.valueOf(456), Integer.valueOf(78),
Integer.valueOf(45)));
E:\sampleprograms>java generic
Integer Max: 456
Double Max: 18.6001
String Max: Strawberry
Boolean Max: true
Byte Max: 127
Result
Thus a program in java language to implement generic function was written and the output
was executed successfully.
PROGRAM TO SIMULATES A TRAFFIC LIGHT
Exp.No : 18
Date :
AIM : Develop a java program that simulates a traffic light. The program lets the user select
one of three lights: Red, Yellow or Green with radio buttons. On selecting a button an
appropriate message with “STOP” or “READY”or ”GO” should appear above the buttons in
selected color. Initially, there is no message shown.
Algorithms:
Program :
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
class A extends JFrame implements ItemListener
{
public JLabel l1, l2;
public JRadioButton r1, r2, r3;
public ButtonGroup bg;
public JPanel p, p1;
public A()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2, 1));
setSize(800, 400);
p = new JPanel(new FlowLayout());
p1 = new JPanel(new FlowLayout());
l1 = new JLabel();
Font f = new Font("Verdana", Font.BOLD, 60);
l1.setFont(f);
add(l1);
p.add(l1);
add(p);
l2 = new JLabel("Select Lights");
p1.add(l2);
JRadioButton r1 = new JRadioButton("Red Light");
r1.setBackground(Color.red);
p1.add(r1);
r1.addItemListener(this);
JRadioButton r2 = new JRadioButton("Yellow Light");
r2.setBackground(Color.YELLOW);
p1.add(r2); r2.addItemListener(this);
JRadioButton r3 = new JRadioButton("Green Light");
r3.setBackground(Color.GREEN);
p1.add(r3);
r3.addItemListener(this);
add(p1);
bg = new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
setVisible(true);
}
public void itemStateChanged(ItemEvent i)
{
JRadioButton jb = (JRadioButton) i.getSource();
switch (jb.getText())
{
case "Red Light":
{
l1.setText("STOP");
l1.setForeground(Color.RED);
}
break;
}
break;
l1.setText("GO");
l1.setForeground(Color.GREEN);
}
break;
}
}
}
public class TLights
{
public static void main(String[] args)
{
A a = new A();
}
}
Output
E:\java>javac TLights.java
E:\java>java TLights
Result
Thus a program in java language to Simulates a Traffic Light was written and the output was
executed successfully.
PROGRAM TO DESIGN A CALCULATOR
EX.NO: 19
DATE:
AIM
To Design a calculator using event driven programming paradigm of java with the following
options
A. decimal manipulator
B. scientific manipulations
ALGORITHM
Programs
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
public class ScientificCalculator1 extends JFrame implements ActionListener
{
JTextField tfield;
double temp,temp1,result,a,ml;
static double m1,m2;
int k=1,x=0,y=0,z=0;
char ch;
JButton
b1,b2,b3,b4,b5,b6,b7,b8,b9,zero,clr,pow2,exp,plus,min,div,log,rec,mul,eq,addsub,dot,sqrt,
sin,cos;
Container cont;
JPanel textpanel,buttonpanel;
ScientificCalculator1()
{
cont=getContentPane();
cont.setLayout(new BorderLayout());
JPanel textpanel=new JPanel();
tfield=new JTextField(25);
tfield.setHorizontalAlignment(SwingConstants.RIGHT);
tfield.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent keyevent)
{
char c=keyevent.getKeyChar();
if(c>='0'&&c<='9')
{
}
else
{
keyevent.consume();
}
}
});
textpanel.add(tfield);
buttonpanel=new JPanel();
buttonpanel.setLayout(new GridLayout(8,4,2,2));
boolean t=true;
b1=new JButton("1");
buttonpanel.add(b1);
b1.addActionListener(this);
b2=new JButton("2");
buttonpanel.add(b2);
b2.addActionListener(this);
b3=new JButton("3");
buttonpanel.add(b3);
b3.addActionListener(this);
b4=new JButton("4");
buttonpanel.add(b4);
b4.addActionListener(this);
b5=new JButton("5");
buttonpanel.add(b5);
b5.addActionListener(this);
b6=new JButton("6");
buttonpanel.add(b6);
b6.addActionListener(this);
b7=new JButton("7");
buttonpanel.add(b7);
b7.addActionListener(this);
b8=new JButton("8");
buttonpanel.add(b8);
b8.addActionListener(this);
b9=new JButton("9");
buttonpanel.add(b9);
b9.addActionListener(this);
zero=new JButton("0");
buttonpanel.add(zero);
zero.addActionListener(this);
plus=new JButton("+");
buttonpanel.add(plus);
plus.addActionListener(this);
min=new JButton("-");
buttonpanel.add(min);
min.addActionListener(this);
mul=new JButton("*");
buttonpanel.add(mul);
mul.addActionListener(this);
div=new JButton("/");
buttonpanel.add(div);
div.addActionListener(this);
dot=new JButton(".");
buttonpanel.add(dot);
dot.addActionListener(this);
eq=new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
rec=new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt=new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
log=new JButton("log");
buttonpanel.add(log);
log.addActionListener(this);
sin=new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
cos=new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
pow2=new JButton("x^2");
buttonpanel.add(pow2);
pow2.addActionListener(this);
exp=new JButton("Exp");
buttonpanel.add(exp);
exp.addActionListener(this);
clr=new JButton("AC");
buttonpanel.add(clr);
clr.addActionListener(this);
cont.add("Center",buttonpanel);
cont.add("North",textpanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("1"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"1");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"1");
z=0;
}
}
if(s.equals("2"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"2");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"2");
z=0;
}
}
if(s.equals("3"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"3");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"3");
z=0;
}
}
if(s.equals("4"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"4");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"4");
z=0;
}
}
if(s.equals("5"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"5");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"5");
z=0;
}
}
if(s.equals("6"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"6");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"6");
z=0;
}
}
if(s.equals("7"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"7");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"7");
z=0;
}
}
if(s.equals("8"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"8");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"8");
z=0;
}
}
if(s.equals("9"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"9");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"9");
z=0;
}
}
if(s.equals("0"))
{
if(z==0)
{
tfield.setText(tfield.getText()+"0");
}
else
{
tfield.setText("");
tfield.setText(tfield.getText()+"0");
z=0;
}
}
if(s.equals("AC"))
{
tfield.setText("");
x=0;y=0;
z=0;
}
if(s.equals("log"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=Math.log(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("1/x"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=1/(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("Exp"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=Math.exp(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("x^2"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=Math.pow(Double.parseDouble(tfield.getText()),2);
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("."))
{
if(y==0)
{
tfield.setText(tfield.getText()+".");
y=1;
}
else
{
tfield.setText(tfield.getText());
}
}
if(s.equals("+"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=0;
ch='+';
}
else
{
temp=Double.parseDouble(tfield.getText());
tfield.setText("");
ch='+';
y=0;x=0;
}
tfield.requestFocus();
}
if(s.equals("-"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=0;
ch='-';
}
else
{
y=0;x=0;
temp=Double.parseDouble(tfield.getText());
tfield.setText("");
ch='-';
}
tfield.requestFocus();
}
if(s.equals("/"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=1;
ch='/';
}
else
{
y=0;x=0;
temp=Double.parseDouble(tfield.getText());
ch='/';
tfield.setText("");
}
tfield.requestFocus();
}
if(s.equals("*"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
temp=1;
ch='*';
}
else
{
y=0;x=0;
temp=Double.parseDouble(tfield.getText());
ch='*';
tfield.setText("");
}
tfield.requestFocus();
}
if(s.equals("Sqrt"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=Math.sqrt(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("SIN"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=Math.sin(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("COS"))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
a=Math.cos(Double.parseDouble(tfield.getText()));
tfield.setText("");
tfield.setText(tfield.getText()+a);
}
}
if(s.equals("="))
{
if(tfield.getText().equals(""))
{
tfield.setText("");
}
else
{
temp1=Double.parseDouble(tfield.getText());
switch(ch)
{
case '+':
result=temp+temp1;
break;
case '-':
result=temp-temp1;
break;
case '/':
result=temp/temp1;
break;
case '*':
result=temp*temp1;
break;
}
tfield.setText("");
tfield.setText(tfield.getText()+result);
z=1;
}
}
}
double fact(double x)
{
/*int er=0;
if(x>0)
{
er=20;
return 0;
}*/
double i,s=1;
for(i=2;i<=x;i+=1.0)
s*=i;
return s;
}
public static void main(String srgs[])
{
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel")
;
} catch (Exception e) {
}
ScientificCalculator1 f = new ScientificCalculator1();
f.setTitle("ScientificCalculator");
f.pack();
f.setVisible(true);
}
}
OUTPUT
Result
Thus a program in java language to design and implement calculator was written and the
output was executed successfully.
MINIPROJECT
EX.NO: 20
DATE:
Result
Thus a Mini project application for hotel management was written and the output was
executed successfully.
ADDITIONAL LAB EXERCISE
STUDENT DATABASE
Exp.No.21
Date
AIM:
To Design a class to represent a Student details. Include the Student ID, name of the Student,
branch, year and assign initial values, calculate average of marks of 6 subjects, calculate
attendance percentage.
Algorithms
1. Create a class for student, which contains student ID, name, branch, year.
2. Define methods in that class to assign intial values, to calculate average of marks, to
calculate percentage.
Program
import java.util.Scanner;
class Student
{
int id;
String name; String branch; int year;
void getData(int a,String b,String c,int d)
{
id=a; name=b; branch=c; year=d;
}
void Avg(int a,int b,int c,int d,int e,int f)
{
float avg; avg=(a+b+c+d+e+f)/6;
System.out.println("Average is"+avg);
}
void Percentage(int a, int b)
{
float per; per=(a/b)*100;
System.out.println("Percentage is"+per);
}
}
class Execute
{
public static void main(String args[])
{
Output
E:\java>java Execute
enter ID,name,branch,year
104
raja
CSE
2
enter 6 subject marks
90
98
97
96
86
90
Average is92.0
enter the present and total working days
35
30
Percentage is100.0
Result:
Thus the Student database application was compiled and executed successfully.
Develop an Application for Dynamic Polymorphism
Exp. No.: 22
Date :
Aim : Develop with suitable hierarchy, classes for Point, Shape, Rectangle, Square, Circle,
Ellipse, Triangle, Polygon, etc. Design a simple test application to demonstrate dynamic
polymorphism
Algorithm
System.out.println("Radius:"+r); System.out.println("Area:"+(3.14*r*r));
}
}
class triangle extends shape{ int b,h;
void tdata(int x,int y){ b=x;h=y;
}
void area(){ System.out.println("Base:"+b); System.out.println("Height:"+h);
System.out.println("Area:"+(0.5*b*h));
}
}
class ShapeTest{
public static void main(String args[]){ rectangle r = new rectangle();
square s = new square(); circle c = new circle(); triangle t = new triangle(); r.show();
s.display(); System.out.println(""); System.out.println("Rectangle:"); System.out.println();
r.getdata(12,6);
r.area(); System.out.println(""); System.out.println("Square:"); System.out.println();
s.gdata(7);
s.area(); System.out.println(""); System.out.println("Circle:"); System.out.println(); c.get(5);
c.area(); System.out.println("");
System.out.println("Triangle:"); System.out.println(); t.tdata(4,7);
t.area();
}
}
output
E:\java>javac ShapeTest.java
E:\java>java ShapeTest
This is the Point Base class
Different shapes can be developed with different number of points
Rectangle:
Length:12
Breadth:6
Area:72
Square:
Side:7
Area:49
Circle:
Radius:5
Area:78.5
Triangle:
Base:4
Height:7
Area:14.0
Result:
Thus the Student database application was compiled and executed successfully.
Multithreading
Exp. No.: 23
Date :
AIM:
Write a program that creates three threads. First thread displays “Good Morning” every one
second, the second thread displays “Hello” every two seconds and the third thread displays
“Welcome” every three seconds.
Algorithms
Program
public class JavaApplication1
{
public static void main(String[] args)
{
One obj1=new One(); Two obj2=new Two(); Three obj3=new Three();
}
}
{
Thread.sleep(1000);
}
catch(InterruptedException e){} System.out.println("Good Morning");
}
}
}
class Two implements Runnable
{
Two( )
{
new Thread(this, "Two").start(); try{
Thread.sleep(2000);
}
catch(InterruptedException e){}
}
public void run()
{
for (int i=0 ;i<10 ;i++ )
{
try
{
Thread.sleep(2000);
}
catch(InterruptedException e){} System.out.println("Hello");
}
}
}
class Three implements Runnable
{
Three( )
{
new Thread(this, "Three").start(); try{
Thread.sleep(3000);
}catch(InterruptedException e){}
}
public void run()
{
for ( int i=0;i<10 ;i++ )
{
try{ Thread.sleep(3000);
}catch(InterruptedException e){} System.out.println("Welcome");
}
}
}
Output
E:\java>javac JavaApplication1.java
E:\java>java JavaApplication1
Good Morning
Good Morning
Good Morning
Hello
Good Morning
Hello
Good Morning
Welcome
Good Morning
Hello
Good Morning
Good Morning
Welcome
Hello
Good Morning
Good Morning
Hello
Welcome
Hello
Welcome
Hello
Hello
Welcome
Hello
Welcome
Hello
Welcome
Welcome
Welcome
Welcome
Result:
Thus the Student database application was compiled and executed successfully.
Factorial Numbers
Exp. No.: 24
Date :
AIM: Write a java programs to find factorial of a number. User is allowed to enter a number
into the text field whose factorial is to be determined. On pressing the button the value of the
text field is firstly converted into integer and then processed to find its factorial. The result
will get displayed in another text field
Algorithms:
Program:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
Factorial() {
JLabel l1 = new JLabel("Enter Number: ");
JLabel l2 = new JLabel("Factorial of Input Number: ");
t1 = new JTextField(20);
t2 = new JTextField(20);
JPanel p = new JPanel(new GridLayout(3, 2));
JButton b = new JButton("Find");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String number = t1.getText();
int num = Integer.parseInt(number);
long fac = num;
for (int i = num; i > 1; i--) {
fac = fac * (i - 1);
}
t2.setText(Long.toString(fac));
}
});
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p.add(b);
add(p);
setVisible(true);
pack();
}
OUTPUT
E:\java>javac Factorial.java
E:\java>java Factorial
Result:
Thus the Student database application was compiled and executed successfully.
VIVAQUESTION AND ANSWER
1. Why Java?
The programs that we are writing are very similar to their counterparts in several other
languages, so our choice of language is not crucial. We use Java because it is widely
available, embraces a full set of modern abstractions, and has a variety of automatic checks
for mistakes in programs, so it is suitable for learning to program. There is no perfect
language, and you certainly will be programming in other languages in the future.
These keywords specify certain properties of main() that you will learn about later in the
book. For the moment, we just include these keywords in the code (because they are
required) but do not refer to them in the text.
Historically, the type for floating point numbers was float, but they had limited accuracy. The
type double was introduced as a floating point type with twice as much accuracy.
A Java package is a mechanism for organizing Java classes into namespaces similar to the
modules of Modula.
5. Define Queue
A queue is a particular kind of abstract data type or collection which follows the First-In-
First-Out (FIFO) data structure principle.
The operations on the collection are the addition of entities to the rear terminal position,
known as enqueue, and removals of entities from the front terminal position, known as
dequeue.
7. Define a Constructor?
A constructor is a member function which initializes a class. It has the same name as the class
itself and has no return type. They are invoked automatically when the objects are created.
Types of constructors
Parameterized constructors
Default constructors
Copy constructors
9. Define Destructor?
Destructor is a special member function of a class which is used to destroy the memory of
object. The destructor has the same name as the class, but with a tilde (~). It does not take any
parameters.
Java.util package contains the collections framework, legacy collection classes, event model,
date and time facilities, internationalization, and miscellaneous utility classes.
The java.util.Date class represents a specific instant in time, with millisecond precision.
Date() - This constructor allocates a Date object and initializes it so that it represents the time
at which it was allocated, measured to the nearest millisecond.
boolean after(Date when) - This method tests if this date is after the specified date.
boolean before(Date when) - This method tests if this date is before the specified date.
int compareTo(Date anotherDate) - This method compares two Dates for ordering.
An Abstract Class is a class that is declared abstract—it may or may not include abstract
methods. Abstract classes cannot be instantiated, but they can be subclassed.
The class must also be declared abstract. If a class contains an abstract method, the class must
be abstract as well.
Any child class must either override the abstract method or declare itself abstract.
18. What is peek operation?
Peek is an operation on certain abstract data types, specifically sequential collections such as
stacks and queues, which returns the value of the top of the collection without removing the
value from the data
20 What is Interface?