Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CSE 114 – Computer Science I Object Oriented Programming Signal Hill, Newfoundland."— Presentation transcript:

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

2 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

3 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

4 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; }

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

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

7 Alternative to Initialization via Constructors public 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

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

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

10 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!

11 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

12 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

13 PairOfDice public 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.

14 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

15 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.

16 Playing anti-doubles public 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"); }

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

18 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.

19 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


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

Similar presentations


Ads by Google