Note that there are some explanatory texts on larger screens.

plurals
  1. POinheritance example for mean and median
    text
    copied!<p>I am working on mean, median in java. I have created a program but I am not able to properly implement inheritance on it. In my code, for every inherited class object, its asking input. </p> <p>I want input to be taken only once and then using inheritance mean and mode to be calculated. Kindly suggest. </p> <pre><code>import java.io.*; import java.util.*; class A { int a[]; int no; A() { System.out.println("Number of elements for an array : "); Scanner in1 = new Scanner(System.in); no = in1.nextInt(); a = new int[no]; for(int i=0; i&lt;no; i++) { System.out.println("Enter elements : "); Scanner in2 = new Scanner(System.in); a[i] = in2.nextInt(); } } void sort() { int tmp; for(int i=0; i&lt;no-1; i++) { for(int j=i+1; j&lt;no; j++) { System.out.println("a[i] : " + a[i] + " a[i+1] : " + a[i+1]); if (a[i]&gt;a[j]) { tmp = a[i]; a[i] = a[j]; a[j] = tmp; } } } } void display() { System.out.println(); for(int i=0; i&lt;no; i++) { System.out.println(a[i]); } } } class B extends A { int tot = 0; float mean; void get_mean() { System.out.println("no : " + no); for(int i=0; i&lt;no; i++) { tot += a[i]; } System.out.println("tot : " + tot); mean = (float) tot/no; System.out.println("mean : " + mean); } } class C extends A { void median() { float median; if(no%2==0) { int middle = no/2; median = (float)((a[middle] + a[middle/2])/2); } else { median = a[(no/2)]; } System.out.println("median : " + median); } } class test { public static void main (String arg[]) throws IOException { B obj1 = new B(); obj1.get_mean(); C obj2 = new C(); obj2.sort(); obj2.median(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload