CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Four Defining Your Own Classes.
Advertisements

Introduction to Java.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Inheritance Inheritance Reserved word protected Reserved word super
Road Map Introduction to object oriented programming. Classes
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Introduction To Computers and Programming Lecture 2: Your first program Professor: Evan Korth New York University.
1 Programming for Engineers in Python Autumn Lecture 5: Object Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Week 2 Recap CSE 115 – Spring Object Oriented Program System of objects that communicate with one another to solve some problem.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
ASP.NET Programming with C# and SQL Server First Edition
Week 3 Recap CSE 115 – Fall Java Source Code File Made up of: Package declaration Class definition.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
By: Mr. Baha Hanene Chapter 3. Learning Outcomes We will cover the learning outcome 02 in this chapter i.e. Use basic data-types and input / output in.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Microsoft Visual Basic 2005: Reloaded Second Edition
Introduction to Programming Writing Java Beginning Java Programs.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]
CSC 212 Object-Oriented Programming and Java Part 1.
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.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
The Java Programming Language
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Introduction to programming in the Java programming language.
Designing Classes Prelude © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
CSC 142 D 1 CSC 142 Instance methods [Reading: chapter 4]
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
Introduction to Programming Writing Java Beginning Java Programs.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Structures and Classes Version 1.0. Topics Structures Classes Writing Structures & Classes Member Functions Class Diagrams.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
OOP Basics Classes & Methods (c) IDMS/SQL News
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming Fifth Edition Chapter 1 Creating Your First Java Classes.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Chapter 7 Object-Oriented Programming
Concepts of Object Oriented Programming
Chapter 3: Using Methods, Classes, and Objects
About the Presentations
SNSCT_CSE_PROGRAMMING PARADIGM_CS206
Corresponds with Chapter 7
Defining Your Own Classes
Packages and Interfaces
Chapter 1: Computer Systems
Chapter 9 Objects and Classes
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object based programming in Java
Java objects: a first view
Object based programming in Java
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

CSC 142 B 1 CSC 142 Java objects: a first view [Reading: chapters 1 & 2]

CSC 142 B 2 What is an object? (1)  An example: a vending machine  It has things: candy, cans, coffee, money, …  Some information is public: number of candy bars…  Some information is private: the amount of money…  The vending machine can perform actions:  accept money, give change, give food, refrigerate…  Some actions are available to anyone, others require special access (repair person)

CSC 142 B 3 What is an object? (2)  The machine provides an interface to its behavior (button panel). The customer doesn't need to know the internal workings of the machine.  There can be many identical machines all based on the same design. However, each machine has its own identity (some are out of order, some have more candy, etc…).  Java allows us to reproduce this view on the computer.

CSC 142 B 4 An object in Java  An object is an instance of a class.  The class is the blueprint. It describes  The data contained in the object. Some are private, some are public.  The actions that the object can perform. Some actions are available to anyone (public methods). Others require special access (private methods).  The interface is made of the public data and methods. It describes what the object can do for us. We don't need to know how the object does it (the details are hidden = private).

CSC 142 B 5 Why using objects?  It corresponds to the way we view the world.  A plane has engines, two wings… It can fly, take off, land, carry passengers…  We can use the same framework to solve problems on the computer.  Objects enhance software reusability.  Once a class is defined, we can use it over and over. We will do so with many classes of the Java API.  As long as the interface is unchanged, the inner workings (=implementation) of the class can be modified without requiring any changes on the part of the users of the class.

CSC 142 B 6 Using objects in Java  An example: Display a circle within a graphics window.  Where are the objects?  We want an object that has two objects, a circle and a graphics window. The object should put the circle in the graphics window.  In Java, do so by writing the blueprint of the object (=class). Then, to get the object, instantiate the class.  What about creating a circle and a graphics window? Difficult from scratch, easy if we use code already written in libraries.

CSC 142 B 7 Interlude: Java libraries  Library  A set of classes already written ready to use.  In our example, we want a library that has classes (=blueprints) for a graphics window and a circle. Use the UW library.  Java has an enormous amount of libraries. Programmers can reuse code already written to write their programs (fast, easy and less likely to have bugs). Important to know what is available  A library is often called an Application Programmer Interface (=API).

CSC 142 B 8 WindowWithCircle class  Name the class  Use a meaningful name,e.g., WindowWithCircle  The style in Java is to capitalize each letter of each word of the class name (do so as well).  A name can contain letters, digits (e.g. CarModel12), the underscore (_), or currency symbols ($, £, ¥, …).  A name can't start with a digit (e.g. 1NoGood is not a valid class name).  A name can't be a reserved java keyword (e.g. class).  In practice, a name can be as long as you want.  case sensitive (MyClass  Myclass).

CSC 142 B 9 Instance fields  Name the objects needed to build a WindowWithCircle object = instance fields  A GWindow object, e.g., window  A Oval object, e.g., circle  (GWindow and Oval are class names from the CSE142 UW library)  Naming instance fields  same rules as for class names, except that the first letter is lowercase  e.g., aBlueCircle, aDialogBox

CSC 142 B 10 Instance methods  Name the actions that a WindowWithCircle object can perform = instance methods  Namely, create a graphics window and a circle. Put the circle in the graphics window.  Do it when building a WindowWithCircle object.  Done in a special method, called the constructor. The constructor of a class has the same name as the class name, WindowWithCircle (no other choice).  No other methods needed here.

