Unit 5: More on Classes/Objects Notes

Similar documents
CS 302 Week 9. Jim Williams

Week 3 Classes and Objects

AP CS Unit 3: Control Structures Notes

APCS Semester #1 Final Exam Practice Problems

Unit 5: More on Classes/Objects Exercises

Intro. Classes Beginning Objected Oriented Programming. CIS 15 : Spring 2007

Introduction to Programming Using Java (98-388)

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Chapter 6 Introduction to Defining Classes

AP CS Unit 7: Arrays Exercises

CS 231 Data Structures and Algorithms, Fall 2016

Programming II (CS300)

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Unit 4: Classes and Objects Notes

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

CS Week 14. Jim Williams, PhD

9 Working with the Java Class Library

AP Computer Science A Unit 7. Notes on Arrays

Array. Prepared By - Rifat Shahriyar

Objects as a programming concept

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Overview CSE 142. Naming Revisited Declarations. Defining Parts of Objects

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

3.1 Class Declaration

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

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Introduction to Java. Handout-1d. cs402 - Spring

CS Week 13. Jim Williams, PhD

AP CS Unit 8: Inheritance Exercises

Programming II (CS300)

CS 1302 Chapter 9 (Review) Object & Classes

Programming II (CS300)

CSE 142 Su 04 Computer Programming 1 - Java. Objects

COE318 Lecture Notes Week 10 (Nov 7, 2011)

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

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Programming II (CS300)

Data Structures. Data structures. Data structures. What is a data structure? Simple answer: a collection of data equipped with some operations.

Coding Standards for Java

Mr. Monroe s Guide to Mastering Java Syntax

AP CS Unit 7: Interfaces Exercises 1. Select the TRUE statement(s).

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

Static, Final & Memory Management

AP CS Unit 6: Inheritance Exercises

CSCI 161 Introduction to Computer Science

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

Math Modeling in Java: An S-I Compartment Model

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

Object Oriented Programming

BM214E Object Oriented Programming Lecture 7

SPRING 13 CS 0007 FINAL EXAM V2 (Roberts) Your Name: A pt each. B pt each. C pt each. D or 2 pts each

CS 102/107 - Introduction to Programming Midterm Exam #2 - Prof. Reed Spring 2011

5.6.1 The Special Variable this

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

CS 1331 Exam 1 ANSWER KEY

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

BM214E Object Oriented Programming Lecture 8

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

AP Computer Science Lists The Array type

Java Fundamentals (II)

Logic & program control part 2: Simple selection structures

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

Introduction to Computer Science I

Introduction to Java

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

AP CS Unit 6: Inheritance Notes

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

Give one example where you might wish to use a three dimensional array

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.

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

CS-140 Fall Binghamton University. Methods. Sect. 3.3, 8.2. There s a method in my madness.

CSE 143 Lecture 20. Circle

Assumptions. History

Introduction to Computer Science Unit 2. Notes

CS1150 Principles of Computer Science Objects and Classes

Java Identifiers, Data Types & Variables

