Thursday, January 3, 2019

DAA 01 A - TO CREATE N STUDENTS

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 n Student objects and print the USN, Name, Branch, and Phone of these objects with suitable headings.

class student 
{
String USN,NAME,BRANCH,PH;
student(String U,String N,String b,String p)

this.USN=U;
this.NAME=N;
this.BRANCH=b;
this.PH=p;
}

void display()
{
System.out.println("\nEnter the students detail");
System.out.println("USN="+this.USN);
System.out.println("Name="+this.NAME);
System.out.println("Branch="+this.BRANCH);
System.out.println("Phone No="+this.PH);
}
}
public class lab1a 
{
     public static void main(String[]args)
     {
      student ob1=new student("CS112","xxx","CSE","119");
      student ob2=new student("CS132","yyy","CSE","118");
      student ob3=new student("CS098","zzz","CSE","117");
      ob1.display();
      ob2.display();
      ob3.display();
     }

}


OUTPUT : (click on image to zoom)



DAA 1 B - IMPLEMENT STACK USING ARRAYS

1 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 lab1b 
{
final int max=5;
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;
lab1b m = new lab1b();
System.out.println("program to perform stack operations");
Scanner sc=new Scanner(System.in);
while(q!=0)
{
System.out.println("1. push 2.pop 3. display 4.Exit");
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 : (click on image to zoom)


DAA 2 A - TO DISPLAY 3 STAFF OBJECTS OF ALL THREE CATEGORIES

2 A - Design a superclass 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
{
private int StaffId;
private String Name;
private String Phone;
private long Salary;

public Staff(int staffId,String name,String phone,long salary)
{
StaffId = staffId;
Name = name;
Phone = phone;
Salary = salary;
}
public void Display()
{
System.out.print("\t"+StaffId+"\t"+Name+"\t\t"+Phone+"\t\t"+Salary);
}
}

class Teaching extends Staff
{
private String Domain;
private int Publications;

public Teaching(int staffId, String name, String phone,long salary, String domain, int publications)
{
super(staffId, name, phone, salary);
Domain = domain;
Publications = publications;
}
public void Display()
{
super.Display();
System.out.print("\t\t"+Domain+"\t\t"+Publications+"\t\t"+"--"+"\t"+"--");
}
}

class Technical extends Staff
{
private String Skills;
public Technical(int staffId, String name, String phone,long salary, String skills)
{
super(staffId, name, phone, salary);
Skills = skills;
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t"+Skills+"\t"+"--");
}
}

class Contract extends Staff
{
private int Period;
public Contract(int staffId, String name, String phone, long salary, int period)
{
super(staffId, name, phone, salary);
this.Period = period;
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t\t"+"--"+"\t"+Period);
}
}

public class lab2a
{
public static void main(String[] args)
{
Staff staff[]=new Staff[3];
staff[0]=new Teaching(0001,"Narendr","271173",90000,"CSE",3);
staff[1]=new Technical(0002,"Ara","271172",2000,"Server Admin");
staff[2]=new Contract(0003,"Rahul","271174",9000,3);
System.out.println("Staff ID\tName\t\tPhone\t\tSalary\t\tDomain\tPublication\tSkills\t\tPeriod");
for(int i=0;i<3;i++)
{
staff[i].Display();
System.out.println();
}
}

}

OUTPUT : (click on image to zoom)


DAA 02 B - CUSTOMER CLASS (USE OF STRINGTOKENIZER)

2 B - 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
{
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()
{
StringTokenizer st=new StringTokenizer(this.date_of_birth,"/");
System.out.print(this.name+",");
while(st.hasMoreTokens())
{
System.out.print(st.nextToken()+",");
}
}
}

public class lab2b
{
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();
}

}

OUTPUT : (click on image to zoom)


DAA 3 A - COMPUTE A/B

3 A - Write a Java program to read two integers a and b. Compute a/b and print, when b is not zero. Raise an exception when b is equal to zero.

import java.util.Scanner;
public class lab3a 
{
lab3a()
{
int a,b;
float c;
Scanner sc=new Scanner(System.in);
try
{
System.out.println("Enter the value of a");
a=sc.nextInt();
System.out.println("Enter the value of b");
b=sc.nextInt();
sc.close();
if(b==0)
throw new ArithmeticException(" Divide by Zero Error");
c=(float)a/b;
System.out.println("value of a is: "+a);
System.out.println("value of b is: "+b);
System.out.println("a/b is: "+c);
}
catch(ArithmeticException e)
{
System.out.println("ERROR..!!");
e.printStackTrace();
}
}
public static void main(String srgs[])
{
new lab3a();
}

}

OUTPUT : (click on image to zoom)


DAA 3 B - MULTI-THREAD APPLICATION (3 THREADS)

3 B - Write a Java program that implements a multi-thread application that has three 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 "+x*x*x+"\n");
}
}

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 lab3b
{
public static void main(String args[])
{
first a=new first();
a.start();
}

}

OUTPUT : (click on image to zoom)