CSC 142 B 11 UML representation  UML: Unified Modeling Language  a set of graphical rules to display a design when programming in an OOP context.  WindowWithCircle class diagram WindowWithCircle class name GWindow window Oval circle instance fields WindowWithCircle() instance methods (explanation for () will come soon)  More rules in UML (see later).

CSC 142 B 12 Code for WindowWithCircle  In a file that must be named WindowWithCircle.java, write import uwcse.graphics.*; //uw graphics library public class WindowWithCircle{ // instance fields private GWindow window; private Oval circle; // Only one instance method: the constructor public WindowWithCircle(){ /* Create the window and the circle Put the circle in the window */ window = new GWindow(); circle = new Oval(); window.add(circle); } } one line comment mutiline comment every java statement ends with a semi colon

CSC 142 B 13 Code organization  Code is written inside of blocks {} that are class definitions  public class WindowWithCircle { /* my code is here */ }  public means that the class is available to everyone (No privileged access is required. More on this later).  The file that contains the definition of the public class WindowWithCircle must be named: WindowWithCircle.java (case sensitive!)  Only one public class per java file

CSC 142 B 14 The import statement  To access the content of libraries  import uwcse.graphics.* means:  we can use any class listed in the folder graphics which is in the folder uwcse.  uwcse.graphics is called a package.  To limit the access to GWindow and Oval only import uwcse.graphics.GWindow; import uwcse.graphics.Oval;  More specific for the reader. But, requires several import statements.  Using import is optional. If omitted, need to write the full name in the code, e.g. uwcse.graphics.GWindow instead of just GWindow.

CSC 142 B 15 Comments  Ignored by the computer. Essential for the reader.  2 types of comments: // and /* */  Everything between // and the end of the line is a comment  Everything between /* and */ is a comment. It can be used on several lines. You can't nest these comments (/* blabla /* blabla */ blabla */ gives an error)  Examples  // this is a comment  /* this is a comment that can be written on several lines */  Also javadoc comments /** and */

CSC 142 B 16 Instance fields  Declaration  e.g., private GWindow window ; access modifier type identifier  identifier: the name of the instance field  access modifier: private (the identifier can only be used in the class, i.e., within the bloc {} of the class). public (the identifier can be used outside the class). More on this later.  type: the class name of the identifier.  A declaration doesn't create an object. It just creates a name for an object.

CSC 142 B 17 Instantiating a class  To create an object, e.g. a GWindow, write window = new GWindow();  An object of type GWindow is created by executing the statements listed in the constructor of the GWindow class.  Now, window refers to an actual object (window refers to a chunk of memory that contains information about a GWindow).  In UML (for object diagrams) window :GWindow

CSC 142 B 18 Constructor: a first view  A special instance method of a class executed when (and only when) an instance of the class is created (using new), as in circle = new Oval(); // execute constructor of Oval class  It must have the same name as the class  Syntax (e.g. for WindowWithCircle) public WindowWithCircle(){/*write code here*/}  public means that anyone can instantiate the class. What if we used private?  Can have multiple constructors in the same class (see later).

CSC 142 B 19 WindowWithCircle constructor(1)  Create a GWindow and an Oval: OK, use new window = new GWindow(); circle = new Oval();  Put the circle in the window: need to know what a GWindow and an Oval can do.  Whenever using a library, read the documentation. It describes every public member of the class (=interface for the user of the class).  Available directly inside the java IDE or online.

CSC 142 B 20 WindowWithCircle constructor(2)  GWindow lists the instance method add, to add an item to a GWindow object.  Use the dot notation to access the instance method via the reference to the object, i.e. window.add(circle);  Note that add takes an input parameter (namely circle). Our next chapter will describe such methods.

CSC 142 B 21 Running the code  Write a main method in the same class or in another class public static void main(String[] args){ new WindowWithCircle(); }  Use the debugger to inspect the object (list its public and private instance fields)

CSC 142 B 22 A few questions  Change the color of the circle?  Displaying the circle at another location within the graphics window?  Creating a window with a different size.  All of the above can be done. It requires using the right methods from the GWindow or Oval classes (try it!).

More on OOP: OOP vs. Procedural  What you’re probably used to is “procedural programming” or linear programming  Like a cookbook; ingredients are like variables, actions are like functions/methods  e.g. int eggs = 2; float flour = 2.5 … batter = mix(eggs, sugar, flour, milk); cake = bake(batter, 45, 350); if(is_birthday == true){ eat(cake); } CSC 142 B 23

OOP vs. procedural (2)  Object Oriented Programming (OOP) is about attributes of and relationships between things  More abstract, not as “linear”  Code often distributed across many files (each class has own.java file)  Classes are a collection of attributes and methods  Objects are specific “instances” of a class  Attributes take on explicit values in an object  Methods (become instance methods) may then depend and change this specific data  Inheritance… later. CSC 142 B 24

CSC 142 B 25 Ex: SCC

CSC 142 B 26 Class diagrams (UML representation)  UML: Unified Modeling Language  a set of graphical rules to display a design when programming in an OOP context.  Student class diagram Student class name GPA Credits Loans instance fields doCSC142hw() payLoans() instance methods (explanation for () will come soon)