Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 10ARRAYS 1 TOPIC 10 continued l Arrays of objects Arrays.

Similar presentations


Presentation on theme: "Topic 10ARRAYS 1 TOPIC 10 continued l Arrays of objects Arrays."— Presentation transcript:

1 Topic 10ARRAYS 1 TOPIC 10 continued l Arrays of objects Arrays

2 Topic 10ARRAYS 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 Topic 10ARRAYS 3 public int getEmpNum() { return empNum; } public double getSalary() { return empSalary; }

4 Topic 10ARRAYS 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 Employee[7]; This statement reserves enough computer memory for seven Employee objects named emp[0] through emp[6], however, the statement does not actually construct those Employee objects, instead, you must call the seven individual constructors.

5 Topic 10ARRAYS 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 < emp.length; index++) { emp[index] = new Employee(101 + index, 5.35); }

6 Topic 10ARRAYS 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 < emp.length; index++) { System.out.println(emp[index].getEmpNum() + “ “ + emp[index].getSalary()); }


Download ppt "Topic 10ARRAYS 1 TOPIC 10 continued l Arrays of objects Arrays."

Similar presentations


Ads by Google