Software Practice 1 - OOP (1) Class and Method

Similar documents
Software Practice 1 - OOP (1) Class and Method What is OOP? Defining Classes Using Classes References vs. Values Encapsulate

Review. Lecture 4. Solutions 2. Solutions 1. Classes and Objects. public static int getminindex(int[] values) {

Visual Programming. Lecture 4: Classes and Objects. Mahmoud El-Gayyar

Software Practice 1 Basic Grammar

Software Practice 1. Course Overview Lecture Schedule Today s Task Contact

Software Practice 1 - File I/O

Software Practice 1 - OOP (3) API

Software Practice 1 - Error Handling

Software Practice 1 - Multithreading

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Introduction to Programming Using Java (98-388)

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Lecture 8 Classes and Objects Part 2. MIT AITI June 15th, 2005

Object Oriented Design: Identifying Objects

JAVA MOCK TEST JAVA MOCK TEST II

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Basics of Java Programming. Hendrik Speleers

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

1B1b Classes in Java Part I

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

CSCI 1301: Introduction to Computing and Programming Spring 2019 Lab 10 Classes and Methods

Who we are: Lecture 1: Overview & Java. Why CS 62? Sample Problems. CS 62 Fall 2016 Kim Bruce & Peter Mawhorter

CSCI 1301: Introduction to Computing and Programming Summer 2018 Lab 07 Classes and Methods

c) And last but not least, there are javadoc comments. See Weiss.

Fall 2017 CISC/CMPE320 9/27/2017

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

C++ Programming: Polymorphism

Lecture 06: Classes and Objects

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

CS 101 Spring 2006 Final Exam Name: ID:

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

9 Working with the Java Class Library

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Lecture 7: Classes and Objects CS2301

Software Practice 1 - Socket

Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Java Programming with Eclipse

JAVA GUI PROGRAMMING REVISION TOUR III

Object-Oriented Programming (OOP) Basics. CSCI 161 Introduction to Programming I

CHAPTER 7 OBJECTS AND CLASSES

Using System.out.println()

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Logistics. Final Exam on Friday at 3pm in CHEM 102

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

Java Object Oriented Design. CSC207 Fall 2014

Chapter 4. Defining Classes I

Programming with Java

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS61C Machine Structures. Lecture 3 Introduction to the C Programming Language. 1/23/2006 John Wawrzynek. www-inst.eecs.berkeley.

6.092: Java for 6.170

Object-Oriented Programming Concepts

Fall 2017 CISC124 9/16/2017

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

What is Inheritance?

CS 231 Data Structures and Algorithms, Fall 2016

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

Announcements. 1. Forms to return today after class:

Classes, Objects, and OOP in Java. June 16, 2017

Midterm Review Topics Java Basics and Structure Variables Branching Loops Methods Arrays. Midterm Review Kage Weiss Lab 001 TA, SPR 17

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

CHAPTER 7 OBJECTS AND CLASSES

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Data Abstraction: Status of an Object

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CSCI 355 Lab #2 Spring 2007

3. Java - Language Constructs I

ITI1120 Introduction to Computing I Exercise Workbook Fall 2012

CSCI 355 LAB #2 Spring 2004

Day 6. COMP1006/1406 Summer M. Jason Hinek Carleton University

1 If you want to store a letter grade (like a course grade) which variable type would you use? a. int b. String c. char d. boolean

Review questions. Review questions, cont d. Class Definition. Methods. Class definition: methods. April 1,

NATIONAL UNIVERSITY OF SINGAPORE

Today s Agenda. Quick Review

CS211 Spring 2005 Prelim 1 March 10, Solutions. Instructions

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

Java Identifiers, Data Types & Variables

Ticket Machine Project(s)

CSCI 136 Data Structures & Advanced Programming. Lecture 3 Fall 2018 Instructors: Bill & Bill

Lecture Set 4: More About Methods and More About Operators

CISC-124. This week we continued to look at some aspects of Java and how they relate to building reliable software.

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

Ex: If you use a program to record sales, you will want to remember data:

COMP-202 More Complex OOP

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

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.

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

ITI Introduction to Computing II

CS171:Introduction to Computer Science II

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

CS111: PROGRAMMING LANGUAGE II

APCS Semester #1 Final Exam Practice Problems

CS260 Intro to Java & Android 03.Java Language Basics

TeenCoder : Java Programming (ISBN )

Transcription:

Software Practice 1 - OOP (1) Class and Method What is OOP? Defining Classes Using Classes References vs. Values Encapsulate Prof. Joonwon Lee T.A. Jaehyun Song Jongseok Kim (42) T.A. Sujin Oh Junseong Lee 1 (43) / 61

2 / 61 Objects in real world Represent the real world Baby

3 / 61 Objects in real world Represent the real world Baby Name Gender Weight Poops

4 / 61 Objects in real world Name Weight Gender cry Baby

