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

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Chapter 7: User-Defined Functions II
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Road Map Introduction to object oriented programming. Classes
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
1 Classes, Encapsulation, Methods and Constructors Class definitions Scope of Data –Instance data –Local data The this Reference Encapsulation and Java.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
1 CSC241: Object Oriented Programming Lecture No 02.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Arithmetic, Class Variables and Class Methods Week 11
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Java Programming, Second Edition Chapter Three Using Methods, Classes, and Objects.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Part III © Copyright by Pearson Education, Inc. All Rights Reserved.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Learners Support Publications Constructors and Destructors.
INTRODUCTION TO CLASSES & OBJECTS CREATING CLASSES USING C#
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Constructors and Destructors
Chapter VII: Arrays.
Topic: Classes and Objects
Classes and Objects Introduced
Creating and Using Objects, Exceptions, Strings
Chapter 7: User-Defined Functions II
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
More About Objects and Methods
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
JavaScript: Functions.
User-Defined Functions
Class Variables We’ve seen how to add extra subroutines into our programs Can be accessed from within one another We can also add variables that are “global”
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
CS 302 Week 11 Jim Williams, PhD.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Arrays An Array is an ordered collection of variables
User Defined Functions
Variables ICS2O.
Classes & Objects: Examples
Variables and Their scope
CHAPTER 6 GENERAL-PURPOSE METHODS
Chapter 4 Writing Classes.
Exam 1 Material Study Guide
Constructors and destructors
Constructors and Destructors
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CS2011 Introduction to Programming I Objects and Classes
More About Objects and Methods
Building Java Programs
Object Oriented Programming in java
CIS 199 Final Review.
Anatomy of a class Part II
Constructors & Destructors
Presentation transcript:

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

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

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

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

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

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

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

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

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

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;

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