Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Similar documents
OOP Part 2. Introduction to OOP with Java. Lecture 08: Introduction to OOP with Java - AKF Sep AbuKhleiF -

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 9 Objects and Classes. OO Programming Concepts. Classes. Objects. Motivations. Objectives. CS1: Java Programming Colorado State University

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

Chapter 8 Objects and Classes

Chapter 8 Objects and Classes Part 1

Object Oriented System Development Paradigm. Sunnie Chung CIS433 System Analysis Methods

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

OO Programming Concepts

ECOM 2324 COMPUTER PROGRAMMING II

Arrays. Introduction to OOP with Java. Lecture 06: Introduction to OOP with Java - AKF Sep AbuKhleiF - 1

Chapter 8 Objects and Classes. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Introduction. Introduction to OOP with Java. Lecture 01: Introduction to OOP with Java - AKF Sep AbuKhleiF -

OO Programming Concepts. Classes. Objects. Chapter 8 User-Defined Classes and ADTs

UFCE3T-15-M Object-oriented Design and Programming

Chapter 9. Objects and Classes

CS 170, Section /3/2009 CS170, Section 000, Fall

Lab Exercise 1. Objectives: Part 1. Introduction

Chapter 3 Objects and Classes

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

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010


Object Oriented Programming

Programming in the Large II: Objects and Classes (Part 1)

Object Oriented Programming. Java-Lecture 1

Tips from the experts: How to waste a lot of time on this assignment

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

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

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

Lecture 7: Classes and Objects CS2301

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

In this lab, you will be given the implementation of the classes GeometricObject, Circle, and Rectangle, as shown in the following UML class diagram.

Object-Oriented Programming Concepts

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Introduction to Classes and Objects

Lecture 6 Introduction to Objects and Classes

Programs as Models. Procedural Paradigm. Class Methods. CS256 Computer Science I Kevin Sahr, PhD. Lecture 11: Objects

Introduction to Programming Using Java (98-388)

Inf1-OOP. Data Types. Defining Data Types in Java. type value set operations. Overview. Circle Class. Creating Data Types 1.

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

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn how to describe objects and classes and how to define classes and create objects

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August


Packages & Random and Math Classes

What is a Computer? Chapter 1-9, 12-13, 18, 20, 23 Review Slides

Chapter 1-9, 12-13, 18, 20, 23 Review Slides. What is a Computer?

Fundamentals of Programming Data Types & Methods

Arrays. Eng. Mohammed Abdualal

CSEN401 Computer Programming Lab. Topics: Introduction and Motivation Recap: Objects and Classes

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

CS1004: Intro to CS in Java, Spring 2005

H212 Introduction to Software Systems Honors

Chapter 6 Introduction to Defining Classes

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Introduction to Java Programming

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

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

Programming II (CS300)

CHAPTER 7 OBJECTS AND CLASSES

2. The object-oriented paradigm!

Chapter 8 Objects and Classes Dr. Essam Halim Date: Page 1

CS 101 Fall 2006 Midterm 1 Name: ID:

Lesson 10: Quiz #1 and Getting User Input (W03D2)

CS 116. Lab Assignment # 1 1

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

Creating Classes and Objects

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Lecture Notes Chapter #9_b Inheritance & Polymorphism

Eng. Mohammed S. Abdualal

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

Object Oriented Programming. Java-Lecture 6 - Arrays

SAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLESAMPLE SAMPLE CSE21

Programming with Java

22. Inheritance. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Introduction to Classes and Objects

Programming 2. Inheritance & Polymorphism

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

CEN 414 Java Programming

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

CHAPTER 7 OBJECTS AND CLASSES

CISC 3115 TY3. C09a: Inheritance. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 9/20/2018 CUNY Brooklyn College

Web-CAT submission URL: CAT.woa/wa/assignments/eclipse

Classes. Classes. Classes. Class Circle with methods. Class Circle with fields. Classes and Objects in Java. Introduce to classes and objects in Java.

2 What are the differences between constructors and methods?

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

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.

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

Abstract Class. Lecture 21. Based on Slides of Dr. Norazah Yusof

Java Coding 3. Over & over again!

Full file at

Recommended Group Brainstorm (NO computers during this time)

CS 113 PRACTICE FINAL

Transcription:

Introduction to OOP with Java Instructor: AbuKhleif, Mohammad Noor Sep 2017

Lecture 07: OOP Part 1 Instructor: AbuKhleif, Mohammad Noor Sep 2017

Instructor AbuKhleif, Mohammad Noor Computer Engineer (JU 2012-2017) Software Automation Engineer @ Atypon John Wiley and Sons Company - Jordan Branch Reach me at: moh.noor94@gmail.com facebook.com/moh.noor94 twitter.com/moh_noor94 3

Course Java SE Basics Object Oriented Programming Course Page: /courses/java-101-sep-2017 Or, go to: Courses Java 101 Course Sep 2017 Course Facebook Group: www.facebook.com/groups/akf2017java 4

