Objects and Classes Continued Engineering 1D04, Teaching Session 10.

Slides:



Advertisements
Similar presentations
Java Coding 8 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Object-Oriented Design Example - The.
Advertisements

Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Classes and Objects. What is Design? The parts of the software including – what information each part holds – what things each part can do – how the various.
CSCI 160 Midterm Review Rasanjalee DM.
Objects, Variables & Methods Java encapsulates data and action modules that access the data in one container, called an object. Object members that.
CS 106 Introduction to Computer Science I 11 / 13 / 2006 Instructor: Michael Eckmann.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer.
Lesson 6 - Pointers Outline Introduction Pointer Variable Declarations and Initialization Pointer Operators Calling Functions by Reference Using the const.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 7 - Pointers Outline 7.1Introduction 7.2Pointer Variable Declarations and Initialization 7.3Pointer.
Modelling & Datatypes Koen Lindström Claessen. Software Software = Programs + Data.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Chapter 16 Templates. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Function Templates  Syntax, defining 
Chapter 5 Black Jack. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 5-2 Chapter Objectives Provide a case study example from problem statement.
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
Chapter 8 Objects & Classes. Definition of Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) uses the analogy of real objects as a template.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 Web Based Programming Section 6 James King 12 August 2003.
Neal Stublen Key Concepts  Classes  Objects  Inheritance  Polymorphism  Encapsulation.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
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.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Arrays, Type Casting and Constants Engineering 1D04, Teaching Session 5.
Efficiently Solving Computer Programming Problems Svetlin Nakov Telerik Corporation
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Introduction to Algorithms Engineering 1D04, Teaching Session 1.
Records Engineering 1D04, Teaching Session 11. © Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Records  So far we've seen a number of different.
1 Advanced Issues on Classes Part 3 Reference variables (Tapestry pp.581, Horton 176 – 178) Const-reference variables (Horton 176 – 178) object sharing:
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
File Input and Output (I/O) Engineering 1D04, Teaching Session 7.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Computers, Variables and Types Engineering 1D04, Teaching Session 2.
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Recitation 5 Enums and The Java Collections classes/interfaces 1.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
February 28, 2005 Introduction to Classes. Object Oriented Programming An object is a software bundle of related variables and methods. Software objects.
Objects and Classes Engineering 1D04, Teaching Session 9.
Comments, Conditional Statements Continued, and Loops Engineering 1D04, Teaching Session 4.
1 Introduction to Object Oriented Programming Chapter 10.
16-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
1 Working with Controls at Run Time. 2 Objectives You will be able to Add controls to a Windows form at run time. Modify controls at run time.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Overview of Previous Lesson(s) Over View 3  CLR Programming  Common Language Runtime (CLR) is a programming, that manages the execution of programs,
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Introduction to Enumerations CMSC 202. Enumerated Values Enumerated values are used to represent a set of named values. Historically in Java (and other.
COMP 14 Introduction to Programming Mr. Joshua Stough March 23, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
Data Abstraction: The Walls
Two Dimensional Arrays and Complex Conditions
Chapter 5 Black Jack.
Section 2 – CSE341 Patrick Larson, Spring 2013.
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Lecture 14 Writing Classes part 2 Richard Gesick.
Section 2 – CSE341 Konstantin Weitz.
Interfaces and Constructors
We’re slowly working our way towards classes and objects
CSE 341 Section 2 Winter 2018 Adapted from slides by Nick Mooney, Nicholas Shahan, Patrick Larson, and Dan Grossman.
Implementing Classes Chapter 3.
CSE 341 Section 2 Nick Mooney Spring 2017
CS100A Lect. 12, 8 Oct More About Arrays
Agenda Warmup Lesson 2.4 (String concatenation, primitive types, etc)
Presentation transcript:

Objects and Classes Continued Engineering 1D04, Teaching Session 10

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng1 Recap: HighScores Example txtName1 txtName2 txtScore2 txtScore1

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng2 recap: HighScores Example public class HighScore { private class hsRecord { public string name; public int score; } private const int maxElements = 10; private hsRecord[] hsArray = new hsRecord[maxElements]; private int length; public bool add(string newName, int newScore) {... } public void show() {... } no room to show this here

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng3 recap: HighScores  What about including a title for the name of the game at the time of instantiation?  In general it is useful to be able to include initial processing for an object at the time of its instantiation.  This is done through a constructor.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng4 Constructors  A constructor is simply a method that is executed automatically when the class is instantiated in an object.  The name of the constructor method is exactly the same as the name of the class.  The constructor method may also include parameters.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng5 A Constructor for HighScores  Consider our example. We want the constructor to include a string parameter for the title of the game.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng6 Constructor Example public class HighScore { private class hsRecord { public string name; public int score; } const int maxElements = 10; private hsRecord[] hsArray = new hsRecord[maxElements]; private int length = 0; private string result; private string hsTitle; public HighScore(string title) { hsTitle = title; }... constructor

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng7 Constructor Example  How do we use the constructor? private void btnStart1_Click(object sender, EventArgs e) { hs1 = new HighScore(gpGame1.Text); display(1, true); btnStart1.Visible = false; txtName1.Focus(); } string parameter currently set to “Group One” instantiate hs1 when we instantiate hs1 the constructor gets invoked (called)

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng8 Multiple Classes in a Project  In reasonably sized projects we typically need a number of classes, not just one.  Each class is normally stored in its own source file.  Visual Studio makes it easy to set up source files for classes.  As an example, let us see how we can set up a source file for the class HighScore.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng9 Source Files for Classes highlight and then right click

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng10 Source Files for Classes choose add and then class

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng11 Source Files for Classes type the name you want for the class

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng12 Source Files for Classes the system sets up the stub for the class note the HighScore class source file has been added

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng13 Another Example  Let’s try another example  Create a class that manages a suit of cards  We need to identify the suit (spades, etc)  We want to “deal” cards from the suit in random order  We need to be able to go back to the initial settings  We also need to be able to access the name of the suit - “Spades”, “Hearts” etc.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng14 Suit of Cards data structure in the class that stores the required info

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng15 More Cards private int numCardsLeft; private string name; private Random rand; private Random start = new Random(); // Constructor - and gets name of suit and seed for random # public Suit(string s, int seed) { int r; name = s; r = start.Next(seed); rand = new Random(r); status[0] = 0; numCardsLeft = MAX; } class variables lots of work to make it generate different numbers for each instance

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng16 More Cards public string deal() { string card; int i, c, n; if (numCardsLeft == 0) return null; else { n = rand.Next(1,numCardsLeft+1); //Find which card c = 0; for (i = 1; i <= n; ) { c++; if (status[c] == 0) i++; } status[c] = 1; status[0]++; numCardsLeft--; generates a number in the range 1 through numCardsLeft don’t count cards where status is 1 mark it as “used” increase #cards used decrease #cards left

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng17 More Cards if (c == 1) card = "Ace"; else if (c <= 10) card = Convert.ToString(c); else if (c == 11) card = "Jack"; else if (c == 12) card = "Queen"; else card = "King"; } return card; } get string value of current card

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng18 More Cards public void restore() { for (int i = 0; i <= MAX; i++) status[i] = 0; numCardsLeft = MAX; } gets everything set back to its original state

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng19 Using the Class Each of these controlled by the class we made

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng20 Using the Class  So - how do we use the class to create the 4 objects we need?

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng21 More Cards public partial class Form1 : Form { Suit spades = new Suit("Spades", 200); Suit hearts = new Suit("Hearts", 300); Suit clubs = new Suit("Clubs", 400); Suit diamonds = new Suit("Diamonds", 500); Declare 4 objects. Note the use of the constructor to set the suit name and a seed for the random number generator.

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng22 More Cards private void btnNextCard_Click(object sender, EventArgs e) { string s, s1, s2, s3, s4; s1 = rtbSpades.Text; s2 = rtbHearts.Text; s3 = rtbClubs.Text; s4 = rtbDiamonds.Text; s = spades.deal(); if (s != null) { if (s1 != "") s1 += ”\n"; s1 += s; btnStartAgain.Visible = true; } s = hearts.deal(); if (s != null) { if (s2 != "") s2 += ”\n"; s2 += s; } get the current string to add to it get next card in that suit

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng23 More Cards s = clubs.deal(); if (s != null) { if (s3 != "") s3 += ”\n"; s3 += s; } s = diamonds.deal(); if (s != null) { if (s4 != "") s4 += ”\n"; s4 += s; } rtbSpades.Text = s1; rtbHearts.Text = s2; rtbClubs.Text = s3; rtbDiamonds.Text = s4; } replace string with new entry

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng24 More Cards private void btnStartAgain_Click(object sender, EventArgs e) { btnStartAgain.Visible = false; spades.restore(); rtbSpades.Text = ""; hearts.restore(); rtbHearts.Text = ""; clubs.restore(); rtbClubs.Text = ""; diamonds.restore(); rtbDiamonds.Text = ""; }

© Copyright 2006 David Das, Ryan Lortie, Alan Wassyng25 A Little More on Classes  We have just scratched the surface on object-oriented programming.  The focus of this course is algorithms and their implementation in C# - so finer details on C# are out of scope.  However, for interest and completeness you may want to read a little on inheritance and polymorphism - especially inheritance.