Lecture 7: Classes and Objects CS2301

Size: px
Start display at page:

Download "Lecture 7: Classes and Objects CS2301"

Transcription

1 Lecture 7: Classes and Objects NADA ALZAHRANI CS2301 1

2 What is OOP? Object-oriented programming (OOP) involves programming using objects. An object represents an entity in the real world that can be distinctly identified. For example, a student, a desk, a circle, chair, board. What is the properties and behavior of each of them? The state of an object consists of a set of data fields (also known as properties) with their current values. The behavior of an object is defined by a set of methods. 2

3 Different Programming Paradigms Functional/procedural programming: program is a list of instructions to the computer Object-oriented programming: program is composed of a collection objects that communicate with each other 3

4 What is procedural Approach? In Procedural approach (procedural programming) code is organised into small "procedures that use and change the data. linear programming: One thing happens and then the next. Code is executed one after the other. Functions take some input. Function Processes the input. Function produce some output. 4

5 What is OOP? Object-oriented programming (OOP): A type of programming in which programmers define not only the attributes, but also the types of operations that can be applied to the data structure. In this way, the application becomes a set of objects that includes both data and functions. 5

6 Object-oriented programming (OOP) Object-Oriented Combines data and behavior into one unit objects Provides Data abstraction and encapsulation Decompose program into objects. Programs are collections of interacting and cooperating objects. Essential for large programs Analyze program requirements, then develop 6

7 Class & Object 7

8 Example: Class ELibarary 8

9 Object Oriented Programming In object-oriented programming, classes are used to group related variables and methods. A class is a collection of a fixed number of components (members). Class creates objects. The data inside an object can only be manipulated by calling the object's methods. Data is locked inside object. Methods are the only way to access data. 9

10 Java Class Libraries Classes Include methods that perform tasks Return information after task completion Used to build Java programs Java provides class libraries Known as Java APIs (Application Programming Interfaces) 10

11 Definition of Class Classes are constructs that define objects of the same type. A Java class describes data (variables to define data fields) and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class. 11

12 About Objects Objects Reusable software components that model real-world items Look all around you People, animals, plants, cars, etc. Each object has attributes and its own behavior. Attributes Size, shape, color, weight, etc. Behaviors Babies cry, sleep, etc. 12

13 About Objects An attribute: is characteristic of an object. It describes the kinds of information that an object needs in order to provide the required behaviors. So a patient object might have attributes such as condition, date admitted, medication and so on. The attribute value of condition might be malaria and the attribute value of date admitted might be 3/1/2006. The behavior of an object: is the collection of actions an object knows how to carry out. The values of all an object s attribute together determine the object s state. 13

14 Diagram of program structure 14

15 To build class A new class will be considered as a new data type, so you can declare a variables (Objects) of them and then you can set and get data to its properties. 15

16 Example 1 1 // Fig. 3.1: GradeBook.java 2 // Class declaration with one method. 3 4 public class GradeBook 5 { 6 // display a welcome message to the GradeBook user 7 public void displaymessage() Print line of text to output 8 { 9 System.out.println( "Welcome to the Grade Book!" ); 10 } // end method displaymessage } // end class GradeBook 16

17 Example 1 1 // Fig. 3.2: GradeBookTest.java 2 // Create a GradeBook object and call its displaymessage method. 3 4 public class GradeBookTest 5 { 6 // main method begins program execution 7 public static void main( String args[] ) 8 { 9 // create a GradeBook object and assign it to mygradebook 10 GradeBook mygradebook = new GradeBook(); // call mygradebook's displaymessage method 13 mygradebook.displaymessage(); 14 } // end main } // end class GradeBookTest Use class instance creation expression to create object of class GradeBook Call method displaymessage using GradeBook object Welcome to the Grade Book! 17

18 Classes, Objects, Methods and Instance Variables Create a new class (GradeBook) Use it to create an object Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Keyword public is an access modifier. Indicates that the class is available to the public Class declarations include: Access modifier Keyword class Pair of left and right braces 18

19 Declaring a Class with a Method and Instantiating an Object of a Class Class GradeBook is not an application because it does not contain main. Can t execute GradeBook; will receive an error message. Must either declare a separate class that contains a main method or place a main method in class GradeBook. 19

20 Declaring a Class with a Method and Instantiating an Object of a Class 20

