Sie sind auf Seite 1von 11

1a.

package daa_1a;
public class Student
{
private String USN;
private String Name;
private String Branch;
private String Phno;
public Student (String USN,String Name, String Branch,String Phno)
{
super();
this.USN=USN;
this.Name=Name;
this.Branch=Branch;
this.Phno=Phno;
}
public String getusn()
{
return USN;
}
public void setusn(String USN)
{
this.USN=USN;
}
public String getname()
{
return Name;
}
public void setname(String Name)
{
this.Name=Name;
}
public String getbranch()
{
return Branch;
}
public void setbranch(String Branch)
{
this.Branch=Branch;
}
public String getphno()
{
return Phno;
}
public void setPhno(String Phno)
{
this.Phno=Phno;
}
public String toString()
{
return(USN+"\t"+Name+"\t\t"+Branch+"\t\t"+Phno);
}
}

package daa_1a;
import java.util.Scanner;
public class StudentDetails {
public static void main(String args[])
{
int i;
String USN;
String Name;
String Branch;
String Phno;
Scanner input=new Scanner(System.in);
Student[] st=new Student[10];
System.out.println("Enter no of students:");
int n=input.nextInt();
System.out.println("Enter"+n+"student details");
for(i=0;i<n;i++)
{
System.out.println("enter student"+(i+1)+"details");
System.out.println("Enter students 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 number");
Phno=input.next();
st[i]=new Student(USN,Name,Branch,Phno);
}
System.out.println("USN\t\tname\t\tbranch\t\tphno.");
for(i=0;i<n;i++)
System.out.println(st[i]);
}
}

==============================================
OUTPUT
===============================================

Enter no of students:
3
Enter3student details
enter student1details
Enter students usn
1JS15CS003
Enter student name
Tyrian
Enter student branch
CSE
Enter student phone number
7845874589
enter student2details
Enter students usn
1JS15CS004
Enter student name
Sansa
Enter student branch
CSE
Enter student phone number
4584584586
enter student3details
Enter students usn
1JS15CS005
Enter student name
Jofrey
Enter student branch
CSE
Enter student phone number
8585854745
USN name branch phno.
1JS15CS003 Tyrian CSE 7845874589
1JS15CS004 Sansa CSE 4584584586
1JS15CS005 Jofrey CSE 8585854745
1b.
package daa_1b;
import java.util.*;
public class Stack {
Scanner keyboard=new Scanner(System.in);
int top=-1;
int stack[]=new int[10];
int max=5;
public void push()
{
int item;
if(top==(max-1))
{
System.out.println("\n Stack overflow");
}
else
{
System.out.println("Enter the element");
item=keyboard.nextInt();
top=top+1;
stack[top]=item;
}
}
void pop()
{
int item;
if(top==-1)
{
System.out.println("\n Stack underflow");
}
else
{
item=stack[top];
top=top-1;
System.out.println("The poped element is:\t"+item);
}
}
void display()
{
int i;
if(top==-1)
{
System.out.println("stack is empty");
}
else
{
System.out.println("Stack elements are");
for(i=top;i>=0;i--)
{
System.out.println(stack[i]);
}
}
}

public static void main(String args[])


{
Stack si=new Stack();
int choice=0;
Scanner keyboard=new Scanner(System.in);
for(;;)
{
System.out.println("\nstack operation");
System.out.println("1.push");
System.out.println("2.pop");
System.out.println("3.display");
System.out.println("4.EXIT");
System.out.println("ENTER YOUR CHOICE");
choice=keyboard.nextInt();
switch(choice)
{
case 1:si.push();
break;
case 2:si.pop();
break;
case 3:si.display();
break;
case 4:System.exit(0);
break;
default:System.out.println("invalid choice");
break;
}
}
}
}

==============================================
OUTPUT
==============================================

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
1
Enter the element
3

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
1
Enter the element
4

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
1
Enter the element
5

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
3
Stack elements are
5
4
3

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
2
The poped element is: 5

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
3
Stack elements are
4
3

stack operation
1.push
2.pop
3.display
4.EXIT
ENTER YOUR CHOICE
4
2a.

