Professional Documents
Culture Documents
10636212
Chapter 03:
– Software objects are often used to model the real-world objects that
you find in everyday life.
– Objects communicate through well-defined interfaces
• What is a Class?
– It is the blueprint from which individual objects are created.
© 2020 Dr. Ashraf Armoush , An-Najah National University 3
• Methods (behavior) :
– setTime(): set a new time value using universal time
– toUniversalString(): convert to String in universal-time
format
– toStandardString(): convert to String in standard-time format
1 import java.text.DecimalFormat;
2
3 public class Time1 extends Object {
4 private int hour; // 0 - 23
5 private int minute; // 0 - 59
6 private int second; // 0 - 59
7 // Time1 constructor initializes each instance variable to zero;
8 // ensures that each Time1 object starts in a consistent state
9 public Time1()
10 {
11 setTime( 0, 0, 0 );
12 }
13 // set a new time value using universal time; perform
14 // validity checks on the data; set invalid values to zero
15 public void setTime( int h, int m, int s )
16 {
17 hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
18 minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
19 second = ( ( s >= 0 && s < 60 ) ? s : 0 );
20 }
Keyword this
1. Allows an object to public class A{
private int x;
explicitly in a non-
static method’s
public class B{
body. private int x,y;
public B()
{ // invoke B constructor with 2 arguments.
this(0,0);
constructor {
x = xValue;
(as constructor) }
y= yValue;
}
}
Composition
A class can have references to objects of other classes
as members.
Composition (cont.)
// return a String of the form month/day/year
public String toString()
{
return String.format("%d/%d/%d", month, day, year);
}
} // end class Date
Composition (cont.)
public class EmployeeTest
{
public static void main(String[] args)
{
Date birth = new Date(7, 24, 1990);
Date hire = new Date(3, 12, 2013);
Employee employee = new Employee("Ahmad", "Ali", birth, hire);
System.out.println(employee);
}
} // end class EmployeeTest
• System.gc();
– Calls Java’s automatic garbage-collection mechanism
Creating Packages
• Each class in the Java API belongs to a package that
contains a group of related classes.
• Packages are defined once, but can be imported into
many programs.
• Packages help programmers manage the complexity of
application components.
• Packages facilitate software reuse by enabling programs
to import classes from other packages, rather than
copying the classes into each program that uses them.
• Packages provide a convention for unique class names,
which helps prevent class-name conflicts.
• Example:
javac -d . Time1.java
• specifies that the first directory in our package name should
be placed in the current directory (.).
Enumerations (cont.)
import java.util.EnumSet;
SATURDAY
SUNDAY
MONDAY
TUESDAY
c:\enumTest>
Enumerations (cont.)
public enum Book
{
• You can declare instance // declare constants of enum type
JHTP("Java How to Program", "2015"),
variables, a constructor CHTP("C How to Program", "2013"),
and methods in an enum IW3HTP("Internet & World Wide Web How to Program","2012"),
CPPHTP("C++ How to Program", "2014"),
type. VBHTP("Visual Basic How to Program", "2014"),
CSHARPHTP("Visual C# How to Program", "2014");
// instance fields
private final String title;
• Each enum constant is private final String copyrightYear;
// enum constructor
optionally followed by Book(String title, String copyrightYear)
arguments which are {
this.title = title;
passed to the enum this.copyrightYear = copyrightYear;
}
constructor.
// accessor for field title
public String getTitle()
{
return title;
}
import java.util.EnumSet;
Enumerations (cont.)
All books:
JHTP Java How to Program 2015
CHTP C How to Program 2013
IW3HTP Internet & World Wide Web How to Program 2012
CPPHTP C++ How to Program 2014
VBHTP Visual Basic How to Program 2014
CSHARPHTP Visual C# How to Program 2014