Академический Документы
Профессиональный Документы
Культура Документы
N.RACHANA
18MIS0042
CODE:
import java.util.*;
class Person
{
String name,gender;
int age;
}
class Student extends Person
{
float mark1,mark2,mark3,avg;
public void set(String n,String g,int a,float m1,float m2,float
m3)
{
name=n;
gender=g;
age=a;
mark1=(float)m1;
mark2=(float)m2;
mark3=(float)m3;
}
public void get()
{
avg=(mark1+mark2+mark3)/3;
System.out.println(name+"\n"+gender+"\n"+age+"\n"+avg);
}
}
class Teacher extends Person
{
float salary;
public void set(String n,String g,int a,float sal)
{
name=n;
gender=g;
age=a;
salary=sal;
}
public void get()
{
System.out.println(name+"\n"+gender+"\n"+age+"\n"+salary);
}
}
public class test1
{
public static void main(String[] args)
{
Student o1=new Student();
Student o2=new Student();
Teacher o3=new Teacher();
o1.set("Rachana","Female",18,79,91,85);
o2.set("Anil","Male",17,79,84,89);
o3.set("Kiran","Male",19,100);
System.out.println("Details of first student");
System.out.println();
o1.get();
System.out.println();
System.out.println("Details of second student");
System.out.println();
o2.get();
System.out.println();
System.out.println("Details of Teacher");
System.out.println();
o3.get();
}
}
OUTPUT:
3.Write an abstract class with an abstract method double Process
(double P, double R). Create a subclass Discount and implement the
Process() method with the following formula: net=P-P*R/100. Return
the net value. Create another subclass Tax and implement the
Process() method with the following formula: total=P+P*R/100.
Return the total. Create necessary objects and give the required
input and shows the output.
CODE:
import java.util.*;
abstract class Net
{
double p;
double r;
abstract void Process(double P,double R);
}
class Discount extends Net
{
public void Process(double a,double b)
{
p=a;
r=b;
}
public double getdata()
{
double net=p-p*r/100;
return net;
}
}
class Tax extends Discount
{
public double getdata()
{
double total=p+p*r/100;
return total;
}
}
public class abstractdemo
{
public static void main(String[] args)
{
Discount o1=new Discount();
Tax o2=new Tax();
o1.Process(5,6);
o2.Process(5,6);
System.out.println("Net="+o1.getdata());
System.out.println("Total="+o2.getdata());
}
}
OUTPUT:
4.Write a program to calculate the grade of a student. Enter marks
for minimum 5 subjects. While entering marks if the mark is
negative throw NegativeMarkException and if the mark is greater
than 100 throw OutofRangeException.
CODE:
import java.util.*;
import java.io.*;
import java.lang.Exception;
class MarksOutOfBounds extends Exception
{
public MarksOutOfBounds(String s)
{
super(s);
}
}
public class Exceptiontest
{
public static void main(String[] args)
{
Scanner inp=new Scanner(System.in);
int[] arr=new int[5];
int c=0;
for(int i=0;i<5;i++)
{
arr[i]=inp.nextInt();
try
{
if(arr[i]<0 || arr[i]>100)
{
c++;
throw new MarksOutOfBounds("Out Of Bounds");
}
}
catch(MarksOutOfBounds e)
{
System.out.println(e.getMessage());
break;
}
}
if(c==0)
{
int s=0;
for(int i=0;i<5;i++)
{
if(arr[i]>0 && arr[i]<100)
{
s=s+arr[i];
}
}
s=s/5;
System.out.println(s);
}
}
}
OUTPUT:
5. Implement the Producer-Consumer problem using threads. Synchronize both the threads so that
the consumer consumes only after the producer produces the second product, and the producer
produces the new value only if the consumer consumes the previous second product.
import java.util.LinkedList;
t1.start();
t2.start();
t1.join();
t2.join();
}
while (list.size()==capacity)
wait();
System.out.println("Producer produced-"
+
value);
list.add(value++);
notify();
Thread.sleep(1000);
}
}
}
while (list.size()==0)
wait();
System.out.println("Consumer consumed-"
+
val);
notify();
Thread.sleep(1000);
}
}
}
}
}