21 Class GradeBookTest Java is extensible Programmers can create new classes Class instance creation expression Keyword new creates new object of the class specified to the right of the keyword. Then name of class to create and parentheses. Calling a method Object name, then dot separator (.) Then method name and parentheses 21

22 UML class diagram for Class GradeBook Top compartment contains name of the class. Middle compartment contains class s attributes or instance variables Bottom compartment contains class s operations or methods Plus sign indicates public methods Fig. 7.3 UML class diagram indicating that class GradeBook has a public displaymessage operation. 22

23 Initializing Objects with Constructors Another definition of constructors: is a piece of code for initializing the state of an object of a class when it is created. The constructor is a method will have the same name of the class, and each class has at least one constructor. Called when keyword new is followed by the class name and parentheses Initialize an object of a class Java requires a constructor for every class Java will provide a default no-argument constructor if none is provided 23

24 Constructors A class may be declared without constructors. In this case, a no-argument constructor with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class. 24

25 Constructors - Example 25

26 Example 2 Example 2 26

27 Overloaded Constructors Overloaded constructors Provide multiple constructor definitions with different signatures No-argument constructor A constructor invoked without arguments 27

28 Access methods While properties should be private, so we need to available using them through methods, which called access methods Attributes(private instance variables) Cannot be accessed directly by clients of the object Use set methods to alter the value Use get methods to retrieve the value 28

29 Instance Variables, set Methods and get Methods Variables declared in the body of method Called local variables Can only be used within that method Variables declared in a class declaration Called fields or instance variables Each object of the class has a separate instance of the variable 29

30 Example 3 30

31 Example 3 31

32 1 // Fig. 3.7: GradeBook.java 2 // GradeBook class that contains a coursename instance variable 3 // and methods to set and get its value. 4 5 public class GradeBook Example 3 6 { 7 private String coursename; // course name for this GradeBook 8 9 // method to set the course name 10 public void setcoursename( String name ) 11 { 12 coursename = name; // store the course name 13 } // end method setcoursename // method to retrieve the course name 16 public String getcoursename() 17 { 18 return coursename; 19 } // end method getcoursename // display a welcome message to the GradeBook user 22 public void displaymessage() 23 { 24 // this statement calls getcoursename to get the 25 // name of the course this GradeBook represents 26 System.out.printf( "Welcome to the grade book for\n%s!\n", 27 getcoursename() ); 28 } // end method displaymessage } // end class GradeBook Instance variable coursename set method for coursename get method for coursename Call get method Example 4 32

33 1 // Fig. 3.8: GradeBookTest.java 2 // Create and manipulate a GradeBook object. 3 import java.util.scanner; // program uses Scanner 4 5 public class GradeBookTest 6 { 7 // main method begins program execution 8 public static void main( String args[] ) 9 { 10 // create Scanner to obtain input from command window 11 Scanner input = new Scanner( System.in ); // create a GradeBook object and assign it to mygradebook 14 GradeBook mygradebook = new GradeBook(); // display initial value of coursename 17 System.out.printf( "Initial course name is: %s\n\n", 18 mygradebook.getcoursename() ); 19 Call get method for coursename Example 4 33

34 20 // prompt for and read course name 21 System.out.println( "Please enter the course name:" ); 22 String thename = input.nextline(); // read a line of text 23 mygradebook.setcoursename( thename ); // set the course name 24 System.out.println(); // outputs a blank line // display welcome message after specifying course name 27 mygradebook.displaymessage(); 28 } // end main } // end class GradeBookTest Call set method for coursename Call displaymessage Example 4 Initial course name is: null Please enter the course name: CS101 Introduction to Java Programming Welcome to the grade book for CS101 Introduction to Java Programming! 34

35 GradeBook s UML Class Diagram with an Instance Variable and set and get Methods Attributes Listed in middle compartment Attribute name followed by colon followed by attribute type Return type of a method Indicated with a colon and return type after the parentheses after the operation name Fig. 7.9 UML class diagram indicating that class GradeBook has a coursename attribute of UML type String and three operations setcoursename (with a name parameter of UML type String), getcoursename (returns UML type String) and displaymessage. 35

