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

Similar documents
7. C++ Class and Object

What we have seen so far CSC 142. Object based programming in Java [Reading: chapter 4] Another example. java.awt. Initializing instance fields

Object-Oriented Programming Concepts

Lecture 02, Fall 2018 Friday September 7

Object Declaration. <class name>: the name of the class to which the object belongs <object name>: the name of the object (any valid identifier)

Basics of Java Programming

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

CS 251 Intermediate Programming Coding Standards

Object Oriented Programming with Java

Identifiers and Variables

CS 152 Computer Programming Fundamentals Coding Standards

9 Working with the Java Class Library

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat Chapter 6 Inheritance Extending a Class

CSC 222: Object-Oriented Programming. Fall 2015

CSE 201 Java Programming I. Smart Coding School website:

Chapter 15: Object Oriented Programming

CSSE 220. Interfaces and Polymorphism. Check out Interfaces from SVN

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Chapter 1 Getting Started

Computational Expression

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

CS 351 Design of Large Programs Coding Standards

PROGRAMMING STYLE. Fundamentals of Computer Science I

JAVA: A Primer. By: Amrita Rajagopal

Lecture 6 Introduction to Objects and Classes

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

Syntax and Variables

CLASSES AND OBJECTS. Fundamentals of Computer Science I

Brief Summary of Java

CompuScholar, Inc. Alignment to Nevada "Computer Science" Course Standards

Classes, objects, methods and properties

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

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

Java: introduction to object-oriented features

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

Objectives: Lab Exercise 1 Part 1. Sample Run. Part 2

CS-202 Introduction to Object Oriented Programming

Lecture 14 CSE11 Fall 2013 For loops, Do While, Break, Continue

Lecture 7: Classes and Objects CS2301

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

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

Lesson 12: OOP #2, Accessor Methods (W03D4)

Java Programming Tutorial 1

Microsoft Visual Basic 2005: Reloaded

CSE 303: Concepts and Tools for Software Development

Chapter 4 Defining Classes I

Recap: Assignment as an Operator CS 112 Introduction to Programming

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

CSE 142/143 Unofficial Style Guide

11 Coding Standards CERTIFICATION OBJECTIVES. Use Sun Java Coding Standards

1.00 Lecture 8. Using An Existing Class, cont.

Java Bytecode (binary file)

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence


CS 11 java track: lecture 3

class objects instances Fields Constructors Methods static

CSC207 Week 3. Larry Zhang

} Each object in a Java program has an identifier (name) } This includes:

Topics. Class Basics and Benefits Creating Objects.NET Architecture and Base Class Libraries 3-2

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Topic 27 classes and objects, state and behavior

Fundamental Concepts and Definitions

Introduction to Classes and Objects. David Greenstein Monta Vista High School

Fundamentals of Object Oriented Programming

Fall 2017 CISC124 9/16/2017

16-Dec-10. Consider the following method:

1.00 Lecture 7. Classes

Object Orientated Analysis and Design. Benjamin Kenwright

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

Informatik II (D-ITET) Tutorial 6

An Introduction To Writing Your Own Classes CSC 123 Fall 2018 Howard Rosenthal

CLASSES AND OBJECTS. Fundamentals of Computer Science I

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Object Oriented Modeling

CS 112 Introduction to Programming

CS1007: Object Oriented Design and Programming in Java. Lecture #2 Sept 8 Shlomo Hershkop Announcements

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

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

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

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Example: Fibonacci Numbers

Topic 6: Inner Classes

Programming with Java

Building Java Programs

Classes. Classes as Code Libraries. Classes as Data Structures. Classes/Objects/Interfaces (Savitch, Various Chapters)

Java Object Oriented Design. CSC207 Fall 2014

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CSCI 135 Exam #2 Fundamentals of Computer Science I Fall 2013

Classes and Objects 3/28/2017. How can multiple methods within a Java class read and write the same variable?

A A B U n i v e r s i t y

Expanded Guidelines on Programming Style and Documentation

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Games Course, summer Introduction to Java. Frédéric Haziza

13 th Windsor Regional Secondary School Computer Programming Competition

The Essence of OOP using Java, Nested Top-Level Classes. Preface

CHAPTER 2 Java Fundamentals

Transcription:

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

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 2

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 3

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 4

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 5

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 6

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 7

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 CSC 142 B 8 Myclass).

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 9

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 10

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 GWindow window Oval circle WindowWithCircle() More rules in UML (see later). class name instance fields instance methods (explanation for () will come soon) CSC 142 B 11

Code for WindowWithCircle In a file that must be named WindowWithCircle.java, write import uwcse.graphics.*; //uw graphics library public class WindowWithCircle{ one line comment // instance fields private GWindow window; every java statement private Oval circle; ends with a semi colon } } // 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); mutiline comment CSC 142 B 12

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 13

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 CSC 142 B 14 GWindow.

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 15

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 16

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 17

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 18

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 19

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 20

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 21

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!). CSC 142 B 22

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

Ex: SCC CSC 142 B 25

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 GPA Credits Loans docsc142hw() payloans() class name instance fields instance methods (explanation for () will come soon) CSC 142 B 26