JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.

Slides:



Advertisements
Similar presentations
Java, Java, Java R. Morelli Object-Oriented Problem Solving
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 7 User-Defined Methods.
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, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Writing Classes (Chapter 4)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
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.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
The Java Programming Language
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: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© 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
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 2 - Using Objects.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
OOP Basics Classes & Methods (c) IDMS/SQL News
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition.
Creating Java Applications (Software Development Life Cycle) 1. specify the problem requirements - clarify 2. analyze the problem - Input? Processes? Output.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Defining Classes and Methods
Topic: Classes and Objects
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
Defining Classes and Methods
Chapter 2: Basic Elements of Java
Group Status Project Status.
Defining Classes and Methods
Object Oriented Programming in java
Defining Classes and Methods
Defining Classes and Methods
Presentation transcript:

JAVA, JAVA, JAVA Object-Oriented Problem Solving Ralph Morelli Trinity College Hartford, CT presentation slides for published by Prentice Hall Second Edition

Java, Java, Java Object Oriented Problem Solving Chapter 2 Objects: Defining, Creating, and Using

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Objectives Be familiar with the relationship between classes and objects in a Java program. Be able to understand and write simple programs in Java. Be familiar with some of the basic principles of object-oriented programming. Understand some of the basic elements of the Java language.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Outline Class Definition Case Study: Simulating a CyberPet Object-Oriented Design: Basic Principles Java Language Summary From the Java Library: BufferedReader, String, Integer In the Laboratory: The Circle Class

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The Rectangle Class A class is a blueprint. It describes an object's form but it has no content. The class contains an object’s method definitions The instance variables, length and width, have no values yet.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The Rectangle Class Definition public class Rectangle { private double length; // Instance variables private double width; public Rectangle(double l, double w) // Constructor method { length = l; width = w; } // Rectangle constructor public double calculateArea() // Access method { return length * width; } // calculateArea } // Rectangle class Instance variables are usually private An object’s public methods make up its interface A public class is accessible to other classes

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The RectangleUser Class The RectangleUser class will create and use 1 or more Rectangle instances. An application has a main() method

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The RectangleUser Class Definition public class RectangleUser { public static void main(String argv[]) { Rectangle rectangle1 = new Rectangle(30,10); Rectangle rectangle2 = new Rectangle(25,20); System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); } // main() } // RectangleUser An application must have a main() method Object Use Object Creation Class Definition

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Creating Rectangle Instances Create, or instantiate, two instances of the Rectangle class: The objects (instances) store actual values. Rectangle rectangle1 = new Rectangle(30,10); Rectangle rectangle2 = new Rectangle(25, 20);

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Using Rectangle Instances We use a method call to ask each object to tell us its area: rectangle1 area 300 rectangle2 area 500 Printed output: System.out.println("rectangle1 area " + rectangle1.calculateArea()); System.out.println("rectangle2 area " + rectangle2.calculateArea()); References to objects Method calls

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Define, Create, Use Class definition: Define one or more classes ( Rectangle, RectangleUser ) Object Instantiation: Create objects as instances of the classes ( rectangle1, rectangle2 ) Object Use: Use the objects to do tasks ( rectangle1.calculateArea() )

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Design Steps –Problem Specification –Problem Decomposition –Class Design: CyberPet –Method Decomposition –CyberPet Design Specification Case Study: Simulating a CyberPet

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Problem Specification Design a program that allows the user to interact with objects that simulate the behavior of a CyberPet. A CyberPet will perform two behaviors, eating and sleeping, when we tell it to.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Problem Decomposition What objects do we need? TestCyberPet serves as a user interface. It creates and uses CyberPets CyberPet objects encapsulate CyberPet state and behavior.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Class Design: CyberPet State: boolean variables, isEating and isSleeping Methods: –A constructor to initialize a CyberPet –An eat method to put pet in eating state –A sleep method to put pet in sleeping state

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects CyberPet Class Specification Class Name: CyberPet –Role: To represent and simulate a CyberPet Information Needed (instance variables) –isEating: Set to true when pet is eating (private) –isSleeping: Set to true when pet is sleeping (private) Manipulations Needed (public methods) –CyberPet(): A constructor method to initialize the pet –sleep(): A method to put the pet in sleeping state –eat(): A method to get the pet in eating state

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects CyberPet Class Definition public class CyberPet { // Data private boolean isEating = true;// CyberPet's state private boolean isSleeping = false; // Methods public void eat() // Start eating { isEating = true; // Change the state isSleeping = false; System.out.println("Pet is eating"); return; } // eat() public void sleep() // Start sleeping { isSleeping = true; // Change the state isEating = false; System.out.println("Pet is sleeping"); return; } // sleep() } // CyberPet class If no constructor method, Java provides a default.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The CyberPet Class A class is a blueprint. In this case every CyberPet created will be in the eating state initially. The instance variables, isEating and isSleeping, have initial values.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects public class CyberPet extends Object The Class Header In General: ClassModifiers opt class ClassName Pedigree opt public class CyberPet Example:

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. 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: CyberPet, eat, sleep, pet1, pet_2 Illegal: Cyber Pet, 30days, pet$, n!

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Declaring Instance Variables In General FieldModifiers opt TypeId VariableId Initializer opt Fields or instance variables have class scope. Their names can be used anywhere within the class. Examples: // Instance variables private boolean isEating = true; private boolean isSleeping = false;

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Public/Private Access (cont) Possible Error: Public instance variables can lead to an inconsistent state Example: Suppose we make isEating and isSleeping public variables. george.isEating = true; // Inconsistent george.isSleeping = true; // State The proper way to make george eat: george.eat(); // eat() is public method

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. 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 boolean isEating = true; private int petSize = 10; private int petAge = true; // Type error Type error: You can’t assign a boolean value to an int variable

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Method Definition Example The Method Header MethodModifiers opt ResultType MethodName (ParameterList ) public static void main (String argv[] ) public void paint (Graphics g) public void init () public void eat () public void MethodName() // Method Header { // Start of method body } // End of method body

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Method Definition public void eat() { isEating = true; isSleeping = false; System.out.println("Pet is eating"); return; } // eat() Header: This method, named eat, is accessible to other objects (public), and does not return a value (void). Body: a block of statements that sets the CyberPet’s state to eating.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The Simple Assignment Statement Type error: The value being assigned must be the same type as the variable. isEating = true; isSleeping = false; isEating = 100; // Type error 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:

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

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Creating CyberPet Instances (cont) Declare and instantiate in one statement: CyberPet pet1 = new CyberPet(); CyberPet pet2 = new CyberPet(); Two CyberPets with names, pet1 and pet2, both eating. UML Object Diagram

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Using and Manipulating CyberPets Objects are used by calling one of their public methods: Now pet1 is eating but pet2 is sleeping. pet1.eat(); // Tell pet1 to eat pet2.sleep(); // Tell pet2 to sleep

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The TestCyberPet Class public class TestCyberPet { public static void main (String argv[]) { // Execution starts here System.out.println("main() is starting"); CyberPet pet1; // Declare two references CyberPet pet2; pet1 = new CyberPet(); // Instantiate the references pet2 = new CyberPet(); // by creating new objects pet1.sleep(); // Tell pet1 to sleep. pet1.eat(); // Tell pet1 to eat. pet2.sleep(); // Tell pet2 to sleep. System.out.println("main() is finished"); return; // Return to the system } // main() } //TestCyberPet

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects An Applet Interface The TestCyberPetApplet will serve as a user interface for the CyberPet simulation.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects The TestCyberPetApplet Class public class TestCyberPetApplet extends Applet { public void init() { // Execution starts here System.out.println("init() is starting"); CyberPet pet1; // Declare two references CyberPet pet2; pet1 = new CyberPet(); // Instantiate the references pet2 = new CyberPet(); // by creating new objects pet1.sleep(); // Tell pet1 to sleep. pet1.eat(); // Tell pet1 to eat. pet2.sleep(); // Tell pet2 to sleep. System.out.println("init() is finished"); return; // Return to the system } // main() } //TestCyberPetApplet

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects CyberPet Application The CyberPet class can be turned into an application by giving it a main() method. Same main() as in TestCyberPet class

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Tracing TestCyberPet public static void main (String argv[]) { 1 System.out.println("main() is starting"); 2 CyberPet pet1; 3 CyberPet pet2; 4 pet1 = new CyberPet(); 7 pet2 = new CyberPet(); 10 pet1.sleep(); 15 pet1.eat(); 20 pet2.sleep(); 25 System.out.println("main() is finished"); 26 return; } // main() public class CyberPet { 5,8 private boolean isEating = true; 6,9 private boolean isSleeping = false; public void eat() { 16 isEating = true; 17 isSleeping = false; 18 System.out.println("Pet is eating"); 19 return; } // eat() public void sleep() { 11,21 isSleeping = true; 12,22 isEating = false; 13,23 System.out.println("Pet is sleeping"); 14,24 return; } // sleep() } // CyberPet class

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Object Oriented Design Encapsulation: The CyberPet class encapsulates a state and a set of actions. Information Hiding: CyberPet’s state is private. Interface: CyberPet’s public methods, eat() and sleep(), limit the way it can be used. Generality/Extensibility: We can easily extend CyberPet’s functionality.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects From the Library: BufferedReader Code Reuse Principle: Before designing and writing your own code, search the Java library for a class that solves your problem. The java.io.BufferedReader class contains methods to perform keyboard I/O. public class BufferedReader extends Reader { public BufferedReader(Reader in); public String readLine(); // Read one line }

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Example: Input a String import java.io.*; // Java I/O classes public class Hello // Performs screen I/O { public static void main(String argv[]) throws IOException { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); System.out.print("Hello, input your name please"); String inputString = input.readLine(); System.out.println("Hello " + inputString); } // main() } // Hello Read one line. Create a reader.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects From the Library: Integer The java.lang.Integer class is a wrapper class that contains methods to convert primitive data into objects and vice versa. The parseInt() method converts a String into an int. Example: Converts “54” into 54 int number = Integer.parseInt( “54”);

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Example: Input a Number public class Grader { public static void main(String argv[]) throws IOException { BufferedReader input = new BufferedReader (new InputStreamReader(System.in)); String inputString; int midterm1, midterm2, finalExam; // Three exam grades float sum; // The sum of the 3 grades System.out.print("Input your grade on the first midterm: "); inputString = input.readLine(); midterm1 = Integer.parseInt(inputString); System.out.println("You input: " + midterm1); // Similar code deleted here to input midterm2 and finalExam sum = midterm1 + midterm2 + finalExam; System.out.print("Your average in this course is "); System.out.println(sum/3); } // main() } // Grader Read an integer.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects In the Laboratory: The Circle Class Objectives –To give practice designing a simple Java class to represent a geometric circle. –To convert the design into a working Java program. –To compile, run, and test the Java program. Problem Statement: Design and implement a class to represent a geometric circle, and test your class by implementing a main() method that creates Circle instances and displays their circumferences and areas.

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects In the Laboratory (cont) Design: Model your design after the Rectangle class from Chapter 1. Develop a detailed specification. Use comments to document your code. Use stepwise refinement when coding. Sample Output: The diameter of circle1 is 20.0 The area of circle1 is The circumference of circle1 is The diameter of circle2 is 30.0 The area of circle2 is The circumference of circle2 is 94.2

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Summary: Technical Terms access modifier assignment statement class scopeescape sequence field declarationfloating point number flow of controlkeyword identifierinitializer expression instance integer interface literal method call and return qualified name void method wrapper class

Java, Java, Java, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects Summary: Key Points (cont) A class definition encapsulates the data and methods needed to carry out the class’s task. Design Goals: –W ell-defined purpose –W ell-articulated ( public ) interface –Hidden implementation details (private state) –G eneral and E xtensible. 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects 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, 2E by R. Morelli Copyright All rights reserved. Chapter 2: Objects 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.