package daa_2a;
public 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(StaffId+"\t"+Name+"\t"+Phone+"\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"+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"+"\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);

}
}

package daa_2a;
public class P_2a
{
public static void main(String[] args)
{
Staff S1[]=new Staff[3];
S1[0]=new Teaching(1098,"John","987654321",90000,"Networks",5);
S1[1]=new Technical(2675,"Fred","987654351",20000,"admin");
S1[2]=new Contract(1098,"Ben","987654361",9000,3);
System.out.println("StaffId\tName\tPhone\t\tSalary\tDomain\t\tPublications\t\tSkills\tPeriods");
for(int i=0;i<3;i++)
{
S1[i].Display();
System.out.println();
}
}
}

==============================================
OUTPUT
==============================================
StaffId Name Phone Salary Domain Publications Skills Periods
1098 John 987654321 90000 Networks 5 -- --
2675 Fred 987654351 20000 -- -- admin --
1098 Ben 987654361 9000 -- -- -- 3
2b.

package daa_2b;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Customer


{
private String name;
private String dob;
public Customer(String name,String dob)
{
super();
this.name=name;
this.dob=dob;
}
public Customer(){}
public void readData(String name,String dob)
{
this.name=name;
this.dob=dob;
}
public void displayData(){
StringTokenizer st=new StringTokenizer(this.dob,"/");
System.out.print(this.name);
while(st.hasMoreTokens())
{
System.out.print(","+st.nextToken());

}
}

package daa_2b;
import java.util.Scanner;
public class Prog_2b
{
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 cus=new Customer();
cus.readData(name,date);
cus.displayData();
}

==============================================
OUTPUT
==============================================

enter name:_
Hailee Steinfield
enter date of birth:
10/10/1996
Hailee Steinfield,10,10,1996
3a.

package daa_3a;
import java.util.Scanner;

public class Prog_3a {


public static void main(String[] args)
{
int a,b;
double c=0.0;
Scanner input = new Scanner(System.in);
System.out.println("Enter 1st number");
a=input.nextInt();
System.out.println("Enter 2nd number");
b=input.nextInt();
try
{
if(b!=0)
{
c=(float)1.0*a/b;
System.out.println("Result "+c);
}
else
throw new ArithmeticException();
}
catch(ArithmeticException e)
{
System.out.println("Denominator is 0!,Division by zero not possible");
}
}

}
==============================================
OUTPUT
==============================================
output:1
Enter 1st number
5
Enter 2nd number
2
Result 2.5
output:2
Enter 1st number
3
Enter 2nd number
0
Denominator is 0!,Division by zero not possible
3b.

package daa_3b;
public class Square implements Runnable
{
public int x;
public Square(int x)
{
this.x=x;
}
public void run()
{
System.out.println("From 2nd thread square "+x+" is "+x*x);
}
}

package daa_3b;
public class Cube implements Runnable
{
public int x;
public Cube(int x)
{
this.x=x;
}
public void run()
{
System.out.println("From 3rd thread cube "+x+" is "+x*x*x);
}

package daa_3b;
import java.util.*;
public class ThreadRandom extends Thread
{
public void run()
{
int n=0;
Random r=new Random();
try
{
for(int i=0;i<2;i++)
{
n=r.nextInt(100);
System.out.println("Main thread started & Generated number is "+n);
Thread t2=new Thread(new Square(n));
t2.start();
Thread t3=new Thread(new Cube(n));
t3.start();
Thread.sleep(1000);
System.out.println("------------------");
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}

package daa_3b;
import java.util.*;
public class MultiThread
{
public static void main(String args[])
{
ThreadRandom ft=new ThreadRandom();
Thread t1=new Thread(ft);
t1.start();
}

==============================================
OUTPUT
==============================================

Main thread started & Generated number is 60


From 2nd thread square 60 is 3600
From 3rd thread cube 60 is 216000
------------------
Main thread started & Generated number is 59
From 2nd thread square 59 is 3481
From 3rd thread cube 59 is 205379
------------------

Das könnte Ihnen auch gefallen