CS100A, Fall 1997 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS100A, Fall 1997

Description:

Functions versus procedures. Some security using access modifiers public and private. ... Procedures versus functions. Method raiseSalary has prefix void: ... – PowerPoint PPT presentation

Number of Views:12
Avg rating:3.0/5.0
Slides: 12
Provided by: david148
Category:

less

Transcript and Presenter's Notes

Title: CS100A, Fall 1997


1
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.

2
CS100A, Fall 1997, 11 September
  • Concepts for this lecture
  • Sequences of charactersclass 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.

3
  • // 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

4
// 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()
5
  • 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

6
  • Checking string equality
  • s1
  • s2
  • if (s1s2) --condition if false!
  • Use instead
  • if (s1.equals(s2))
  • equalsa method of class String.

7
  • 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()

8
  • 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.

9
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.
10
  • 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.

11
  • // Yield the persons name
  • public String getName ()
  • return name
  • Execution of statement
  • return ltexpressiongt
  • terminates execution of the method (function) in
    which it appears and returns the value of
    ltexpressiongt to the place of call.
  • Two examples of calls
  • String s e1.getName()
  • System.out.println(name e1.getName())
Write a Comment
User Comments (0)
About PowerShow.com