Download presentation source

Download Report

Transcript Download presentation source

CS100A, 10 Sept. 1998
Lecture 4: Continue discussion of classes and introduces
• Class String,
• Printing output using System.out.print, System,out.println,
and System.out.flush().
• Accessing modifiers public and private
• Functions versus procedures, and the modifier void.
• Constructors
Concepts demonstrated using class Employee, an instance of
which records information about an employee. This class
appears later in these slides.
With each concept, CodeWarrior Java on the Macintosh is
used to demonstrate the concept and its application. This is
important. For example, after making a field private, we
syntax-check the program and see that references to it
(outside the class) are now illegal.
CS100A, 10 Sept. 1998.
Lecture 4
1
CS100A, Fall 1998, 10 September
Concepts for this lecture:
• Sequences of characters:class String.
Holmes 1.8 (p. 30-34).
• Output, using System.out
• Method of a class. Holmes, Sec. 6.2.
• Functions versus procedures.
• Some security using access
modifiers public and private.
Holmes p 229.
• Constructors: Holmes, Sec. 205 (not
worth much)
CS100A, 10 Sept. 1998.
Lecture 4
2
// An instance of Employee contains a person's name,
// salary, and year hired. It has a constructor and
// methods for raising the salary, printing the data, and
// retrieving the person's name and the year hired.
public class Employee
{public String name;
// The person's name
public double pay;
// The person's yearly salary
public int hireDate;
// The year hired
// Set the name to n
public void setName(String n)
{name= n;}
// Set the salary to s
public void setSalary(double s)
{pay= s;}
// Set the year to y
public void setHireDate(int y)
{hireDate= y;}
CS100A, 10 Sept. 1998.
Lecture 4
3
// Yield the year the person was hired
public int hireYear()
{return hireDate;}
// Raise the pay by p percent
public void raiseSalary(double p)
{pay= pay * (1 + p/100.0);}
// Yield the person's name
public String getName()
{return name;}
// Yield a String containing person’s data
public String toString() {
String s= name;
s= s + " " + pay + " " + hireDate;
return s;
}
}
CS100A, 10 Sept. 1998.
Lecture 4
4
Class String. An instance is a “string” of
characters.
Declaration
String d;
Assignment
d= new String(“David Gries”);
or
d= “David Gries”;
Catenation of strings: infix operator +
d + “ ” + “xyz”
evaluates to “David Gries xyz”
d= “David” + “ ” + “Gries”
Holmes: Sect. 1.8, p 30
CS100A, 10 Sept. 1998.
Lecture 4
5
Checking string equality
s1
gries
s2
gries
if (s1==s2) --condition if false!
Use instead
if (s1.equals(s2))
equals:a method of class String.
CS100A, 10 Sept. 1998.
Lecture 4
6
To print string s in the output window, use
System.out.print(s);
or
System.out.println(s);
The latter one prints an “end of line” after
printing s. Thus, the following are equivalent:
(1)
System.out.print(s1);
System.out.println(s2);
(2)
System.out.println(s1 + s2);
// “Flush” the output buffer, making
// sure that everything is written out.
System.out.flush();
CS100A, 10 Sept. 1998.
Lecture 4
7
Accessing a field
of an Employee object.
Employee c;
c= new Employee();
c.name= “G”;
c.pay= 1000000;
c.hireDate= 1998;
c.pay= 220000;
Allowing access to field pay allows any
program that can reference c to change
pay. This may not be desired!
Changing a field like pay should be
limited to methods of the class.
CS100A, 10 Sept. 1998.
Lecture 4
8
Alternative:
// Anyone can reference field salary!
public int salary;
Alternative:
// Only methods within Employee can
// reference pay!
private int pay;
So, the following is illegal:
Employee c;
c= new Employee( );
c.pay= 1000000;
Now, to change c1.pay, you have to call
method setSalary or raiseSalary.
CS100A, 10 Sept. 1998.
Lecture 4
9
Procedures versus functions
Method raiseSalary has prefix void:
public void raiseSalary(double p);
void indicates that the method performs a task
but does not “return” a result.
Method getName has prefix String:
public String getName ( );
The term String indicates that the method
“returns” a result.
CS100A, 10 Sept. 1998.
Lecture 4
10
// Yield the person’s name
public String getName ();
{return name;}
Execution of statement
return <expression>
terminates execution of the method (function)
in which it appears and “returns” the value of
<expression> to the place of call.
Two examples of calls:
String s= e1.getName();
System.out.println(e1.toString());
CS100A, 10 Sept. 1998.
Lecture 4
11
Constructors
A constructor: a normal method except
(0) Its name is the same as the name of the class
(1) It has no type-class prefix or void
//Constructor: name n, year d hired, //salary s
public Employee(String n, double s, int d) {
name= n; pay= s; hireDate= d; }
When the following is executed, after the
instance of Employee is created, the constructor
is executed, with “Gries” for n, 50000 for s, and
1969 for d:
c= new Employee(“Gries”, 50000, 1969);
Constructor is used to provide initialization of
fields of the instance.
CS100A, 10 Sept. 1998.
Lecture 4
12