Let s Start! 5

What is an Object? Object-oriented programming (OOP) involves programming using objects. An object represents an entity that can be distinctly identified. For example, a student, a desk, a circle, and a button can all be viewed as objects. 6

What is an Object? An object has a unique: Identity State Defines the object Also known as properties or attributes Represented by data fields with their current values Behavior Defines what the object does Also known as actions Defined by methods To invoke a method on an object is to ask the object to perform an action 7

What is a class? A class is a template, or construct that defines what an objects data fields and methods will be. A class is essentially a programmer-defined type. An object is an instance of a class. You can create many instances of a class. Creating an instance is referred to as instantiation. The terms object and instance are often interchangeable. Objects of the same type are defined using a common class. A Java class uses: Variables to define data fields, and Methods to define actions Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class. 8

Classes and Objects, Example Class Name: Circle A class template Data Fields: radius is Methods: getarea Circle Object 1 Data Fields: radius is 10 Circle Object 2 Data Fields: radius is 25 Circle Object 3 Data Fields: radius is 125 Three objects of the Circle class 9

Classes and Objects, Example class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { /** Construct a circle object */ Circle(double newradius) { radius = newradius; Data field Constructors /** Return the area of this circle */ double getarea() { return radius * radius * 3.14159; Method 10

Declaring and Creating Objects The syntax to declare an object reference variable is: ClassName objectrefvar; Example: Circle mycircle; A class is a reference type: a variable of the class type can reference an instance (object) of the class. To create an object and assign its reference to a declared object reference variable: objectrefvar = new ClassName (); Example: mycircle = new Circle(); 11

Declaring and Creating Objects, cont. A single statement can be used to combine 1. The declaration of an object reference variable, 2. The creation of an object, and 3. The assigning of an object reference to the variable as follows: ClassName objectrefvar = new ClassName(); Example: Circle mycircle = new Circle (); 12

Accessing an Object s Members In OOP, object s members are its data fields and methods. An object s data can be accessed and its methods invoked using the dot operator (.). To reference a data field in an object: objectrefvar.datafield Example: mycircle.radius To invoke a method on an object: objectrefvar.method(arguments) Example: mycircle.setradius(5); 13

Example: TestCircle.java Only one class in a file can be a public class. public class TestCircle { public static void main(string[] args) { Circle circle1 = new Circle(); circle1.setradius(5); System.out.println("The radius of this circle is" + circle1.radius); The public class must have the same name as the file name. class Circle { double radius; void setradius(double newradius) { radius = newradius; Remember: this is where the program starts execution. 14

Constructing Objects Using Constructors 15

Constructing Objects Using Constructors A constructor is invoked to create an object using the new operator. Constructors are a special kind of method. They have three peculiarities: A constructor must have the same name as the class itself. Constructors do not have a return type. Not even void. Constructors are invoked using the new operator when an object is created. They play the role of initializing objects. 16

Constructing Objects Using Constructors, cont. A class may be defined without constructors. In this case, a default constructor is provided automatically: A default constructor is a public no-argument (no-arg) constructor with an empty body which is implicitly defined in the class. A default constructor is provided only if there are no other constructors explicitly defined in the class. 17

Constructing Objects Using Constructors, Example public class TestCircle { public static void main(string[] args) { Circle circle1 = new Circle(5); System.out.println("The radius of this circle is " + circle1.radius); class Circle { double radius; Circle(double initialradius) { radius = initialradius; void setradius(double newradius) { radius = newradius; 18

Constructors Overloading Like regular methods, constructors can be overloaded. Example: class Circle{ double radius; Circle(){ radius = 1; Circle (double initialradius){ radius = initial Radius; void setradius (double newradius){ radius = newradius; Circle myfirstcircle = new Circle (); Circle mysecondcircle = new Circle(5); 19

IntelliJ IDEA Shortcuts Alt + Insert 20

Reading from the Console - Revisit import java.util.scanner; public class AgeCalculater { public static void main(string[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Please enter your name: "); String name = scanner.nextline(); System.out.println("Please enter your birth year: "); int age = scanner.nextint(); System.out.println("Welcome " + name + ", you are " + (2017 - age) + " years now :D"); 21

Differences between Variables of Primitive Data Types and Object Types 22

Reference Data Fields and the null Value Java assigns default values to data fields when an object is created. 0 for numeric type. false for a boolean type. \u0000 for a char type. null for a reference type. Null is a special literal used for reference types. However, Java assigns no default value to a local variable inside a method. 23

Differences between Variables of Primitive Data Types and Object Types Every variable represents a memory location that holds a value. A variable of a primitive type holds a value of the primitive type, and a variable of a reference type holds a reference to where an object is stored in memory. Primitive type int i = 1 i 1 Created using new Circle() Object type Circle c c reference c: Circle radius = 1 24

Differences between Variables of Primitive Data Types and Object Types Object type assignment c1 = c2 Primitive type assignment i = j Before: After: Before: After: c1 c1 i 1 i 2 c2 c2 j 2 j 2 c1: Circle radius = 5 C2: Circle radius = 9 c1: Circle radius = 5 C2: Circle radius = 9 25

Garbage Collection 26

Garbage Collection As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. The object previously referenced by c1 is no longer referenced. This object is known as garbage. Garbage is automatically collected by JVM. 27

Garbage Collection TIP: If you know that an object is no longer needed, you can explicitly assign null to a reference variable for the object. The JVM will automatically collect the space if the object is not referenced by any variable. 28

Let s Example! 29

public class TV { int channel = 1; // Default channel is 1 int volumelevel = 1; // Default volume level is 1 boolean on = false; // By default TV is off public TV() { public void turnon() { on = true; public void turnoff() { on = false; TV.java public void setchannel(int newchannel) { if (on && newchannel >= 1 && newchannel <= 120) channel = newchannel; 30

public void setvolume(int newvolumelevel) { if (on && newvolumelevel >= 1 && newvolumelevel <= 7) volumelevel = newvolumelevel; public void channelup() { if (on && channel < 120) channel++; public void channeldown() { if (on && channel > 1) channel--; TV.java public void volumeup() { if (on && volumelevel < 7) volumelevel++; public void volumedown() { if (on && volumelevel > 1) volumelevel--; 31

public class TestTV { public static void main(string[] args) { TV tv1 = new TV(); tv1.turnon(); tv1.setchannel(30); tv1.setvolume(3); TV tv2 = new TV(); tv2.turnon(); tv2.channelup(); tv2.channelup(); tv2.volumeup(); System.out.println("tv1's channel is " + tv1.channel + " and volume level is " + tv1.volumelevel); System.out.println("tv2's channel is " + tv2.channel + " and volume level is " + tv2.volumelevel); TestTV.java 32

Let s Code Create a class called Date that includes three pieces of information as instance variables: A month (int), a day (int) and a year (int). Your class should have a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a method displaydate that displays the month, day and year separated by forward slashes (/). Write a test application named DateTest that demonstrates classdate s capabilities. 33

Let s Code Create a class called Employee that includes three pieces of information as instance variables: A first name (String), a last name (String) and a monthly salary (double). Your class should have a constructor that initializes the three instance variables. If the monthly salary is not positive, set it to 0.0. Write a test application named EmployeeTest that demonstrates class Employee s capabilities. Create two Employee objects and display each object s yearly salary. Then give each Employee a 10% raise and display each Employee s yearly salary again. 34

UML Class Diagrams (1) 35

UML Class Diagrams A standardized notation to illustrate classes and objects is the Unified Modeling Language (UML) class diagram. UML Class Diagram Circle Class name radius: double Data fields Circle() Circle(newRadius: double) Constructors and methods getarea(): double circle1: Circle radius = 1.0 circle2: Circle radius = 25 circle3: Circle radius = 125 UML notation for objects 36

Homework

Homework Part 1- Implement the following Polygon class: numberofsides: int sideslengths: double [] Polygon() Polygon(number: int) Polygon(number: int, values: double[]) isequilateral(): Boolean getperimeter(): double Polygon 38

Homework The Polygon class contains: An integer data field named (numberofsides) that represents the number of the polygon sides. An array of double values called (sideslengths) that represent the lengths (in cm) of the polygon sides. The size of this array equals the (numberofsides) value. 39

Homework The Polygon class contains: A no-arg constructor that creates a polygon with 4 sides all with length of 1 cm. A constructor that creates a polygon with the specified number of sides (passed as a parameter), and initializes the sides lengths by reading them from the user. A constructor that creates a polygon with the specified number of sides and sides lengths (both passed as parameters). 40

Homework The Polygon class contains: A method named isequilateral that returns true if all the polygon s sides are of the same length and false otherwise. A method named getperimeter that returns the perimeter of the polygon. 41

Homework Part 2- In your main class: Declare and create 3 polygon objects: 1. The first polygon using the no-arg constructor. 2. The second polygon with 3 sides whose lengths read from the user. 3. The third polygon with 4 sides and the following lengths: 1, 2, 2, and 4 cm. Print the following table which contains info of the three polygons: Polygon# number of sides sides lengths Equilateral? Perimeter 42

Homework Submission Submit only the Polygon.java file. Upload your file to the Facebook group. Submission due: Thursday, Sep 28-08:00 PM Late submission will not be reviewed by the instructor. Public solutions upload goal is to share knowledge, you can see other s solutions, but, please, don t cheat yourself! Don t forget, your solution should be well-documented, welldesigned, and well-styled. 43

References: - Eng. Asma Abdel Karim Computer Engineering Department, JU Slides. - Liang, Introduction to Java Programming 10/e Instructor: AbuKhleif, Mohammad Noor Sep 2017 End of Lecture =D