Computer Components. Software{ User Programs. Operating System. Hardware

class objects instances Fields Constructors Methods static

Static Methods. Why use methods?

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

AP CS Unit 4: Classes and Objects Programs

Methods. Methods. Mysteries Revealed

CISC-124. Dog.java looks like this. I have added some explanatory comments in the code, and more explanation after the code listing.

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

CS 200 Arrays, Loops and Methods Jim Williams, PhD

I. True/False: (2 points each)

C10: Garbage Collection and Constructors

University of Cape Town ~ Department of Computer Science Computer Science 1015F ~ Test 2. Question Max Mark Internal External

Chapter 5 Names, Bindings, Type Checking, and Scopes

CS18000: Problem Solving And Object-Oriented Programming

More About Classes CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario

Methods and Data (Savitch, Chapter 5)

Recitation 3 Class and Objects

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Java Review. Fundamentals of Computer Science

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

public void m1(){ int n = 5; m2( n ); System.out.println( n ); }

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

ESE115 Introduction to Programming with Java. Midterm 2 November 10, 2005 SOLUTION

Transcription:

Unit 5: More on Classes/Objects Notes AP CS A The Difference between Primitive and Object/Reference Data Types First, remember the definition of a variable. A variable is a. So, an obvious question is: what is stored in a particular variable? For primitive data types (e.g. ) the answer is easy. The variable stores the data. int x = 78; int y = x; For object variables, the answer is more complicated. An object variable contains a to an object - not the object itself. Point p1 = new Point( 3, 4 ); Point p2; p2 = p1; 1. How many Point objects does this code create? 2. How many Point objects does this code create? Point x1; Point x2; Point x3 = new Point( 17, -22 ); Point x4 = x3; is a special value that can assigned to any object variable. This indicates that the variable does not contains a reference to an object. Reference variables can be initialized with a value of null. For example: Point p1 = null; To check if a variable has a value of null, use ==. For example: public void dosomething( String x ){ if ( x == null ) x does not contain a reference to a String object Page 1

If you attempt to call a method but the object variable does not contain a reference to an object, then you will get this runtime error:. For example: Point pt = null; double d = pt.distance( 0, 5.5 ); // If an object is instantiated but at some later time no variable contains a reference to that object, the JVM will delete that object in a process called. For example: Point man = new Point( -1, 0 ); man = null; // Why public and private? When someone uses an object, they don t usually need to know how an object does something; they only need to know what an object does. For example, we ve been using Scanner objects without ever seeing the actual code inside that class. This is similar to how we use everyday objects. In the process of writing a class, we have (so far) always made the instance variables private and everything else public. Let s consider why making instance variables private is useful. Look over the code below. public class Person1{ private int age = 8; public int get(){ return age; public void set(int n){ age = n; public class Person2{ public int age = 9; // another class Person1 p1 = new Person1(); Person2 p2 = new Person2(); System.out.println( p1.get() ); System.out.println( p2.age ); p1.set( 24 ); p2.age = 25; System.out.println( p1.get() ); System.out.println( p2.age ); The above code compiles and runs. What is displayed? The Person1 and Person2 classes do the same thing. Unfortunately, both have a design flaw How can the flaw be fixed in the Person1 class? How can the flaw be fixed in the Person2 class? This is one reason to make the instance variable private and then write getter and setter methods. Also, if an instance variable is public then you cannot change its name, or data type, without breaking any code that uses this class. Conclusion: Page 2

The of an object refers to the current values of all its instance variables. If the value of even one instance variable changes, then the state of the object has changed. Two methods in the same class are if they have the same name but different parameters. Two methods in the same class cannot differ by just their. Default Constructors. If a class does not define a constructor, it automatically has one. All instance variables are given default values. ints and doubles have default values of booleans have a default value of reference (aka object) variables have a default value of public static void ( String [] args ){ public class Panda { private int x; public int get(){ return x; IMPORTANT. If you write even one constructor, then you lose the default constructor. The Scope of a variable refers both to its lifetime and its accessibility. Lifetime means how long does the variable exist and accessibility means who can see/use the variable. Variable Lifetime Accessibility public instance variable Any class that can access the object. private instance variable parameters in a constructor or method local variables in a constructor or method Only the constructors and methods within its class. Only the constructor or method that it is declared in. Only within the block that it is declared in. A block of code is a section of code that is explicitly or implicitly contained with a pair of curly braces. Page 3

Heap vs. Stack. Memory to run a program can be divided into the heap and the stack. The heap is for objects (and other things) and the stack is for local variables and parameters. Each call to a method generates a new stack frame for that method. 1 2 3 4 public class Practice { public static void ( String [] args ) { Dog a = new Dog( 2 ); int z = 5; z = a.bark( 3 ); System.out.print( z ); 5 6 7 public class Dog{ private int w; public Dog(int x){ w = x; public int bark(int y) { int n = y + w; return n; Line currently being executed Stack Heap 1. The method is called. 2. The Dog constructor is called and a new stack frame is generated constructor Completion of line 2. The stack frame for the constructor is removed. 3. Another local variable is declared. 4/5. The bark method is called and a new stack frame is generated. bark 6. A local variable in the bark method is declared. bark 7. The value of n is returned and assigned to z. The stack frame for the bark method is deleted. Page 4

this. Within an instance method or a constructor, this is a reference to the current object the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this. - http://docs.oracle.com/javase/tutorial/java/javaoo/thiskey.html In many circumstances, the use of this is optional. Compare these two versions of the same class. The Way We Write a Class The Way Some People Like to Write a Class public class Square{ public class Square{ private int side; private int side; public Square( int s ){ side = s; public Square( int s ){ this.side = s; public int area(){ return side * side; public int area(){ return this.side * this.side; Some people think that the version on the right makes it clearer that we are changing the value of the instance variable of the current object. Here s some sad code where the use of this is required. In this example, every Ape that is created must immediately be added to a Zoo. public class Zoo{ public class Ape { private String names = ""; private String name; public void add( Ape a ){ names += a.get() + " "; public String getnames(){ return names; All of this code compiles and runs. What is displayed when the Runner class is executed? public Ape( String s, Zoo z ){ this.name = s; z.add( ); public String get(){ return name; public static void (String [] args){ Zoo z = new Zoo(); Ape a1 = new Ape( "Bob", z ); Ape a2 = new Ape( "Ann", z ); String s = z.getnames(); System.out.println( s ); Page 5

You can access the private instance variables of a parameter IF the parameter is an object of that class. For example: public class Box{ private int width; public static void ( String [] args ){ private int length; Box b1 = new Box( 3, 6 ); Box b2 = new Box( 5, 4 ); System.out.println(b1.biggerThan( b2 )); System.out.println(b2.biggerThan( b1 )); public Box( int wid, int len ){ width = wid; length = len; public boolean biggerthan( Box b ){ int my_area = this.width*this.length; int other_area = b.width * b.length; return my_area > other_area; Constants. Sometimes you want to create a variable that never changes. For example: public class Bill{ private final double TAX_RATE = 0.1; public static void ( String [] args ){ private double total = 0; Bill b = new Bill(); public double add( double item ){ total += item + item*tax_rate; return total; Use the keyword final to make a variable a constant. double x = b.add( 10 ); System.out.println( x ); x = b.add( 20 ); System.out.println( x ); It is customary to use all upper-case characters for a constant s name. Constants improve your code s readability and tainability. Trying to change the value of a constant generates a compiler error. Functional Decomposition is the process of breaking a method into Smaller, more focused methods are The helper methods will almost always be. After function decomposition, a method should be easier to understand and may be shorter than the original code. You should think of using Page 6

It can look something like this: First Version public return_type method( parameters ){ section A (some lines of code) section B (some lines of code) section C (some lines of code) After Functional Decomposition public return_type method( parameters ){ methoda( parameters ); methodb( parameters ); methodc( parameters ); private return_type methoda( parameters ){ code private return_type methodb( parameters ){ code private return_type methodc( parameters ){ code Here s a more concrete example. The purpose of the method is to print out a grid similar to the one shown. The size of the grid depends on the parameter. In the figure to the right, size is 5. +++++++++++++++ 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3 0 +++++++++++++++ First Version public void method( int size ){ for ( int k = 0; k < size; k++ ) System.out.print( "+++" ); System.out.println(); int x = 0; for ( int row = 0; row < size; row++ ){ for ( int col = 0; col < size; col++ ){ if ( x == size-1 ) x = 0; System.out.print( " " + x + " " ); x++; System.out.println(); After Functional Decomposition public void method( int size ){ printplus( size ); Notice how printnumbers( size ); we call these printplus( size ); methods. private void printplus( int size ){ private void printnumbers ( int size ){ for ( int k = 0; k < size; k++ ) System.out.print( "+++" ); System.out.println(); Page 7