Tutorial 4. Values and References. Add One A. Add One B. Add One C

Similar documents
PROGRAMMING FUNDAMENTALS

CT 229 Arrays Continued

b. Suppose you enter input from the console, when you run the program. What is the output?

Array. Array Declaration:

Inf1-OP. Functions aka Static Methods. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. School of Informatics

Functions / Static Methods. Inf1-OP. What s this static method business? Functions / Static Methods. Haskell function: type declaration

CS111: PROGRAMMING LANGUAGE II

Functions / Static Methods. Why are functions so helpful? Inf1-OP. Find Nearest Neighbour to a Central Point

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Building Java Programs

Lab Assignment Three

Inf1-OOP. Functions aka Static Methods. Perdita Stevens, adapting earlier version by Ewan Klein. January 11, School of Informatics

Building Java Programs

Lecture 2: Java & Javadoc

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

Computational Expression

AP Computer Science Unit 1. Programs

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Java Identifiers, Data Types & Variables

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

Prof. Navrati Saxena TA: Rochak Sachan

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

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Programming Basics. Digital Urban Visualization. People as Flows. ia

Following is the general form of a typical decision making structure found in most of the programming languages:

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Getting started with Java

Lec 3. Compilers, Debugging, Hello World, and Variables

University of Palestine. Mid Exam Total Grade: 100

Inf1-OP. Arrays 1. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. January 30, School of Informatics

COS 126 General Computer Science Fall Written Exam 1

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

Building Java Programs

/* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

13 th Windsor Regional Secondary School Computer Programming Competition

1.00/1.001 Introduction to Computers and Engineering Problem Solving Fall 2002

Fall 2017 CISC124 9/16/2017

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Lecture Set 4: More About Methods and More About Operators

Selected Questions from by Nageshwara Rao

CS110D: PROGRAMMING LANGUAGE I

(A) 99 (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Inf1-OP. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein. February 26, School of Informatics

CSE 142, Spring 2009, Final Exam

Passing Array to Methods

Some Sample AP Computer Science A Questions - Solutions

Introduction to Computer Science Unit 2. Notes

( &% class MyClass { }

Announcements. Lab 03 inbase late submissions Midterm #1 Friday. Project 2 is posted. Covers everything through basics of OOP

Lab Exercise 1. Objectives: Part 1. Introduction

Crash Course in Java. Why Java? Java notes for C++ programmers. Network Programming in Java is very different than in C/C++

ANSWERS. Birkbeck (University of London) Software and Programming 1 In-class Test Feb Student Name Student Number. Answer all questions

Pearson Education Limited Edinburgh Gate Harlow Essex CM20 2JE England and Associated Companies throughout the world

Complexity, General. Standard approach: count the number of primitive operations executed.

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Arrays. Inf1-OP. Arrays. Many Variables of the Same Type. Arrays 1. Volker Seeker, adapting earlier version by Perdita Stevens and Ewan Klein

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

I. True/False: (2 points each)

1. Introduction to Java for JAS

x++ vs. ++x x=y++ x=++y x=0; a=++x; b=x++; What are the values of a, b, and x?

1 Definitions & Short Answer (4 Points Each)

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

Exceptions: When something goes wrong. Image from Wikipedia

1. An operation in which an overall value is computed incrementally, often using a loop.

Computer Science II (20082) Week 1: Review and Inheritance

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

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Lecture 5: Methods CS2301

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

This page intentionally left blank

Methods. Bok, Jong Soon

Prelim 1. Solution. CS 2110, 14 March 2017, 7:30 PM Total Question Name Short answer

Oct. 3 fixup - Here are notes and codes in the proper order that they should be.

Arrays Classes & Methods, Inheritance

CS Week 5. Jim Williams, PhD

Java Programming. Atul Prakash

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CIS 1068 Program Design and Abstraction Spring2016 Midterm Exam 1. Name SOLUTION

Last Name: Circle One: OCW Non-OCW

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Scope of this lecture

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Sorting and searching arrays

Introduction to Computer Science Unit 2. Notes

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Prelim 1. Solution. CS 2110, 14 March 2017, 5:30 PM Total Question Name Short answer

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

AP Computer Science Java Mr. Clausen Program 6A, 6B

Praktische Softwaretechnologie

BSc (Hons) Computer Science with Network Security/ BSc (Hons) Software Engineering/ BSc (Hons) Web Technologies. Examinations for 2016 Semester 1

1. Download the JDK 6, from

Topic 4 Expressions and variables

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Opening Problem. Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.

Object-Oriented Programming

Transcription:

Tutorial 4 Values and References Here are some "What Does it Print?" style problems you can go through with your students to discuss these concepts: Add One A public static void addone(int num) { num++; int x = 0; addone(x); System.out.println(x); Add One B public static int addone(int num) { num++; return num; int x = 0; x = addone(x); System.out.println(x); Add One C public static void addone(int[] anarray) { anarray[0]++; int[] a = { 0, 1 ; addone(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]);

Add One D public static void addone(int[] anarray) { anarray = new int[2]; int[] a = { 0, 1 ; addone(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); Add One E public static int[] addone(int[] anarray) { anarray = new int[2]; return anarray; int[] a = { 0, 1 ; a = addone(a); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); Object Call A class Number { int x; public class Reference { public static void main ( String[] args ) { Number a = new Number(); a.x=4; System.out.println(a.x); Number b=a; b.x=5; System.out.println(b.x);

Object Call B class Operation{ int data; public Operation() { data = 50; void change(int data){ data = data+100; public static void main(string[] args){ Operation op = new Operation(); System.out.println("before change "+op.data); op.change(500); System.out.println("after change "+op.data); Object Call C class Operation2{ int data; public Operation2() { data = 50; void change(operation2 op){ op.data = op.data+100; public static void main(string[] args){ Operation2 op = new Operation2(); System.out.println("before change "+op.data); op.change(op); System.out.println("after change "+op.data); Comparison A

int a = 5; int b = 5; Comparison B class Num { int value; public Num(int v) { value = v; Num a = new Num(5); Num b = new Num(5); Comparison C class Num { int value; public Num(int v) { value = v; Num a = new Num(5); Num b = a; Comparison D Integer a = 5 Integer b = 5;

Comparison E Integer a = 128; Integer b = 128; Comparison F Integer a = 128; Integer b = 128; System.out.println(a.equals(b)); Comparison G String a = "5"; String b = "5"; Comparison H String a = new String("5"); String b = new String("5"); Comparison I String a = new String("5"); String b = new String("5"); System.out.println(a.equals(b));

Solutions Add One A: prints 0 due to call by value Add One B: prints 1, function is called by value but the result is returned and assigned Add One C: prints 1 1 due to call by reference (classes and arrays are objects in Java) Add One D: prints 0 1 since new memory is allocated Add One E: prints 0 0 since new memory is allocated, automatically initialised and then returned to replace the original array in the main function Object Call A: prints 4 and 5, assigning reference to original object Object Call B: prints 50, since change function modifies local field and not member (could have called this.data instead) Object Call C: prints 150 due to call by reference Comparison A: prints true for primitives Comparison B: prints false, since comparing references not member values Comparison C: prints true, same reason as B Comparison D: prints true, actually comparing references but Integers are immutuable hence a and b are actually referencing the same memory location Comparison E: prints false, immutuable Integers are only cached from -128 to 127 everything else gets its own memory location (stays immutuable though) Comparison F: prints true, since Integer's equals method checks the actual values Comparison G: prints true, since the string literal "5" is cached by the compiler Comparison H: prints false, since different memory locations are used Comparison I: prints true, same as for comparison F