Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Similar documents
Chapter 6 Introduction to Defining Classes

A A B U n i v e r s i t y

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

STUDENT LESSON A10 The String Class

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Strings. Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

CST242 Strings and Characters Page 1

JAVA GUI PROGRAMMING REVISION TOUR III

7. C++ Class and Object

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Strings. Strings and their methods. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

TEST (MODULE:- 1 and 2)

Chapter 4. Defining Classes I

Programming with Java

Fundamentals of Programming Session 25

Using Java Classes Fall 2018 Margaret Reid-Miller

More non-primitive types Lesson 06

Chapter 4 Defining Classes I

Fundamentals of Programming Session 23

Java s String Class. in simplest form, just quoted text. used as parameters to. "This is a string" "So is this" "hi"

Strings. Strings and their methods. Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

9 Working with the Java Class Library

Lecture 7: Classes and Objects CS2301

Programming II (CS300)

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Chapter 1 Introduction to java

Strings, Strings and characters, String class methods. JAVA Standard Edition

Introduction to Programming Using Java (98-388)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

COMP 111. Introduction to Computer Science and Object-Oriented Programming. Week 2

Basic Computation. Chapter 2

Lecture Notes for CS 150 Fall 2009; Version 0.5

Classes and Objects. CGS 3416 Spring 2018

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

by Pearson Education, Inc. All Rights Reserved. 2

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

Chapter 2 Working with Data Types and Operators

Datatypes, Variables, and Operations

Activity 9: Object-Oriented

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Slide 1 CS 170 Java Programming 1 More on Strings Duration: 00:00:47 Advance mode: Auto

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Data Structures (list, dictionary, tuples, sets, strings)

COE318 Lecture Notes Week 4 (Sept 26, 2011)

CST141 Thinking in Objects Page 1

Creating Strings. String Length

OBJECTS AND CLASSES CHAPTER. Final Draft 10/30/2011. Slides by Donald W. Smith TechNeTrain.com

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Index COPYRIGHTED MATERIAL

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Using Objects. a first example of an object classes and objects in Java

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CS313D: ADVANCED PROGRAMMING LANGUAGE

Creating an object Instance variables

Microsoft Visual Basic 2005: Reloaded

CS260 Intro to Java & Android 03.Java Language Basics

Java Primer 1: Types, Classes and Operators

The Irving K. Barber School of Arts and Sciences COSC 111 Final Exam Winter Term II Instructor: Dr. Bowen Hui. Tuesday, April 19, 2016

Eng. Mohammed Abdualal

CS 2530 INTERMEDIATE COMPUTING

String. Other languages that implement strings as character arrays

Introduction to Java

CS 231 Data Structures and Algorithms, Fall 2016

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 29: String and Object References Bradley Kjell (Revised 06/15/2008)

Java and OOP. Part 2 Classes and objects

Activity 9: Object-Oriented

Chapter 7. Inheritance

In Java there are three types of data values:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

CPS 109 Lab 2 Alexander Ferworn Updated Fall 05. Ryerson University. School of Computer Science CPS109. Lab 2

Defining Classes and Methods


Object Oriented Design

Overview of the Ruby Language. By Ron Haley

Section 2.2 Your First Program in Java: Printing a Line of Text

CS313D: ADVANCED PROGRAMMING LANGUAGE

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

CLASSES AND OBJECTS. Summer 2018

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

2559 : Introduction to Visual Basic.NET Programming with Microsoft.NET

Full file at

Chapter 3 Objects and Classes

ECOM 2324 COMPUTER PROGRAMMING II

CMPT 125: Lecture 3 Data and Expressions

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

CHAPTER 7 OBJECTS AND CLASSES

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

Cpt S 122 Data Structures. Introduction to C++ Part II

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Transcription:

Classes & Objects

Topics Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

Object-Oriented Programming Classes combine data and the methods (code) to manipulate the data Classes are a template used to create specific objects All C# programs consist of at least one class. 3-3

Example Student class Data: name, year, and grade point average Methods: store/get the value of each piece of data, promote to next year, etc. Student Object: student1 Data: Maria Gonzales, Sophomore, 3.5 3-4

Some Terminology Object reference: identifier of the object Instantiating an object: creating an object of a class Instance of the class: the object Methods: the code to manipulate the object data Calling a method: invoking a service for an object. 3-5

Class Data Members of a class: the class's fields and methods Fields: instance variables and class variables Fields can be: any primitive data type (int, double, etc.) objects Instance variables: variables defined in the class and given values in the object 3-6

What s in a Class Class contains Members are Fields Instance variables Class variables Methods 3-7

Encapsulation Instance variables are usually declared to be private, which means users of the class must reference the data of an object by calling methods of the class. Thus the methods provide a protective shell around the data. We call this encapsulation. Benefit: the class methods can ensure that the object data is always valid. 3-8

Naming Conventions Class names: start with a capital letter Object references: start with a lowercase letter In both cases, internal words start with a capital letter Example: class: Student objects: student1, student2 3-9

1. Declare an Object Reference Syntax: ClassName objectreference; or ClassName objectref1, objectref2 ; Object reference holds address of object Example: Date d1; d1 contains the address of the object, but the object hasn t been created yet 3-10

2. Instantiate an Object Objects MUST be instantiated before they can be used Call a constructor using new keyword Constructor has same name as class. Syntax: objectreference = new ClassName( arg list ); Arg list (argument list) is comma-separated list of initial values to assign to object data, and may be empty 3-11

