Communicating Between Java Objects

Communicating Between Objects in Java

Project on Object Communication

In this project, you are asked to modify the Human class shown in the above video to add a Lung class. You must then set up the heart and lung objects inside the human so that they have handles to each other. This represents an additional complexity in that the pointers (handles) to both of these objects are not available at the time of construction of each object because one of these objects must be created before the other.

To get around this issue, instead of establishing the handles at the time of construction, create a separate method in the heart class to set its handle to its corresponding lung object and vice versa. Then, in the constructor of the human, create both the heart and lung objects and then call the separate methods you have created to initialize these pointers. 

Now add a method called breatheFaster to the Lung class. Have the heart call this method when danger is sensed. Also add a method called getMoreOxygen to the heart class. Have the lung object call this method whenever the breatheFaster method is invoked. 

Below is some starter code to reduce your typing:

public class Human {
private String name;

//my body parts
private Heart myHeart;
private Brain myBrain;

public Human(String name){ = name;

myHeart = new Heart();
myBrain = new Brain();

System.out.println(name + " is born");

public void sweat(){
System.out.println(name + "is sweating");

public void senseDanger(){
int rate = myHeart.increaseHeartRate(10);
System.out.println("New heart rate for " + name + " is: " + rate);

public class Heart {
private int heartRate;

public Heart(){

public void setHeartRate(int hr){
heartRate = hr;

public int getHeartRate() {
return heartRate;

public int increaseHeartRate(int increase){
heartRate += increase;
return heartRate;