hgg ohh hgg ohh - 4 months ago 8
Java Question

<Why doesn't my mean increase above zero?> Convert to double

UPDATE: I've tried changing the return type on the getCalcMean() and calcMean() methods to double to make it more accurate, and everything inside the Person class works fine, but Inside the program class it's now saying "Incompatible Types: possible lossy conversion from double to int." at System.out.println(math1.calcMean(math1.getCalcMean())); I understand what the error means but I'm not sure how to fix it as the final result needs to be double and I thought java could calculate a double and int and get a double. What am I doing wrong?
<<>>




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 static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num = 0;
int count = 1;
Math math1 = new Math(num, count);
while (num != 999) {
num = kb.nextInt();
if (num != 999) {
math1.adder(num);
count ++;
System.out.println("Total till now:" + math1.getNumTotal());
}
}
math1.setCount(count);
System.out.println(math1.getNumTotal());
System.out.println(math1.calcMean(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 double mean;
private int count;

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

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

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

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

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

//set//

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

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

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

public void setCount(int value){
this.count = 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 double calcMean(int num){
this.numTotal = numTotal;
mean = numTotal / this.count;
return mean;
}

}

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;
}
}
Comments