Collections and classes. Projects in this ppt Collection interface practice The microwave oven simulator Bank account.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Practical Programming COMP153-08S Lecture: Repetition Continued.
Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Summary of text to be covered Create your own class Create and use objects of your own class Control access to object instance variables Use keyword private.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Programming Based on Events
Control structures Part 2 iteration control To enable repetition of a statement block.
Odds and Ends Component Tray Menu and contextmenu Splash Screen.
VB Default Controls List Box, Combo Box
An array of controls Not particularly convenient in VB Examples: array of pictureboxes Array of textboxes Notes on Concentration game (a possible final.
Arrays & Strings part 2 More sophisticated algorithms, including sorting.
Arrays One dimensional arrays. Index of projects Random picture display Sum array values Display names in listbox Name search Largest/smallest Car sales.
Apply Sub Procedures/Methods and User Defined Functions
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Module 7: Object-Oriented Programming in Visual Basic .NET
Chapter 6 Procedures and Functions Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Why to Create a Procedure
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
COMPUTER PROGRAMMING I Objective 8.03 Apply Animation and Graphic Methods in a Windows Form (4%)
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Menus,MonthCalender, DateTimePicker, MDI,Tree View, List View,
Structural Pattern: Decorator There are times when the use of subclasses to modify the behavior of individual objects is problematic. C h a p t e r 4.
Chapter 9: Getting Comfortable with Object- Oriented Programming.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
11 Web Services. 22 Objectives You will be able to Say what a web service is. Write and deploy a simple web service. Test a simple web service. Write.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
1 Forms and Form elements CSD 340 McCoey. 2 Form Object Properties action Usually a call to a server elements encoding method post or get target Methods.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
VB.NET Additional Topics
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
Object-Oriented Application Development Using VB.NET 1 Chapter 6 Writing a Problem Domain Class Definition.
CIS 338: Classes and Modules Dr. Ralph D. Westfall May, 2011.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Microwave Oven Application Building Your Own Classes and Objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Chapter 4 Introduction to Classes, Objects, Methods and strings
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
05/02/ Records. 205/02/2016 Learning Objectives State: The difference between records and arrays. The difference between records and arrays. How.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Computer Science Up Down Controls, Decisions and Random Numbers.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Object-Oriented Programming: Classes and Objects
Apply Procedures to Develop Message, Input, and Dialog Boxes
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Object-Oriented Programming: Classes and Objects
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
GUI Programming in Visual Studio .NET
Presentation transcript:

Collections and classes

Projects in this ppt Collection interface practice The microwave oven simulator Bank account

A collection is Linear, like an array Not static, but extensible in length Homogeneous, but made up of key-value pairs that don’t have to have the same data type. You don’t always reference indices, although you may –You can reference “before” or “after” –You can get an index out of range error, as with an array, if you use an illegal index

Adding items Given Dim guys as Collection guys= new Collection Then use: mycollection.add(theItem) Or mycollection.add(theItem, keyval) Or mycollection.add(theItem, keyval,beforeguy) Or mycollection.add(theItem, keyval,,afterguy)

An interface to test collection

Suppose 9999 is added with key=“Special”

“Find” entry with key=“Special”

Code for Find button uses the collection.contains() method Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click Dim key As String key = txtKey.Text If Guys.Contains(key) Then lstdisplay.Items.Add("Found") Else lstdisplay.Items.Add("Not Found") End If End Sub

Removing “Special” value

Adding “before” another key uses collection.add(data,key,beforewhichone) Private Sub btnBefore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBefore.Click data = txtData.Text keyVal = txtKey.Text Dim before As String before = txtPosition.Text Guys.Add(data, keyVal, before) Display() End Sub

Add Jim before Sue

Add before another key

Object-Oriented programming Object-oriented languages make abstractions and simplifications of real world entities which need to be modeled to define “objects” in the software. Essential features of the real-world “object” are “properties” of the software object. Essential functionality of the real-world entity becomes a service or method or function of the software entity. Objects have fields and methods. Fields hold values, methods do stuff. A “student” object might have fields: name, address, GPA, and so on.

Object-Oriented programming continued Simple accessor and mutator methods allow a programmer to get or set field values of an object. Get and set are the words which are used to define these methods in VB, although attributes of a class called its “properties” can also be used to get at this information in VB So a student object might have a String field called Name, and methods getName() to retrieve the name and setName(toNewName) to change the name. We won’t talk about these methods for quite a while, but in VB they are called subs and functions.

OOP…building your own classes in VB OOP refers to object-oriented programming. A class is a definition of an object. Objects are programmatic analogies to real-life entities. Objects have fields, constructors and methods. VB supports objects. Notice, your form is a “class”. A class is an instantiation of an object

Microwave project

Building a class: select vb class

Code for time class Public Class Time Dim minutes, seconds As Integer Public Sub New(ByVal mtmp As Integer, ByVal sectmp As Integer) minutes = mtmp seconds = sectmp End Sub Public Sub New() minutes = 0 seconds = 0 End Sub End Class

constructor Constructor for a class is New() Multiple constructors are allowed – see previous slide Arguments passed depend on the class being constructed

Adding an instance of a class to a project

Getter/Setter methods (Accessor and mutator methods) Accessor methods are called get Mutator methods are called set These appear in VB as properties of fields

Time class with minute property Public Class Time Dim minutes, seconds As Integer Public Sub New(ByVal mtmp As Integer, ByVal sectmp As Integer) minutes = mtmp seconds = sectmp End Sub Public Property minute() As Integer Get ‘code goes here End Get Set(ByVal Value As Integer) ‘code goes here End Set End Property End Class

Constructor setting properties Public Sub New(ByVal mtmp As Integer, ByVal sectmp As Integer) minute = mtmp 'using type checking of property second = sectmp End Sub

The entire time class Public Class Time Dim minutes, seconds As Integer Public Sub New(ByVal mtmp As Integer, ByVal sectmp As Integer) minute = mtmp 'using type checking of property second = sectmp End Sub Public Property minute() As Integer Get Return minutes End Get Set(ByVal Value As Integer) If Value >=0 and Value < 60 Then minutes = Value Else minutes = 0 End If End Set End Property Public Property second() As Integer Get Return seconds End Get Set(ByVal Value As Integer) If Value >=0 and Value < 60 Then seconds = Value Else seconds = 0 End If End Set End Property End Class

Timer object you can get a timer from the tookbox and add it (to your component tray)

Microwave, running

Microwave done

Using a timer Be sure to enable your timer. Tmrclock.enabled=true ' Sets the timer interval to 1 seconds. tmrclock.Interval = 1000 strtime = "“ Be sure to start your timer Tmrclock.start() In the microwave example, the timer needs to be “reset” when they press the start button.

The clock tick sub Private Sub tmrClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrclock.Tick If timeobj.second > 0 Then timeobj.second -= 1 ElseIf timeobj.minute > 0 Then timeobj.minute -= 1 timeobj.second = 59 Else tmrclock.Enabled = False Beep() Lbl.Text = "done" pnl.BackColor = Color.Black Return End If Lbl.Text = String.Format("{0:d2}:{1:d2}", timeobj.minute, timeobj.second) End Sub

Display time sub Sub displayTime() Dim intsec, intmin As Integer Dim strdisplay As String If strtime.length > 4 Then strtime = strtime.substring(0, 4) End If strdisplay = strtime.padleft(4, convert.tochar("0")) intsec = Convert.ToInt32(strdisplay.Substring(2)) intmin = Convert.ToInt32(strdisplay.Substring(0, 2)) timeobj = New Time(intmin, intsec) Lbl.Text = String.Format("{0:D2}:{1:D2}", intmin, intsec) End Sub

Start button Private Sub start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click Dim intsec As Integer Dim intmin As Integer strtime = strtime.PadLeft(4, Convert.ToChar("0")) intsec = Convert.ToInt32(strtime.substring(2)) intmin = Convert.ToInt32(strtime.Substring(0, 2)) timeobj = New Time(intmin, intsec) Lbl.Text = String.Format("{0:d2}:{1:d2}", timeobj.minute, timeobj.second) strtime = "" tmrClock.enabled = True pnl.BackColor = Color.Yellow End Sub

Clear button Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click Lbl.Text = "Microwave" strtime = "" timeobj = New Time(0, 0) tmrClock.enabled = False pnl.BackColor = Color.Black End Sub

A button click Private Sub Btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click Beep() strtime &= "1" displayTime() End Sub

A sort of bank account project

Button events First and last buttons display respectively the customers who are first and last in the customer array Next displays the next customer and previous displays the previous customer

The account class Public Class Account Dim first, last, middle As String Dim balance As Double Dim accountnum As String ‘constructor sets name fields, balance and account number info Public Sub New(ByVal ftmp As String, ByVal mtmp As String, ByVal ltmp As String, ByVal bal As Double, ByVal acct As String) firstName = ftmp midName = mtmp lastName = ltmp acctBalance = bal accountNumber = acct End Sub Public Property firstName() As String Get Return first End Get Set(ByVal ftmp As String) first = ftmp End Set End Property

Account class continued Public Property midName() As String Get Return middle End Get Set(ByVal ftmp As String) middle = ftmp End Set End Property Public Property lastName() As String Get Return last End Get Set(ByVal ftmp As String) last = ftmp End Set End Property Public Property acctBalance() As Double Get Return balance End Get Set(ByVal ftmp As Double) balance = ftmp End Set End Property Public Property accountNumber() As String Get Return accountnum End Get Set(ByVal ftmp As String) accountnum = ftmp End Set End Property End Class

Bank proj uses an array of accounts Dim customers As Account() Const num = 20 Dim current As Integer = -1

accounts Dim i As Integer For i = 1 To num customers(i) = New Account("bob" & i, "micky" & i, "jones" & i, 1.0, "0" & i & "0" & i) Next

The set button sets current customer fields to updated content

Revisiting this entry shows updated data

The get button Get button should take a last name or account number and display that person’s information

Data is retrieved

One of the two lookup functions Private Function lookupName(ByVal last As String) As Integer Dim temp As Integer = 0 Dim j As Integer For j = 1 To num If last = customers(j).lastName Then temp = j End If Next lookupName = temp End Function

Optional arguments There are ways to get around writing two different lookup functions, one for a last name and one for an account number. We could send either last name or account number along with a control parameter (1=lastname supplied, 2= accountnumber supplied) C++ and VB allow optional arguments to be passed. It doesn’t save a lot of work, but a later slide shows the code with optional arguments.

Preparing to look for a name

Customer is found by last name or acct

Code for get button Private Sub btnGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGet.Click Dim find As Integer If txtLast.Text = "" And txtAccount.Text = "" Then MessageBox.Show("you must enter name or account number", "data entry error", MessageBoxButtons.OK, MessageBoxIcon.Error) Return Else If txtLast.Text = "" Then find = lookup(, txtAccount.Text) Else find = lookup(txtLast.Text, ) End If If find < 1 Then MessageBox.Show("you must enter valid name or account number", "data entry error", MessageBoxButtons.OK, MessageBoxIcon.Error) Else current = find display() End If End Sub