TOPIC 10 continued - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

TOPIC 10 continued

Description:

empSalary = sal; Topic 10. ARRAYS. 3. public int getEmpNum() return empNum; ... You can create separate Employee objects with unique names, such as Employee ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 7
Provided by: LewRa5
Category:
Tags: topic | continued | sal

less

Transcript and Presenter's Notes

Title: TOPIC 10 continued


1
TOPIC 10 continued
Arrays
  • Arrays of objects

2
Declaring an array of objects
  • Assume that the following Employee class is
    already created.
  • public class Employee
  • private int empNum
  • private double empSalary
  •  
  • Employee(int num, double sal)
  • empNum num
  • empSalary sal

3
  • public int getEmpNum()
  • return empNum
  •  
  • public double getSalary()
  • return empSalary

4
  • You can create separate Employee objects with
    unique names, such as Employee painter,
    electrician, plumber, etc. but for many programs
    it is far more convenient to create an array of
    Employees.
  • Employee emp new Employee7
  • This statement reserves enough computer memory
    for seven Employee objects named emp0 through
    emp6, however, the statement does not actually
    construct those Employee objects, instead, you
    must call the seven individual constructors.

5
  • The Employee constructor requires 2 arguments an
    employee number and salary. If you want to
    number your Employees 101, 102, etc. and start
    each Employee at a salary of R5.35.
  • for (index 0 index lt emp.length index)
  • empindex new Employee(101 index, 5.35)

6
  • To use a method that belongs to an object that is
    part of an array, you insert the appropriate
    subscript notation after the array name and
    before the dot that precedes the method name.
  • for (index 0 index lt emp.length index)
  • System.out.println(empindex.getEmpNum()
  • empindex.getSalary())
Write a Comment
User Comments (0)
About PowerShow.com