36 Access modifier Access modifier: tells the Java compiler if other objects could access or invoke the corresponding class member. In this chapter, we will discuss 2 access modifiers: private: class members are accessible (visible) only inside the class, i.e. instances of other classes cannot directly access those members. If a member of a class is an attribute (instance variable) it is declared as follows: Access modifier data type attribute name; (E.g. private int position;) private keyword used for most instance variables public: class members are accessible inside and outside the class, i.e. visible to all classes. Instance variables are usually declared as private 36

37 Access Modifiers public and private private keyword used for most instance variables private variables and methods are accessible only to methods of the class in which they are declared Declaring instance variables private is known as data hiding Public variables and methods are accessible from outside the class. 37

38 My first class class Rectangle { public float length; public float width; public float area; Attributes(properties, instance variables) of rectangle class public Rectangle(){ length = 1.0f; width = 1.0f; area = recarea(); } Default Constructor of class It just gives any values to properties public Rectangle(float l, float w){ length = l; width = w; area = recarea(); } Overloaded constructor of class It gives values to properties by user } public float recarea(){ return length*width; } Method to calculate the area of rectangle Any rectangle has an area behavior 38

39 Example 5 1 // Fig. 3.4: GradeBook.java 2 // Class declaration with a method that has a parameter. 3 4 public class GradeBook 5 { 6 // display a welcome message to the GradeBook user 7 public void displaymessage( String coursename ) 8 { 9 System.out.printf( "Welcome to the grade book for\n%s!\n", 10 coursename ); 11 } // end method displaymessage } // end class GradeBook Call printf method with coursename argument 39

40 1 // Fig. 3.5: GradeBookTest.java 2 // Create GradeBook object and pass a String to 3 // its displaymessage method. 4 import java.util.scanner; // program uses Scanner 5 6 public class GradeBookTest 7 { Example 3 8 // main method begins program execution 9 public static void main( String args[] ) 10 { 11 // create Scanner to obtain input from command window 12 Scanner input = new Scanner( System.in ); // create a GradeBook object and assign it to mygradebook 15 GradeBook mygradebook = new GradeBook(); // prompt for and input course name 18 System.out.println( "Please enter the course name:" ); 19 String nameofcourse = input.nextline(); // read a line of text 20 System.out.println(); // outputs a blank line // call mygradebook's displaymessage method 23 // and pass nameofcourse as an argument 24 mygradebook.displaymessage( nameofcourse ); 25 } // end main } // end class GradeBookTest Please enter the course name: CS101 Introduction to Java Programming Welcome to the grade book for CS101 Introduction to Java Programming! Call nextline method to read a line of input Call displaymessage with an argument 40

41 OOP Main concepts in OOP: Abstraction Encapsulation Inheritance Polymorphism 41

42 Abstraction and Encapsulation Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user. 42

43 Abstraction and Encapsulation Class Encapsulation Units (classes) normally hide the details of their implementation from their clients. 43

44 Primitive Types vs. Reference Types Types in Java: Primitive : boolean, byte, char, short, int, long, float, double Reference (sometimes called nonprimitive types): Objects Used to invoke an object s methods Reference type variables are variables which are declared to hold objects. The word reference, means the reserved memory location for this variable will not hold object itself but it will hold reference (address) to where the object is stored in memory 44

45 Primitive Types vs. Reference Types Similarities and differences between value type variables & reference type variables: Reference type variables are declared in the same way as value type variables. The values of primitive data types (e.g. 42, 12.4 or d ) are predefined; they already exist. They are built into the language. You just use them. In contrast, objects have to be created as required. You can create as many objects of a class as you want while any primitive data type has a finite number of values. A programmer can invent new classes of objects. It is not possible to make new primitive data types in Java. Objects usually take up much more memory than values of primitive data types. 45

46 Referring to the Current Object s Members with the this Reference The this reference Any object can access a reference to itself with keyword this classللوصول لعناصر ال thisتستخدم Non-static methods implicitly use this when referring to the object s instance variables and other methods Can be used to access instance variables when they are shadowed by local variables or method parameters 46

47 Example 6 Example 6 47

48 Example 6 Example 6 48

49 References: There are some slides from the lectures of 1- Dr. Hikmat Jabir 2- Dr. Bayan Abu Shawar 3- lecturer Mohamed Alfara 4- Dr. Fatimah rayan 49

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved.

