Sword Knight Sword Knight - 10 months ago 29
Java Question

What is the cause of my error?

Here's my demo.

import java.util.Scanner;

public class TestScoresDemo
public static void main(String[]args)
{ TestScoresClass2WIP yourTestScoresClass2WIP;

double Average;
double Total;

Scanner keyboard = new Scanner(System.in);

TestScoresClass2WIP scores = new TestScoresClass2WIP();

System.out.print("First score?");
double setScore1 = keyboard.nextDouble();

System.out.print("Second score?");
double setScore2 = keyboard.nextDouble();

System.out.print("Third score?");
double setScore3 = keyboard.nextDouble();

System.out.println("The total of all three tests is: " +scores.getTotal());
System.out.println("The third score is: " +scores.getScore3());
System.out.println("The average of all the tests is: " + scores.getAverage());



Here's my class.

public class TestScoresClass2WIP
private double score1;
private double score2;
private double score3;
private double total;
private double average = ((score1 + score2 + score3) /3);

public void setScore1(double S1)
{score1 = S1;}
public void setScore2(double S2)
{score2 = S2;}
public void setScore3(double S3)
{score3 = S3;}
public double getScore1() {return score1;}

public double getScore2() {return score2;}

public double getScore3() {return score3;}

public double getTotal()
{return score1 + score2 + score3;}

public double getAverage()
{return ((score1 + score2 + score3) /3);}

For some reason, neither code displays any errors, but when I run the demo I get back nothing but zeroes. Why is this?

Sorry if this has been asked before but I've been looking for an answer to this specific question and haven't found one on google.
I've been tinkering with this code for going on three hours trying to get it to work.

The how to ask a question section said to openly state if it's a homework question. This is. It's the first problem of this kind that I've tried to do.

Answer Source

You've never called the methods scores.setScoreX() anywhere. From the point of view of your class, when would it ever receive the scores setScore1, setScore2, or setScore3 from the main function?

Instead of giving you an exact fix, I'm advocating you debug this further yourself. Look specifically at how your class is supposed to set and get the necessary variables, what your specific variables should be called (both inside the class and outside in your main function), and which functions are directly responsible for which variables.