CSC245 Lecture 1
CSC245 Lecture 1
1
Classes and Objects:
Review
Diana Haidar
2
OBJECTIVES
In this chapter you will learn:
What classes, objects, methods and instance variables
are.
How to declare a class and use it to create an object.
How to declare methods in a class to implement the
class’s behaviors.
How to declare instance variables in a class to
implement the class’s attributes.
How to call an object’s method to make that method
perform its task.
The differences between instance variables of a class
and local variables of a method.
How to use a constructor to ensure that an object’s
data is initialized when the object is created.
The differences between primitive and reference types.
Diana Haidar
3
3.1 Introduction
3.2 Classes, Objects, Methods and Instance Variables
3.3 Declaring a Class with a Method and Instantiating an
Object of a Class
3.5 Instance Variables, set Methods and get Methods
3.6 Primitive Types vs. Reference Types
3.7 Initializing Objects with Constructors
3.10 (Optional) Software Engineering Case Study: Identifying
the Classes in a Requirements Document
Diana Haidar
4
3.1 Introduction
• Classes
Diana Haidar
5
Diana Haidar
6
Diana Haidar
1 // Fig. 3.1: GradeBook.java 7
2 // Class declaration with one method. Outline
3
4 public class GradeBook
5 {
GradeBook.java
6 // display a welcome message to the GradeBook user
Print line of text to output
7 public void displayMessage()
8 {
9 System.out.println( "Welcome to the Grade Book!" );
10 } // end method displayMessage
11
12 } // end class GradeBook
Diana Haidar
8
Class GradeBookTest
• Java is extensible
– Programmers can create new classes
• Class instance creation expression
– Keyword new
– Then name of class to create and parentheses
• Calling a method
– Object name, then dot separator (.)
– Then method name and parentheses
Diana Haidar
1 // Fig. 3.2: GradeBookTest.java 9
2 // Create a GradeBook object and call its displayMessage method. Outline
3
4 public class GradeBookTest
5 {
GradeBookTest.java
6 // main method begins program execution
7 public static void main( String args[] )
8 {
Use class instance creation
9 // create a GradeBook object and assign it to myGradeBook
expression to create object of class
10 GradeBook myGradeBook = new GradeBook(); GradeBook
11
12 // call myGradeBook's displayMessage method
Call method displayMessage
13 myGradeBook.displayMessage();
using GradeBook object
14 } // end main
15
16 } // end class GradeBookTest
Diana Haidar
10
Diana Haidar
11
Fig. 3.3 | UML class diagram indicating that class GradeBook has a public
displayMessage operation.
Diana Haidar
12
Diana Haidar
13
Fig. 3.6 | UML class diagram indicating that class GradeBook has a displayMessage
operation with a courseName parameter of UML type String.
Diana Haidar
14
Diana Haidar
1 // Fig. 3.7: GradeBook.java 15
// GradeBook class that contains a courseName instance variable
2
Outline
3 // and methods to set and get its value.
4
5 public class GradeBook Instance variable courseName
6 {
7 private String courseName; // course name for this GradeBook GradeBook.java
8
9 // method to set the course name
10 public void setCourseName( String name )
set method for courseName
11 {
12 courseName = name; // store the course name
13 } // end method setCourseName
14
15 // method to retrieve the course name
16 public String getCourseName()
get method for courseName
17 {
18 return courseName;
19 } // end method getCourseName
20
21 // display a welcome message to the GradeBook user
22 public void displayMessage()
23 {
24 // this statement calls getCourseName to get the
25 // name of the course this GradeBook represents
26 System.out.printf( "Welcome to the grade book for\n%s!\n",
27 getCourseName() );
28 } // end method displayMessage
Call get method
29
30 } // end class GradeBook
Diana Haidar
16
• private keyword
– Used for most instance variables
– private variables and methods are accessible only
to methods of the class in which they are declared
– Declaring instance variables private is known as
data hiding
• Return type
– Indicates item returned by method
– Declared in method header
Diana Haidar
17
Diana Haidar
1 // Fig. 3.8: GradeBookTest.java 18
// Create and manipulate a GradeBook object.
2
Outline
3 import java.util.Scanner; // program uses Scanner
4
5 public class GradeBookTest
6 {
7 // main method begins program execution
GradeBookTest.java
8 public static void main( String args[] )
9 {
(1 of 2)
10 // create Scanner to obtain input from command window
11 Scanner input = new Scanner( System.in );
12
13 // create a GradeBook object and assign it to myGradeBook
14 GradeBook myGradeBook = new GradeBook();
15
16 // display initial value of courseName
17 System.out.printf( "Initial course name is: %s\n\n",
18 myGradeBook.getCourseName() );
Call get method for courseName
19
Diana Haidar
20 // prompt for and read course name 19
21 System.out.println( "Please enter the course name:" ); Outline
22 String theName = input.nextLine(); // read a line of text
23 myGradeBook.setCourseName( theName ); // set the course name
Call set method for courseName
24 System.out.println(); // outputs a blank line
25 GradeBookTest.java
26 // display welcome message after specifying course name
(2 of 2)
27 myGradeBook.displayMessage();
Call displayMessage
28 } // end main
29
30 } // end class GradeBookTest
Diana Haidar
20
• Attributes
– Listed in middle compartment
– Attribute name followed by colon followed by
attribute type
• Return type of a method
– Indicated with a colon and return type after the
parentheses after the operation name
Diana Haidar
21
Fig. 3.9 | UML class diagram indicating that class GradeBook has a courseName attribute
of UML type String and three operations—setCourseName (with a name parameter of
UML type String), getCourseName (returns UML type String) and displayMessage.
Diana Haidar
22
• Types in Java
– Primitive
• boolean, byte, char, short, int, long, float,
double
– Reference (sometimes called nonprimitive types)
• Objects
• Default value of null
• Used to invoke an object’s methods
Diana Haidar
23
• Constructors
– Initialize an object of a class
– Java requires a constructor for every class
– Java will provide a default no-argument constructor
if none is provided
– Called when keyword new is followed by the class
name and parentheses
Diana Haidar
1 // Fig. 3.10: GradeBook.java 24
2 // GradeBook class with a constructor to initialize the course name. Outline
3
4 public class GradeBook
5 {
6 private String courseName; // course name for this GradeBook GradeBook.java
7
8 // constructor initializes courseName with String supplied as argument (1 of 2)
9 public GradeBook( String name )
10 { Constructor to initialize
11 courseName = name; // initializes courseName courseName variable
12 } // end constructor
13
14 // method to set the course name
15 public void setCourseName( String name )
16 {
17 courseName = name; // store the course name
18 } // end method setCourseName
19
20 // method to retrieve the course name
21 public String getCourseName()
22 {
23 return courseName;
24 } // end method getCourseName
Diana Haidar
25 25
26 // display a welcome message to the GradeBook user Outline
27 public void displayMessage()
28 {
29 // this statement calls getCourseName to get the
GradeBook.java
30 // name of the course this GradeBook represents
31 System.out.printf( "Welcome to the grade book for\n%s!\n", (2 of 2)
32 getCourseName() );
33 } // end method displayMessage
34
35 } // end class GradeBook
Diana Haidar
1 // Fig. 3.11: GradeBookTest.java 26
2 // GradeBook constructor used to specify the course name at the Outline
3 // time each GradeBook object is created.
4
5 public class GradeBookTest
6 { GradeBookTest.java
7 // main method begins program execution
8 public static void main( String args[] ) Call constructor to create first grade
9 { book object
10 // create GradeBook object
11 GradeBook gradeBook1 = new GradeBook(
12 "CS101 Introduction to Java Programming" );
13 GradeBook gradeBook2 = new GradeBook(
14 "CS102 Data Structures in Java" );
15 Create second grade book object
16 // display initial value of courseName for each GradeBook
17 System.out.printf( "gradeBook1 course name is: %s\n",
18 gradeBook1.getCourseName() );
19 System.out.printf( "gradeBook2 course name is: %s\n",
20 gradeBook2.getCourseName() );
21 } // end main
22
23 } // end class GradeBookTest
Diana Haidar
27
Diana Haidar
28
Fig. 3.12 | UML class diagram indicating that class GradeBook has a constructor that has
a name parameter of UML type String.
Diana Haidar