Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

File: /home/prajwal/DAA_programs Page 1 of 12

1a.Class Name:Student
package DAA;
import java.util.Scanner;

public class Student


{

String usn,name,branch,phno;
void read()
{
Scanner Input = new Scanner(System.in);
System.out.println("Enter Student USN");
usn = Input.next();
System.out.println("Enter Student Name");
name = Input.next();
System.out.println("Enter Student Branch");
branch = Input.next();
System.out.println("Enter Student Phone");
phno = Input.next();
}
void display()
{
System.out.println(usn+" "+name+" "+branch+" "+phno);
}
public static void main(String[] args)
{
Scanner Input = new Scanner(System.in);
System.out.println("Enter number of student objects to create");
int n = Input.nextInt();
Student[] s=new Student[n];
for(int i =0;i<n;i++)
{
s[i]=new Student();
}
System.out.println("Enter Student Details");
for(int i =0;i<n;i++)
{
s[i].read();
}
System.out.println("USN || NAME || BRANCH || PHONE_NO");
for(int i =0;i<n;i++)
{
s[i].display();
}

}
}

----------------------------------
1b.Class Name:Stack
package Stack;
public class Stack {
static int ch;
static int maxsize;
long[] stackarray;
static int top;
public Stack(int s)
{
maxsize = s;
stackarray = new long[maxsize];
top = -1;
}
public void push(long j)
{
if(top == maxsize-1)
System.out.println("Stack Overflow");
File: /home/prajwal/DAA_programs Page 2 of 12

else
stackarray[++top] = j;
}
public long pop()
{
if(top==-1)
System.out.println("Stack underflow");

return stackarray[top--] ;
}
public void display()
{
System.out.print("\nStack = ");
if (top==-1)
{
System.out.println("Stack is Empty");
}
else
{
for (int i = top; i >= 0; i--)
System.out.print(stackarray[i] + " ");
System.out.println();
}
}
}
1b.Class Name:Stack_demo
package Stack;
import java.util.Scanner;
import Stack.Stack;
public class Stack_demo {
static int ch;
static int top;
static int maxsize;
public static void main(String[] args)
{
System.out.print("Size of the stack:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
maxsize=n;
Stack stack = new Stack(n);
for(;;){
System.out.print("Menu\n1-Push\n2-Pop\n3-Display\n4-Exit\nEnter
Your Choice:");
int ch=s.nextInt();
switch(ch)
{
case 1:
System.out.print("Item to be pushed:");
int datastack=s.nextInt();
stack.push(datastack);

break;
case 2:
long popeddata=stack.pop();
System.out.println("Poped element is "+popeddata);
break;
case 3: stack.display();
break;
case 4:System.exit(0);
break;
}
if(ch==4)
break;
}
}
}
File: /home/prajwal/DAA_programs Page 3 of 12

2a.Class Name:SecondA
package DAA;
import java.io.IOException;
import java.util.Scanner;
public class SecondA
{
static int m,n,o;
static Scanner input=new Scanner(System.in);
public static void main(String args[])throws IOException
{
teaching[] s;
technical[] t;
contractor[] c;
/** READING **/
System.out.println("\n***** Read Teaching Staff Details ****");
System.out.println("Enter the number of Teaching staff");
m=input.nextInt();
s=new teaching[m];
for(int i=0;i<m;i++)
{
s[i]=new teaching();
System.out.println("-------- Enter Teaching Staff"+
(i+1)+"Details--------");
s[i].read();
}
System.out.println("\n******* Read Technical Staff Details *******");
System.out.println("Enter the number of Technical staff");
n=input.nextInt();
t=new technical[n];
for(int i=0;i<n;i++)
{
t[i]=new technical();
System.out.println("-------Enter Technical Staff"+
(i+1)+"Details------");
t[i].read();
}
System.out.println("\n****** Read Contactor Staff Details ******");
System.out.println("Enter the number of Contractor staff");
o=input.nextInt();
c=new contractor[o];
for(int i=0;i<o;i++)
{
c[i]=new contractor();
System.out.println("--------- Enter contractor Staff"+
(i+1)+"Details-------");
c[i].read();
}
/**DISPLAY**/
System.out.println("\n\t*****TEACHING STAFF ******\t");
System.out.println("SID\tNAME\t\tPHONE NO\tSALARY\tDOMAIN\tPUBLISHER");
System.out.println("--------------------------------");
for(int i=0;i<m;i++)
{
s[i].display();
}
System.out.println("\n\t****************TECHNICAL STAFF *********\t");
System.out.println("SID\tNAME\t\tPHONE NO\tSALARY\tSKILLS");
System.out.println("-----------------------------------------------");
for(int i=0;i<n;i++)
{
t[i].display();
}
System.out.println("\n\t*************CONTRACTOR STAFF ***********\t");
System.out.println("SID\tNAME\t\tPHONE NO\tSALARY\tYEAR OF CONTRACT");
System.out.println("-----------------------------------------------");
File: /home/prajwal/DAA_programs Page 4 of 12

for(int i=0;i<o;i++)
{
c[i].display();
}
}
}

2a.Class Name:Staff
package DAA;
import java.io.*;
import java.util.Scanner;
class Staff
{
protected String name;
protected int staffid;
protected int salary;
protected String phone;
//BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Scanner input=new Scanner(System.in);
protected void read() throws IOException
{
System.out.println("Enter Staff ID:");
staffid=input.nextInt();
System.out.println("Enter Staff Name:");
name=input.next();
System.out.println("Enter Staff Phone Number");
phone=input.next();
System.out.println("Enter Staff Salary");
salary=input.nextInt();
}
}
class teaching extends Staff
{
protected String domain;
protected String publisher;
protected void read()throws IOException
{
super.read();
System.out.println("Enter the Staff Domain");
domain=input.next();
System.out.println("Enter the Publisher");
publisher=input.next();
}
protected void display()
{

System.out.println(staffid+"\t"+name+"\t\t"+phone+"\t"+salary+"\t"+domain+"\t"+publisher);
}
}
class technical extends Staff
{
int n;
protected String[] skils;
protected void read()throws IOException
{
super.read();
System.out.println("Enter the subject skills He/She Has");
n=input.nextInt();
skils=new String[n];
for(int i=0;i<n;i++)
{
System.out.println("Enter Skills"+(i+1));
File: /home/prajwal/DAA_programs Page 5 of 12

skils[i]=input.next();
}
}
void display()
{
System.out.println(staffid+"\t"+name+"\t\t"+phone+"\t"+salary+"\t");
for(int j=0;j<skils.length;j++)
System.out.println(skils[j]+",");
System.out.println(" ");
}
}
class contractor extends Staff
{
protected int period;
protected void read() throws IOException
{
super.read();
System.out.println("How many years of Contract");
period=input.nextInt();
}
void display()
{

System.out.println(staffid+"\t"+name+"\t\t"+phone+"\t"+salary+"\t"+period);
}
}
================================================================================================

2b.Class Name:SecondB
package DAA;
import java.util.Scanner;
public class SecondB
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter Name :-");
String name=in.nextLine();
System.out.println("Enter Date of birth:-");
String date=in.next();
Customer customer=new Customer();
customer.readData(name, date);
customer.displayData();
}
}
2b.Class Name:Customer
package DAA;
import java.util.StringTokenizer;
class Customer
{
private String name;
private String date_of_birth;
public Customer(String name, String date_of_birth)
{
super();
this.name = name;
this.date_of_birth = date_of_birth;
}
public Customer (){
}
public void readData(String name, String date_of_birth)
{
this.name = name;
this.date_of_birth = date_of_birth;
}
public void displayData()
File: /home/prajwal/DAA_programs Page 6 of 12

{
StringTokenizer st=new StringTokenizer(this.date_of_birth,"/");
System.out.print(this.name+",");
while(st.hasMoreTokens())
{
System.out.print(st.nextToken()+" ");
}
}
}

