[PROGRAM 1 BOOK BACK EXERCISE]
import java.util.*;
class d2point
{
double x,y;
d2point()
{
x=0;y=0;
}
d2point(double nx,double ny)
{
x=nx;
y=ny;
}
double distance2d(d2point b)
{
double d=Math.sqrt(Math.pow(b.x-x,2)+Math.pow(b.y-y,2));
return d;
}
}
class d3point extends d2point
{
double z;
Scanner sc=new Scanner(System.in);
d3point()
{
z=0;
}
d3point(double nz,double nx,double ny)
{
super(nx,ny);
z=nz;
}
double distance3d(d3point b)
{
double d=Math.sqrt(Math.pow(b.x-x,2)+Math.pow(b.y-y,2)+Math.pow(b.z-z,2));
return d;
}
public static void main()
{
d3point ob=new d3point(5,7,8);
d3point ob1=new d3point(9,12,13);
double d=ob.distance3d(ob1);
System.out.println(d);
}
}
[PROGRAM 2 BOOK BACK EXERCISE]
class Employee
{
int empn;
double sal;
Employee(int e,double b)
{
empn=e;
sal=b;
}
void salinfo()
{
System.out.println("Employee name::"+empn);
System.out.println("Basic Salary:"+sal);
}
}
class salary extends Employee
{
float da,hra,spl,tsal;
salary(int e ,int sa,float d,float h,float s)
{
super(e,sa);
da=d;
hra=h;
spl=s;
tsal=0.0f;
}
double calculate_sal()
{
tsal=(float)(sal+da+hra+spl);
return tsal;
}
void show_salary()
{
salinfo();
double p=calculate_sal();
System.out.println("Total Salary:"+tsal);
}
public static void main()
{
salary ob=new salary(1234,200000,1000,500,500);
ob.show_salary();
}
}
[PROGRAM 6 BOOK BACK EXERCISE]
import java.util.*;
abstract class Point
{
int x1,x2,y1,y2;
int dis;
Point()
{
x1=0;x2=0;y1=0;y2=0;
}
abstract void readPoint();
abstract void FindDistance();
abstract void show();
}
class Distance extends Point
{
int midx,midy;
void readPoint()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the co ordinates for x and y");
x1=sc.nextInt();
x2=sc.nextInt();
y1=sc.nextInt();
y2=sc.nextInt();
}
void FindDistance()
{
dis=(int)(Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)));
}
void FindMidPoint()
{
midx=(x1+x2)/2;
midy=(y1+y2)/2;
}
void show()
{
System.out.println("Distance:"+dis);
System.out.println("midx:"+midx);
System.out.println("midy:"+midy);
}
public static void main()
{
Distance ob=new Distance();
ob.readPoint();
ob.FindDistance();
ob.FindMidPoint();
ob.show();
}
}
[PROGRAM 8 BOOK BACK EXERCISE]
class slope extends Point
{
int m1,m2;
void findslope()
{
m1=(y2-y1)/(x2-x1);
m2=(y4-y3)/(x4-x3);
}
void display()
{
System.out.println(m1);
System.out.println(m2);
}
void check()
{
if(m1==m2)
System.out.println("parallel");
else(m1*m2==-1)
System.out.println("perpendicular");
}
}
[PROGRAM 9 BOOK BACK EXERCISE]
class Personal
{
String name;
int PAN,basic,accno;
Personal(String n,int p,int b,int acc)
{
name=n;
PAN=p;
basic=b;
accno=acc;
}
void display()
{
System.out.println(name);
System.out.println(PAN);
System.out.println(basic);
System.out.println(accno);
}
}
class Retire extends Personal
{
int yrs;double pf,grat;
Retire(String n,int p,int b,int acc,int y)
{
super(n,p,b,acc);
yrs=y;
pf=0.0;
grat=0.0;
}
void provident()
{
pf=2.0/100*basic*yrs;
}
void gratuity()
{
if(yrs>=10)
grat=12*basic;
else
grat=0;
}
void display()
{
super.display();
System.out.println(pf);
System.out.println(grat);
}
}
[PROGRAM 10 BOOK BACK EXERCISE]
class Author
{
int authorno;
String name;
Author(int no,String n)
{
authorno=no;
name=n;
}
void show()
{
System.out.println(authorno);
System.out.println(name);
}
}
class Booklist extends Author
{
int bookno,edition;
String bookname;
float price;
Booklist(int no,String n,int bno,int e,String bname,float p)
{
super(no,n);
bookno=bno;
edition=e;
bookname=bname;
price=p;
}
void show()
{
super.show();
System.out.println(bookno);
System.out.println(edition);
System.out.println(bookname);
System.out.println(price);
}
public static void main()
{
Booklist ob=new Booklist(23,"JAY",1249,2022,"computer",1200);
ob.show();
}
}
[PROGRAM 11 BOOK BACK EXERCISE]
class Representative
{
String name,address;
Representative()
{
name="";
address="";
}
void read(String n,String add)
{
name=n;
address=add;
}
void display()
{
System.out.println(name);
System.out.println(address);
}
}
class Sales extends Representative
{
int bno,qty;
double pr,sale;
String pname;
void read(String nn,String add,int b,int q,double p,String pn)
{
super.read(nn,add);
bno=b;
qty=q;
pr=p;
pname=pn;
}
void cal()
{
sale=pr*qty;
}
void display()
{
super.display();
System.out.println(bno);
System.out.println(qty);
System.out.println(pr);
System.out.println(sale);
}
}
[PROGRAM 12 BOOK BACK EXERCISE]
import java.util.*;
class Derive extends Base
{
int hc,lc;
void swap()
{
if(n1<n2)
{
int t=n1;
n1=n2;
n2=t;
}
}
void findhcf()
{
while(n2!=0)
{
int r=n1%n2;
n1=n2;
n2=r;
}
hc=n1;
}
void findlcm()
{
lc=(n1*n2)/hc;
}
void display()
{
super.display();
System.out.println(hc);
System.out.println(lc);
}
}
class Base
{
int n1,n2;
void accept()
{
Scanner sc=new Scanner(System.in);
n1=sc.nextInt();
n2=sc.nextInt();
}
void display()
{
System.out.println(n1);
System.out.println(n2);
}
}
[PROGRAM 13 BOOK BACK EXERCISE]
class salary extends Employee
{
float basic;
Salary(int no,String name,String desig)
{
super(no,name,desig);
basic=0.0f;
}
void calculate()
{
int da,hra,salary,pf,netsalary;
da=10.0/100*basic;
hra=15.0/100*basic;
salary=basic+da+hra;
pf=8.0/100*salary;
netsalary=salary-pf;
System.out.println(netsalary);
}
void display()
{
super.display();
calculate();
}
}
[PROGRAM 14 BOOK BACK EXERCISE]
class worker
{
String name;
double basic;
worker(String n,double bas)
{
name=n;
basic=bas;
}
void display()
{
System.out.println("NAME:"+name);
System.out.println("Basic Salary:"+basic);
}
}
class wages extends worker
{
int hrs,rate;double wages;
wages(String n,double bas,int h,int r)
{
super(n,bas);
hrs=h;
rate=r;
wages=0;
}
double calculate()
{
return (hrs*rate);
}
void display()
{
super.display();
wages=calculate()+basic;
System.out.println(wages);
}
}
[PROGRAM 15 BOOK BACK EXERCISE]
class detail
{
String name,address;
int telno,rent;
detail(String n,String a,int t,int r)
{
name=n;
address=a;
telno=t;
rent=r;
}
void show()
{
System.out.println(name);
System.out.println(address);
System.out.println(telno);
System.out.println(rent);
}
}
class Bill extends detail
{
int n;
double amt;
Bill(String nn,String a,int t,int r,int no)
{
super(nn,a,t,r);
n=no;amt=0.0;
}
void cal()+
{
if(n>=1 && n<=100)
amt=rent;
else if(n>100 && n<=200)
amt=(n-100)*0.60+rent;
else if(n>200 && n<=300)
amt=100*0.60+(n-200)*0.80+rent;
else if(n>300)
amt=100*0.60+100*0.80+(n-300)*1+rent;
}
void show()
{
super.show();
System.out.println(n);
System.out.println(amt);
}
public static void main()
{
Bill ob=new Bill("meera","annanagar",632533123,500,200);
ob.cal();
ob.show();
}
}
[PROGRAM 16 BOOK BACK EXERCISE]
A superclass Stock has been defined to store the details of the stock of a retail store. Define a
subclass Purchase to store the details of the items purchased with the new rate and updates the
stock. Some of the members of the classes are given below:
Class name: Stock
Data members/instance variables:
item: to store the name of the item
qt: to store the quantity of an item in stock
rate: to store the unit price of an item
amt: to store the net value of the item in stock
Member functions:
Stock (…): parameterized constructor to assign values to the data members
void display(): to display the stock details
Class name: Purchase
Data members/instance variables:
pqty: to store the purchased quantity
prate: to store the unit price of the purchased item
Member functions/ methods:
Purchase(…): parameterized constructor to assign values to the data members of both classes
void update (): to update stock by adding the previous quantity by the purchased quantity and
replace the rate of the item if there is a difference in the purchase rate. Also, update the current
stock value as (quantity * unit price)
void display(): to display the stock details before and after updation.
Specify the class Stock, giving details of the constructor() and void display(). Using the concept of
inheritance, specify the class Purchase, giving details of the constructor(), void update() and void
display().
The main function and algorithm need not be written.
Answer:
class Stock
{
String item; doubleqty,rate,amt;
Stock(String a, double b, double c)
{
item=a;
qty=b;
rate=c;
amt=qty * rate;
}
void display()
{
System.out.println("Name of the item: "+item);
System.out.println("Quantity: "+qty);
System.out.println("Rate per unit: "+rate);
System.out.println("Net value: "+amt);
}
}
class Purchase extends Stock
{
int pqty;
double prate;
Purchase(String a, double b, double c, int d, double e)
{
super(a, b, c);
pqty=d;
prate=e;
}
void update()
{
qty += pqty;
if(prate!=rate)
rate=prate;
amt = qty * rate;
}
void display()
{
super.display();
update();
super.display();
}
}
[PROGRAM 17 BOOK BACK EXERCISE]
import java.util.Scanner;
public class Account extends Bank
{
static Scanner sc=new Scanner(System.in);
double amt;
Account(String n, String a, double pp)
{
super(n,a,pp);
amt=0.0;
}
void deposit()
{
System.out.println("Enter amount");
amt=sc.nextDouble();
p=p-amt;
}
void withdraw()
{
System.out.println("Enter amount");
amt=sc.nextDouble();
if(amt>p)
System.out.println("INSUFFICIENT BALANCE");
else
{
p=p-amt;
if(p<500)
p=p-(500-p)/10;
}
}
void display()
{
super.display();
}
}
[PROGRAM 18 BOOK BACK EXERCISE ISC 2018]
class Number
{
int n;
Number(int nn)
{
n=nn;
}
int factorial(int a)
{
int f=1;
for(int i=1;i<=n;i++)
f=f*i;
return f;
}
void display()
{
System.out.println(n);
}
}
class Series extends Number
{
long sum;
Series(int nn)
{
super(nn);
sum=0;
}
void calsum()
{
for(int i=1;i<=n;i++)
sum=sum+factorial(i);
}
void display()
{
super.display();
System.out.println("sum of the series="+sum);
}
}
[PROGRAM 19 BOOK BACK EXERCISE]
An interface Data is defined with a data member and a method volume( ) which returns the
volume of the implementing shape. A super class Base has been defined to contain the radius of a
geometrical shape. Define a sub class CalVol which uses the properties of the interface Data and
the class Base and calculates the volume of a cylinder.
The details of the members of the interface and both the classes are given below:
Interface name : Data
Data member:
double pi : initialize pi = 3.142
Member functions/methods:
double volume( ) :
Class name: Base
Data member/instance variable:
rad : to store the radius in decimal
Member functions/methods:
Base(…) : parameterized constructor to initialize
the data member
void show( ) : displays the radius with an appropriate
message
Class name: CalVol
Data member/instance variable:
ht : to store the height in decimal
Member functions/methods:
CalVol(…) : parameterized constructor to initialize
the data members of both the classes
double volume( ) : calculates the volume of a sphere by
using the formula ( pi x radius2 x height )
void show( ) : displays the data members of both the
classes and the volume of the sphere with
appropriate message
Assume that the interface Data and the super class Base has been defined. Using the concept of
inheritance, specify the class CalVol giving the details of the constructor(…), double volume( )
and void show( ).
The interface, super class, main function, and algorithm need NOT be written
abstract class baseclass
{
double rad;
int n=900;
baseclass(double r)
{
rad=r;
}
void show()
{
System.out.println("Radius:"+rad);
}
}
interface data
{
double pi=3.14;// by default final and static
double volume();//by abstract and public
}
class CalVol extends baseclass implements data
{
int ht;int n;
CalVol(int r,int h)
{
super(r);
ht=h;
n=500;
}
public double volume()
{
double vol=pi*ht*rad*rad;
return vol;
}
void show()
{
System.out.println("Height:"+ht);
System.out.println("super.n"+super.n);
double t=volume();
System.out.println("Volume:"+t);
super.show();
}
public static void main()
{
CalVol ob=new CalVol(4,5);
ob.show();
}
}
EXTRA PROGRAMS
PROGRAM 1:
class salesman
{
String name,address;
salesman()
{
name="";address="";
}
void readdata(String n,String ad)
{
name=n;
address=ad;
}
void show()
{
System.out.println(name);
System.out.println(address);
}
}
class sales extends salesman
{
int billno,qty;
double price,psales;
String pname;
void readdetails(int b,int q,double p,double s,String pr,String na,String add)
{
readdata(na,add);
billno=b;qty=q;price=p;psales=s;pname=pr;
}
double calculate()
{
double val=price*qty+psales;
return val;
}
void show()
{
super.show();
System.out.println(billno);
System.out.println(price);
System.out.println(qty);
System.out.println(psales);
System.out.println(calculate());
}
public static void main()
{
sales ob=new sales();
ob.readdetails(123,23,455,10000,"pen","meera","annanagar");
ob.show();
}
}
PROGRAM 2:
A super class Product has been defined to store the details of a product sold by a wholesaler to a
retailer. Define a subclass Sales to compute the total amount paid by the retailer with or without
fine along with service tax. Some of the members of both classes are given below:
Class name: Product
Data members/instance variables:
name: stores the name of the product
code: integer to store the product code
amount: stores the total sale amount of the product (in decimals)
Member functions/methods:
Product (String n, int c, double p): parameterized constructor to assign data members: name = n,
code = c and amount = p
void show(): displays the details of the data members
Class name: Sales
Data members/instance variables:
day: stores number of days taken to pay the sale amount
tax: to store the service tax (in decimals)
totamt: to store the total amount (in decimals)
Member functions/methods:
Sales(….): parameterized constructor to assign values to data members of both the classes
void compute(): calculates the service tax @ 12.4% of the actual sale amount
calculates the fine @ 2.5% of the actual sale amount only if the amount paid by the retailer to the
wholesaler exceeds 30 days calculates the total amount paid by the retailer as (actual sale amount
+ service tax + fine)
void show (): displays the data members of the superclass and the total amount
Assume that the superclass Product has been defined. Using the concept of inheritance, specify the
class Sales giving the details of the constructor (…), void compute() ) and void show(). The
superclass, main function and algorithm need NOT be written.
Answer:
import java.io.*;
class Sales extends Product
{
int day;
double tax;
double totamt;
double fine = 0.0;
Sales(String n,int c,double p, int d)
{
super(n, c, p);
day = d;
}
void compute()
{
if(day < 30)
{
tax = 12.4 * amount /100;
totamt = amount + tax;
}
if(day > 30)
{
tax= 12.4 * amount /100;
fine = 2.5 * amount /100;
totamt = amount + tax + fine;
}
}
void show ()
{
super.show();
System.out.println("Total amount to be paid::"+ totamt);
}
public static void main()
{
Sales ob=new Sales("pen",123,1000.0,20);
ob.compute();
ob.show();
}
}
class Product
{
String name;
int code;
double amount;
Product(String n, int c, double p)
{
name = n;
code = c;
amount = p;
}
void show()
{
System.out.println("Name is :"+ name);
System.out.println("Code is :" + code);
System.out.println("Total Sale Amount:" + amount);
}
}
LIBRARY PROGRAM
class compute extends library
{
int D,F;
compute(String n,String A,int price,int days)
{
super(n,A,price);
D=days;
F=0;
}
void find()
{
double charge=2.0/100*p;
amt=D*charge;
if(D<=7)
F=0;
else if(D>7)
{
int days=D-7;
if(days>=1 && days<=5)
F=2*days;
if(days>5 && days<=10)
F=3*days;
if(days>10)
F=4*days;
}
System.out.println(amt+F);
}
void display()
{
System.out.println(D);
System.out.println(F);
find();
}
}