Chapter 8 Objects and Classes Part 1

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

OO Programming Concepts

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

Chapter 8 Objects and Classes

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

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

ECOM 2324 COMPUTER PROGRAMMING II

Chapter 9. Objects and Classes

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

Chapter 8 Objects and Classes

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

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

UFCE3T-15-M Object-oriented Design and Programming

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

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

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

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

Chapter 3 Objects and Classes

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

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

Object Oriented Programming

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

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

We are on the GUI fast track path

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

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

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

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

Lecture 6 Introduction to Objects and Classes

2 What are the differences between constructors and methods?

Fundamentals of Programming Data Types & Methods

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

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

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

Lecture 7: Classes and Objects CS2301

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

CS1150 Principles of Computer Science Objects and Classes

CLASSES AND OBJECTS. Fundamentals of Computer Science I

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

Chapter 6 Introduction to Defining Classes

Chapter 2 Primitive Data Types and Operations. Objectives

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

Object-Oriented Programming Concepts

MIDTERM REVIEW. midterminformation.htm

Computational Expression

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Chapter 2. Elementary Programming

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

A Foundation for Programming

Object Oriented Programming SCJ2153. Class and Object. Associate Prof. Dr. Norazah Yusof

Chapter 2 Elementary Programming

Programming II (CS300)

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

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

CHAPTER 7 OBJECTS AND CLASSES

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

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

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

Object-Oriented Programming Concepts

Final Examination Semester 2 / Year 2010

Chapter 10 Inheritance and Polymorphism. Dr. Hikmat Jaber

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Questions Answer Key Questions Answer Key Questions Answer Key

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.

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

CS1004: Intro to CS in Java, Spring 2005

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

CS Week 13. Jim Williams, PhD

Object Oriented Modeling

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

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

CHAPTER 7 OBJECTS AND CLASSES

Inheritance and Polymorphism

A foundation for programming. Classes and objects. Overview. Java primitive types. Primitive types Creating your own data types

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

Inheritance and Polymorphism

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

Classes - 2. Data Processing Course, I. Hrivnacova, IPN Orsay

ITI Introduction to Computing II

Lab Exercise 1. Objectives: Part 1. Introduction

Chapter 2 Elementary Programming

Chapter 11 Inheritance and Polymorphism

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

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

Graphical User Interfaces. Comp 152

ITI Introduction to Computing II

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

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Chapter 1: Introduction to Computers, Programs, and Java

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Chapter 15: Object Oriented Programming

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

Lecture Notes Chapter #9_b Inheritance & Polymorphism

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

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

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

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Transcription:

Chapter 8 Objects and Classes Part 1 1

OO Programming Concepts 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, a button, and even a loan can all be viewed as objects. An object has a unique identity, state, and behaviors. 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

Objects 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 An object has both a state and behavior. The state defines the object, and the behavior defines what the object does. 3

Classes Classes are constructs that define objects of the same type. A Java class uses 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. 4

Classes 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 5

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 6

Example: Defining Classes and Creating Objects Objective: Demonstrate creating objects, accessing data, and using methods. TestCircle1 Run 7

Example: Defining Classes and Creating Objects Objective: Demonstrate creating objects, accessing data, and using methods. TV TestTV Run 8

Constructors Circle() { } Constructors are a special kind of methods that are invoked to construct objects. Circle(double newradius) { radius = newradius; } 9

Constructors, cont. A constructor with no parameters is referred to as a no-arg constructor. Constructors 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. Constructors play the role of initializing objects. 10

Creating Objects Using new ClassName(); Constructors Example: new Circle(); new Circle(5.0); 11

Default Constructor A class may be declared without constructors. In this case, a no-arg 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. 12

Declaring/Creating Objects in a Single Step ClassName objectrefvar = new ClassName(); Assign object reference Create an object Example: Circle mycircle = new Circle(); 13

Accessing Objects Referencing the object s data: objectrefvar.data e.g., mycircle.radius Invoking the object s method: objectrefvar.methodname(arguments) e.g., mycircle.getarea() 14

animation Trace Code Declare mycircle Circle mycircle = new Circle(5.0); SCircle yourcircle = new Circle(); yourcircle.radius = 100; mycircle no value 15

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle no value yourcircle.radius = 100; : Circle radius: 5.0 Create a circle 16

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; Assign object reference to mycircle radius: 5.0 : Circle 17

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle no value Declare yourcircle 18

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle no value : Circle Create a new Circle object radius: 0.0 19

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle reference value Assign object reference to yourcircle : Circle radius: 1.0 20

animation Trace Code, cont. Circle mycircle = new Circle(5.0); Circle yourcircle = new Circle(); mycircle reference value yourcircle.radius = 100; : Circle radius: 5.0 yourcircle reference value Change radius in yourcircle : Circle radius: 100.0 21

