Вы находитесь на странице: 1из 12

2d.

import java.io.*;
public class Division
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter the First Number: ");


int a=Integer.parseInt(br.readLine());
System.out.print("Enter the Second Number: ");
int b=Integer.parseInt(br.readLine());
int c;
try
{
c=a/b;
System.out.println("Quotient is: "+c);
}
catch(ArithmeticException e)
{
//System.out.println(e.getMessage());
System.out.println("Division By Zero");
}
}
}
4.
import java.io.*;

class TwoVehicles
{
static String t1;
static String t2;

public static void main(String args[])throws IOException


{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
t1="";
t2="";

System.out.print("Enter the direction of First Vehicle: ");


t1=br.readLine();
System.out.print("Enter the direction of Second Vehicle: ");
t2=br.readLine();

try
{
if(!t1.equals(t2))
throw new Collision("Collision About to Happen!!");
else
System.out.println("No Collision!!");
}
catch(Collision c)
{
System.out.println(c.getMessage());
t2=t1;
}
finally
{
System.out.println("Direction of Vehicle 1 is: "+t1);
System.out.println("Direction of Vehicle 2 is: "+t2);
}
}
}

class Collision extends Exception


{
Collision(String Ex)
{
super(Ex);
}
}
7.
import java.io.*;
import java.util.*;
public class Employee
{
String name;
String id;
String salary;

Employee(String n,String i,String s)


{
name=n;
id=i;
salary=s;
}

public String toString()


{
String s="";
s=s+"Name: "+name+"\t ID: "+id+"\t Salary: "+salary+".\n";
return s;
}
}

class List_Collection
{
public static void main()throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String name;
String id;
String salary;
String temp="";

ArrayList<String> arr = new ArrayList<>();

System.out.print("Enter a Name: ");


name=br.readLine();
System.out.print("Enter the ID: ");
id=br.readLine();
System.out.print("Enter the Salary: ");
salary=br.readLine();

System.out.print("Enter temp: ");


temp=br.readLine();

List<String> tempCopies = Collections.nCopies(5,temp);


Employee emp=new Employee(name,id,salary);
List<Employee> empCopies = Collections.nCopies(5,emp);

System.out.println("Copies of Employee:-");
Iterator itr=empCopies.iterator();
while (itr.hasNext())
System.out.println(itr.next());

System.out.println("Copies of Temp:");
itr = tempCopies.iterator();
while (itr.hasNext())
System.out.println(itr.next());
}
}
5a.public class Thread_Main
{
public static void main()throws InterruptedException
{
Thread t1,t2,t3,t4,t5;
t1=new Thread("Thread 1");
t2=new Thread("Thread 2");
t3=new Thread("Thread 3");
t4=new Thread("Thread 4");
t5=new Thread("Thread 5");

t1.setPriority(10);
t2.setPriority(4);
t3.setPriority(6);
t4.setPriority(8);
t5.setPriority(2);

t1.start();
t1.sleep(10);
if(t1.isAlive())
{
System.out.println("Thread is Alive");
}
else
{
System.out.println("Thread is Not Alive");
}

t2.start();
//t2.sleep(10);
if(t2.isAlive())
{
System.out.println("Thread is Alive");
}
else
{
System.out.println("Thread is Not Alive");
}

t3.start();
t3.sleep(3000);
if(t3.isAlive())
{
System.out.println("Thread is Alive");
}
else
{
System.out.println("Thread is Not Alive");
}

t4.start();

if(t4.isAlive())
{
System.out.println("Thread is Alive");
}
else
{
System.out.println("Thread is Not Alive");
}

t5.start();

if(t5.isAlive())
{
System.out.println("Thread is Alive");
}
else
{
System.out.println("Thread is Not Alive");
}
}
}
6.
import java.io.*;

public class ReverseWord


{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str="", revstr="";
System.out.print("Enter a Sentence: ");
str=br.readLine();

str.trim();
str=str+" ";
revstr=reverseWordByWord(str);
System.out.println("The Sentence in Reversed Words is: "+revstr);
}
public static String reverseWordByWord(String s)
{
int strleng=s.length();
String reverse="", temp="";
int i,j;
char ch;

for(i=0;i<strleng;i++)
{
ch=s.charAt(i);
if(ch!=' ')
{
temp=temp+ch;
}
else
{
for(j=temp.length()-1;j>=0;j--)
{
reverse=reverse+temp.charAt(j);
}
temp=" ";
reverse=reverse+" ";
}
}
return reverse.trim();
}
}
5b.
import java.util.*;

public class Threadexample


