Top Banner
CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland
19

CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

Dec 17, 2015

Download

Documents

Britton Manning
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

CSE 114 – Computer Science IObject Oriented Programming

Signal Hill, Newfoundland

Page 2: 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

Page 3: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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

Page 4: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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;

}

Page 5: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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);

Page 6: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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() { }

Page 7: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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

Page 8: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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)

Page 9: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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;

Page 10: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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!

Page 11: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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

Page 12: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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

Page 13: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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.

Page 14: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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

Page 15: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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.

Page 16: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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");

}

Page 17: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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!");

}

}

Page 18: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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.

Page 19: CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland.

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