0% found this document useful (0 votes)
83 views

Class HCF: Class Extends Int Void

The Hcf class extends the Calculate class and overrides its methods to find the highest common factor (HCF) and lowest common multiple (LCM) of two numbers by accepting their input, calculating the HCF using a while loop, and displaying the output including HCF and LCM calculations. It demonstrates inheritance by calling the parent class methods and overriding the display method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Class HCF: Class Extends Int Void

The Hcf class extends the Calculate class and overrides its methods to find the highest common factor (HCF) and lowest common multiple (LCM) of two numbers by accepting their input, calculating the HCF using a while loop, and displaying the output including HCF and LCM calculations. It demonstrates inheritance by calling the parent class methods and overriding the display method.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Class Hcf 1/1

1 // The Subclass
2 class Hcf extends Calculate
3 {
4 int h;
5 void findhcf()
6 {
7 accept(); // Concept of inheritance
8 int a=super.n1;
9 int b=super.n2;
10 if(a<b)
11 {
12 a=b;
13 b=super.n1;
14 }
15 int t;
16 // Calculation of hcf
17 while((a%b)!=0)
18 {
19 t=a%b;
20 a=b;
21 b=t;
22 }
23 h=b;
24 } // end of void findhcf()
25 void show()
26 {
27 System.out.println("n1"+"\t"+"n2"+"\t"+"HCF"+"\t"+"LCM");
28 super.display(); // Method overriding
29 System.out.print("\t"+h);
30 // Calculation of lcm
31 int lcm=(super.n1*super.n2)/h;
32 System.out.println("\t"+lcm);
33 System.out.println();
34 System.out.println();
35 } // end of void show()
36 public static void main(String args[])
37 {
38 Hcf obj=new Hcf();
39 // Carries out the same procedure as in class hcf
40 obj.findhcf();
41 obj.show();
42 } // end of void main()
43 } // end of class Hcf

Jul 27, 2017 5:31:33 PM

You might also like