{
public static void main()throws InterruptedException
{
// Object of a class that has both produce()
// and consume() methods
final PC pc = new PC();

// Create producer thread


Thread t1 = new Thread(new Runnable()
{
//@Override
public void run()
{
try
{
pc.produce();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});

// Create consumer thread


Thread t2 = new Thread(new Runnable()
{
//@Override
public void run()
{
try
{
pc.consume();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
// Start both threads
t1.start();
t2.start();

// t1 finishes before t2
t1.join();
t2.join();
}

// This class has a list, producer (adds items to list


// and consumber (removes items).
public static class PC
{
// Create a list shared by producer and consumer
// Size of list is 2.
LinkedList<Integer> list = new LinkedList<>();
int capacity = 2;

// Function called by producer thread


public void produce() throws InterruptedException
{
int value = 0;
while (true)
{
synchronized (this)
{
// producer thread waits while list
// is full
while (list.size()==capacity)
wait();

System.out.println("Producer produced-"
+ value);

// to insert the jobs in the list


list.add(value++);

// notifies the consumer thread that


// now it can start consuming
notify();

// makes the working of program easier


// to understand
Thread.sleep(1000);
}
}
}

// Function called by consumer thread


public void consume() throws InterruptedException
{
while (true)
{
synchronized (this)
{
// consumer thread waits while list
// is empty
while (list.size()==0)
wait();
//to retrive the ifrst job in the list
int val = list.removeFirst();

System.out.println("Consumer consumed-"
+ val);

// Wake up producer thread


notify();

// and sleep
Thread.sleep(1000);
}
}
}
}
}
2a.
import java.io.*;
class Player
{
String name;
int age;

Player()
{
name="";
age=0;
}
Player(String nm,int a)
{
name=nm;
age=a;
}

public void show()


{
System.out.println("Name = "+name);
System.out.println("Age = "+age);
}
}

class Cricket_Player extends Player


{
String type;

Cricket_Player(String n,int a,String t)


{
super(n,a);
type=t;
}
public void show()
{
System.out.println("The Cricket Player Details are:- ");
super.show();
System.out.println("Type = "+type);
System.out.println();
}
}
class Football_Player extends Player
{
String type;

Football_Player(String n,int a,String t)


{
super(n,a);
type=t;
}
public void show()
{
System.out.println("The Football Player Details are:- ");
super.show();
System.out.println("Type = "+type);
System.out.println();
}
}

class Hokey_Player extends Player


{
String type;

Hokey_Player(String n,int a,String t)


{
super(n,a);
type=t;
}
public void show()
{
System.out.println("The Hokey Player Details are:- ");
super.show();
System.out.println("Type = "+type);
System.out.println();
}
}

class Player_Main
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the details of Cricket Player: ");


System.out.print("Name = ");
String nc=br.readLine();
System.out.print("Age = ");
int ac=Integer.parseInt(br.readLine());
System.out.print("Type = ");
String tc=br.readLine();
Cricket_Player cp=new Cricket_Player(nc,ac,tc);
System.out.println();

System.out.println("Enter the details of Football Player: ");


System.out.print("Name = ");
String nf=br.readLine();
System.out.print("Age = ");
int af=Integer.parseInt(br.readLine());
System.out.print("Type = ");
String tf=br.readLine();
Football_Player fp=new Football_Player(nf,af,tf);
System.out.println();

System.out.println("Enter the details of Hokey Player: ");


System.out.print("Name = ");
String nh=br.readLine();
System.out.print("Age = ");
int ah=Integer.parseInt(br.readLine());
System.out.print("Type = ");
String th=br.readLine();
Hokey_Player hp=new Hokey_Player(nh,ah,th);
System.out.println();

cp.show();
fp.show();
hp.show();
}
}
3b.
import java.io.*;
interface Stackop
{
void push(int item);
int pop();
}
class Dynstack implements Stackop
{
int tos;
int stk[];
int size;

Dynstack(int s)
{
tos=-1;
size=s;
stk=new int[size];
}

public void push(int item)


{
if(tos==size)
System.out.println("Stack Overflow!!");
else
stk[++tos]=item;
}
public int pop()
{
int v;
if(tos==-1)
{
System.out.println("Stack Underflow!!");
return -9999;
}
else
{
v=stk[tos--];
return v;
}
}
}

class Dynamic_Stack
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int size,i,n;
System.out.println("**************STACK 1*****************");
System.out.print("Enter the Number of Elements: ");
size=Integer.parseInt(br.readLine());

Dynstack ds=new Dynstack(size);

System.out.println("Enter the Stack:- ");


for(i=0;i<size;i++)
{
n=Integer.parseInt(br.readLine());
ds.push(n);
}

System.out.println("The Stack is:-");


for(i=0;i<size;i++)
{
n=ds.pop();
if(n!=-999)
System.out.println(n+" ");
else
break;
}
System.out.println("\n");

System.out.println("**************STACK 2*****************");
System.out.print("Enter the Number of Elements: ");
size=Integer.parseInt(br.readLine());

Stackop mystk;
Dynstack dyn=new Dynstack(size);
mystk=dyn;

System.out.println("Enter the Stack:- ");


for(i=0;i<size;i++)
{
n=Integer.parseInt(br.readLine());
mystk.push(n);
}

System.out.println("The Stack is:-");


for(i=0;i<size;i++)
{
n=mystk.pop();
if(n!=-999)
System.out.println(n+" ");
else
break;
}
System.out.println("\n");
}
}
2b
import java.io.*;
class Call
{
float dur;
String type;

float rate()
{
if(type.equalsIgnoreCase("urgent"))
return 4.5f;
else if(type.equalsIgnoreCase("lightning"))
return 3.5f;
else
return 3f;
}
}

class Bill extends Call


{
float amount;

void read()throws IOException


{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the Type of Call: ");
super.type=br.readLine();
System.out.print("Enter the Duration of call: ");
super.dur=Float.parseFloat(br.readLine());
}

void calculate()
{
if(super.dur<1.5)
amount=super.rate()*super.dur+1.5f;
else if(super.dur<3)
amount=super.rate()*super.dur+2.5f;
else if(super.dur<5)
amount=super.rate()*super.dur+4.5f;
else
amount=super.rate()*super.dur+5f;
}

void print()
{
System.out.println("The Call Details are:-");
System.out.println("Type: "+super.type);
System.out.println("Duration: "+super.dur);
System.out.println("Amount: "+amount);
}
}

class Trunk_Calls
{
public static void main(String args[])throws IOException
{
Bill bl=new Bill();
bl.read();
bl.calculate();
bl.print();
}
}

Вам также может понравиться