Presentation is loading. Please wait.

Presentation is loading. Please wait.

Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before.

Similar presentations


Presentation on theme: "Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before."— Presentation transcript:

1 Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before These variables are created when the object is initialized The “new” operator is what initializes the object in memory We can perform more complex tasks at the time an object is initialized, automatically The constructor is a special function that executes only at initialization time Distinguishable from other methods because it Has no return type Has the same name as the class

2 Example Class DiceRoll { public int dieOne; public int dieTwo; public DiceRoll(){ dieOne = (int) (Math.random()*6 + 1); dieTwo = (int) (Math.random()*6 + 1); System.out.println(“New pair of dice rolled!”); }

3 Object Initialization
Constructors can take arguments! They act basically the same as standard functions, just get called at a special time E.g. for the dice, we could specify number of sides on the dice: public DiceRoll(int sides){ dieOne = (int) (Math.random()*sides + 1); dieTwo = (int) (Math.random()*sides + 1); System.out.println(“New pair of dice rolled!”); }

4 Object Initialization
Constructors can be overloaded! Important concept: default and non-default constructors: For the dice example: public DiceRoll(){ ... } Will get called by new DiceRoll(); public DiceRoll(int sides){ ... } Will get called by new DiceRoll(10);

5 Example: Dice Rolls Create a class called “PairOfDice”
Has an attribute for number of sides Has an attribute for value Default constructor sets them to be six-sided Constructor can take an integer to denote the number of sides of each Public method to re-roll the dice Public method to print the current values Public method to calculate the total of the current values Write a main program to roll two pairs of dice until they are the same, count number needed

6 Example: Students Create a class called “Student”
Each new student should get assigned a unique ID number Will need to use a static variable! Constructor should also take a name of the student Three public attributes for three different test scores Public method to calculate their exam average Public method to print the student info (name, ID, average grade) Main function to instantiate two students, assign test scores, and print their info

7 Objects as Parameters We can pass an object to a subroutine or function! public void printStudent(Student s){ … } But, like arrays, we have to be careful inside the subroutines, because any modification to the object remains after the subroutine is finished. E.g. if in the printStudent function we accidentally re-assign their name

8 Object Arrays Similar to other data types, we can make arrays of objects! Student[] studentArray = new Student[10]; Can access a single student as: studentArray[5].name = “Bob”;

9 The “final” modifier As a modifier to any variable (as well as class or method, with different meanings) we may add the ‘final’ keyword: public final int age; What this means: the variable can be assigned once Either initialized at declaration, or later in an assignment statement Final instance (non-static) object variables must be either initialized in their declaration or definitely assigned in every constructor

10 Final Variables Convention: write the name in all caps, using underscores to separate words E.g. final int NUMBER_OF_STUDENTS = 100; Another common use: to hold representative values of more ambiguous concepts E.g. card suits final ACE = 1; final TWO = 2; final THREE = 2;

11 Example: Card Game Create a class called “Card”
Has thirteen static variables for the possible values of a playing card Has four static variables, one for each suit Has two instance attributes: value and suit Create a class called “Deck” Has an array of 52 cards Initialize the deck to one card of each value and of each suit Write a main function that plays “war”: Draw two cards from a deck and compare their value to see which one wins


Download ppt "Class Constructor Recall: we can give default values to instance variables of a class The “numberOfPlayers” variable from the “PlayerData” class before."

Similar presentations


Ads by Google