DOC PREVIEW
IIT CS 201 - Introduction to Object-Oriented Programming: Using Classes

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

1Chapter 3Introduction to Object-Oriented Programming: Using ClassesOO ProgrammingCS201 Home PageFindWeighted AveragePrintWeighted AverageModule Structure ChartMainPrint DataPrint HeadingGet DataPrepare File for ReadingCS201 Home PageTwo Design Strategies FUNCTIONFUNCTIONFUNCTIONOBJECT. . . . ........OBJECT. . . . ........OBJECT. . . . ........FUNCTIONAL OBJECT-ORIENTEDDECOMPOSITION DESIGNCS201 Home PageTopics• Class Basics and Benefits• Creating Objects Using Constructors• Calling Methods• Using Object References• Calling Static Methods and Using Static Class Variables• Using Predefined Java ClassesCS201 Home PageObject-Oriented Programming• Classes combine data and the methods(code) to manipulate the data• Classes are a template used to create specific objects• All Java programs consist of at least one class.• Two types of classes– Application/Applet classes– Service classes2CS201 Home PageExample• 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.5CS201 Home PageSome 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. CS201 Home PageClass Data• Instance variables: variables defined in the class and given values in the object• Fields: instance variables and staticvariables (we'll define static later)• Members of a class: the class's fields and methods• Fields can be:– any primitive data type (int, double, etc.)– objects CS201 Home PageEncapsulation• 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.CS201 Home PageNaming 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: Studentobjects: student1, student2CS201 Home PageReusability• Reuse: class code is already written and tested, so you build a new application faster and it is more reliableExample: A Date class could be used in a calendar program, appointment-scheduling program, online shopping program, etc.3CS201 Home PageHow 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 methodsCS201 Home Page1. Declare an Object ReferenceSyntax:ClassName objectReference;or ClassName objectRef1, objectRef2…;• Object reference holds address of object• Example:Date d1;CS201 Home Page2. 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 dataCS201 Home PageConstructor• Constructor method special method with the same name as the class that is used with newwhen a class is instantiatedpublic class name{public name(String frst,String lst){first = frst;last = lst;}}Name name;name = new Name(“john”, “Dewey”);Note: argument cannot be the same as fieldCS201 Home PageDate Class APIconstructor: special method that creates an object and assigns initial values to dataDate( int mm, int dd, int yy )creates a Date object with initial month, day, and year values of mm, dd, and yyDate( )creates a Date object with initial month, day, and year values of 1, 1, 2000Date Class Constructor SummaryCS201 Home PageInstantiation Examples Date independenceDay;independenceDay = new Date( 7, 4, 1776 );Date graduationDate = new Date( 5, 15, 2008 );Date defaultDate = new Date( );Example Next Slide4CS201 Home PageExample 3.1 Constructors.javapublic class Constructors{public static void main( String [] args ){Date independenceDay;independenceDay = new Date( 7, 4, 1776 );Date graduationDate = new Date( 5, 15, 2008 );Date defaultDate = new Date( );}}Figure Next SlideCS201 Home PageObjects After InstantiationCS201 Home PageCalling a MethodCS201 Home PageMethod Classifications• Accessor methods– get…– gives values of object data• Mutator methods– set…– change values of object dataCS201 Home PageDate Class MethodsMethod name and argument listReturn valuegetMonth( )returns the value of monthintgetDay( )returns the value of dayintgetYear( )returns the value of yearintsetMonth( int mm )sets the value of month to mmvoidsetDay( int dd )sets the value of day to ddvoidsetYear( int yy )sets the value of year to yyvoidCS201 Home PageThe Argument List in an API• Pairs ofdataType variableName• Specify– Order of arguments– Data type of each argument• Arguments can be:– Any expression that evaluates to the specified data type5CS201 Home Page• 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.CS201 Home PageVoid Methods• Void method Does not return a valueSystem.out.print(“Hello”);System.out.println(“Good bye”);name.setName(“CS”, “201”);object method argumentsCS201 Home PageValue-Returning Methods• Value-returning method Returns a value to the calling programString first; String last;Name name;System.out.print(“Enter first name: “);first = inData.readLine();System.out.print(“Enter last name: “);last = inData.readLine();name.setName(first, last);CS201 Home PageValue-returning examplepublic String firstLastFormat(){return first + “ “ + last;}System.out.print(name.firstLastFormat());object method object methodArgument to print method is string returned fromfirstLastFormat methodCS201 Home PageMethod Return Values• Can be a primitive data type, class type, or void• A value-returning


View Full Document

IIT CS 201 - Introduction to Object-Oriented Programming: Using Classes

Download Introduction to Object-Oriented Programming: Using Classes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Object-Oriented Programming: Using Classes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Object-Oriented Programming: Using Classes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?