Download Analog desing and algorithm and more Lecture notes Computer Science in PDF only on Docsity!
Channabasaveshwara Institute of Technology
(An ISO 9001:2008 Certified Institution)
NH 206 (B.H. Road), Gubbi, Tumkur ā 572 216. Karnataka.
DESIGN AND ANALYSIS OF
ALGORITHMS LABORATORY
(15CSL47)
Prepared By,
Monika M & G L Gowda, Assistant professors,
Department of CSE, CIT Gubbi.
Experiment No. 1
1. A) Create a Java class called Student with the following details as variables within it.
(i) USN
(ii) Name
(iii) Branch
(iv) Phone
Write a Java program to create nStudent objects and print the USN, Name, Branch,
and Phone of these objects with suitable headings.
import java.util.Scanner;
public class student {
String USN;
String Name;
String branch;
int phone;
void insertRecord(String reg,String name, String brnch, int ph)
USN=reg;
Name=name;
branch=brnch;
phone=ph;
void displayRecord()
System. out .println(USN+" "+Name+" "+branch+" "+phone);
public static void main(String args[]){
student s[]= new student [100];
Scanner sc= new Scanner(System. in );
System. out .println("enter the number of students");
int n=sc.nextInt();
for ( int i=0;i<n;i++)
s[i]= new student();
for ( int j=0;j<n;j++)
{ System. out .println("enter the usn,name,branch,phone");
String USN=sc.next();
String Name=sc.next();
String branch=sc.next();
int phone=sc.nextInt();
s[j].insertRecord(USN,Name,branch,phone);
for ( int m=0;m<n;m++){
s[m].displayRecord();
B) Write a Java program to implement the Stack using arrays. Write Push(), Pop(),and
Display() methods to demonstrate its working.
import java.util.Scanner;
public class stack {
final int max=100;
int s[]= new int [max];
int top=-1;
void push( int ele)
if (top>=max-1)
System. out .println("stack overflow");
else
s[++top]=ele;
int pop()
int z=0;
if (top==-1)
System. out .println("stack underflow");
else
z=s[top--];
return z;
void display()
if (top==-1)
System. out .println("stack empty");
else
for ( int i=top;i>-1;i--)
System. out .println(s[i]+" ");
public static void main(String args[])
int q=1;
stack m = new stack();
System. out .println("program to perform stack operations");
Scanner sc= new Scanner(System. in );
while (q!=0)
System. out .println("enter 1. push 2.pop 3. display ");
System. out .println("enter your choice");
int ch=sc.nextInt();
switch (ch)
case 1:System. out .println("enter the element to be pushed");
int ele=sc.nextInt();
m.push(ele);
break ;
case 2: int popele;
popele=m.pop();
System. out .println("the poped element is");
System. out .println(popele+" ");
break ;
case 3:System. out .println("elements in the stack are");
m.display();
break ;
case 4:q=0;
Output:
program to perform stack operations enter 1. push 2.pop 3. display enter your choice 1 enter the element to be pushed 10 enter 1. push 2.pop 3. display enter your choice 1 enter the element to be pushed 20 enter 1. push 2.pop 3. display enter your choice 3 elements in the stack are 20 10 enter 1. push 2.pop 3. display enter your choice 1 enter the element to be pushed 30 enter 1. push 2.pop 3. display enter your choice 1 enter the element to be pushed 40 enter 1. push 2.pop 3. display enter your choice
Experiment No. 2a
Design a super class called Staff with details as StaffId, Name, Phone, Salary.
Extend this class by writing three subclasses namely Teaching (domain, publications),
Technical (skills), and Contract (period). Write a Java program to read and display at
least 3 staff objects of all three categories.
class Staff { int staffid,phone,salary; String name; public Staff(int id , int no, int sal, String na){ staffid=id; phone=no; salary=sal; name=na; } void display(){ System.out.println("-------------------------------------"); System.out.println("Staff ID:"+ " "+ staffid); System.out.println("Staff Phone number:" + " "+ phone); System.out.println("Staff Salary:" +" "+ salary); System.out.println("Staff Name:" +" "+ name); } } class Teaching extends Staff { String domain; int no_of_publications; public Teaching(int id, int no, int sal, String na,String d,int nop){ super(id,no,sal,na); domain=d; no_of_publications=nop; } void Tdisplay(){ System.out.println("-------------------------------------"); System.out.println("Teaching Staff Details"); super.display(); System.out.println("Domain :" +" "+domain); System.out.println("No_of_publications:"+" "+no_of_publications); } } class Technical extends Staff{ String skills; public Technical(int id , int no, int sal, String na,String sk){ super(id,no,sal,na); skills=sk; } void Tedisplay(){ System.out.println("-------------------------------------"); System.out.println("Technical Staff Details"); super.display(); System.out.println("Skills :" + " "+skills); } } class Contract extends Staff{ int period; public Contract(int id , int no, int sal, String na,int pd){ super(id,no,sal,na); period=pd;
void Cdisplay(){ System.out.println("-------------------------------------"); System.out.println("Contract Staff Details"); super.display(); System.out.println("ContractPeriod:" + " "+period + "years"); } } public class Multilevel{ public static void main(String args[]){ Teaching t1=new Teaching(11,998765434,31000,"Anil","CSE",10); Teaching t2=new Teaching(12,996655546,30000,"Anu","ISE",9); Teaching t3=new Teaching(13,999933442,32000,"Anusha","EEE",8); t1.Tdisplay(); t2.Tdisplay(); t3.Tdisplay(); Technicalte1=new Technical(21,994433221,22000,"Kumar","C"); Technicalte2=new Technical(22,998877665,28000,"Krisna","Java"); Technical te3=new Technical(23,991654321,33000,"Kiran","Java"); te1.Tedisplay(); te2.Tedisplay(); te3.Tedisplay(); Contract ct1=new Contract(31,998765434,35000,"Anil",3); Contract ct2=new Contract(32,912345678,39000,"Meghana",2); Contract ct3=new Contract(33,992233445,30000,"Uma",4); ct1.Cdisplay(); ct2.Cdisplay(); ct3.Cdisplay(); } }
Output:
Teaching Staff Details
Staff ID: 11 Staff Phone number: 998765434 Staff Salary: 31000 Staff Name: Anil Domain : CSE No_of_publications: 10
Teaching Staff Details
Staff ID: 12 Staff Phone number: 996655546 Staff Salary: 30000 Staff Name: Anu Domain : ISE No_of_publications: 9
Teaching Staff Details
Staff ID: 13 Staff Phone number: 999933442 Staff Salary: 32000 Staff Name: Anusha Domain : EEE No_of_publications: 8
Experiment No. 2b
Write a Java class called Customer to store their name and date_of_birth. The date_of_birth
format should be dd/mm/yyyy. Write methods to read customer data as <name, dd/mm/yyyy>
and display as <name, dd, mm, yyyy> using StringTokenizer class considering the delimiter
character as ā/ā.
import java.util.Scanner;
import java.util.StringTokenizer;
class customer
String name;
String date;
public void read()
Scanner input =new Scanner(System.in);
name=input.next();
date=input.next();
public void display()
System.out.print(name+",");
String delims="/";
StringTokenizer st=new StringTokenizer(date,delims);
while(st.hasMoreElements()){
System.out.print(st.nextElement()+",");
System.out.println();
public static void main(String[] args)
System.out.println("Enter the customer detail");
customer[] cus=new customer[30];
Scanner sc =new Scanner(System.in);
System.out.println("enter the number of customer");
int n=sc.nextInt();
for(int i=0;i<n;i++)
cus[i]=new customer();
cus[i].read();
for(int i=0;i<n;i++)
cus[i].display();
Output:
Enter the customer detail
enter the number of customer
Enter the customer name and date
monika
gowda
monika,12,2,2017,
gowda,11,4,2017,
Experiment No. 3b
Write a Java program that implements a multi-thread application that hash tree threads.
First thread generates a random integer for every 1 second; second thread computes the
square of the number and prints; third thread will print the value of cube of the number.
import java.util.*;
class second implements Runnable { public int x; public second ( int x) { this .x=x; } public void run() { System. out .println("Second thread:Square of the number is"+x*x);
} } class third implements Runnable { public int x; public third( int x) { this .x=x;
} public void run() { System. out .println("third thread:Cube of the number is"+xxx); } }
class first 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("first thread generated number is"+num); Thread t2= new Thread ( new second(num)); t2.start(); Thread t3= new Thread( new third(num)); t3.start(); Thread. sleep (1000);
} } catch (Exception e) { System. out .println(e.getMessage());
public class multithread { public static void main(String args[]) { first a= new first(); a.start(); }
}
OUTPUT:
first thread generated number is
Second thread:Square of the number is
third thread:Cube of the number is
first thread generated number is
Second thread:Square of the number is
third thread:Cube of the number is
first thread generated number is
Second thread:Square of the number is
third thread:Cube of the number is
first thread generated number is
Second thread:Square of the number is
third thread:Cube of the number is
first thread generated number is
Second thread:Square of the number is
third thread:Cube of the number is
int[] a; int i; System.out.println("Enter the array size"); Scanner sc =new Scanner(System.in); int n=sc.nextInt(); a= new int[max]; Random generator=new Random(); for( i=0;i<n;i++) a[i]=generator.nextInt(20); System.out.println("Array before sorting"); for( i=0;i<n;i++) System.out.println(a[i]+" "); long startTime=System.nanoTime();
quicksort m=new quicksort(); m.sort(a,0,n-1); long stopTime=System.nanoTime(); long elapseTime=(stopTime-startTime); System.out.println("Time taken to sort array is:"+elapseTime+"nano seconds"); System.out.println("Sorted array is"); for(i=0;i<n;i++) System.out.println(a[i]);
}
}
OUTPUT:
Enter the array size 10 Array before sorting 17 17 12 2 10 3 18 15 15 17 Time taken to sort array is:16980 nano seconds Sorted array is 2 3 10 12 15 15 17 17 17 18
Experiment No. 5
Sort a given set of n integer elements using Merge Sort method and compute its time
complexity. Run the program for varied values of n > 5000, and record the time taken to sort.
Plot a graph of the time taken versus n on graph sheet. The elements can be read from a file
or can be generated using the random number generator. Demonstrate using Java how the
divideand- conquer method works along with its time complexity analysis: worst case,
average case and best case.
import java.util.Random;
import java.util.Scanner;
public class mergesort {
static int max=10000;
void merge( int[] array,int low, int mid,int high)
int i=low;
int j=mid+1;
int k=low;
int[]resarray;
resarray=new int[max];
while(i<=mid&&j<=high)
if(array[i]<array[j])
resarray[k]=array[i];
i++;
k++;
else
resarray[k]=array[j];
j++;
k++;
while(i<=mid)
resarray[k++]=array[i++];
while(j<=high)
resarray[k++]=array[j++];
for(int m=low;m<=high;m++)
array[m]=resarray[m];
void sort( int[] array,int low,int high)
if(low<high)
Experiment No. 6
Implement in Java, the 0/1 Knapsack problem using
(a) Dynamic Programming method
(b)Greedy method.
(a) Dynamic Programming method
import java.util.Scanner;
public class knapsackDP {
/**
- @param args */ public void solve( int [] wt, int [] val, int W, int N) { int i,j;
int [][] sol = new int [N + 1][W + 1];
for ( i = 0; i <= N; i++) { for ( j = 0; j <= W; j++) { if (i==0||j==0) sol[i][j]=0; else if (wt[i]>j) sol[i][j]=sol[i-1][j];
else sol[i][j]=Math. max ((sol[i- 1][j]), (sol[i - 1][j - wt[i]] + val[i]));
System. out .println("The optimal solution is"+sol[N][W]); int [] selected = new int [N + 1]; for (i=0;i<N+1;i++) selected[i]=0; i=N; j=W; while (i>0&&j>0) { if (sol[i][j] !=sol[i-1][j]) { selected[i] = 1; j = j - wt[i]; } i--; }
System. out .println("\nItems selected : "); for ( i = 1; i < N + 1; i++) if (selected[i] == 1) System. out .print(i +" ");