Objects and Classes Lecture 2

Similar documents
Chapter 6 Introduction to Defining Classes

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

CS 1316 Exam 1 Summer 2009

Chapter 14 Abstract Classes and Interfaces

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Object-Oriented Programming Concepts

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

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

EECS168 Exam 3 Review

Introduction to Programming Using Java (98-388)

A Short Summary of Javali

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

Declarations and Access Control SCJP tips

Do not start the test until instructed to do so!

Ticket Machine Project(s)

Objects and Classes Lecture 1

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

2. The object-oriented paradigm

Chapter 7 Exercise Solutions

Defining Classes and Methods

Object interactions Lecture 6

5. Defining Classes and Methods

CS1150 Principles of Computer Science Methods

VARIABLES AND TYPES CITS1001

Defining Classes and Methods

Java. Representing Data. Representing data. Primitive data types

Short Notes of CS201

Chapter 4 Defining Classes I

CS201 - Introduction to Programming Glossary By

Lab5. Wooseok Kim

ECE 122. Engineering Problem Solving with Java

Defining Classes and Methods

Event Handling Java 7

Java: Classes. An instance of a class is an object based on the class. Creation of an instance from a class is called instantiation.

APCS Semester #1 Final Exam Practice Problems

Java: introduction to object-oriented features

COMP 202. More on OO. CONTENTS: static revisited this reference class dependencies method parameters variable scope method overloading

Programming with Java

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

PROGRAMMING FUNDAMENTALS

5. Defining Classes and Methods

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Final Exam. COMP Summer I June 26, points

CMSC 132: Object-Oriented Programming II

Java Primer 1: Types, Classes and Operators

Java Identifiers, Data Types & Variables

Chapter 5 Names, Bindings, Type Checking, and Scopes

Array. Prepared By - Rifat Shahriyar

CS313D: ADVANCED PROGRAMMING LANGUAGE

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

ITI Introduction to Computing II

CS1150 Principles of Computer Science Methods

Java Fundamentals (II)

Lecture 02, Fall 2018 Friday September 7

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

NAMESPACES IN C++ You can refer the Programming with ANSI C++ by Bhushan Trivedi for Understanding Namespaces Better(Chapter 14)

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Final Exam CS 152, Computer Programming Fundamentals December 5, 2014

Language Reference Manual

CS112 Lecture: Defining Classes. 1. To describe the process of defining an instantiable class

Programming II (CS300)

CS-201 Introduction to Programming with Java

Programming II (CS300)

ITI Introduction to Computing II

Fundamental Concepts and Definitions

CPS122 Lecture: Defining a Class

Defining Classes and Methods

Java Bytecode (binary file)

Classes and Inheritance Extending Classes, Chapter 5.2

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

Lecture 5: Inheritance

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

THE UNIVERSITY OF WESTERN AUSTRALIA. School of Computer Science & Software Engineering CITS1001 OBJECT-ORIENTED PROGRAMMING AND SOFTWARE ENGINEERING

Full file at

CS-140 Fall 2017 Test 1 Version Practice Practie for Sept. 27, Name:

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

Use the scantron sheet to enter the answer to questions (pages 1-6)

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

Pace University. Fundamental Concepts of CS121 1

Final Exam CS 251, Intermediate Programming December 13, 2017

7. C++ Class and Object

MIDTERM REVIEW. midterminformation.htm

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

CIS3023: Programming Fundamentals for CIS Majors II Summer 2010

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Lexical Considerations

C++ (classes) Hwansoo Han

Final Exam CS 152, Computer Programming Fundamentals May 9, 2014

Binghamton University. CS-140 Fall Data Types in Java

CS 1331 Exam 1. Fall Failure to properly fill in the information on this page will result in a deduction of up to 5 points from your exam score.

Introduction Welcome! Before you start Course Assessments The course at a glance How to pass M257

Chapter 13 Object Oriented Programming. Copyright 2006 The McGraw-Hill Companies, Inc.

Final Exam COMP 401 Fall 2014

CS260 Intro to Java & Android 03.Java Language Basics