Introduction to Classes and Objects Pearson Education, Inc. All rights reserved. 1 3 Introduction to Classes and Objects 2 You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 1 2 Introduction to Classes and Objects You will see something new. Two things. And I call them Thing One and Thing Two. Dr. Theodor Seuss Geisel Nothing can have value without being an object of utility.

More information

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program. 1 Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program. public class Welcome1 // main method begins execution of Java application System.out.println( "Welcome to Java Programming!" ); } //

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

How to engineer a class to separate its interface from its implementation and encourage reuse.

How to engineer a class to separate its interface from its implementation and encourage reuse. 1 3 Introduction to Classes and Objects 2 OBJECTIVES In this chapter you ll learn: What classes, objects, member functions and data members are. How to define a class and use it to create an object. How

More information

A A B U n i v e r s i t y

A A B U n i v e r s i t y A A B U n i v e r s i t y Faculty of Computer Sciences O b j e c t O r i e n t e d P r o g r a m m i n g Week 4: Introduction to Classes and Objects Asst. Prof. Dr. M entor Hamiti mentor.hamiti@universitetiaab.com

More information

Object Oriented Programming. Java-Lecture 1

Object Oriented Programming. Java-Lecture 1 Object Oriented Programming Java-Lecture 1 Standard output System.out is known as the standard output object Methods to display text onto the standard output System.out.print prints text onto the screen

More information

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved. Assoc. Prof. Dr. Marenglen Biba Laboratory Session: Exercises on classes Analogy to help you understand classes and their contents. Suppose you want to drive a car and make it go faster by pressing down

More information

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 3- Control Statements: Part I Objectives: To use the if and if...else selection statements to choose among alternative actions. To use the

More information

by Pearson Education, Inc. All Rights Reserved. 2

by Pearson Education, Inc. All Rights Reserved. 2 Suppose you want to drive a car and make it go faster by pressing down on its accelerator pedal. Before you can drive a car, someone has to design it and build it. A car typically begins as engineering

More information

Computer Programming C++ Classes and Objects 6 th Lecture

Computer Programming C++ Classes and Objects 6 th Lecture Computer Programming C++ Classes and Objects 6 th Lecture 엄현상 (Eom, Hyeonsang) School of Computer Science and Engineering Seoul National University Copyrights 2015 Eom, Hyeonsang All Rights Reserved Outline

More information

Introduction to Computer Science I

Introduction to Computer Science I Introduction to Computer Science I Classes Janyl Jumadinova 5-7 March, 2018 Classes Most of our previous programs all just had a main() method in one file. 2/13 Classes Most of our previous programs all

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M Control Structures Intro. Sequential execution Statements are normally executed one

More information

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved. Java How to Program, 10/e Education, Inc. All Rights Reserved. Each class you create becomes a new type that can be used to declare variables and create objects. You can declare new classes as needed;

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Nothing can have value without being an object of utility. Karl Marx Your public servants serve you right. Adlai E. Stevenson Knowing how to answer one who speaks, To reply to one who sends a message.

More information

Introduction to Classes and Objects

Introduction to Classes and Objects 3 Introduction to Classes and Objects OBJECTIVES In this chapter you will learn: What classes, objects, methods and instance variables are. How to declare a class and use it to create an object. How to

More information

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes CS111: PROGRAMMING LANGUAGE II Lecture 1: Introduction to classes Lecture Contents 2 What is a class? Encapsulation Class basics: Data Methods Objects Defining and using a class In Java 3 Java is an object-oriented

More information

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of Basic Problem solving Techniques Top Down stepwise refinement If & if.. While.. Counter controlled and sentinel controlled repetition Usage of Assignment increment & decrement operators 1 ECE 161 WEEK

More information

Fundamentals of Programming Session 25

Fundamentals of Programming Session 25 Fundamentals of Programming Session 25 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2014 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

Fundamentals of Programming Session 23

Fundamentals of Programming Session 23 Fundamentals of Programming Session 23 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 These slides have been created using Deitel s slides Sharif University of Technology Outlines

More information

JAVA- PROGRAMMING CH2-EX1

JAVA- PROGRAMMING CH2-EX1 http://www.tutorialspoint.com/java/java_overriding.htm package javaapplication1; public class ch1_ex1 JAVA- PROGRAMMING CH2-EX1 //main method public static void main(string[] args) System.out.print("hello

More information

Object Oriented Design

