Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer."— Presentation transcript:

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

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

3 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

4 What is a Class?

5 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

6 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

7 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

8 How to Create a Class?

9 Syntax for Defining a Class 9 accessModifier class ClassName { // class definition goes here } ***Note that the curly braces are required. i.e public class Student { // class definition goes here }

10 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

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

12 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

13 Access Modifiers 13 Access ModifierClass or members can be referenced by… publicmethods of the same class and methods of other classes privatemethods of the same class only protectedmethods 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.

14 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

15 Accessor methods return the value of an instance variable i.e. public String getAge() { return this.age; } 15

16 Mutator methods Set the value of an instance variable i.e. public void setAge (int age) { this.age=age; } 16

17 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

18 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

19 How to Use Classes?

20 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

21 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

22 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

23 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

24 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

25 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

26 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

27 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. 27 Tip: this resolves ambiguity in a class method when method parameters are named identical to the class variables

28 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

29 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


Download ppt "CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer."

Similar presentations


Ads by Google