2

I'm very new to java (only been using it for 2 days now), and am trying to make a class that lets you input three numbers, and then outputs the average of all three. When the code is like this the output always equals 0 and I don't know why? I am able to get it to work if I change add "static" to all the public integers, but why do I have to do that? Is there another way I can do it without making them static?

import java.util.Scanner;

public class lettuce 
{
public int num1;
public int num2;
public int num3;

public static void main(String args[])
{

    lettuce lettuceObject = new lettuce();

    int total = 0;
    int average;


    int array[] = {lettuceObject.num1,lettuceObject.num2,lettuceObject.num3};

    lettuceObject.getNum1();
    System.out.println(lettuceObject.num1);
    System.out.println(array[0]);
    lettuceObject.getNum2();
    System.out.println(lettuceObject.num2);
    System.out.println(array[1]);
    lettuceObject.getNum3();
    System.out.println(lettuceObject.num3);
    System.out.println(array[2]);


    for(int counter = 0; counter < array.length;counter++)
    {
        total = total + array[counter];
    }
    average = total/array.length;

    System.out.println("The average of the three numbers is: " + average);



}

public int getNum1()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type your first number: ");
    return num1 = keyboard.nextInt();
}
public int getNum2()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type your second number: ");
    return num2 = keyboard.nextInt();
}
public int getNum3()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Please type your third number: ");
    return num3 = keyboard.nextInt();   
}

}

7
  • What does num1/2/3equal? Commented Jan 8, 2015 at 3:49
  • they are equal to whatever the user inputs. it asks the user to input 3 numbers and then it is meant to average all of them Commented Jan 8, 2015 at 3:59
  • And where does the user get the input? Commented Jan 8, 2015 at 4:01
  • when it runs getNum1, getNum2, getNum3, its meant to assign the users input at "keyboard.nextInt()" to num1 num2 and num3 Commented Jan 8, 2015 at 4:06
  • Didn't see the getNum methods. Commented Jan 8, 2015 at 4:10

2 Answers 2

1

The output is 0 because you have never initialized your num(s), you're assigning to them on get(s) which you never call - and you're trying to set them in get(s) which isn't the customary approach.

public int num1 = 3;
public int num2 = 3;
public int num3 = 3;

And you should get 3. A getter should look like

public int getNum1()
{
    return num1;
}

A setter should look like

public void setNum1(int num1) {
    this.num1 = num1;
}

And then you would customarily name your class Lettuce and call it from main like

Lettuce lettuce = new Lettuce();
lettuce.setNum1(10);
System.out.println(lettuce.getNum1());

You would customarily also make your fields private and access them through your mutator and accessor methods (getters and setters)

private int num1;
private int num2;
private int num3;

You could choose to create a constructor

public Lettuce(int num1, int num2, int num3) {
    this.num1 = num1;
    this.num2 = num2;
    this.num3 = num3;
}

You could also calculate the average from "lettuce" with something like

public double average() {
    return (num1 + num2 + num3) / 3.0;
}

Edit

Please don't edit your question like that. Also, consider the order of your operations. Your get methods are what set the values. So call them before you create your array!

lettuceObject.getNum1();
lettuceObject.getNum2();
lettuceObject.getNum3();
// Each of those values is 0 until you call the previous three lines.
int array[] = {lettuceObject.num1,lettuceObject.num2,lettuceObject.num3};
System.out.println(array[0]);
System.out.println(array[1]);
System.out.println(array[2]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick response! That has helped a lot. However, how come if I change all the public integer things to "static" ie. "public static int num1;" and change "int array[] = {lettuce.num1,lettuce.num2,lettuce.num3};" to "int array[] = {num1,num2,num3};" and then move it below the getNum()3 line, it starts working? Sorry if this isnt in code and everything, I am very new to stack overflow.
@AaronBarnard You edited your question. Please read the updated answer above. Also, try and think about the order of your calls there.
@ElliotFrisch oh okay, I get it now, thanks heaps for the help!
0

As you are new I will give you some more tips they making this work.

1: The static modifier specifies that you don't need to instanciate a Class to use that attribute (variable or method ). For example, if you have a class with one static variable:

public class Clazz {
   static int variable=1;
}

You may call it without creating an instance of Clazz. System.out.println(Clazz.variable); would compile with no problems. Otherwise, a non-staticattribute will need an Instance of Clazz to be accessed:

Clazz instanceOfClazz = new Clazz();
System.out.println(instanceOfClazz.variable);

2: The type intis native. So, when you create your array, you are passing no values, and after reading the output, your array is not updated.

3: a double variable would be more precise to store the result of an average.

4: last but not least, your getNum method could be merged into just 1 method receiving the message as parameter, so you hace a best and clear reuse of the code. That can be staticbecause it doesn't need to interact with any object of the class Lettuce (with receive as parameter all it needs to execute and return the integer sent by the user, you can assing the return outside the method)

Ps.: by notation, the class name should start with capital letter.

Your final class would look better this way:

import java.util.Scanner;

public class Lettuce 
{
public int num1;
public int num2;
public int num3;

public static void main(String args[])
{

    Lettuce lettuceObject = new Lettuce();

    int total = 0;
    double average;



    lettuceObject.num1 = lettuceObject.getNum("Please type your first number: ");
    System.out.println(lettuceObject.num1);
    System.out.println(array[0]);
    lettuceObject.num2 = lettuceObject.getNum("Please type your second number: ");
    System.out.println(lettuceObject.num2);
    System.out.println(array[1]);
    lettuceObject.num2 = lettuceObject.getNum("Please type your third number: ");
    System.out.println(lettuceObject.num3);
    System.out.println(array[2]);


    int array[] = {lettuceObject.num1,lettuceObject.num2,lettuceObject.num3};
    for(int counter = 0; counter < array.length;counter++)
    {
        total = total + array[counter];
    }
    average = total/array.length;

    System.out.println("The average of the three numbers is: " + average);



}
public int getNum(String message)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println(message);
    return keyboard.nextInt();
}
}

Hope this helped.

1 Comment

thanks for the tips man, they really helped explain some of the things i didn't really know!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.