Professional Documents
Culture Documents
INHERITANCE
INHERITANCE
I) ALGORITHM
1. START
2. DECLARE instance variables:
1. variable name[] of String array
2. variable rnk[] of Integer array
3. BEGIN Constructor Record()
4. initialise name <- new String[10]
5. initialise rnk <- new int[10]
END Constructor
6. BEGIN Method readvalues()
7. DECLARE object sc of Scanner Class
8. Println "Input The Names and Corresponding Ranks"
9. FOR int i<- 0 to 5, STEP 1
10. Print "Enter name of student no."+(i+1)+" :- "
11. READ/INPUT name[i]
12. Print "Enter his/her rank :- "
13. READ/INPUT rnk[i]
END FOR
END Method
14. BEGIN Method display()
15. Println "Name Rank"
16. FOR int i<-0 to 5, STEP 1
17. Println name[i]+" "+rnk[i]
END FOR
END Method
18. DECLARE Class Rank which is subclass to class Record
19. DECLARE instance variable index of Integer data type.
20. BEGIN Constructor Rank()
21. Call the constructor of the class Record using super()
22. Initialise index <- 0
END Constructor
23. BEGIN Method highest () of void return type.
24. DECLARE variables m, i of the integer type.
25. FOR i<- 0 to 5, STEP 1
26. m<- rnk [ index ]
27. IF rnk [ i ]> m
28. index <- i
END FOR
END Method
29. BEGIN Method display()
30. Call the method display() of the super class using super.method()
31. Println "First rank = "+rnk[index]
32. Println "Student with the First rank = "+name[index]
END Method
33. BEGIN Method main(String args[])
34. DECLARE object ob of Rank Class
35. Call by reference readvalues()
36. Call by reference highest()
37. Call by reference display()
END Method
II) PROGRAM
import java.util.*;
public class Record
{
String name[];
int rnk[];
Record()
{
name = new String[10];
rnk = new int[10];
}
void readvalues()
{
Scanner sc = new Scanner(System.in);
System.out.println("Input The Names and Corresponding Ranks");
for(int i=0;i<5;i++)
{
System.out.print("Enter name of student no."+(i+1)+" :-
");
name[i] = sc.nextLine();
System.out.print("Enter his/her rank :- ");
rnk[i] = sc.nextInt();
}
}
void display()
{
System.out.println("Name Rank");
for(int i=0;i<5;i++)
{
System.out.println(name[i]+"
"+rnk[i] );
}
}
}
import java.util.*;
public class Rank extends Record
{
int index;
Rank()
{
super();
index=0;
}
void highest()
{
int m,i;
for(i=0;i<5;i++)
{
m=rnk[index];
if(rnk[i]>m)
{
index=i;
}
}
}
void display()
{
super.display();
System.out.println("First rank = "+rnk[index]);
System.out.println("Student with the First rank =
"+name[index]);
}
I) ALGORITHM
1. START
2. DECLARE instance variables:
1. Variable name of String data type.
2. Variable address of String data type.
3. Variable telno of String data type.
4. Variable rent of double data type.
3. BEGIN Constructor Detail with parameters: String n, String a,
String t, double r.
4. Initialise name <- n
5. Initialise address <- a
6. Initialise telno <- t
7. Initialise rent <- r
END Constructor
8. BEGIN Method show() of void return type.
9. Println “The name, address, telephone no. of the customer are: ”
10. Println name
11. Println address
12. Println telno
END Method.
import java.util.*;
public class Detail
{
String name;
String address, telno;
double rent;
Detail(String n, String a, String t,double r)
{
name=n;
address=a;
telno=t;
rent=r;
}
void show()
{
System.out.println("The name, address, telephone no. of the
customer are:");
System.out.println(name);
System.out.println(address);
System.out.println(telno);
}
}
import java.util.*;
class Bill extends Detail
{
int n;
double amt;
Bill(String nn,String aa,String tt,double r, int n1)
{
super(nn,aa,tt,r);
n=n1;
amt=0.0;
}
void show()
{
super.show();
System.out.println("The number of calls: "+n);
}
void cal()
{
if(n<=100)
amt+=rent;
else if(n>100&&n<=200)
amt=(n-100)*0.6+rent;
else if(n>200&&n<=300)
amt=(n-200)*0.8+(60)+rent;
else
amt=(n-300)+(140)+rent;
System.out.println("The total Bill: "+amt);
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the name, address, telephone no. of
the customer, the number of calls done by the user and the rental
charges");
String nme=sc.nextLine();
String add=sc.nextLine();
String tele=sc.nextLine();
int n2=sc.nextInt();
double rnt=sc.nextDouble();
Bill ob=new Bill(nme,add,tele,rnt,n2);
ob.show();
ob.cal();
}
}
III) OUTPUT