Download presentation
Presentation is loading. Please wait.
1
Introduction to Classes and Objects
3 Introduction to Classes and Objects
2
3.2 Classes, Objects, Methods and Instance Variables
Class provides one or more methods Method represents task in a program Describes the mechanisms that actually perform its tasks Hides from its user the complex tasks that it performs Method call tells method to perform its task
3
Common Programming Error 3.1
Declaring more than one public class in the same file is a compilation error.
4
Print line of text to output
Outline GradeBook.java Print line of text to output
5
Outline GradeBookTest.java
6
Compiling an Application with Multiple Classes
Compiling multiple classes List each .java file separately separated with spaces Compile with *.java to compile all .java files in that directory
7
UML Class Diagram for Class GradeBook
UML class diagrams Top compartment contains name of the class Middle compartment contains class’s attributes or instance variables Bottom compartment contains class’s operations or methods Plus sign indicates public methods
8
3.4 Declaring a Method with a Parameter
Method parameters Additional information passed to a method Supplied in the method call with arguments
9
Outline GradeBook.java
10
Outline GradeBookTest.java
11
Outline GradeBookTest.java
12
Software Engineering Observation 3.1
Normally, objects are created with new. One exception is a string literal that is contained in quotes, such as "hello". String literals are references to String objects that are implicitly created by Java.
13
Common Programming Error 3.2
A compilation error occurs if the number of arguments in a method call does not match the number of parameters in the method declaration.
14
Common Programming Error 3.3
A compilation error occurs if the types of the arguments in a method call are not consistent with the types of the corresponding parameters in the method declaration.
15
Updated UML Class Diagram for Class GradeBook
Parameters specified by parameter name followed by a colon and parameter type
16
Fig. 3.6 | UML class diagram indicating that class GradeBook has a displayMessage operation with a courseName parameter of UML type String.
17
Notes on Import Declarations
java.lang is implicitly imported into every program Default package Contains classes compiled in the same directory Implicitly imported into source code of other files in directory Packages unnecessary if fully-qualified names are used
18
Software Engineering Observation 3.2
The Java compiler does not require import declarations in a Java source code file if the fully qualified class name is specified every time a class name is used in the source code. But most Java programmers consider using fully qualified names to be cumbersome, and instead prefer to use import declarations.
19
3.5 Instance Variables, set Methods and get Methods
Variables declared in the body of method Called local variables Can only be used within that method Variables declared in a class declaration Called fields or instance variables Each object of the class has a separate instance of the variable
20
Outline GradeBook.java
21
Access Modifiers public and private
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
22
Software Engineering Observation 3.3
Precede every field and method declaration with an access modifier. As a rule of thumb, instance variables should be declared private and methods should be declared public.
23
Good Programming Practice 3.1
We prefer to list the fields of a class first, so that, as you read the code, you see the names and types of the variables before you see them used in the methods of the class. It is possible to list the class’s fields anywhere in the class outside its method declarations, but scattering them tends to lead to hard-to-read code.
24
GradeBookTest Class That Demonstrates Class GradeBook
Default initial value Provided for all fields not initialized Equal to null for Strings
25
set and get methods private instance variables
Cannot be accessed directly by clients of the object Use set methods to alter the value Use get methods to retrieve the value
26
Outline GradeBookTest.java (1 of 2)
27
Outline GradeBookTest.java (2 of 2)
28
GradeBook’s UML Class Diagram with an Instance Variable and set and get Methods
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
29
Primitive Types vs. Reference Types
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
30
Software Engineering Observation 3.4
A variable’s declared type (e.g., int, double or GradeBook) indicates whether the variable is of a primitive or a reference type. If a variable’s type is not one of the eight primitive types, then it is a reference type. For example, Account account1 indicates that account1 is a reference to an Account object).
31
3.7 Initializing Objects with 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
32
Outline GradeBook.java (1 of 2)
33
Outline GradeBook.java (2 of 2)
34
Outline GradeBookTest.java
35
Error-Prevention Tip 3.1 Unless default initialization of your class’s instance variables is acceptable, provide a constructor to ensure that your class’s instance variables are properly initialized with meaningful values when each new object of your class is created.
36
Adding the Constructor to Class GradeBookTest’s UML Class Diagram
Constructors go in third compartment Place “<<constructor>>” before constructor name By convention, place constructors first in their compartment
37
Fig | UML class diagram indicating that class GradeBook has a constructor that has a name parameter of UML type String.
38
3.8 Floating-Point Numbers and Type double
Stores numbers with greater magnitude and precision than float
39
Floating-Point Number Precision and Memory Requirements
Single-precision floating-point numbers Seven significant digits double Double-precision floating-point numbers Fifteen significant digits
40
Common Programming Error 3.4
Using floating-point numbers in a manner that assumes they are represented precisely can lead to logic errors.
41
Outline Account.java
42
AccountTest Class to use Class Account
Format specifier %f Used to output floating-point numbers Place a decimal and a number between the percent sign and the f to mandate a precision
43
Outline AccountTest.java (1 of 3)
44
Outline AccountTest.java (2 of 3) AccountTest.java
45
Outline AccountTest.java (3 of 3) AccountTest.java
46
Fig | UML class diagram indicating that class Account has a private balance attribute of UML type Double, a constructor (with a parameter of UML type Double) and two public operations—credit (with an amount parameter of UML type Double) and getBalance (returns UML type Double).
47
Displaying Text in a Dialog Box
Windows and dialog boxes Many Java applications use these to display output JOptionPane provides prepackaged dialog boxes called message dialogs
48
Outline Dialog1.java
49
Displaying Text in a Dialog Box
Package javax.swing Contains classes to help create graphical user interfaces (GUIs) Contains class JOptionPane Declares static method showMessageDialog for displaying a message dialog
50
Entering Text in a Dialog Box
Input dialog Allows user to input information Created using method showInputDialog from class JOptionPane
51
Outline NameDialog.java
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.