Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

INHERITANCE

1] Student record and Rank class

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]);
}

public static void main(String args[])


{
Rank ob = new Rank();
ob.readvalues();
ob.highest();
ob.display();
}
}
III) OUTPUT
2] Calculating and printing Details for a telephone Bill

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.

13. DECLARE class Bill, which is a subclass to Detail.


14. DECLARE instance variables:
1. Variable n of integer type
2. Variable amt of double type
15. DECLARE Constructor Bill with the parameters: String aa, String
tt, double r, int n1
16. Initialise the instance variables of the subclass to nn,aa,tt,r
respectively using the super() function.
17. Initialize variable n <- n1
18. Initialize amt<- 0.0
END Constructor
19. BEGIN Method show() of void return type
20. Call the method show() of the super class using super.method()
function.
21. Println “The number of calls: ”+n
END Method.
22. BEGIN Method cal() of void return type.
23. IF n<=100
24. amt<- amt + rent
25. END IF
26. ELSE IF n >100 & n<=200
27. amt <- (n-100) x 0.6 + rent
28. END ELSE IF
29. ELSE IF n>200 & n<=300
30. amt<- (n-200) x 0.8 +(60)+rent
31. END ELSE IF
32. ELSE
33. amt <- (n-300) + 140 + rent
34. END ELSE
35. Println "The total Bill: "+amt
END Method
36. BEGIN Method main(String args[])
37. DECLARE object sc of Scanner Class
38. Println"Enter the name, address, telephone no. of the customer,
the number of calls done by the user and the rental charges"
39. READ/INPUT nme of String type.
40. READ/INPUT add of String type.
41. READ/INPUT tele of String type.
42. READ/INPUT n2 of integer type.
43. READ/INPUT rnt of double type.
44. DECLARE object ob of Bill class with parameters : nme, add, tele,
rnt, n2.
45. Call by reference show()
46. Call by reference cal().
END Main Method.
II) PROGRAM

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

You might also like