-2

I'm a beginner learning java. I have the following question;

I want to assign the value of the static variable Dog.averageWeight to an object variable dog1.weight.

I want the dog.weight = 3;

I've tried different options, but it doesn't work. Can someone please tell me if this works and how?

    public static int numberOfAllDogs;
    public static int totalWeight;
    public static int averageWeight;

    public String name;
    public int weight;

    public void initialize(String name) {
        this.name = name;
        this.weight = weight;

        numberOfAllDogs++;

    }

    public void initialize(String name, int weight) {
        this.name = name;
        this.weight = weight;

        numberOfAllDogs++;
        totalWeight = totalWeight + this.weight;
    }

    public static void main(String[] args) {

        Dog dog1 = new Dog();
        Dog dog2 = new Dog();

        dog1.initialize("daisy");
        dog2.initialize("chuck", 5);
        dog2.initialize("marco", 5);
        dog2.initialize("emy", 5);

        averageWeight = totalWeight / numberOfAllDogs;

        System.out.println("Total weight: " + totalWeight); //15
        System.out.println("Avvarage weight: " + averageWeight); // 3
        System.out.println(dog2.weight);
        System.out.println(dog1.weight);
    }
}
alvira
  • 147
  • 1
  • 7

1 Answers1

1

First of all change public static int averageWeight;

to public static double averageWeight = 0;

Solution :

public void initialize(String name, int weight) {
        this.name = name;
        this.weight = weight;

        numberOfAllDogs++;
        totalWeight = totalWeight + this.weight;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }

So when you init a new dog, averageWeight changes with it

But I would actually refactor this code:

class Dog
{
    /* You cannot change it by yourself so make it private */
    private static int numberOfAllDogs = 0;
    private static int totalWeight = 0;
    private static double averageWeight = 0;

    private String name;
    private int weight;
    /* constructor for dog without weight :D (idk why you created this one) */
    public Dog (String name){ 
        this.name = name;
        weight = 0;
        numberOfAllDogs++;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }
     /* constructor for dog*/
    public Dog (String name,int weight){
        this.name = name;
        this.weight = weight;
        numberOfAllDogs++;
        totalWeight += this.weight;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }
    /* getters and setters */

    /* you can only get static content, not to change, cuz of your task*/
    public static int getNumberOfAllDogs() {
        return numberOfAllDogs;
    }

    public static int getTotalWeight() {
        return totalWeight;
    }

    public static double getAverageWeight() {
        return averageWeight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setWeight(int weight) {
        totalWeight -= this.weight;
        this.weight = weight;
        totalWeight += weight;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }

     public int getWeight() {
        return weight;
    }
}
// our public class 
public class Main {
    public static void main(String[] args){
        Dog dog1 = new Dog("Doggy1"); // dog1 creation
        Dog dog2 = new Dog("Doggy2", 5); // dog2 creation
        System.out.println("Total weight: " + Dog.getTotalWeight()); // total weight
        System.out.println("Avvarage weight: " + Dog.getAverageWeight()); // avg weight
        System.out.println(dog1.getWeight()); // dog1 weight
        System.out.println(dog2.getWeight()); // dog2 weight
    }
}
Antaaaa
  • 233
  • 3
  • 14
  • Thank you Antaaaa :) – alvira Jun 30 '20 at 15:54
  • I just put the code with getter / setter in the eclipse but it still shows dog1.getWeight () = 0; :( Could you pls check it? – alvira Jun 30 '20 at 16:23
  • @alvira because ```Dog dog1 = new Dog("Doggy1"); // dog1 creation``` makes dog1 weight = 0 – Antaaaa Jun 30 '20 at 16:25
  • That line I can understand, but I still don't get it how to change dog1.getWeight()=3; sorry and thx :( – alvira Jun 30 '20 at 16:32
  • @alvira you need to use ```dog1.setWeight(3);```. Check this out about ```getters``` and ```setters``` in Java [Why use getters and setters/accessors?](https://stackoverflow.com/questions/1568091/why-use-getters-and-setters-accessors) – Antaaaa Jun 30 '20 at 16:34
  • Ah yeah, thx :) I hoped I could write this.weight = Dog.averageWeight; ;) Sorry I can't vote up yet: Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score. – alvira Jun 30 '20 at 16:41
  • @alvira I tried to make your code clear. For your purpose, you can use an updated version of this code and something like this: dog1.setWeight(Dog.averageWeight); – Antaaaa Jun 30 '20 at 16:54
  • It just worked ; dog1.setWeight(averageWeight); Thank you ver much :) :) – alvira Jun 30 '20 at 16:56
  • @alvira - Nice to hear that. Learn Java it's really beautifull language – Antaaaa Jun 30 '20 at 16:59