Unformatted text preview:

Slide 1OutlineSlide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12UML Class Diagram - One of the diagrams in UMLSlide 14More resources for UMLAssociations between Classes3.3 Declaring a Method with a ParameterRevised GradeBook.java ()Slide 19ExerciseSlide 21Slide 22Slide 23Slide 243.4 Instance Variables, set Methods and get MethodsRevised GradeBook.java classSlide 27Slide 28Slide 29Slide 30Slide 31Slide 323.5 Primitive Types vs. Reference TypesSlide 343.6 Initializing Objects with ConstructorsSlide 36Slide 37Slide 38Slide 39Slide 403.7 float and doubleA new class: AccountSlide 43Slide 44Slide 453.8 (Optional) GUI and Graphics Case Study: Using Dialog BoxesSlide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Object-Oriented Programming- Classes, Objects Methods, Strings1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. YangOutline2•Classes•Objects•Methods•Parameters•double primitive type•GUI dialog boxes343.2DeclaringaClasswithaMethodandInstantiatinganObjectofaClass•Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the .java file-name extension. •Keyword public is an access modifier. –Indicates that the class is “available to the public”–W/o the keyword public, the class is ‘package-private'Modifier Class Package Subclass Worldpublic Y Y Y Ypro tected Y Y Y Nno modifier (package-private)Y Y N Nprivate Y N N Nsource: http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.htmlclass5•MethodsoThe main method is called automatically by the Java Virtual Machine (JVM) when you execute an application. Normally, the calling method must call methods explicitly to tell them to perform their tasks. oA public method is “available to the public”It can be called from methods of other classes. Four different access modifier for methodsoThe return type specifies the type of data the method returns after performing its task. oReturn type void indicates that a method will perform a task but will not return (i.e., give back) any information to its calling method when it completes its task.6•Methods (cont.)oBy convention, method names begin with a lowercase first letter and subsequent words in the name begin with a capital letter.oEmpty parentheses after the method name indicate that the method does not require additional information to perform its task. oTogether, everything in the first line of the method is typically called the Method headeroEvery method’s body is delimited by left and right braces.oThe method body contains one or more statements that perform the method’s task.7•Example Class Definition: GradeBook.java8oUse class GradeBook in an application: GradeBookTest.javaoClass GradeBook is not an application because it does not contain main. oCan’t execute GradeBook; will receive an error message like: Exception in thread "main" java.lang.NoSuchMethodError: mainoMust either declare a separate class that contains a main method or place a main method in class GradeBook. oUse a separate class containing method main( ) to test each new class. oSome programmers refer to such a class as a driver class.9•Compiling an Application with Multiple Source Files:–Compile the classes in Fig. 3.1 and Fig. 3.2. –Type the commandjavac GradeBook.java GradeBookTest.java–If the directory containing the application includes only this application’s files, you can compile all the classes in the directory with the commandjavac *.java10•A static method (such as main) is special.–It can be called without first creating an object of the class in which the method is declared.•c.f., instance method: Typically, you cannot call a method that belongs to another class until you create an object of that class.•Declare a variable of the class type. –Each new class you create becomes a new type that can be used to declare variables and create objects.–You can declare new class types as needed; this is one reason why Java is known as an extensible language.11•Creating an instance out of a class: –Keyword new creates a new object of the class specified to the right of the keyword. –Used to initialize a variable of a class type.–The parentheses to the right of the class name are required. –Parentheses in combination with a class name represent a call to a constructor, which is similar to a method but is used only at the time an object is instantiated.12•Calling a method:–Variable name followed by a dot separator (.), the method name and parentheses. –Calling a method causes the object to perform its task defined in that method. •Calling the main method:–Any class can contain a main method.–The JVM invokes the main method only in the class used to execute the application. –If multiple classes that contain main, then one that is invoked is the one in the class named in the java command.13UML Class Diagram- One of the diagrams in UML•Source: http://ias.uni-klu.ac.at/projects/uml/ECOOP99/sld012.htm14•Figure 3.3: UML class diagram for class GradeBook. •Each class is modeled in a class diagram as a rectangle with three compartments. –Top: contains the class name centered horizontally in boldface type. –Middle: contains the class’s attributes, which correspond to instance variables. –Bottom: contains the class’s operations, which correspond to methods. •Operations are modeled by listing the operation name preceded by an access modifier (in this case +) and followed by a pair of parentheses. -The plus sign (+) corresponds to the keyword public.#: protected -: privateMore resources for UML•http://sce.uhcl.edu/yang/teaching/UML_Resources.html•A sample Use Case Diagram http://ias.uni-klu.ac.at/projects/uml/ECOOP99/sld022.htm 15Associations between Classes•Source: http://ias.uni-klu.ac.at/projects/uml/ECOOP99/sld053.htm 16173.3DeclaringaMethodwithaParameter•Parameter: Additional information a method needs to perform its task.•A method can require one or more parameters that represent additional information it needs to perform its task.–Defined in a comma-separated parameter list–Located in the parentheses that follow the method name –Each parameter must specify a type and an identifier. •A method call supplies values — called arguments — for each of the method’s


View Full Document

UHCL CSCI 3134 - Object Oriented Programming

Download Object Oriented Programming
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 Object Oriented Programming 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 Object Oriented Programming 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?