Tutorial 6 February 4th/5th, 2015

Slides:



Advertisements
Similar presentations
1 Classes and Objects in Java Basics of Classes in Java.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
***** SWTJC STEM ***** Chapter 4-1 cg 42 Object Oriented Program Terms Up until now we have focused on application programs written in procedural oriented.
OOP: Inheritance By: Lamiaa Said.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Lecture 3. 2 Introduction Java is a true OO language -the underlying structure of all Java programs is classes. Everything must be encapsulated in a class.
Object Oriented Software Development
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture # 5 Methods and Classes. What is a Method 2 A method is a set of code which is referred to by name and can be called (invoked) at any point in.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
1 Classes and Objects in Java Basics of Classes in Java.
1 Software Construction Lab 3 Classes and Objects in Java Basics of Classes in Java.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
1 COS240 O-O Languages AUBG, COS dept Lecture 12 Title: Java Classes and Objects Reference: COS240 Syllabus.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
INTRODUCTION Java is a true OO language the underlying structure of all Java programs is classes. Everything must be encapsulated in a class that defines.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Topic: Classes and Objects
Lecture 3: Introduction to Object and Classes
Classes and Objects in Java
Classes and Objects in Java
Classes and Objects in Java
Creating Your OwnClasses
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
Classes In C#.
Chapter 8 Objects and Classes Part 1
Chapter 8 Objects and Classes
Defining Classes and Methods
Objects and Classes Creating Objects and Object Reference Variables
Chapter 6 Objects and Classes
Chapter 8 Objects and Classes
OO Programming Concepts
Chapter 6 Objects and Classes
Chapter 7 Objects and Classes
Presentation transcript:

Tutorial 6 February 4th/5th, 2015 CPSC 233 Tutorial 6 February 4th/5th, 2015

Defining Classes and Mothods Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything we wish to represent in Java must be encapsulated in a class that defines the “state” and “behaviour” of the basic program components known as objects.

Classes A class is a collection of fields (data) and methods (procedure or function) that operate on that data. Circle centre radius circumference() area()

Creating objects/instances of a class Objects are created dynamically using the new keyword. aCircle and bCircle refer to Circle objects aCircle = new Circle() ; bCircle = new Circle() ;

Executing Methods in Object/Circle Using Object Methods: sent ‘message’ to aCircle Circle aCircle = new Circle(); double area; aCircle.r = 1.0; area = aCircle.area();

Constructors Circle(double r) { radius = r; } Circle() { radius = 1.0; myCircle = new Circle(5.0); Constructors are a special kind of methods that are invoked to construct objects.

Constructors, cont. A constructor with no parameters is referred to as a default 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.

Visibility Modifiers and Accessor Methods public The class, data, or method is visible to any class in any package. private The data or methods can be accessed only by the declaring class. The get and set methods are used to read and modify private properties

Exercise 1 Define a class called Counter. An object of this class is used to count things, so it records a count that is a nonnegative whole number.

Exercise 1 Include methods to: set the counter to zero to increase the count by 1 and to decrease the count by 1 include an accessor method that returns the current count value include a method that outputs the count to the screen

Exercise 1 Be sure that no method allows the value of the counter to become negative. The only method that can set the counter is the one that sets it to zero. Also, write a program to test your class definition.

Counter Class

CounterTest

Exercise 2 Consider a class PersonAddress that represents an entry in an address book. Its attributes are:      * The first name of the person      * The Last name of the person      * The e-mail address of the person      * The telephone number of the person  It will have methods to:      * Access each attribute      * Change the e-mail address      * Change the telephone number      * Test whether two instances are equal based solely on name     1. Write a method heading for each method.     2. Write preconditions and postconditions for each method.     3. Write some Java statements that test the class.     4. Implement the class.

PersonAddress Class

PersonAddress Test