Reference Data Fields The data fields can be of reference types. For example, the following Student class contains a data field name of the String type. public class Student { String name; // name has default value null int age; // age has default value 0 boolean issciencemajor; // issciencemajor has default value false char gender; // c has default value '\u0000' } 22

The null Value If a data field of a reference type does not reference any object, the data field holds a special literal value, null. 23

Default Value for a Data Field The default value of a data field is null for a reference type, 0 for a numeric type, false for a boolean type, and '\u0000' for a char type. However, Java assigns no default value to a local variable inside a method. public class Test { } public static void main(string[] args) { } Student student = new Student(); System.out.println("name? " + student.name); System.out.println("age? " + student.age); System.out.println("isScienceMajor? " + student.issciencemajor); System.out.println("gender? " + student.gender); 24

Example Java assigns no default value to a local variable inside a method. public class Test { public static void main(string[] args) { int x; // x has no default value String y; // y has no default value System.out.println("x is " + x); System.out.println("y is " + y); } } Compilation error: variables not initialized 25

The Date Class Java provides a system-independent encapsulation of date and time in the java.util.date class. You can use the Date class to create an instance for the current date and time and use its tostring method to return the date and time as a string. The + sign indicates public modifer java.util.date +Date() +Date(elapseTime: long) +tostring(): String +gettime(): long +settime(elapsetime: long): void Constructs a Date object for the current time. Constructs a Date object for a given time in milliseconds elapsed since January 1, 1970, GMT. Returns a string representing the date and time. Returns the number of milliseconds since January 1, 1970, GMT. Sets a new elapse time in the object. 26

The Date Class Example For example, the following code java.util.date date = new java.util.date(); System.out.println(date.toString()); displays a string like Sun Mar 09 13:50:19 EST 2003. 27

The Random Class You have used Math.random() to obtain a random double value between 0.0 and 1.0 (excluding 1.0). A more useful random number generator is provided in the java.util.random class. java.util.random +Random() +Random(seed: long) +nextint(): int +nextint(n: int): int +nextlong(): long +nextdouble(): double +nextfloat(): float +nextboolean(): boolean Constructs a Random object with the current time as its seed. Constructs a Random object with a specified seed. Returns a random int value. Returns a random int value between 0 and n (exclusive). Returns a random long value. Returns a random double value between 0.0 and 1.0 (exclusive). Returns a random float value between 0.0F and 1.0F (exclusive). Returns a random boolean value. 28

The Random Class Example If two Random objects have the same seed, they will generate identical sequences of numbers. For example, the following code creates two Random objects with the same seed 3. Random random1 = new Random(3); System.out.print("From random1: "); for (int i = 0; i < 10; i++) System.out.print(random1.nextInt(1000) + " "); Random random2 = new Random(3); System.out.print("\nFrom random2: "); for (int i = 0; i < 10; i++) System.out.print(random2.nextInt(1000) + " "); From random1: 734 660 210 581 128 202 549 564 459 961 From random2: 734 660 210 581 128 202 549 564 459 961 29

Displaying GUI Components When you develop programs to create graphical user interfaces, you will use Java classes such as JFrame, JButton, JRadioButton, JComboBox, and JList to create frames, buttons, radio buttons, combo boxes, lists, and so on. Here is an example that creates two windows using the JFrame class. TestFrame Run 30

animation JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); Trace Code frame1 reference : JFrame title: width: height: visible: Declare, create, and assign in one statement 31

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 reference : JFrame title: "Window 1" width: height: visible: Set title property 32

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 reference : JFrame title: "Window 1" width: 200 height: 150 visible: Set size property 33

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 reference : JFrame title: "Window 1" width: 200 height: 150 visible: true Set visible property 34

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 frame2 reference : JFrame title: "Window 1" width: 200 height: 150 visible: true reference : JFrame title: width: height: visible: Declare, create, and assign in one statement 35

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 frame2 reference : JFrame title: "Window 1" width: 200 height: 150 visible: true reference : JFrame title: "Window 2" width: height: visible: Set title property 36

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 frame2 reference : JFrame title: "Window 1" width: 200 height: 150 visible: true reference : JFrame title: "Window 2" width: 200 height: 150 visible: Set size property 37

animation Trace Code JFrame frame1 = new JFrame(); frame1.settitle("window 1"); frame1.setsize(200, 150); frame1.setvisible(true); JFrame frame2 = new JFrame(); frame2.settitle("window 2"); frame2.setsize(200, 150); frame2.setvisible(true); frame1 frame2 reference : JFrame title: "Window 1" width: 200 height: 150 visible: true reference : JFrame title: "Window 2" width: 200 height: 150 visible: true Set visible property 38