Transcription:

Objects and Classes Lecture 2 Waterford Institute of Technology January 12, 2016 John Fitzgerald Waterford Institute of Technology, Objects and ClassesLecture 2 1/32

Classes and Objects Example of class Abstract description or specification of some entity Example Car with following specification: Make Model Color Example of object Specific instance of an entity defined by its class Example: car object Make: VW Model: Golf Color: red Waterford Institute of Technology, Objects and ClassesLecture 2 2/32

Concepts Class Describes, defines or specifies objects Car class broadly descriptive Actual make, model not known to class Car object clearly specified Object created in conformance with class specification Exact make, model known Waterford Institute of Technology, Objects and ClassesLecture 2 3/32

Create objects with BlueJ Open Shapes project in BlueJ Select Circle class & right click Choose new Circle Accept default name Circle object now on object workbench Waterford Institute of Technology, Objects and ClassesLecture 2 4/32

Method invocation Still working with Shapes project Right click on Circle object Select makeinvisible Object disappears Select makevisible Object reappears Waterford Institute of Technology, Objects and ClassesLecture 2 5/32

Method description What is a method? A program within the class Methods Perform actions Can optionally return data Circle method actions: Make circle object appear Make circle object disappear Change color circle object Change size circle object void: no data returned void makevisible(); void makeinvisible(); void changecolor(string); void changesize(int newdiameter); Waterford Institute of Technology, Objects and ClassesLecture 2 6/32

Parameters What are parameters? Some methods require no further information. Example: makeinvisible() Others require additional information. Additional information termed parameters Zero, one or more parameters permitted. Example changecolor(string) String a Java object representing new color. Waterford Institute of Technology, Objects and ClassesLecture 2 7/32

Method Signature Provides information required to invoke method Method signature comprises method name & parameter list Method: public void changecolor(string a). Signature:changeColor(String a). Excludes return type (void) Excludes access modifier (public) public void changecolor(string color) { this.color = color; } Waterford Institute of Technology, Objects and ClassesLecture 2 8/32

Method Signature Another example public int getcredits(). Signature: getcredits(). int getcredits() Waterford Institute of Technology, Objects and ClassesLecture 2 9/32

Method Signature Class methods must have different signatures Illegal: method signatures the same public class Circle { public void setcolor(string c) {this.color = c;} public String setcolor(string c){this.color = c; return this.color;} } Legal: method signatures different public class Circle { public void setcolor() {this.color = "red";} public void setcolor(string c){this.color = c;} } Waterford Institute of Technology, Objects and ClassesLecture 2 10/32

Data types Signature of method: Informs number of parameters Informs type of each parameter Eight primitive data types, e.g: int: represents integer values, e.g. 10, 25 boolean: may be true or false String: is an object. Represents text, e.g. "color", "10" Some primitive Java types int float double long boolean Waterford Institute of Technology, Objects and ClassesLecture 2 11/32

Multiple instances Many objects may be created from single class. Object: an instance of a class Instantiating class produces object Each object can have own set of internal data Waterford Institute of Technology, Objects and ClassesLecture 2 12/32

State Objects have state. Class Circle has fields Circle object field color has value (attribute) Example: "blue" Object state: set of all values of all fields Waterford Institute of Technology, Objects and ClassesLecture 2 13/32

Object interaction Could create Picture manually Or could be created by program Picture Class instance creates Two Square objects One Triangle object One Circle object Objects states determine Size of each object Position of each object Color or each object Waterford Institute of Technology, Objects and ClassesLecture 2 14/32

How Picture object created Picture class contains Square class (wall) Square class (window) Triangle class (roof) Circle class (sun) Picture has a method draw that Instantiates these classes Sets the state of each object Waterford Institute of Technology, Objects and ClassesLecture 2 15/32

Picture object source code Source code Java text Defines fields and methods private Square wall; public void draw(); When source code compiled Object can be created State can be changed Object methods callable Waterford Institute of Technology, Objects and ClassesLecture 2 16/32

