hgg ohh hgg ohh - 4 months ago 6
Java Question

Why doesn't my mean increase above zero?

I'm trying to calculate the mean of the total after 999 is typed, but it keeps showing 0, and I can't see why.

Can someone tell me how to get my getCalcMean() method to show the mean as numTotal/count?

--- Class Program ---

public class Program {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num = 0;
int counter = 1;
Math math1 = new Math(num, counter);
while (num != 999) {
num = kb.nextInt();
if (num != 999) {
math1.adder(num);
counter ++;
System.out.println("Total till now:" + math1.getNumTotal());
}
}
System.out.println(math1.getNumTotal());
System.out.println(math1.getCalcMean());
//System.out.println(math1.getNum());
kb.close();
/*Scanner kb = new Scanner(System.in);
String input = kb.nextLine();
Scanner scn = new Scanner(input);
int num = scn.nextInt();
Math math1 = new Math(num,0);
while(num != 999){
math1.adder(num);
input = kb.nextLine();
}

System.out.println(math1.getNumTotal());*/
} //main
}


---- Class Math ----

public class Math {

private int num;
private int numTotal;
private int mean;
private int count;

/*public Math(int num, int numTotal){
this.num = num;
}*/

public Math(int num, int counter) {
this.num = num;
this.count = counter;
}

//get//
public int getNum(){
return this.num;
}

public int getNumTotal(){
return this.numTotal;
}

public int getCalcMean(){
return this.mean;
}

//set//

public void setNumTotal(int value){
this.numTotal = value;
}

public void setNum(int value){
this.num = value;
}

public void setCalcMean(int value){
this.mean = value;
}

//other
/*public void adder(int num){
numTotal = numTotal + num;
}*/

public void adder(int num) {
this.num = num;
numTotal = numTotal + this.num;
}
//added after//
public void calcMean(int num){
this.numTotal = numTotal;
mean = numTotal / this.count;
}
}

Answer
  1. counter ++ in your code is meaningless since you never pass it to Math object.
  2. math1.getCalcMean() will return the mean in Math object but mean hasn't been calculated yet.

Advice:

  1. Add getter and setter for counter in Math class.
  2. Calculate the mean in getCalcMean() method.

Plese see the code below. I assume that you use 999 as the end flag so haven't took it into account. The counter is initialized with 0.

import java.util.Scanner;

public class Program {
public static void main(String[] args) {
    Scanner kb = new Scanner(System.in);
    int num = 0;
    int counter = 0;
    Math math1 = new Math(num, counter);
    while (num != 999) {
        num = kb.nextInt();
        if (num != 999) {
            math1.adder(num);
            counter ++;
            math1.setCounter(counter);
            System.out.println("Total till now:" + math1.getNumTotal());
        }
    }
    System.out.println(math1.getNumTotal());
    System.out.println(math1.getCalcMean());
    //System.out.println(math1.getNum());
    kb.close();
    /*Scanner kb = new Scanner(System.in);
    String input = kb.nextLine();
    Scanner scn = new Scanner(input);
    int num = scn.nextInt();
    Math math1 = new Math(num,0);
    while(num != 999){
        math1.adder(num);
        input = kb.nextLine();
    }

    System.out.println(math1.getNumTotal());*/
} //main
}

And also the Math class.

public class Math {

private int num;
private int numTotal;
private int mean;
private int counter;

/*public Math(int num, int numTotal){
this.num = num;
}*/

public Math(int num, int counter) {
    this.num = num;
    this.counter = counter;
}

//get//
public int getNum(){
    return this.num;
}

public int getNumTotal(){
    return this.numTotal;
}

public int getCalcMean(){
    mean = numTotal / this.counter;
    return mean;
}

public int getCounter(){
    return this.counter;
}

//set//

public void setNumTotal(int value){
    this.numTotal = value;
}

public void setNum(int value){
    this.num = value;
}

public void setCalcMean(int value){
    this.mean = value;
}

public void setCounter(int counter){
    this.counter = counter;
}

//other
/*public void adder(int num){
numTotal = numTotal + num;
}*/

public void adder(int num) {
    this.num = num;
    numTotal = numTotal + this.num;
}
//added after//
public void calcMean(int num){
    mean = numTotal / this.counter;
}
}