Object Oriented Design Object Oriented Design Lecture 3: Introduction to C++ (Continue) Examples using declarations that eliminate the need to repeat the std:: prefix 1 Examples using namespace std; enables a program to use

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

Cpt S 122 Data Structures. Introduction to C++ Part II

Cpt S 122 Data Structures. Introduction to C++ Part II Cpt S 122 Data Structures Introduction to C++ Part II Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Objectives Defining class with a member function

More information

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes

Objects and Classes. 1 Creating Classes and Objects. CSCI-UA 101 Objects and Classes Based on Introduction to Java Programming, Y. Daniel Liang, Brief Version, 10/E 1 Creating Classes and Objects Classes give us a way of defining custom data types and associating data with operations on

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Real world objects include things like your car, TV etc. These objects share two characteristics: they all have state and they all have behavior. Software objects are

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 20, 2014 Abstract

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Inheritance Introduction Generalization/specialization Version of January 21, 2013 Abstract

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

Full file at

Full file at Chapter 2 Introduction to Java Applications Section 2.1 Introduction ( none ) Section 2.2 First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler

More information

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved.

Functions and an Introduction to Recursion Pearson Education, Inc. All rights reserved. 1 6 Functions and an Introduction to Recursion 2 Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and Classes CS 251 Intermediate Programming Methods and Classes Brooke Chenoweth University of New Mexico Fall 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

CS 251 Intermediate Programming Methods and More

CS 251 Intermediate Programming Methods and More CS 251 Intermediate Programming Methods and More Brooke Chenoweth University of New Mexico Spring 2018 Methods An operation that can be performed on an object Has return type and parameters Method with

More information

Section 2.2 Your First Program in Java: Printing a Line of Text

Section 2.2 Your First Program in Java: Printing a Line of Text Chapter 2 Introduction to Java Applications Section 2.2 Your First Program in Java: Printing a Line of Text 2.2 Q1: End-of-line comments that should be ignored by the compiler are denoted using a. Two

More information

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal Lesson Goals Understand Object Oriented Programming The Syntax of Class Definitions Constructors this Object Oriented "Hello

More information

Dr.Ammar Almomani. Dept. of Information Technology, Al-Huson University College, Al- Balqa Applied University. Dr. Ammar Almomani-2017-BAU Page 1

Dr.Ammar Almomani. Dept. of Information Technology, Al-Huson University College, Al- Balqa Applied University. Dr. Ammar Almomani-2017-BAU Page 1 Dr.Ammar Almomani Dept. of Information Technology, Al-Huson University College, Al- Balqa Applied University Dr. Ammar Almomani-2017-BAU Page 1 Java Programming Syllabus First semester/ 2017 Course Title:

More information

Chapter 4 Defining Classes I

Chapter 4 Defining Classes I Chapter 4 Defining Classes I This chapter introduces the idea that students can create their own classes and therefore their own objects. Introduced is the idea of methods and instance variables as the

More information

CSCI 355 Lab #2 Spring 2007

CSCI 355 Lab #2 Spring 2007 CSCI 355 Lab #2 Spring 2007 More Java Objectives: 1. To explore several Unix commands for displaying information about processes. 2. To explore some differences between Java and C++. 3. To write Java applications

More information

JAVA: A Primer. By: Amrita Rajagopal

JAVA: A Primer. By: Amrita Rajagopal JAVA: A Primer By: Amrita Rajagopal 1 Some facts about JAVA JAVA is an Object Oriented Programming language (OOP) Everything in Java is an object application-- a Java program that executes independently

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson Introduction History, Characteristics of Java language Java Language Basics Data types, Variables, Operators and Expressions Anatomy of a Java Program

More information

Object Oriented Programming with Java

Object Oriented Programming with Java Object Oriented Programming with Java What is Object Oriented Programming? Object Oriented Programming consists of creating outline structures that are easily reused over and over again. There are four

More information

Defining Classes and Methods

Defining Classes and Methods Defining Classes and Methods Chapter 5 Modified by James O Reilly Class and Method Definitions OOP- Object Oriented Programming Big Ideas: Group data and related functions (methods) into Objects (Encapsulation)

More information

Object Orientated Analysis and Design. Benjamin Kenwright