Date Class API Constructor: special method that creates an object and assigns initial values to data Date( ) Date Class Constructor Summary creates a Date object with initial month, day, and year values of 1, 1, 2000 Date( int mm, int dd, int yy ) creates a Date object with initial month, day, and year values of mm, dd, and yy 3-12

Instantiation Examples Date independenceday; independenceday = new Date( 7, 4, 1776 ); Date graduationdate = new Date( 5, 15, 2008 ); Date defaultdate = new Date( ); 3-13

Objects After Instantiation Object Instances 3-14

Object Reference vs. Object Data Object references point to the location of object data. An object can have multiple object references pointing to it. Or an object can have no object references pointing to it. If so, the garbage collector will free the object's memory 3-15

Creating Aliases Date hiredate = new Date( 2, 15, 2003 ); Date promotiondate = new Date( 9, 28, 2004 ); promotiondate = hiredate; int x = 5, y = 3; x = y; 3-16

Two References to an Object After program runs, two object references point to the same object 3-17

null Object References An object reference can point to no object. In that case, the object reference has the value null Object references have the value null when they have been declared, but have not been used to instantiate an object. Attempting to use a null object reference causes a run time exception. 3-18

NullReference Date adate; adate.setmonth( 5 ); Date independenceday = new Date( 7, 4, 1776 ); // set object reference to null independenceday = null; // attempt to use object reference independenceday.setmonth(5); 3-19

String and StringBuilder string is like a primitive data type, but creates an immutable object once created, cannot be changed does not need to be instantiated StringBuilder is a class must be instantiated can be changed use StringBuilder when many concatenations needed 3-20

Reusability Reuse: class code is already written and tested, so you build a new application faster and it is more reliable Example: A Date class could be used in a calendar program, appointmentscheduling program, online shopping program, etc. 3-21

How To Reuse A Class You don't need to know how the class is written. You do need to know the application programming interface (API) of the class. The API is published and tells you: How to create objects What methods are available How to call the methods 3-22

The Argument List in an API Pairs of datatype variablename Specify Order of arguments Data type of each argument Arguments can be: Any expression that evaluates to the specified data type 3-23

Method Classifications Accessor methods Gets the values of object data Mutator methods Writes/changes values of object data Others to be defined later 3-24

Dot Notation Use when calling method to specify which object's data to use in the method Syntax: objectreference.methodname( arg1, arg2, ) Note: no data types are specified in the method call; arguments are values only! 3-25

Calling a Method 3-26

The length Method Return type Method name and argument list int length( ) returns the number of characters in the String Example: String hello = "Hello"; int len = hello.length( ); The value of len is 5 3-27

The touppercase and tolowercase Methods Return type Method name and argument list String touppercase( ) returns a copy of the String will all letters uppercase String tolowercase( ) returns a copy of the String will all letters lowercase Example: String hellostring = "Hello"; hellostring = hellostring.touppercase( ); The value of hellostring is "HELLO" 3-28

The indexof Methods Return type Method name and argument list int indexof( String searchstring ) returns the index of the first character of searchstring or -1 if not found int indexof( char searchchar ) returns the index of the first character of searchchar or -1 if not found The index of the first character of a String is 0. Example: String hello = "Hello"; int index = hello.indexof( 'e' ); The value of index is 1. 3-29

The substring Method Return type Method name and argument list String substring( int startindex, int endindex ) returns a substring of the String object beginning at the character at index startindex and ending at the character at index ( endindex 1 ) Example: String hello = "Hello"; String lostr = hello.substring( 3, hello.length()); The value of lostr is lo 3-30

Example 3.6 StringDemo.C# String s1 = new String( "OOP in C# " ); System.out.println( "s1 is: " + s1 ); String s2 = "is not that difficult. "; System.out.println( "s2 is: " + s2 ); String s3 = s1 + s2; // new String is s1, followed by s2 System.out.println( "s1 + s2 returns: " + s3 ); System.out.println( "s1 is still: " + s1 ); // s1 is unchanged System.out.println( "s2 is still: " + s2 ); // s2 is unchanged String greeting1 = "Hi"; // instantiate greeting1 System.out.println( "\nthe length of " + greeting1 + " is + greeting1.length( ) ); String greeting2 = new String( "Hello" ); // instantiate greeting2 int len = greeting2.length( ); // len will be assigned 5 System.out.println( "The length of " + greeting2 + " is " + len ); 3-31

Example 3.6 StringDemo.C# String empty = new String(); System.out.println( "The length of the empty String is " + empty.length()); String greeting2upper = greeting2.touppercase(); System.out.println(); System.out.println(greeting2 + " converted to upper case is " + greeting2upper ); String invertedname = "Lincoln, Abraham"; int comma = invertedname.indexof(','); // find the comma System.out.println( "\nthe index of " + ',' + " in + invertedname + " is " + comma ); // extract all characters up to comma String lastname = invertedname.substring(0, comma); System.out.println("Dear Mr. " + lastname); 3-32

When calling a method, include only expressions in your argument list. Including data types in your argument list will cause a compiler error. If the method takes no arguments, remember to include the empty parentheses after the method's name. The parentheses are required even if there are no arguments. 3-33

.NET Architecture Framework When you press F5 source code compiled into IL submitted to.net engine for execution 3-34

3-35

Base Class Libraries Need to become familiar with libraries to use most efficiently Group services under Namespace Most commonly accessed Namespaces are grouped under System Namespace 3-36

using Declaration Must have using statement to use values in library: using System.Text; Or you can fully qualify: System.Text.StringBuilder phrase = new System.Text.StringBuilder ( Change is inevitable ); 3-37