Presentation is loading. Please wait.

Presentation is loading. Please wait.

Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};

Similar presentations


Presentation on theme: "Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};"— Presentation transcript:

1 Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
int [] iArray = {1, 2, 3, 4}; double [] dArray = {3.4, 5.67e4};

2 Two Dimensional Arrays
char [][] cArray = new char[10][20]; int [][] iArray = new int[100][50]; cArray[0][0] = ‘a’; cArray[0][1] = ‘b’; cArray[9][19] = ‘x’; iArray[0][0] = 99; iArray[1][5] = 135; iArray[99][49] = 0;

3 Classes A Class Is an Object Type
A Class Represents a “Thing” (e.g., employee, wrench, list, store, shopping cart, etc.) Service Class – Class used by Other Programs Programmers Define Classes with Data Members (or Fields) and Methods Must Be Created in ClassName.java File Client Program – Program using a class

4 Access Modifiers Used to Identify What May Use Methods
public – any method may use (Usually methods) private – only methods in same class may use (usually data members)

5 Designing a Class Decide How It Will Be Used
Decide on Interface (i.e., public representation, public methods) Decide on Implementation (i.e., private data and data manipulation)

6 Example Class Class Employee firstName lastName salary

7 Constructor Class Method of Same Name
Example: class Employee Method Employee() Called When Variable Declared (instantiated) of Class Type Initializes Instantiated Object May Have Multiple Constructors Each with Different Parameters

8 class Employee // Employee.java
{ private String firstName; private String lastName; private double salary; public Employee() firstName = "NoFirstName"; lastName = "NoLastName"; salary = 0.0; }

9 Accessor Methods Public Methods for Getting Attributes of Class

10 public String GetLastName() return lastName; public double GetSalary()
class Employee { private String firstName; private String lastName; private double salary; public Employee() firstName = "NoFirstName"; lastName = "NoLastName"; salary = 0.0; } public String GetFirstName() return firstName; public String GetLastName() return lastName; public double GetSalary() return salary;

11 Client Program A Program that Uses a Class Is a Client of that Class
Class Objects are Declared and Instantiated Using the new keyword. new ClassName(parameters) Calls Constructor for Class Class Public Methods Called Using Dot (e.g., variable.GetFirstName();)

12 class useEmployee //useEmployee.java
{ public static void main(String [ ] args) Employee emp1 = new Employee(); System.out.println("Name is" emp1.GetFirstName() + " " + emp1.GetLastName()); } firstName emp1 “NoFirstName” lastName “NoLastName” salary 0.0

13 Mutator Methods Class Methods Used to Modify a Class Object are Called Mutator Methods Allows Restricted Manipulation of Class Object Data

14 public boolean SetSalary(double passedSalary)
{ if (salary <= MAXSALARY) salary = passedSalary; return true; } else return false;

15 class useEmployee if (emp1.SetSalary(55000))
{ public static void main(String [ ] args) Employee emp1 = new Employee(); System.out.println("Name is" + emp1.GetFirstName() + " " + emp1.GetLastName()); if (emp1.SetSalary(55000)) System.out.println("Salary set to 55000"); } else System.out.println("Salary not set");


Download ppt "Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};"

Similar presentations


Ads by Google