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

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Inheritance Inheritance Reserved word protected Reserved word super
Sub and superclasses using extends
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Class Modifiers So far we have seen many examples of such modifiers E.g. Public Abstract etc.
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 7 Object-Oriented Programming Part 2: User-Defined Classes.
CSE 1301 Lecture 5 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
10-Nov-15 Java Object Oriented Programming What is it?
Chapter 7 Object-Oriented Programming Part 2: User-Defined Classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Announcements Final Exam:TBD. public static void main(String [] args) { final int ASIZE = 5; int [] intArray= new int[ASIZE]; for(int i = 0; i < ASIZE;
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
08 Encapsulation and Abstraction. 2 Contents Defining Abstraction Levels of Abstraction Class as Abstraction Defining a Java Class Instantiating a Class.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2014 Illinois Institute of Technology- George Koutsogiannakis 1.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
OOP Basics Classes & Methods (c) IDMS/SQL News
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Comp1004: Building Better Objects II Encapsulation and Constructors.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
CLASSES IN JAVA Primitive Types Example of a Class Allocating Objects of a Class Protecting Class data Constructors Static data and Static Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 1 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING II LECTURE 7 GEORGE KOUTSOGIANNAKIS
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
Chapter 3: Using Methods, Classes, and Objects
Lecture 13 Writing Classes Richard Gesick.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
OBJECT ORIENTED PROGRAMMING I LECTURE 7 GEORGE KOUTSOGIANNAKIS
Classes Variables That Are Not of a Built-in Type Are Objects
Corresponds with Chapter 7
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 9 GEORGE KOUTSOGIANNAKIS
OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS
Object-Oriented Programming
CIS 199 Final Review.
OO Programming Concepts
Chapter 7 Objects and Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

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?

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

What is a Class?

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

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

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

How to Create a Class?

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 }

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

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

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

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.

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

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

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

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

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

How to Use Classes?

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

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

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

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

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

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

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

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

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

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