Download presentation source

Download Report

Transcript Download presentation source

CS100A, Fall 1997
Lecture, Thursday, 12 September.
This lecture continues the discussion of classes. In addition,
the following will be introduced:
• 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.
The concepts are demonstrated using a 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, Fall 1997.
Lecture 4
1
CS100A, Fall 1997, 11 September
Concepts for this lecture:
• Sequences of characters:class String.
L/L 125-126.
• Output, using System.out
• Methods of a class. L/L 134-140.
• Functions versus procedures
• Some security using access
modifiers public and private. L/L
146-147.
CS100A, Fall 1997.
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 salary;
// The person's salary
public int hireDate;
// The year hired
// Constructor: a person with name n, salary s, and
// year d hired
public Employee(String n, double s, int d)
{name= n;
salary= s;
hireDate= d;
}
CS100A, Fall 1997.
Lecture 4
3
// Raise the salary by p percent
public void raiseSalary(double p)
{salary= salary * (1 + p/100.0);}
// Yield the year the person was hired
public int hireYear()
{return hireDate;}
// Yield the person's name
public String getName()
{return name;}
// Print the data for the person
public void print()
{System.out.println(name + “ “ +
salary + “ “ + hireDate);
System.out.flush();
}
}
CS100A, Fall 1997.
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”
L/L 125-127, 800-803
CS100A, Fall 1997.
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, Fall 1997.
Lecture 4
6
To print string s in the output widnow, 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, Fall 1997.
Lecture 4
7
Accessing a field
of an Employee object.
Employee c;
c= new Employee(“G”, 20000, 1969);
c.salary= 220000;
(Demonstrate using CodeWarrior)
Allowing access to field salary allows
any program that can reference c to
change salary. This may not be desired!
Changing a field like salary should be
limited to methods of the class.
CS100A, Fall 1997.
Lecture 4
8
Alternative:
// Anyone can reference field salary!
public int salary;
Alternative:
// Only methods within Employee can
// reference salary!
private int salary;
So, the following is illegal:
Employee c;
c= new Employee(“G”,3000,1969);
c.salary= 250000;
Now, to change c1.salary, you have to call
method raiseSalary.
CS100A, Fall 1997.
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, Fall 1997.
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(“name ” + e1.getName());
CS100A, Fall 1997.
Lecture 4
11