Object-Oriented Programming with Java, part I – Exercise 85

This exercise implements the class Reformatory which returns thew persons weight, how many times they have been feed, and how many times they have been weighed.

My Code

Main


public class Main {

public static void main(String[] args) {
    Reformatory eastHelsinkiReformatory = new Reformatory();

    Person brian = new Person("Brian", 1, 110, 7);
    Person pekka = new Person("Pekka", 33, 176, 85);

    System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());

    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(pekka);

    System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());

    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(brian);

    System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());
}
}

Person

/*
 *  DO TOT TOUCH THIS CLASS!
 */

public class Person {

    private String name;
    private int age;
    private int height;
    private int weight;

    public Person(String name, int age, int height, int weight) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }

    public void becomeOlder() {
        this.age++;
    }

    public boolean adult() {
        if (this.age < 18) {
            return false;
        }

        return true;
    }

    public double weightIndex() {
        double heightInMeters = this.height / 100.0;

        return this.weight / (heightInMeters * heightInMeters);
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getHeight() {
        return this.height;
    }

    public int getWeight() {
        return this.weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getName() {
        return this.name;
    }

    public String toString() {
        return this.name + " I am " + this.age + " years old, my weight index is " + this.weightIndex();
    }
}

Reformatory

public class Reformatory {
    private int twm;
    public Reformatory() {
        this.twm = 0;
    }

    public int weight(Person person) {
        // return the weight of the person
        twm++;
        return person.getWeight();
    }
    
    public void feed(Person person){
        person.setWeight(person.getWeight() + 1);
    }
    
    public int totalWeightsMeasured(){
        return this.twm;
    }

}

Model Code

Main

public class Main {

public static void main(String[] args) {
    Reformatory eastHelsinkiReformatory = new Reformatory();

    Person brian = new Person("Brian", 1, 110, 7);
    Person pekka = new Person("Pekka", 33, 176, 85);

    System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());

    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(pekka);

    System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());

    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(brian);
    eastHelsinkiReformatory.weight(brian);

    System.out.println("total weights measured "+eastHelsinkiReformatory.totalWeightsMeasured());
}
}

Person

/*
 *  DO TOT TOUCH THIS CLASS!
 */

public class Person {

    private String name;
    private int age;
    private int height;
    private int weight;

    public Person(String name, int age, int height, int weight) {
        this.name = name;
        this.age = age;
        this.height = height;
        this.weight = weight;
    }

    public void becomeOlder() {
        this.age++;
    }

    public boolean adult() {
        if (this.age < 18) {
            return false;
        }

        return true;
    }

    public double weightIndex() {
        double heightInMeters = this.height / 100.0;

        return this.weight / (heightInMeters * heightInMeters);
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getHeight() {
        return this.height;
    }

    public int getWeight() {
        return this.weight;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public String getName() {
        return this.name;
    }

    public String toString() {
        return this.name + " I am " + this.age + " years old, my weight index is " + this.weightIndex();
    }
}

Reformatory

public class Reformatory {

    private int weightsMeasured;

 

    public Reformatory() {

        this.weightsMeasured = 0;

    }

 

    public int weight(Person person) {

        // return the weight of the person

        this.weightsMeasured++;

        return person.getWeight();

    }

 

    public void feed(Person person) {

        int weightBeforeEating = person.getWeight();

        person.setWeight(weightBeforeEating + 1);

    } 

 

    public int totalWeightsMeasured() {        

        return weightsMeasured;

    }

}

 

Comments

This appears to be one of those rare instances where my code is better….except perhaps for my variable name for the times weight was measured (twm).

Leave a comment