CS 302: Introduction to Programming in Java. Lecture 15

Similar documents
Test 6. moodle.yorku.ca CSE 1020

Understanding class definitions. Looking inside classes (based on lecture slides by Barnes and Kölling)

Binghamton University. CS-140 Fall Problem Solving. Creating a class from scratch

Today s Agenda. Quick Review

public class TicketMachine Inner part omitted. public class ClassName Fields Constructors Methods

VARIABLES AND TYPES CITS1001

Objects and Iterators

ECE 122. Engineering Problem Solving with Java

// constructor: takes a String as parameter and creates a public Cat (String x) { name = x; age = 0; lives = 9; // cats start out with 9 lives }

Chapter 4 Defining Classes I

For this section, we will implement a class with only non-static features, that represents a rectangle

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

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

CS1004: Intro to CS in Java, Spring 2005

Understanding class definitions

Chapter 4. Defining Classes I

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

Inheritance and Interfaces

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

System.out.print(); Scanner.nextLine(); String.compareTo();

CS112 Lecture: Defining Instantiable Classes

CS 302: INTRODUCTION TO PROGRAMMING IN JAVA. Lecture 16

CMSC131. Library Classes

Chapter 15: Object Oriented Programming

Classes Classes 2 / 36

Lecture 10: Interfaces

Lecture 7. Log into Linux New documents posted to course webpage

Lecture 07: Object Encapsulation & References AITI Nigeria Summer 2012 University of Lagos.

This exam is open book. Each question is worth 3 points.

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

ITI Introduction to Computing II

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 2

MIT AITI Lecture 18 Collections - Part 1

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

EEE-425 Programming Languages (2013) 1

EECS 1001 and EECS 1030M, lab 01 conflict

Classes. Classes (continued) Syntax The general syntax for defining a class:

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Assignment 1 due Monday at 11:59pm

Comparing Objects 3/14/16

if (x == 0); System.out.println( x=0 ); if (x = 0) System.out.println( x=0 );

Lecture 7 Objects and Classes

Object-oriented basics. Object Class vs object Inheritance Overloading Interface

Standard ML. Data types. ML Datatypes.1

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

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

Chapter 5: Writing Classes and Enums

CPS122 Lecture: Defining a Class

CS 2530 INTERMEDIATE COMPUTING

Classes Classes 2 / 35

Implementing Abstractions

Solutions to Quiz 1 (March 14, 2016)

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

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

What is an algorithm?

Polymorphism: Inheritance Interfaces

Java Review. Fundamentals of Computer Science

Building Java Programs

Introduction to Programming Using Java (98-388)

CS 106A, Lecture 27 Final Exam Review 1

Chapter 4: Writing Classes

Distributed Systems Recitation 1. Tamim Jabban

Activity 11: Designing Classes

Lecture 06: Classes and Objects

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

Defensive Programming

Module Contact: Dr Geoff McKeown, CMP Copyright of the University of East Anglia Version 1

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

Polymorphism. return a.doublevalue() + b.doublevalue();

Object-Oriented Modeling Using UML. CS151 Chris Pollett Aug. 29, 2005.

CS 302 Week 9. Jim Williams

CompuScholar, Inc. 9th - 12th grades

Software Design and Analysis for Engineers

What will this print?

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

EXAM Computer Science 1 Part 1

CS1083 Week 2: Arrays, ArrayList

Handout 7. Defining Classes part 1. Instance variables and instance methods.

UNDERSTANDING CLASS DEFINITIONS CITS1001

TeenCoder : Java Programming (ISBN )

Ch 7 Designing Java Classes & Class structure. Fields have data values define/describe an instance

Distributed Systems Recitation 1. Tamim Jabban

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

COMP-202. Objects, Part III. COMP Objects Part III, 2013 Jörg Kienzle and others

Encapsulation. Mason Vail Boise State University Computer Science

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

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

CS112 Lecture: Working with Numbers

Java Classes, Inheritance, and Interfaces

Ch 7 Designing Java Classes & Class structure. Methods: constructors, getters, setters, other e.g. getfirstname(), setfirstname(), equals()

Module Contact: Dr Taoyang Wu, CMP Copyright of the University of East Anglia Version 1

CS 320: Concepts of Programming Languages

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Assignment 8B SOLUTIONS

Transcription:

CS 302: Introduction to Programming in Java Lecture 15

Class Instances of the class (objects) only valid at runtime

Private Instance Methods Instance methods usually public why? If we have an internal function that we do not want others to be able to call, make it private Ex. a sort method on a phonebook object Phonebook could have public methods to add and remove people from the phonebook but should keep itself sorted no matter what Both the add() and remove() instance methods could call a private sort() method

Accessing Instance Variables Global scope within the class why? this.varname ALWAYS refers to the instance variable varname will refer to the instance variable if the varname is unique

Accessing Instance Variables Example public class BankAccount { private int balance; public BankAccount(int balance) { //what goes here? } }

Constructors Form: public <ClassName> (<param list>) Can have multiple constructors as long as they take in different parameters The appropriate constructor will be called based on what arguments are passed in Method Overloading: having multiple methods with the same name that take in different arguments

Practice 1 Create the class for a Motorcycle Object Motorcycles have: Color Current Speed Number of Gears Motorcycles can: Accelerate Decelerate

Practice 2 Create the class for a Chess Piece: Chess Pieces have: Color Type (pawn, rook, knight, bishop, queen, king) A grid location (a1 h8) Status (live or dead) Chess Pieces can: Move to a new location Die

Aggregation "has a" relationship Objects of one class contain objects of another class Ex. class Phonebook aggregates the class Contacts class Wizard aggregates the class Wand Mission aggregates Commodity, Location, and String

Object References All object variables are reference variables Variable stores the memory location of the object, NOT the object itself (think arrays) 2 or more object variables can point to the same object Ex: BankAccount dansaccount = suesaccount; Now modifying 1 will modify both Different from: int x = y; modifying x will not change y

Special Instance Methods: tostring() tostring() If I have an object variable and print it out, what happens? To fix this, create a tostring() method that returns a String this will automatically be called if you try to print out your object (System.out.println(varName)) Each object will have its own unique String representation (up to you) Ex. A BankAccount might return a String with the balance, the account number, and the date it was created Method header: public String tostring() - no params

Special Instance Methods: equals() If I have two objects and compare them using ==, what is being compared? equals(object o) is another method commonly implemented to fix this Code that goes inside will be unique for all objects Ex. for a phonebook, the.equals() method might return true if one phonebook has all the same contacts as another Header: public boolean equals(object o)

Review Writing Object Classes Object classes have 3 parts: Instance data, Constructor(s), and Instance methods Instance Data: Should be declared private and represents the internal data that the object will work with Instance data can be other objects (ex. a quiz could be comprised of question objects) this.variablename will always refer to the instance data for the particular object Constructors Used to create a new instance of this object type Can have multiple as long as they have different parametes Written as: public ObjectName(<params>) Purpose: initialize instance data Instance Methods: Define the way to interact with an object of this type Can be public or private 2 types: Accessors and Mutators

Practice 1 Create a Contact class Contacts have: Name Phonenumber (Address) Contacts have accessors and mutators for all their instance data

Practice 2 Create a Phonebook class Phonebooks have: An ArrayList of Contacts Phonebooks can: Add/remove contacts Look up the phonenumber of a contact Change the name / phonenumber of a contact Print all contacts names and numbers in alphabetical order