Object Orientated Analysis and Design. Benjamin Kenwright Notation Part 2 Object Orientated Analysis and Design Benjamin Kenwright Outline Review What do we mean by Notation and UML? Types of UML View Continue UML Diagram Types Conclusion and Discussion Summary

More information

Object-Oriented Programming Concepts

Object-Oriented Programming Concepts Object-Oriented Programming Concepts Object-oriented programming מונחה עצמים) (תכנות involves programming using objects An object ) represents (עצם an entity in the real world that can be distinctly identified

More information

JAVA GUI PROGRAMMING REVISION TOUR III

JAVA GUI PROGRAMMING REVISION TOUR III 1. In java, methods reside in. (a) Function (b) Library (c) Classes (d) Object JAVA GUI PROGRAMMING REVISION TOUR III 2. The number and type of arguments of a method are known as. (a) Parameter list (b)

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Programming with Java

Programming with Java Programming with Java Data Types & Input Statement Lecture 04 First stage Software Engineering Dep. Saman M. Omer 2017-2018 Objectives q By the end of this lecture you should be able to : ü Know rules

More information

Object Oriented Methods : Deeper Look Lecture Three

Object Oriented Methods : Deeper Look Lecture Three University of Babylon Collage of Computer Assistant Lecturer : Wadhah R. Baiee Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces,

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Outline Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters Variable Length Parameter Lists split() Method from String Class Integer & Double Wrapper

More information

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I AL GHURAIR UNIVERSITY College of Computing CSC 209 JAVA I week 2- Arithmetic and Decision Making: Equality and Relational Operators Objectives: To use arithmetic operators. The precedence of arithmetic

More information

CLASSES AND OBJECTS IN JAVA

CLASSES AND OBJECTS IN JAVA Lesson 8 CLASSES AND OBJECTS IN JAVA (1) Which of the following defines attributes and methods? (a) Class (b) Object (c) Function (d) Variable (2) Which of the following keyword is used to declare Class

More information

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Assignments Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4 Lecture 6 Complete for Lab 4, Project 1 Note: Slides 12 19 are summary slides for Chapter 2. They overview much of what we covered but are not complete.

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved. 1 1 Introduction to OOP Using Java 2 Introduction Sun s implementation called the Java Development Kit (JDK) Object-Oriented Programming Java is language of choice for networked applications Java Enterprise

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M Università degli Studi di Bologna Facoltà di Ingegneria Principles, Models, and Applications for Distributed Systems M tutor Isam M. Al Jawarneh, PhD student isam.aljawarneh3@unibo.it Mobile Middleware

More information

Lecture 6 Introduction to Objects and Classes

Lecture 6 Introduction to Objects and Classes Lecture 6 Introduction to Objects and Classes Outline Basic concepts Recap Computer programs Programming languages Programming paradigms Object oriented paradigm-objects and classes in Java Constructors

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 02: Using Objects MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Using Objects 2 Introduction to Object Oriented Programming Paradigm Objects and References Memory Management

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

2. The object-oriented paradigm!

2. The object-oriented paradigm! 2. The object-oriented paradigm! Plan for this section:! n Look at things we have to be able to do with a programming language! n Look at Java and how it is done there" Note: I will make a lot of use of

More information

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide AP Computer Science Chapter 10 Implementing and Using Classes Study Guide 1. A class that uses a given class X is called a client of X. 2. Private features of a class can be directly accessed only within

More information

More C++ : Vectors, Classes, Inheritance, Templates

More C++ : Vectors, Classes, Inheritance, Templates Vectors More C++ : Vectors,, Inheritance, Templates vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes defined differently can be resized without explicit

More information

5. Defining Classes and Methods

5. Defining Classes and Methods 5. Defining Classes and Methods Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich http://seal.ifi.uzh.ch/info1 Objectives Describe and define concepts of class, class object Describe use

More information

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding Java Class Design Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding eugeny.berkunsky@gmail.com http://www.berkut.mk.ua Objectives Implement encapsulation Implement inheritance

More information

Introduction to Java Applications

Introduction to Java Applications 2 Introduction to Java Applications OBJECTIVES In this chapter you will learn: To write simple Java applications. To use input and output statements. Java s primitive types. Basic memory concepts. To use

More information

Lecture 5: Methods CS2301

Lecture 5: Methods CS2301 Lecture 5: Methods NADA ALZAHRANI CS2301 1 Opening Problem Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively. 2 Solution public static int sum(int i1, int i2) { int

More information

MIDTERM REVIEW. midterminformation.htm

MIDTERM REVIEW.   midterminformation.htm MIDTERM REVIEW http://pages.cpsc.ucalgary.ca/~tamj/233/exams/ midterminformation.htm 1 REMINDER Midterm Time: 7:00pm - 8:15pm on Friday, Mar 1, 2013 Location: ST 148 Cover everything up to the last lecture

More information

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. Chapter 9 Objects and Classes 1 Objectives Classes & Objects ( 9.2). UML ( 9.2). Constructors ( 9.3). How to declare a class & create an object ( 9.4). Separate a class declaration from a class implementation

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

Chapter 4. Defining Classes I

Chapter 4. Defining Classes I Chapter 4 Defining Classes I Introduction Classes are the most important language feature that make object oriented programming (OOP) possible Programming in Java consists of dfii defining a number of

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com More C++ : Vectors, Classes, Inheritance, Templates with content from cplusplus.com, codeguru.com 2 Vectors vectors in C++ basically arrays with enhancements indexed similarly contiguous memory some changes

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Control Statements: Part Pearson Education, Inc. All rights reserved.

Control Statements: Part Pearson Education, Inc. All rights reserved. 1 5 Control Statements: Part 2 2 Not everything that can be counted counts, and not every thing that counts can be counted. Albert Einstein Who can control his fate? William Shakespeare The used key is

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

Object Oriented Programming

Object Oriented Programming Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Lab 11 Object Oriented Programming Eng. Mohammed Alokshiya December 16, 2014 Object-oriented

More information

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5 Defining Classes and Methods Chapter 5 Objectives Describe concepts of class, class object Create class objects Define a Java class, its methods Describe use of parameters in a method Use modifiers public,

More information

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003

Outline. Object Oriented Programming. Course goals. Staff. Course resources. Assignments. Course organization Introduction Java overview Autumn 2003 Outline Object Oriented Programming Autumn 2003 2 Course goals Software design vs hacking Abstractions vs language (syntax) Java used to illustrate concepts NOT a course about Java Prerequisites knowledge

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

CS304 Object Oriented Programming Final Term

CS304 Object Oriented Programming Final Term 1. Which of the following is the way to extract common behaviour and attributes from the given classes and make a separate class of those common behaviours and attributes? Generalization (pg 29) Sub-typing

More information

Building custom components IAT351

Building custom components IAT351 Building custom components IAT351 Week 1 Lecture 1 9.05.2012 Lyn Bartram lyn@sfu.ca Today Review assignment issues New submission method Object oriented design How to extend Java and how to scope Final

More information

2. The object-oriented paradigm

2. The object-oriented paradigm 2. The object-oriented paradigm Plan for this section: Look at things we have to be able to do with a programming language Look at Java and how it is done there Note: I will make a lot of use of the fact

More information

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

CHAPTER 7 OBJECTS AND CLASSES

CHAPTER 7 OBJECTS AND CLASSES CHAPTER 7 OBJECTS AND CLASSES OBJECTIVES After completing Objects and Classes, you will be able to: Explain the use of classes in Java for representing structured data. Distinguish between objects and

More information

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous CS256 Computer Science I Kevin Sahr, PhD Lecture 25: Miscellaneous 1 main Method Arguments recall the method header of the main method note the argument list public static void main (String [] args) we

More information

Software Design and Analysis for Engineers

Software Design and Analysis for Engineers Software Design and Analysis for Engineers by Dr. Lesley Shannon Email: lshannon@ensc.sfu.ca Course Website: http://www.ensc.sfu.ca/~lshannon/courses/ensc251 Simon Fraser University Slide Set: 2 Date:

More information

Sri Vidya College of Engineering & Technology

Sri Vidya College of Engineering & Technology UNIT I INTRODUCTION TO OOP AND FUNDAMENTALS OF JAVA 1. Define OOP. Part A Object-Oriented Programming (OOP) is a methodology or paradigm to design a program using classes and objects. It simplifies the

More information

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal COMSC-051 Java Programming Part 1 Part-Time Instructor: Joenil Mistal Chapter 4 4 Moving Toward Object- Oriented Programming This chapter provides a provides an overview of basic concepts of the object-oriented

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information