3a.Class Name:Multithreading
package Multithreading;

import java.util.Random;

class Square implements Runnable


{
public int x;
public Square(int x)
{
this.x = x;
}
public void run()
{
System.out.println("From Second thread - Square of "
+ x + " is: " + x * x);
}
}
class Cube implements Runnable
{
public int x;
public Cube(int x)
{
this.x = x;
}
public void run()
{
System.out.println("From Third thread - Cube of " +
x + " is: " + x * x * x);
}
}
class FirstThreadIsRandom 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 Started and Generated Number is " + num);
Thread t2 = new Thread(new Square(num));
t2.start();
Thread t3 = new Thread(new Cube(num));
t3.start();
Thread.sleep(1000);
System.out.println("---------------------");
}
}
catch (Exception ex)
{
System.out.println(ex.getMessage());
}
File: /home/prajwal/DAA_programs Page 7 of 12

}
}
public class Multithraed {
public static void main(String[] args)
{
FirstThreadIsRandom firstThread = new
FirstThreadIsRandom();
Thread t1 = new Thread(firstThread);
t1.start();
}

3b.Class Name:DivideException
package DAA;
import java.util.Scanner;
public class DivideException
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Please enter first number(numerator): ");
int numerator = input.nextInt();
System.out.print("Please enter second number(denominator): ");
int denominator = input.nextInt();
try {
new DivideException().doDivide(numerator, denominator);
}
catch (Exception e)
{
System.out.println("Exception Condition Program is ending ");
}
}
public void doDivide(int a, int b) throws Exception
{
float result = a/b;
System.out.println("Division result of "+ a +"/"+b +"= " +result);
}
}

