CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland
Midterm 1
• Tuesday, 10/12
• A written exam
• Sample exam posted to schedule page
• Exam Material– Lectures 1 – 8 (through Iteration)– HWs 1 & 2– Labs 1 – 8
Die Object
STATE (What information describes it?)
•Number of Faces – maximum numbers on dice
•Value facing up – number on upward face
BEHAVIOR (What can we do with it?)
•Initialize – initializes state
•Roll – choose a random up-face value
•Read the value – get current up-face value
Constructors (revisited)
• Another constructor for Die:
public Die(int initNumFaces, int initUpValue)
{
if ( (initUpValue > 0) && (initNumFaces >= 4)
&& (initUpValue <= initNumFaces) )
{
numFaces = initNumFaces;
upValue = initUpValue;
}
}
•Original (default) constructor for Die:public Die(){
numFaces = 6; upValue = 1;
}
•Our second constructor for Die:public Die(int faces){
numFaces = faces; upValue = 1;
}
More on Constructors
• The Die class can have ALL THREE constructors!
• How does the compiler know which one to run?
Die die1 = new Die();
Die die2 = new Die(20);
Die die3 = new Die(8,3);
More on Constructors (cont’d)
• The constructor that is executed depends on:– the number of parameters– the types of the parameters– the order of the parameters
• Called the method signature
• If no constructor is given in a class, the Java compiler adds an empty constructor with no parameters:
public void Die() { }
Alternative to Initialization via Constructorspublic class Die{
private int numFaces = 6;private int upValue = 1;
public Die() {}
public Die(int faces){ numFaces = faces; }
public Die(int initNumFaces, int initUpValue){
if ( (initUpValue > 0) && (initNumFaces >= 4) && (initUpValue <= initNumFaces) )
{ numFaces = initNumFaces;upValue = initUpValue;
} }
…
You may declare & initialize instance variables simultaneously
What does new do?
• We use it every time we construct something. Ex:Die die1 = new Die();
• new is a request for a block of memory– How big a block?
• as much needed by the constructed object
– How much for a Die?• 8 bytes (4 bytes for each int)
• What does new return?– a memory address (of the reserved block)
Information Hiding• Remember how we wrote the setUpValue method?
• We could use it like:Die die1 = new Die();die1.setUpValue(3);
• Why didn’t we just change the variable directly? Ex:die1.upValue = 3; ???
• Why can’t we access die1.upValue?– Because it’s private!
• Why make it private rather than public?– so another class won’t misuse it:
die1.upValue = -3;
Classes must protect their data• A class is responsible for its data (instance variables)
• private variables:– a class’ methods can access/change them– external classes cannot
• To prevent misuse. Ex:die1.upValue = -3;
• The next line of code to execute may depend on upValue being 1-6, and if it is –3, a runtime error may occur.
• The only way to access the data within an object should be through its public methods– this provides a layer of error protection!
Accessor/Mutator methods
• Used for accessing/changing private variables
• Accessor (GET) methods– Used to retrieve private instance variables from objects– Should usually be provided
• Mutator (SET) methods– Used to assign values to private instance variables– Only provide when necessary
GET/SET method naming conventions
• For instance variable named myVariable– getMyVariable– setMyVariable
• Common pattern for defining a class:– make all instance variables private – provide get and set methods as needed
PairOfDicepublic class PairOfDice{ private Die die1 = new Die(); private Die die2 = new Die();
public PairOfDice(){}
public Die getDie1() { return die1; } public Die getDie2() { return die2; } public int getTotal() {
return die1.getUpValue() + die2.getUpValue(); }
public void rollDice() { die1.roll(); die2.roll(); }}
LEGAL: only constructor methods may be invoked here
Why not provide a total instance variable?
It would be redundant data, a no-no for instance variables.
public class DoublesGame{ public static void main(String args[]) { PairOfDice dice = new PairOfDice();
System.out.println("\n**LET'S PLAY DOUBLES**"); Die die1 = dice.getDie1(); Die die2 = dice.getDie2();
dice.rollDice(); System.out.println("Roll: " + dice.getTotal()
+ " (" + die1.getUpValue() + " & "+ die2.getUpValue() + ")");
if (die1.getUpValue() == die2.getUpValue()) System.out.println("YOU WIN!"); else System.out.println("YOU LOSE."); }}
Playing Doubles
DoublesGame Output for 3 games
**LET'S PLAY DOUBLES**
Roll: 8 (5 & 3)
YOU LOSE.
**LET'S PLAY DOUBLES**
Roll: 6 (3 & 3)
YOU WIN!
**LET'S PLAY DOUBLES**
Roll: 5 (2 & 3)
YOU LOSE.
Playing anti-doublespublic class AntiDoublesGame
{
public static void main(String args[])
{
char input;
Scanner keyboard = new Scanner(System.in);
do
{
System.out.println("\n**WELCOME TO ANTI-DOUBLES**");
System.out.print("Type 'p' to play, 'q' to quit: ");
input = keyboard.nextLine();
if (input.equals("p"))
playAntiDoubles();
} while(!input.equals("q");
}
Playing anti-doubles (continued) public static void playAntiDoubles()
{
PairOfDice dice = new PairOfDice();
Die die1 = dice.getDie1();
Die die2 = dice.getDie2();
dice.rollDice();
System.out.println("Roll: " + dice.getTotal()
+ " (" + die1.getUpValue() + " & "
+ die2.getUpValue() + ")");
if (die1.getUpValue() == die2.getUpValue())
System.out.println("YOU LOSE.");
else
System.out.println("YOU WIN!");
}
}
AntiDoublesGames Output for 3 games
**LET'S PLAY ANTI-DOUBLES**
Roll: 4 (1 & 3)
YOU WIN!
**LET'S PLAY ANTI-DOUBLES**
Roll: 6 (2 & 4)
YOU WIN!
**LET'S PLAY ANTI-DOUBLES**
Roll: 12 (6 & 6)
YOU LOSE.
private variables, a catch• What’s strange here?public class Die{
…public boolean equals(Die testDie){
if (( upValue == testDie.upValue ) && (numFaces == testDie.numFaces))
return true;else
return false;}…
}
Won’t these produce syntax errors?
• No syntax error because private variables are accessible from within objects of similar types