The Student class

package bowlsnakepackage

public class Snake{

private final int maxHeight = 120;

private int currentHeight;

public Snake(){

this.currentHeight = 0;

}

public int getCurrentHeight(){

return this.currentHeight;

}

public int getMaxHeight(){

return this.maxHeight

}

public void setCurrentHeight(int newHeight){

this.currentHeight = newHeight;

}

}

The Student class

package bowlsnakepackage

public class Bowl{

private int bowlTemp;

private Snake snake;

private boolean isCracked;

public Bowl(Snake snake){

this.snake = snake;

this.bowlTemp = 20;

this.isCracked = false;

}

public int getBowlTemp(){

return this.bowlTemp;

}

public void setBowlTemp(){

//set the bowl's temp between 20 and 120

bowlTemp = (int)(Math.random()*101)+20;

//move the Snake according to the temperature

//update the bowl's cracked status

moveSnake();

checkCracked();

}

private void moveSnake(){

snake.setCurrentHeight(bowlTemp);

}

private void checkCracked(){

if(bowlTemp >= 120){

this.isCracked = true;

}

}

public boolean getCracked(){

return this.isCracked;

}

}

Objectives

Students will be able to:

  • appreciate that different modifiers can be used on methods and attributes in a class to help control access to athose attributes

Object Oriented Programming

Modifiers

 
 

Aggregation Review

A Bowl HAS-A Snake.

The Bowl also has a temperature.

The Snake rises to random heights depending on the temperature of the Bowl.

The maximum height the snake can rise to is 120cm - this can never change.

If the Snake can rise to this maximum height it means that the Bowl temperature is very high and the Bowl cracks and the Snake escapes.

is an abstraction of a Snake.

is an abstraction of a Bowl.

Redesign the classes using as many modifiers as you can.

Write a main program based on the scenarion described above.

In your design, use as many modifiers as you can!

Can you find a way to include an attribute which uses the static modifier in any of the classes (see video)?

make sure your classes are all contained in a package, including your Main class.

We have become very familar with 2 modifiers in Java that can be applied to both methods and attributes of a class:

  1. public: visible/available to other classes
  2. private: hidden/invisible to other classes

There are 3 other modifiers that are useful to be familiar with when programming in Java:

  1. protected
  2. final
  3. static

Complete the Worksheet followed by the coding activity.

Helper Video

Glossary

public

private

protected

final

static

package

class

object (instance of a class)

abstraction