4.Quick-sort
package DAA;
import java.util.Random;
import java.util.Scanner;
public class Quicksort
{
public static int[] a;
static void sort( )
{
quicksort(0, a.length - 1);
}
static void quicksort(int low, int high)
{
if (low < high)
{
int s = partition(low, high);
quicksort(low, s - 1);
quicksort(s + 1, high);
}
}
static int partition(int low, int high)
{
int pivot = a[low];
File: /home/prajwal/DAA_programs Page 8 of 12

int i = low;
int j = high + 1;
do
{
do
{
++i;
}while (i < high && a[i] < pivot);
do
{
--j;
}while (a[j] > pivot);
swap(i,j);
}while (i < j);
swap(i,j);
swap(low,j);
return j;
}
static void swap(int i, int j)
{
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String args[ ])
{
Scanner in = new Scanner(System.in);
Random rm = new Random();
System.out.print("Enter the value of n: ");
int n = in.nextInt();
a = new int[n];
System.out.print("Enter the array Elements");
for (int i = 1; i < n; i++)
{
a[i] = rm.nextInt(100000);
//a[i]=in.nextInt();
}
System.out.println("Array before sorting");
for(int i=0;i<a.length;i++)
//for (int i : a)
{
System.out.print(a[i] + " ");
}
long startTime = System.nanoTime();
sort();
long endTime = System.nanoTime();
double duration = (endTime - startTime) / 1000000.00;//divide by
10,00,000 to get milliseconds.
System.out.println("\nArray After sorting");
for(int i=0;i<a.length;i++)
//for (int i : a)
{
System.out.print(a[i] + " ");
}
System.out.println("\nTime for sorting is " + duration + " milli
seconds");
}
}

5.Merge-sort

package DAA;
import java.util.Random;
import java.util.Scanner;
public class Mergesort
{
File: /home/prajwal/DAA_programs Page 9 of 12

public static int[] a;


static void sort( )
{
mergesort(0, a.length - 1);
}
static void mergesort(int left, int right)
{
int mid;
if (left < right)
{
mid = (left + right) / 2;
mergesort(left, mid);
mergesort(mid + 1, right);
merge(left, mid, right);
}
}
static void merge(int left, int mid, int right)
{
int temp[] = new int[a.length];
for (int i = left; i <= right; i++)
{
temp[i] = a[i];
}
int i = left;
int j = mid + 1;
int k = left;
while (i <= mid && j <= right)
{
if (temp[i] <= temp[j])
{
a[k++] = temp[i++];
}
else
{
a[k++] = temp[j++];
}
}
while (i <= mid)
{
a[k++] = temp[i++];
}
while (j <= right)
{
a[k++] = temp[j++];
}
}
public static void main(String args[ ])
{
Scanner sc = new Scanner(System.in);
Random rm = new Random();
System.out.println("---MergeSort---");
System.out.print("Enter the value of n: ");
int n = sc.nextInt();
a = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = rm.nextInt(10000);
}
System.out.println("Array before sorting");
for (int i : a)
{
System.out.print(i + " ");
}
long startTime = System.nanoTime( );
sort( );
long endTime = System.nanoTime( );
File: /home/prajwal/DAA_programs Page 10 of 12

double duration = (endTime - startTime) / 1000000.00;


System.out.println("\nArray After sorting");
for (int i : a)
{
System.out.print(i + " ");
}
System.out.println("\nTime for sorting is " + duration + " milli
seconds");
}
}

6A.Knapsack-Dynamic

package Knapsack;

import java.util.Scanner;

public class Knapsack_dynamic {


static int max(int a, int b)
{
return (a > b)? a : b;
}
static void knapSack(int W, int wt[], int val[], int n)
{
int i, j;
int [][]K = new int[n+1][W+1];
for (i = 0; i <= n; i++)
{
for (j = 0; j <= W; j++)
{
if (i==0 || j==0)
K[i][j] = 0;
else if (wt[i-1] <= j)
K[i][j] = max(val[i-1]+K[i-1][j-wt[i-1]],K[i-1][j]);
else
K[i][j] = K[i-1][j];
}
}
System.out.println("The maximum Profit obtained is: "+K[n][W]);

while(n!=0)
{
if(K[n][W]!=K[n-1][W])
{
System.out.println ("\t item:"+n);
W=W-wt[n-1];
}
n--;
}
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of items: ");
int n = sc.nextInt();
System.out.println("Enter the items weights: ");
int []wt = new int[n];
for(int i=0; i<n; i++)
wt[i] = sc.nextInt();
System.out.println("Enter the items values: ");
int []val = new int[n];
for(int i=0; i<n; i++)
val[i] = sc.nextInt();
System.out.println("Enter the maximum capacity: ");
int W = sc.nextInt();
File: /home/prajwal/DAA_programs Page 11 of 12

knapSack(W, wt, val, n);

}
}

6B.Knapsack-Greedy

package DAA;
import java.util.Scanner;
class KObject
{
float w;
float p;
float r;
}
public class Knapscak_greedy
{
static final int MAX = 20;
static int n;
static float M;
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number of objects: ");
n = scanner.nextInt();
KObject[] obj = new KObject[n];
for(int i = 0; i<n;i++)
obj[i] = new KObject();// allocate memory for members
ReadObjects(obj);
Knapsack(obj);
}
static void ReadObjects(KObject obj[])
{
KObject temp = new KObject();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the max capacity of knapsack: ");
M = scanner.nextFloat();
System.out.println("Enter Weights: ");
for (int i = 0; i < n; i++)
obj[i].w = scanner.nextFloat();
System.out.println("Enter Profits: ");
for (int i = 0; i < n; i++)
obj[i].p = scanner.nextFloat();
for (int i = 0; i < n; i++)
obj[i].r = obj[i].p / obj[i].w;
// sort objects in descending order, based on p/w ratio
for(int i = 0; i<n-1; i++)
for(int j=0; j<n-1-i; j++)
if(obj[j].r < obj[j+1].r){
temp = obj[j];
obj[j] = obj[j+1];
obj[j+1] = temp;
}

}
static void Knapsack(KObject kobj[])
{
float x[] = new float[MAX];
float totalprofit;
int i;
float U; // U place holder for M
U = M;
totalprofit = 0;
for (i = 0; i < n; i++)
x[i] = 0;
File: /home/prajwal/DAA_programs Page 12 of 12

for (i = 0; i < n; i++)


{
if (kobj[i].w > U)
break;
else
{
x[i] = 1;
totalprofit = totalprofit + kobj[i].p;
U = U - kobj[i].w;
}
}
System.out.println("i = " + i);
if (i < n)
x[i] = U / kobj[i].w;
totalprofit = totalprofit + (x[i] * kobj[i].p);
System.out.println("The Solution vector, x[]: ");
for (i = 0; i < n; i++)
System.out.print(x[i] + " ");
System.out.println("\nTotal profit is = " + totalprofit);
}
}

You might also like