5 / 61 Objects in real world Name Weight Gender cry Name Weight Gender cry Name Weight Gender cry More Babies Baby1 Baby2 Baby3

6 / 61 Objects in real world Baby1 Babies Nurses Baby2 Nursery Baby3

7 / 61 Objects in real world Nurse1 Baby1 Babies Nurse2 Nurses Baby2 Nurse3 Nursery Baby3

Objects in real world Nurse1 Baby1 Nurse2 Babies Nurses Baby2 Nurse3 Nursery Baby3 ER Hospital 8 / 61

Objects in real world What do we need to model this objects with programming language? Nurse1 Baby1 Nurse2 Babies Nurses Baby2 Nurse3 Nursery Baby3 ER Hospital 9 / 61

10 / 61 Object Oriented Programming Definition A method of programming based on a hierarchy of classes, and well-defined and cooperating objects. Class a structure that defines the fields to store the data the methods to work on that data Object an executable copy of a class

11 / 61 Object Oriented Programming A class can be inherited by only one other class can implement one or more interfaces An object can establish the relationship with other objects through the reference encapsulates some fields or methods for hiding them from other objects

12 / 61 DEFINING CLASSES

13 / 61 Class definition public class Baby { String name; boolean gender; double weight; int numpoops = 0; void poop() { numpoops += 1; System.out.println ( Dear mother, + I have pooped. Ready the diaper. );

14 / 61 Let s declare a baby! public class Baby { Fields Methods

15 / 61 Baby fields public class Baby { TYPE var_name; OR TYPE var_name = some_value;

16 / 61 Baby fields public class Baby { String name; boolean gender; double weight = 5.0; int numpoops = 0;

17 / 61 Baby Siblings? public class Baby { String name; boolean gender; double weight = 5.0; int numpoops = 0; XXXXX YYYYY;

18 / 61 Baby Siblings? public class Baby { String name; boolean gender; double weight = 5.0; int numpoops = 0; Baby[] siblings;

19 / 61 Baby methods public class Baby { String name = Slim Shady ;... void sayhi() { System.out.println ( Hi, my name is.. + name);

20 / 61 Baby methods public class Baby { double weight = 5.0;... void eat(double foodweight) { if (foodweight >= 0 && foodweight < weight) { weight += foodweight;

21 / 61 Baby class public class Baby { String name; boolean gender; double weight; int numpoops = 0; Baby[] siblings; void sayhi() {... void eat(double foodweight) {...

22 / 61 Ok, let s make this baby! Baby mybaby = new Baby(); But what about his/her name? gender?

23 / 61 Constructors public class CLASSNAME { CLASSNAME ( ) { CLASSNAME ( [ARGUMENTS] ) { CLASSNAME obj1 = new CLASSNAME(); CLASSNAME obj2 = new CLASSNAME([ARGUMENTS]);

24 / 61 Constructors Constructor name == the class name No return type never returns anything Usually initialize fields All classes need at least one constructor If you don t write one, defaults to CLASSNAME () {

25 / 61 Baby constructor public class Baby { String name; boolean gender; Baby(String myname, boolean mygender) { name = myname; gender = mygender;

26 / 61 Final Baby class public class Baby { String name; boolean gender; double weight; int numpoops = 0; Baby[] siblings; Baby() {... void sayhi() {... void eat(double foodweight) {...

27 / 61 USING CLASSES

28 / 61 Classes and Instances // class Definition public class Baby { // class Instances Baby shiloh = new Baby ( Shilo Jolie-Pitt, true); Baby knox = new Baby ( Knox Jolie-Pitt, true);

29 / 61 Accessing fields Object.FIELDNAME Baby shiloh = new Baby ( Shiloh Jolie-Pitt, true); System.out.println (shiloh.name); System.out.println (shiloh.numpoops);

30 / 61 Using methods Object.METHODNAME([ARGUMENTS]) Baby shiloh = new Baby ( Shiloh Jolie-Pitt, true); shiloh.sayhi(); // Hi, my name is.. Shiloh Jolie-Pitt shiloh.eat(1);

31 / 61 REFERENCES VS. VALUES

32 / 61 Primitives vs. References Primitive types are basic java types int, long, double, boolean, char, short, byte, float The actual values are stored in the variable Reference types are arrays and objects Class, Interface, Array, Enum, ETC Ex) String, int[], Baby,

33 / 61 How java stores primitives Variables are like fixed size cups Primitives are small enough that they just fit into the cup int double char boolean

34 / 61 How java stores objects Objects are too big to fit in a variable Stored somewhere else Variable stores an address that locates the object Object

35 / 61 How java stores objects Objects are too big to fit in a variable Stored somewhere else Variable stores an address that locates the object Object s location Object Object Object Object Object Object

36 / 61 References The object s location is called a reference == compares the references Baby shiloh1 = new Baby( Shiloh ); Baby shiloh2 = new Baby( Shiloh ); Does shiloh1 == shiloh2?

37 / 61 References The object s location is called a reference == compares the references Baby shiloh1 NO! = new Baby( Shiloh ); Baby shiloh2 = new Baby( Shiloh ); Does shiloh1 == shiloh2?

38 / 61 References Baby shiloh1 = new Baby( Shiloh ); Baby shiloh2 = new Baby( Shiloh ); reference reference Name= Shiloh Name= Shiloh shiloh1 shiloh2

39 / 61 References Baby shiloh1 = new Baby( Shiloh ); Baby shiloh2 = new Baby( Shiloh ); reference reference Name= Shiloh Name= Shiloh shiloh1 shiloh2

40 / 61 Relations between objects 1. using == shiloh1 == shiloh2 Check two variables reference exactly same 2. using field shiloh1.name == shiloh2.name Compare a field of the object 3. using user-defined method shiloh1.equals (shiloh2) Used when the objects are different, but determine the same or not by the fields stored in the object

41 / 61 References Using = updates the reference baby1 = baby2; //? baby1 location baby2 location baby1 baby2 baby1 object baby2 object

42 / 61 References Using = updates the reference baby1 = baby2; baby2 location baby1 baby2 baby1 object baby2 object

43 / 61 References Baby mybaby = new Baby( Davy, true); mybaby s location name = Davy gender = true

44 / 61 References Baby mybaby = new Baby( Davy, true); mybaby.name = David ; mybaby s location name = Davy gender = true

45 / 61 References Baby mybaby = new Baby( Davy, true); mybaby.name = David ; mybaby s location name = David gender = true

46 / 61 References Using [ ] or. Follows the reference to the object May modify the object, but never the reference Imagine Following directions to a house Moving the furniture around Analogous to Following the reference to an object Changing fields in the object

47 / 61 Self reference Java class has a special way to access itself class Baby { String[] words; void remember (String[] words) { String word; for (word : words) words[top++] = word; //???????

48 / 61 Self reference Java class has a special way to access itself class Baby { String[] words; void remember (String[] words) { String word; for (word : words) this.words[top++] = word; //!

49 / 61 Methods and references void dosomething(int x, int[] ys, Baby b) { x = 99; ys[0] = 99; b.name = 99 ; int i = 0; int[] j = { 0 ; Baby k = new Baby( 50, true); dosomething (i, j, k); i =? j[0] =? k.name =?

50 / 61 ENCAPSULATE

51 / 61 Encapsulation In real world, there are huge # of objects and all of them have privacy. Electric objects, too!

52 / 61 Public and Private public Able to access/modify it whoever having the object public class Baby { public String nickname; public class Stranger { void makenicknameof (Baby b) { b.nickname = cuty ;

53 / 61 Public and Private private Only the object itself can access it public class Baby { private String nickname; public class Stranger { void makenicknameof (Baby b) { b.nickname = cuty ; // Error!

54 / 61 How to access private data? public class Baby { private String nickname; public void setnickname (String nickname, Object o) { // check if the object is instance of [Stranger] or not if (o instanceof Stranger) { return; this.nickname = nickname; public class Stranger { void makenicknameof (Baby b) { b.setnickname ( cuty, this);

55 / 61 Then, what is String in java? Built-in class for handling the sequence of characters in high level abstraction Usage String name = Simon ; char[] data = { s, i, n, g, l, e ; String state = new String (data); // String concatenate System.out.println (name + is a + state); // Simon is single // access the substring with the range of indexes System.out.println (state.substring (1)); System.out.println (state.substring (2,4)); // ingle // ngl

56 / 61 Then, what is String in java? Built-in class for handling the sequence of characters in high level abstraction Usage String name = Simon ; char[] data = { s, i, n, g, l, e ; String state = new String (data); // comparison System.out.println (name == Simon ); System.out.println (name.compareto ( Simon )); // false // true // get length of a string System.out.println (name.length ()); // 5

[Lab Practice #2] Modeling Book and Libraries class Book { class Library { Books can be borrowed returned Library keeps track of books Hint: use Book[] 57 / 61

58 / 61 [Lab Practice #2] Four books (each book count is one) Beauty and Beast Helen Keller Gulliver s Travels The Three Little Pigs Three people Sam Susan John

59 / 61 [Lab Practice #2] Skeleton code is uploaded on i-campus Fill in the Book, Library class & main function Left image is console output example

60 / 61 [Submit] Upload to i-campus Compress your project directory to zip file File name: studentid_lab02.zip Due date Today 23:59:59 Class 42 (3/19 Monday) Class 43 (3/21 Wednesday) Penalty: -10% of each lab score per one day

61 / 61 [Project Export] 2. Choose General > File system and click next button 1. Click mouse right button at project you want to export & choose Export 3. Designate save directory path and click Finish button