Java, Java, Java R. Morelli Object-Oriented Problem Solving

Slides:



Advertisements
Similar presentations
Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view class.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Introduction to C Programming
 2005 Pearson Education, Inc. All rights reserved Introduction.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Chapter 3: OOD and Writing Worker Classes Clark Savage Turner, Ph.D. Copyright 2003, Csturner, adapted from notes.
Datalogi A 2: 15/9. Java Slides based on Horstmann chapter 2&3 Objects and classes Import, methods, references Implementing a class.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Java Programming, 3e Concepts and Techniques Chapter 2 - Part 2 Creating a Java Application and Applet.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
The Java Programming Language
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Chapter 5 Introduction to Defining Classes
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli | Ralph Walde Trinity College Hartford, CT presentation slides for published by Prentice.
Creating a Java Application and Applet
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
3 Introduction to Classes and Objects.
Chapter 3 Syntax, Errors, and Debugging
Chapter 2 Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 4: Writing classes
Chapter 2: Basic Elements of Java
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Object Oriented Programming in java
Presentation transcript:

Java, Java, Java R. Morelli Object-Oriented Problem Solving presentation slides for JAVA, JAVA, JAVA Object-Oriented Problem Solving Third Edition Ralph Morelli | Ralph Walde Trinity College Hartford, CT published by Prentice Hall Copyright 2000 by Ralph Morelli. All rights reserved.

Java, Java, Java R. Morelli Object Oriented Problem Solving Chapter 2 Objects: Using, Creating, and Defining Copyright 2000 by Ralph Morelli. All rights reserved.

Objectives Learn how to use variables to store data. Be familiar with creating and using objects. Understand the relationship between classes and objects. Understand the difference between objects and primitive data. Understand the difference between the static and instance elements of a class. Be able to understand and a simple class in Java. Be familiar with some of the basic principles of object-oriented programming. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Java, Java, Java R. Morelli Outline Using String Objects Drawing Shapes with a Graphics Object Class Definition Case Study: Simulating a Two-Person Game From the Java Library: java.util.Scanner Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects Copyright 2000 by Ralph Morelli. All rights reserved.

Using String Objects Strings are used in most computer programs. A partial representation of the String class. If we have a String object named str, here’s how we print its length: System.out.println(str.length()); // Print str’s length Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Creating and Combining Strings To create a String object in a program, first declare a String variable. Use new and a String constructor to create an object; String str; // Declare a String variable named str str = new String(“Hello”); // Create a String object String str2 = new String(“”); // Declare and create an empty String Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

String Examples Using the length() and concat() methods. Shortcut way of doing the same thing. Using the equals() method. System.out.println(str.length()); // Prints 5 System.out.println(str2.length()); // Prints 0 String s1 = new String(“George ”); String s2 = new String(“Washington”); System.out.println(s1.concat(s2)); // Prints George Washington String s1 = “George ”; String s2 = “Washington”; System.out.println(s1 + s2); // Prints George Washington String s1 = “Hello”, s2 = “Hello”, s3 = “hello”; String s4; // s4 is null System.out.println(s1.equals(s2)); // Prints true System.out.println(s1.equals(s3)); // Prints false System.out.println(s4.equals(s3)); // Null pointer error because s4 is null Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