Compilation Source code is compiled Computer processor requires binary (machine code) 0011000111010101011 Difficult programming in binary Hence human readable Java Compiler: source code to machine code Changed source requires recompilation Waterford Institute of Technology, Objects and ClassesLecture 2 17/32

Bits and Bytes Bit (Binary Digit): smallest unit of compilation Value range 0, 1 Byte: 8 bits 00000000 01001101 MegaByte (MB): 1024 bytes Megabit (Mb): 1024 bits 1 megabyte (MB) Waterford Institute of Technology, Objects and ClassesLecture 2 18/32

Using parameters when creating objects Student project example Create new student Object name required Parameters required String fullname String studentid Waterford Institute of Technology, Objects and ClassesLecture 2 19/32

Object state Student object state private String name : "Bill Gates" private String id : "3221144" private int credits : 0 Notice double quotes These required for String objects Notice third field undefined Assigning value here later task Waterford Institute of Technology, Objects and ClassesLecture 2 20/32

Return values Notice Student class methods Some methods return data String getname() Method getname when invoked Sends back String object String object contains student name Method header informs return type void means no value returned int means an integer returned Waterford Institute of Technology, Objects and ClassesLecture 2 21/32

Objects as parameters Parameters may be Primitive data types (example: int, float) Objects (example: String) LabClass has students Enrolling new student passes Student object as parameter Waterford Institute of Technology, Objects and ClassesLecture 2 22/32

Objects as parameters (continued) Objects can be passed as parameters Student gates3455 labclass.enrollstudent(gates3455); Notice no double quotes labclass is LabClass object gates3455 is Student object Waterford Institute of Technology, Objects and ClassesLecture 2 23/32

The this keyword Object reference Memory address where object stored Address accessible by this keyword Common usage where field shadowed by parameter public class BankAccount { int sum; public BankAccount(int sum) { this.sum = sum; } } Waterford Institute of Technology, Objects and ClassesLecture 2 24/32

The this keyword Here is class Student constructor as written in the BlueJ example / Create a new student with a given name and ID number. / public Student(String fullname, String studentid) { name = fullname; id = studentid; credits = 0; } Here is an alternative approach using the this reference. / Create a new student with a given name and ID number. / public Student(String name, String id) { this.name = name; this.id = id; credits = 0; } Waterford Institute of Technology, Objects and ClassesLecture 2 25/32

Package Package definition A grouping of related types Example: a folder of class files One benefit to provide access protection Waterford Institute of Technology, Objects and ClassesLecture 2 26/32

Controlling access Access modifier Determines other class access to field or method Fields may be declared thus: int value; public int value; private int value; protected int value; Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y N N private Y N N N Table 1 : Access Levels Waterford Institute of Technology, Objects and ClassesLecture 2 27/32

Block Block is code between curly braces. public Tree(int val) { this.val = val; } Blocks can be nested. Example of method block enclosed by class block: public class Student { String name; public String getname() { return name; } } Waterford Institute of Technology, Objects and ClassesLecture 2 28/32

Scope Scope refers to lifetime and accessibility of variable public class Tree { int val;... public Tree(int val) { this.val = val; } } this.val val Has class scope Visible (usable) throughout class Has local scope Visible (usable) only within constructor Waterford Institute of Technology, Objects and ClassesLecture 2 29/32

Summary Classes and Objects Object is instance of class Objects of same class have same fields Field types & names declared in class Field values set when object created Field values typically differ across objects Waterford Institute of Technology, Objects and ClassesLecture 2 30/32

Summary Classes and Objects Class represents general concept Several objects creatable from single class Objects store data in fields Object state comprises all data values Objects have methods Methods can change object state Methods can retrieve information from objects Waterford Institute of Technology, Objects and ClassesLecture 2 31/32

Summary Classes and Objects Method : method invocation communicates with objects Return value : data sent to caller when method invoked Signature : header of method facilitating invocation Parameter : data passed to method Type : defines kind of data State : set of field values (attributes) in object Source code : Java language description of program Compiler : software program converts source code to bytecode Waterford Institute of Technology, Objects and ClassesLecture 2 32/32