Unformatted text preview:

Chapter 4IntroductionClass DefinitionsA Class Is a TypePrimitive Type Values vs. Class Type ValuesThe Contents of a Class DefinitionSample Class DefinitionThe new OperatorInstance Variables and MethodsCreating ObjectsSlide 11File Names and LocationsInvoking MethodsSample Driver (Application Program)More About MethodsSlide 16main is a void Methodreturn StatementsSlide 19Slide 20Sample methodAnother Sample MethodMethod DefinitionsAny Method Can Be Used As a void MethodSlide 25Demo Sample Program: dateSecondTryLocal VariablesGlobal VariablesBlocksDeclaring Variables in a for StatementDeclaring Variables in a for Statement (an example)Parameters of a Primitive TypeSlide 33Slide 34Sample Method with ParametersInvoking Method with ParametersSlide 37Slide 38Slide 39A Formal Parameter Used as a Local Variable (Part 1 of 5)A Formal Parameter Used as a Local Variable (Part 2 of 5)A Formal Parameter Used as a Local Variable (Part 3 of 5)A Formal Parameter Used as a Local Variable (Part 4 of 5)A Formal Parameter Used as a Local Variable (Part 5 of 5)Pitfall: Use of the Terms "Parameter" and "Argument"The this ParameterAn example of Using this ParameterSlide 48Slide 49An example of when this parameter must be usedSlide 51Slide 52Slide 53Slide 54Methods That Return a Boolean ValueThe methods equals and toStringTesting MethodsThe Fundamental Rule for Testing MethodsInformation Hiding and EncapsulationA Couple of Important Acronyms: API and ADTpublic and private ModifiersAccessor and Mutator MethodsEncapsulationPreconditions and PostconditionsOverloadingPitfall: You Can Not Overload Based on the Type ReturnedConstructorsSlide 68You Can Invoke Another Method in a ConstructorA Constructor Has a this ParameterInclude a No-Argument ConstructorDefault Variable InitializationsThe StringTokenizer ClassSome Methods in the StringTokenizer Class (Part 1 of 2)Some Methods in the StringTokenizer Class (Part 2 of 2)Slide 76Chapter 4Defining Classes ICopyright © 2008 Pearson Addison-Wesley. All rights reservedIntroduction•Classes are the most important language feature that make object-oriented programming (OOP) possible•Programming in Java consists of defining a number of classes–Every program is a class–All helping software consists of classes–All programmer-defined types are classes•Classes are central to Java4-2Copyright © 2008 Pearson Addison-Wesley. All rights reservedClass Definitions•You already know how to use classes and the objects created from them, and how to invoke their methods–For example, you have already been using the predefined String and Scanner classes• Now you will learn how to define your own classes and their methods, and how to create your own objects from them4-3Copyright © 2008 Pearson Addison-Wesley. All rights reservedA Class Is a Type•A class is a special kind of programmer-defined type, and variables can be declared of a class type•A value of a class type is called an object or an instance of the class–If A is a class, then the phrases "bla is of type A," "bla is an object of the class A," and "bla is an instance of the class A" mean the same thing•A class determines the types of data that an object can contain, as well as the actions it can perform4-4Copyright © 2008 Pearson Addison-Wesley. All rights reservedPrimitive Type Values vs. Class Type Values•A primitive type value is a single piece of data•A class type value or object can have multiple pieces of data, as well as actions called methods–All objects of a class have the same methods–All objects of a class have the same pieces of data (i.e., name, type, and number)–For a given object, each piece of data can hold a different value 4-5Copyright © 2008 Pearson Addison-Wesley. All rights reservedThe Contents of a Class Definition•A class definition specifies the data items and methods that all of its objects will have•These data items and methods are sometimes called members of the object•Data items are called fields or instance variables•Instance variable declarations and method definitions can be placed in any order within the class definition4-6Copyright © 2008 Pearson Addison-Wesley. All rights reserved4-7Sample Class Definition•public class DateFirstTry{ public String month; public int day; public int year; //a four digit number. public void writeOutput( ) { System.out.println(month + " " + day + ", " + year); }}The new Operator•An object of a class is named or declared by a variable of the class type: ClassName classVar; DateFirstTry birthDay;•The new operator must then be used to create the object and associate it with its variable name: classVar = new ClassName(); birthDay = new DateFirstTry ( );•These can be combined as follows: ClassName classVar = new ClassName(); DateFirstTry birthDay = new DateFirstTry ( ); 4-8Copyright © 2008 Pearson Addison-Wesley. All rights reservedInstance Variables and Methods•Instance variables can be defined as in the following two examples–Note the public modifier (for now): public String instanceVar1; public int instanceVar2;•In order to refer to a particular instance variable, preface it with its object name as follows:objectName.instanceVar1objectName.instanceVar2birthday.month;birthday.day;Birthday.year;4-9Copyright © 2008 Pearson Addison-Wesley. All rights reserved4-10Creating Objects• DateFirstTry date1, date2; date1 = new DateFirstTry( ); date2 = new DateFirstTry( ); date1.month = "December"; date1.day = 31; date1.year = 2007; date2.month = "July"; date2.day = 4; date2.year = 1776;•Method definitions are divided into two parts: a heading and a method body: public void myMethod() Heading { code to perform some action Body and/or compute a value } •Methods are invoked using the name of the calling object and the method name as follows: classVar.myMethod();date1.writeOutput( );•Invoking a method is equivalent to executing the method bodyInstance Variables and Methods4-11Copyright © 2008 Pearson Addison-Wesley. All rights reservedFile Names and Locations•Reminder: a Java file must be given the same name as the class it contains with an added .java at the end–For example, a class named MyClass must be in a file named MyClass.java•For now, your program and all the classes it uses should be in the same directory or folder4-12Copyright © 2008 Pearson


View Full Document

SJU CSC 3405 - Absolute Java

Download Absolute Java
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 Absolute Java 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 Absolute Java 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?