What is printed by this program? public class StringPuns { public static void main(String args[]) { String s = new String("string"); String s1 = s.concat(" puns."); System.out.println("Here are the top 5 " + s1); String s2 = "5. Hey baby, wanna "; String s3 = s + " along with me."; System.out.println(s2 + s3); System.out.println("4. I've got the world on a " + s + "."); String s4 = new String("two"); String s5 = ". You have more class than a "; System.out.print(s4.length()); System.out.println(s5 + s + " of pearls."); System.out.print("2. It is "); System.out.print(s.equals("string")); System.out.println(" that I am no " + s + " bean."); String s6 = " quintet."; System.out.println("1. These puns form a " + s + s6); } // main() } // StringPuns class Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Using a Graphics Object A Graphics object was used in HelloWorldApplet to draw “HelloWorld” on a browser: public void paint (Graphics g) { g.drawString(“Hello World”, 10, 10); g.drawString(“Welcome to Java”, 10, 35); } In a Java window, the origin of the coordinate system, the point (0,0), is at the top-left corner. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Graphics Drawing Methods The Graphics class contains useful drawing methods. Some Examples: g.setColor(Color.blue); // Sets the drawing color to blue g.fillRect(25, 25, 140, 40); // Draws a 140x40 blue rectangle // at coordinate (25,25) g.setColor(Color.black); // Sets the drawing color to black g.drawRect(25,25,140,40); // Draws the rectangle outline Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The HelloWorldGraphic Applet The HelloWorldGraphic applet draws the following picture Click the link for the source code or click here to run it. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Class Definition Five basic design questions: What role will the object perform? What data or information will it need? What actions will it take? What public interface will it present to other objects? What information will it hide (keep private) from other objects? Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The Riddle Class A class is a blueprint. It describes an object's form but it has no content. The instance variables, question and answer, have no values yet. The class contains an object’s method definitions Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The Riddle Class Definition public class Riddle { private String question; //Instance variables private String answer; public Riddle(String q, String a) // Constructor { question = q; answer = a; } // Riddle constructor public String getQuestion() // Instance method { return question; } // getQuestion() public String getAnswer() // Instance method { return answer; } //getAnswer() } //Riddle class A public class is accessible to other classes Instance variables are usually private An object’s public methods make up its interface Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The RiddleUser Class The RiddleUser class will create and use 1 or more Riddle instances. An application has a main() method Figure 2.13. The user interface handles interactions between the user and the rest of the program. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The RiddleUser Class Definition public class RiddleUser { public static void main(String argv[]) { Riddle riddle1 = new Riddle( "What is black and white and red all over?", "An embarrassed zebra."); Riddle riddle2 = new Riddle( "What is black and white and read all over?", "A newspaper."); System.out.println("Here are two riddles:"); System.out.println(riddle1.getQuestion()); System.out.println(riddle2.getQuestion()); System.out.println("The answer to the first riddle is:"); System.out.println(riddle1.getAnswer()); System.out.println("The answer to the second is:"); System.out.println(riddle2.getAnswer()); } // main() } // RiddleUser An application must have a main() method Object Creation Object Use Object Use Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Define, Create, Use Class definition: Define one or more classes (Riddle, RiddleUser) Object Instantiation: Create objects as instances of the classes (riddle1, riddle2) Object Use: Use the objects to do tasks (riddle1.getAnswer() ) Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Case Study: A Two-Person Game Design Steps Problem Specification Problem Decomposition Class Design: OneRowNim Method Decomposition Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Problem Specification Design a program that simulates the game of One-Row Nim with a row of sticks. A OneRowNim object will keep track of how many sticks remain and whose turn it is. A OneRowNim object should allow a player to pick up 1, 2, or 3 sticks. A OneRowNim object should know when the game is over and who won the game. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Problem Decomposition What objects do we need? The OneRowNim object will represent and manage the game. We design OneRowNim to be used with different kinds of interfaces (Chapter 4). Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Class Design: OneRowNim State: Two int variables, nSticks and player nSticks keeps tracks of the remaining sticks. player keeps track of whose turn it is. Methods: A takeOne() method to pick up 1 stick. A takeTwo() method to pick up 2 sticks. A takeThree() method to pick up 3 sticks. A report() method describes the game’s state. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

OneRowNim Class Specification Class Name: OneRowNim Role: To represent and simulate a One-Row Nim game Information Needed (instance variables) nSticks: Stores the number of sticks left (private) player : Stores whose turn it is (private) Manipulations Needed (public methods) takeOne(), takeTwo(), takeThree()-- Methods to pick up 1, 2, or 3 sticks. report(): A method to report the current state of the game (nSticks and player ) Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

OneRowNim Class Definition public class OneRowNim { private int nSticks = 7; // Start with 7 sticks. private int player = 1; //Player 1 plays first. public void takeOne() { nSticks = nSticks - 1; player = 3 - player; } // takeOne() public void takeTwo() { nSticks = nSticks - 2; } // takeTwo() public void takeThree() { nSticks = nSticks - 3; } // takeThree() public void report() { System.out.println("Number of sticks left: " + nSticks); System.out.println("Next turn by player " + player); } // report() } // OneRowNim1 class Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The OneRowNim Class A class is a blueprint. In this case every OneRowNim created will… Have 7 sticks. Player 1 will have the first turn. But after calling the game.takeThree() method, the game object will change to: Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The Class Header Example: In General: public class OneRowNim In General: ClassModifiersopt class ClassName Pedigreeopt public class OneRowNim extends Object Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Identifiers An identifier is a name for a variable, method, or class. Rule: An identifier in Java must begin with a letter, and may consist of any number of letters, digits, and underscore (_) characters. Legal: OneRowNim, takeOne, nSticks Illegal: One Row Nim, 5sticks, game$, n! Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Declaring Instance Variables Examples: // Instance variables private int nSticks = 7; private int player = 1; In General FieldModifiersopt TypeId VariableId Initializeropt Fields or instance variables have class scope. Their names can be used anywhere within the class. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Public/Private Access Instance variables should usually be declared private. This makes them inaccessible to other objects. Generally, public methods are used to provide carefully controlled access to the private variables. An object’s public methods make up its interface -- that part of its makeup that is accessible to other objects. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Public/Private Access (cont) Possible Error: Public instance variables can lead to an inconsistent state Example: Suppose we make nSticks and player public variables. nim.nSticks = -1; // Inconsistent nim.player = 0; // State The proper way to change the game’s state: nim.takeOne(); // takeOne() is public method Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Java’s Accessibility Rules Packages contain classes which contain members (methods and fields). Access is determined from the top down. If no explicit declaration given, a default is used. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Initializer Expressions General Form: Variable = expression The expression on the right of the assignment operator (=) is evaluated and its value is stored in the variable on the left. Examples: private int nSticks = 7; private int player = “joe”; // Type error Type error: You can’t assign a String value to an int variable Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Method Definition Example The Method Header public void MethodName() // Method Header { // Start of method body } // End of method body The Method Header MethodModifiersopt ResultType MethodName (ParameterList ) public static void main (String argv[] ) public void takeOne () public void takeTwo () public void report () Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Method Definition Header: This method, named takeOne, is accessible to other objects (public), and does not return a value (void). public void takeOne() { nSticks = nSticks - 1; player = 3 - player; } // takeOne() Body: a block of statements that removes one stick and changes the player’s turn Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Designing Methods The public methods serve as a class’s interface. If a method is intended to be used to communicate with or pass information to an object, it should be declared public. A class’s methods have class scope. They can be used anywhere within the class. Methods that do not return a value should be declared void. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

The Simple Assignment Statement General Form: VariableName = Expression The expression on the right of the assignment operator (=) is evaluated and its value is stored in the variable on the left. Examples: nSticks = nSticks - 1; player = 3 - player; player = “joe”; // Type error Type error: The value being assigned must be the same type as the variable. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Creating OneRowNim Instances A reference variable refers to an object by storing the address of the object. // Declare a reference variable OneRowNim game; game The reference variable, game, will refer to a OneRowNim objet, (b), but its initial value is null. (a) (b) // Create an instance game = new OneRowNim(); game (c) After instantiation, game refers to a OneRowNim object (c) Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Using OneRowNim Objects Objects are used by calling one of their public methods: game.report(); // Tell game to report game.takeOne(); // Tell game to take one stick away Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

OneRowNimTester Application The OneRowNimTester is an application with main() method. public class OneRowNimTester { public static void main(String args[]) { OneRowNim1 game = new OneRowNim(); game.report(); game.takeThree(); game.takeOne(); } //main() } Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Tracing OneRowNim To trace OneRowNim, download its sourcecode, compile it and run it. OneRowNim.java OneRowNimTester.java See also Figure 2.22 on page 88. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Method Call and Return A method call causes a program to transfer control to the first statement in the called method. A return statement returns control to the calling statement. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Object Oriented Design Encapsulation: The OneRowNim class encapsulates a state and a set of actions. Information Hiding: OneRowNim’s state is defined by private variables, nSticks and player. Interface: OneRowNim’s interface is defined in terms of its public methods. Generality/Extensibility: We can easily extend OneRowNim’s functionality. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

From the Library: java.util.Scanner The java.util.Scanner class is new in J2SE 5.0 It provides a simple means of reading keyboard input in Java. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Example: Input a Number import java.util.Scanner; public class TestScanner { public static void main(String[] args) { // Create Scanner object Scanner sc = new Scanner(System.in); System.out.print("Input an integer:"); // Prompt int num = sc.nextInt(); // Read an integer System.out.println(num + " squared = " + num*num); } //main() } // TestScanner class Read an integer. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Example: Waiting for User Input import java.util.Scanner; public class TestScanner2 { public static void main(String[] args) { // Create Scanner object Scanner sc = new Scanner(System.in); Riddle riddle = new Riddle( "What is black and white and red all over?", "An embarrassed zebra."); System.out.println("Here is a riddle:"); System.out.println(riddle.getQuestion()); System.out.print("To see the answer, "); // Prompt System.out.println("type a letter and enter."); String str = sc.next(); // Wait for input System.out.println(riddle.getAnswer()); } //main() } // TestScanner2 class Wait for the user to type any letter before displaying the answer. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects

Summary: Technical Terms Java, Java, Java R. Morelli Summary: Technical Terms access modifier assignment statement class scope escape sequence field declaration floating point number flow of control keyword identifier initializer expression instance integer interface literal method call and return qualified name void method wrapper class Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects Copyright 2000 by Ralph Morelli. All rights reserved.

Java, Java, Java R. Morelli Summary: Key Points A Java program is a set of interacting objects. A Java class serves as a template for objects. Classes contain instance variables (state) and methods. Java class hierarchy organizes all classes into a single subclass and superclass relationship rooted in Object. A class definition: header, which names the class and describes its use and pedigree body , which contains its details. A pedigree describes where it fits in the Java class hierarchy. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects Copyright 2000 by Ralph Morelli. All rights reserved.

Summary: Key Points (cont) Java, Java, Java R. Morelli Summary: Key Points (cont) A class definition encapsulates the data and methods needed to carry out the class’s task. Design Goals: Well-defined purpose Well-articulated (public) interface  Hidden implementation details (private state) General and Extensible. A boolean is a primitive type that can be true or false. Object interface: The public class elements Keyword: a term that has special meaning. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects Copyright 2000 by Ralph Morelli. All rights reserved.

Summary: Key Points (cont) Java, Java, Java R. Morelli Summary: Key Points (cont) An identifier begins with a letter followed by any number of letters, digits and the underscores (_) and cannot be identical to a keyword. Field declaration (instance variable) reserves memory within the object associates a name and type with the location specifies its accessibility (public or private) Information Hiding: Instance variables should be private. Identifier Scope: Where an identifier can be used. Class scope: Fields and methods can be used anywhere in the class. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects Copyright 2000 by Ralph Morelli. All rights reserved.

Summary: Key Points (cont) Java, Java, Java R. Morelli Summary: Key Points (cont) Method definition: Header: names the method and provides other general information Body:  contains its executable statements. Methods that have a return type must return a value of that type. Methods that don’t return a value should be declared void. A method’s formal parameters  are variables that are used to bring information into the method. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects Copyright 2000 by Ralph Morelli. All rights reserved.

Summary: Key Points (cont) A qualified name is one which involves the dot operator (.) and is used to refer to an object's methods and instance variables. Declaring a reference  variable creates a name for an object but doesn't create the object itself. Instantiating a reference  variable creates an object and assigns the variable as its name or reference. Execution of a Java application begins with the first statement in the body of the main() method. Java, Java, Java, 3E by R. Morelli | R. Walde Copyright 2006. Chapter 2: Objects