CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis Matt Bauer.

29
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer

description

Specific Objectives What is a class? Abstract data type Class variables Class methods How to create a class? Defining a class Defining class variables/methods Static vs non-static Access modifiers How to use a class? Instantiating classes Service and client classes Accessor and mutator methods

Transcript of CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis Matt Bauer.

Page 1: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

CS 116 Lecture 1John Korah

Contains content provided by George Koutsogiannakis & Matt Bauer

Page 2: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Today’s Learning Objectives• What is a class?• How to create a class?• How to use a class?

Page 3: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Specific Objectives• What is a class?

• Abstract data type• Class variables• Class methods

• How to create a class?• Defining a class• Defining class variables/methods• Static vs non-static• Access modifiers

• How to use a class?• Instantiating classes• Service and client classes• Accessor and mutator methods

Page 4: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

What is a Class?

Page 5: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Class• An abstract representation of a concept or category or a group.

i.e. We can represent Students by creating a class called Student.• Classes are data structures that implement the concepts of Object

Oriented Programming• Attributes: to store data• Methods: to manipulate the data• Classes can interact with each other• Later in the course: inheritance, polymorphism

5

Page 6: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

What is a Class? cont.• Types of Class:• Pre-Defined Classes: Classes that are part of the Java Library of

classes• Pre-defined class is also a class that someone has already created and

made available for us to use.• Java API specifications/documentation available online.

• User Defined Classes : Classes that the programmer creates• Attributes representing the data to be stored• Methods that operate on the data

6

Page 7: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

What is a Class? cont.• Class Members: A class can have the following• Class Attributes (Instance Variables)• Class methods

• Function to manipulate the attributes• Constructors:

• Use the name of the class and have no return type• Default: has no arguments. Initializes the instance variables to pre determined value (i.e.

zero, “ “, etc).• Non default: has arguments. Initializes the instance variables to values passed in the

arguments.• Question: If a class has a defined non-default constructor but no default

constructor, can I still instantiate using a default constructor?

7

Page 8: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

How to Create a Class?

Page 9: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Syntax for Defining a Class

9

accessModifier class ClassName{ // class definition goes here}

***Note that the curly braces are required. i.epublic class Student{// class definition goes here

}

Page 10: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Defining Class Attributes• Syntax: accessModifier dataType identifierList;

dataType can be primitive date type or a class type identifierList can contain:

• one or more attributes names of the same data type• multiple attributes names separated by commas• initial values

• Class attributes can be declared as final

10

Page 11: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Examples of Class Attributes Definitionsprivate String name = ""; private final int PERFECT_SCORE = 100, PASSING_SCORE = 60; public int startX, startY,width,height;

11

Page 12: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Example: Student Class

public class Student{ private String lastName;

private int age; private double height; } The Student class has three class attrributes: lastName, age and height.• Question: can a class be declared private?

12

Page 13: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Access Modifiers

13

Access Modifier Class or members can be referenced by…

public methods of the same class and methods of other classes

private methods of the same class only

protected methods of the same class, methods of subclasses (this term will be explained later on in the course), and methods of classes in the same package

No access modifier (package access)

methods in classes belonging to the same package as the class

Tip of the Day: Use the most restrictive access modifier that makes sense, i.e. use private unless you have a reason not to.

Page 14: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Software Design Pointers

• Define class attributes for the data that all objects will have in common.• Define class attributes as private so that only the methods of the class

will be able to set or change their values.• To better protect and encapsulate data• Define methods to access and manipulate data• Reality is specifications change!

14

Page 15: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Accessor methods• Accessor methods• return the value of an instance variable i.e. public String getAge()

{return this.age;

}

15

Page 16: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Mutator methods• Mutator methods • Set the value of an instance variable

i.e. public void setAge (int age) {

this.age=age; }

16

Page 17: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Static Class Variables• Only one copy of a static variable is created per class• static variables are associated with a class NOT with an object• static constants are often declared as public• To define a static variable, include the keyword static in its definition:Syntax: accessSpecifier static dataType variableName,…;Example: public static int countStudents = 0;

17

Page 18: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Static Class Variables cont.• Every instance of the class (object) can see the same value of the

static variable (the latest value assigned to that variable).• If an object modifies the value of the static variable then all objects of

that class see the new value.

18

Page 19: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

How to Use Classes?

Page 20: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Instantiating Objects• Classes are a template used to create specific objects• An object of a class is created by using the new operator and calling

one of the constructors of the class:i.e. Student student1=new Student();

or Student student1=new Student(name, 12, 5.6);

20

Page 21: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Service Class Advantages:

The class methods are responsible for the validity of the data

Implementation details can be hidden

The class can be reused. Does not need a main method.

21

public class Student{ private String lastName; private int age; private double height; Student(){…} //non-default constructor Student(String name, int age, double height){…}//non-default constructor public void getAge(){…}//accessor public void setAge(int age){…}//mutator private boolean checkAge(){…}//helper methods}

Helper methods• Class methods that are used by other class methods.• Usually private methods

Page 22: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Client Classes• Client of a Service class(classes)• Needs a main method.• A program that instantiates objects and calls (invokes) the methods of the

Service class (or classes) in its main method.• In the main method we can instantiate an object of the Client class and use it

to invoke not static methods of the Client class.• We can make direct calls to its static methods (without an object).

22

Page 23: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Client Class• public class ClientStudent { //must have a main method (plus other methods also). //Use objects to invoke methods or access fields of the Service class Student.

//we would instantiate Student service class //objects in the method(s) of this class

}

23

Page 24: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

To String method• Often classes have a toString method that outputs the values of its

instance variables.

public String toString(){

String output=“The value of model is”+this.getModel() + ”\n” + ”The miles driven:” + this.getMilesDriven()………..etc…..”);

return output;}

24

Page 25: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Comparing Objects For Equality• Objects of the same class can be compared for equality.• The definition of the term equality is up to the programmer:

• i.e Suppose we have class Student with instance variables as shown public class Student

{ String firstName=“ “;

String lastName=“ “; int StudentID=0; String address=“ “; ……………………………………………. ……………………………………………..

25

Page 26: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Comparing Objects For Equality• We may decide that two Student objects are equal if and only if the

studentID instance variable has the same value for both objects (regardless if the rest of the instance variables have the same values or not).• We create a method called equals to test for the equality of two

objects of the same class.

26

Page 27: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Comparing Objects For Equality• Example code for equals method to be included in Student service class: public boolean equals (Student st) {

if(this.getStudentID()==st.getStudentID()) return true; else

return false; }• this refers to object that invokes the equals method.

27Tip: this resolves ambiguity in a class method when method parameters are named identical to the class variables

Page 28: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Comparing Objects For Equality• In the client class that uses Student we can make comparisons of Student objects: public class StudentClient

{

public static void main(String [] args)

{

Student st=new Student(“George”, “Kay”, 1234, “654 somestreet”);

Student st1=new Student(“Nicholas”, “Jones”, 1234, “654 somestreet”);

boolean b= st.equals(st1); if(b==true)

System.out.println(“They are equal”);

else

System.out.println(“They are NOT re equal”);

}

} 28

Page 29: CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis  Matt Bauer.

Terminology• Object reference: identifier of the object. The name we used for the object.• Object data: The data encaptulated by a specific object (reference). The values of the

instance variables as pertained to that specific object.i. e. If we create a Student template class then a specific object reference called st may have such data as: firstName=“John”

lastName=“Doe”• Instantiating an object: creating an object of a class by using the new operator.• Instance of the class: the object we created.• Methods: the code to manipulate the object data.• Calling a method: invoking a service for an object.

29