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.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
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.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
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,
UML Basics & Access Modifier
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Syllabus (1) WeekChapters 1Introduction to the course, basic java language programming concepts: Primitive Data Types and Operations 1, 2 2Methods, Control.
An Object-Oriented Approach to Programming Logic and Design
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.
CS 106 Introduction to Computer Science I 03 / 19 / 2007 Instructor: Michael Eckmann.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
Objects and Classes Chapter 6 CSCI CSCI 1302 – Objects and Classes2 Outline Introduction Defining Classes for Objects Constructing Objects Accessing.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
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.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Objects and Classes Mostafa Abdallah
Chapter 5 Programming with Objects and Classes OO Programming Concepts OO Programming Concepts Declaring and Creating Objects Declaring and Creating Objects.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
© 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.
Liang, Introduction to Java Programming, Sixth Edition1 Objects and Classes Gang Qian Department of Computer Science University of Central Oklahoma.
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.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
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.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 8 Objects and Classes.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Chapter 7 Objects and Classes. OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
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.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Dr. Majed Abdouli © Objects and Classes 1 Dr. Majed Abdouli © 2015, adapted from Liang, Introduction to Java Programming, Eighth Edition, (c) 2011.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Chapter 7 Objects and Classes
Chapter 15 Abstract Classes and Interfaces
HIGHLEVEL REVIEW Chapter 9 Objects and Classes
CS 302 Week 10 Jim Williams.
Outline Writing Classes Copyright © 2012 Pearson Education, Inc.
Chapter 8 Objects and Classes Part 1
Implementing Non-Static Features
Chapter 9 Objects and Classes
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Polymorphism
OO Programming Concepts
Final and Abstract Classes
Object-Oriented Programming and class Design
Chapter 7 Objects and Classes
Presentation transcript:

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 break.

Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output data. Object-oriented programming (OOP) is a programming language model organized around "objects" rather than "actions" data rather than logic.

What is an object? An entity in the real world: a student, a desk, a circle, a button, a window… How do we describe an object? (a student, for example) State of an object (properties or attributes) name, ID, year, address… Behavior of an object (actions) drop a class, add a class…

Some more examples of objects Thinking of a circle as an object: Properties: radius (or diameter) Actions: calculate the area of the circle, calculate the circumference of the circle …

Classes A class is a template that defines objects of the same type.

Why OOP? The definition of a class is reusable. It is possible to define subclasses to inherit some or all of the main class characteristics. Person  Student, Teacher The concept of data classes allows a programmer to create any new data type that is not already defined in the language itself.

OOP Languages Simula (1967) was the first object-oriented programming language. Java, Python, C++, Visual Basic.NET and Ruby are the most popular OOP languages today.

How to describe an object? A circle is an object. Properties: radius (or diameter)  data field (variable) double radius = 5; Actions: get the area of the circle  method double getArea(){ double area = 3.14 * radius * radius; return area; }

How to describe an object? An object is a bundle of variables and related methods.

Let’s try to define this class in Java!

How to create an object? an object = an instance You can create many objects (instances) of a class in the main class.

Declaring an object Syntax: ClassName objectName; Example: Circle circle1; Circle circle2; Circle circle3; int[] scores;

Creating an object circle1 = new Circle(); scores = new int[20]; You can also combine the declaration and creation of an object. int[] scores = new int[20]; Circle circle1 = new Circle(); Circle circle2 = new Circle(); Circle circle3 = new Circle(); Scanner input = new Scanner(System.in);

Accessing an Object’s Data and Methods dot operator (.) objectName.dataField objectName.method() For example: circle2.radius circle3.radius circle1.getArea(); circle2.getArea();

What does “static” mean? “Static” means that variable or method marked as such is available at class level. You don’t need to create an instance (object) to access it. “Static” means there is only one! E.g., public static void printMessage(){ System.out.println(“This is a circle object.”); } radius and getArea() are at instance level. We will talk about static more..

Let’s try to create a few Circle objects! Two ways: In another file (same package!) In the same file

You can also give a default value to the data fields. You can always overwrite them in the main class.

Exercise 1: The Rectangle Class Follow the example of the Circle class, design a class named Rectangle to represent a rectangle. The class contains: Two double data fields named width and height, The default values are 1 for both. A method named getArea(). A method named getPerimeter().

Constructors Circle circle1 = new Circle(); What is Circle()?? Constructor is a special type of method, that is invoked to create a new object. By default: (if no constructor is provided) Circle() { }

Constructors Circle() { } A constructor must have the same name as the class. 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.

Constructors Circle(){ radius = 1; }

Constructors You can overload constructors. Multiple constructors can have the same name but different signatures. Circle(){ radius = 1; } Circle(double r){ radius = r; }

Add to Exercise 1.. A no-arg constructor that creates a rectangle with width of 1 and height of 1. A constructor that creates a rectangle with the specified width and height. In your TestRectangle.java, create two Rectangle objects by invoking the two different constructors.

Primitive Data Types vs. Object Types

When Copying Variables..

Static variables Instance variable rectangle2.width = 5; rectangle2.height = 10; class variable (static variable) static int numberOfRectangles;

Static methods Instance method rectangle2.getArea() static method static int getNumberOfRectangles()

Static constants final static double PI = 3.14;

Visibility Modifiers By default, the class, variable, or method can be accessed by any class in the same package.  public The class, data, or method is visible to any class in any package.  private The data or methods can be accessed only from within its own class.

Data fields should be private. This is not good: double radius; //In Circle.java circle2.radius = 5; //In TestCircle.java Why? Data is not protected. class becomes difficult to maintain. Data fields should be private. private double radius;

Then how can we give a value to radius of circle1? circle1.radius = 5 does NOT work any more! The get and set methods are used to read and modify private properties. getRadius(): to read setRadius(double r): to write

Add to exercise 1.. In your Rectangle.java, make width and height private. Add get and set methods for the two private variables. In TestRectangle.java, create another two rectangles. Set one rectangle to have width of 10, height of 5. Set another rectangle to have width of 25, height of 10; Print their width, height, area and perimeter.

Exercise 2: The Student class Design a class named Student that contains: A private String data field named name; A private int data field named id; A private String data field named major; A constructor that creates a student with the specified id, name and major. The get and set methods for name, id and major.

Array of Objects int[] scores = new int[10]; Circle[] circles = new Circle[10];

Passing Objects to Methods public static void printRadius(Circle c)

Use classes from the Java library The Date Class import java.util.Date toString() returns the date and time as a string

Use classes from the Java library GUI Components The JFrame, JButton, JTextfield, JPanel class import java.swing.*;