University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Similar documents
! This week: Chapter 6 all ( ) ! Formal parameter: in declaration of class. ! Actual parameter: passed in when method is called

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

! Midterm 2: Thu Mar 16, 6:30pm (TODAY!) ! Woodward 2. ! hour-long exam, reserve 6:30-8 time slot. ! no labs/tutorials this week. ! Bunny.

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

! labs this week. ! midterms returned. ! work through what you got wrong on midterm. ! can earn back up to 5 out of 70 points

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner

Midterms Save the Dates!

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

Last Time. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Introduction to Computer Science Unit 2. Notes

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

! labs last week. ! still time to work through lab 7 (midterm correction) ! can earn back up to 5 out of 70 points

AP CS Unit 3: Control Structures Notes

! Rest of Chap 2 ! 2.3-4, ! Rest of Chap 4 ! ! Things that do not vary. ! unlike variables. ! will never change. !

Midterms Save the Dates!

Midterms Save the Dates!

Introduction to Computer Science Unit 2. Notes

Midterms Save the Dates!

Introduction to Computer Science Unit 2. Exercises

Survey #2. Variable Scope. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings. Scope Static.

Objectives of CS 230. Java portability. Why ADTs? 8/18/14

CS111: PROGRAMMING LANGUAGE II

Java Classes: Math, Integer A C S L E C T U R E 8

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

CS 112 Introduction to Programming

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

Data Conversion & Scanner Class

Practice Midterm 1 Answer Key

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Midterms Save the Dates!

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

CT 229 Java Syntax Continued

Survey #2. Teen Talk Barbie TM Reloaded. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu Partially Filled Arrays ArrayLists

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

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

CS110: PROGRAMMING LANGUAGE I

Lecture 5: Methods CS2301

Midterms Save the Dates!

Lecture Set 4: More About Methods and More About Operators

Program Fundamentals

Lecture Set 4: More About Methods and More About Operators

12. Numbers. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Chapter. Let's explore some other fundamental programming concepts

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

Chapter 2: Data and Expressions

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

Midterms Save the Dates!

Programming with Java

PeerWise Study. Midterms Save the Dates! University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Constants Using Classes and Objects

Lecture 6: While Loops and the Math Class

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CMPT 125: Lecture 3 Data and Expressions

Advanced Java Concept Unit 1. Mostly Review

2: Basics of Java Programming

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

Algorithms and Conditionals

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Building Java Programs

Chapter 2. Elementary Programming

CS110: PROGRAMMING LANGUAGE I

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

Supplementary Test 1

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Chapter 2: Data and Expressions

ECE 122 Engineering Problem Solving with Java

Inf1-OOP. Textbooks. Who and What. Organizational Issues. Why Java? Course Overview. Hello, World! in Java. Ewan Klein, Perdita Stevens

CS 335 Lecture 02 Java Programming

CS111: PROGRAMMING LANGUAGE II

CS 200 Using Objects. Jim Williams, PhD

CIS133J. Working with Numbers in Java

Building Java Programs

Practice Midterm 1. Problem Points Score TOTAL 50

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

Chapter 4: Conditionals and Recursion

Programming Languages and Techniques (CIS120)

1.00/1.001 Tutorial 1

CSC 1051 Data Structures and Algorithms I

IEEE Floating-Point Representation 1

Calculations, Formatting and Conversions

Final Exam. Programming Assignment 3. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu. Readings

Primitive Data Types: Intro

Chapter 5 Methods. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Define a method vs. calling a method. Chapter Goals. Contents 1/21/13

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.

CS 335 Java Programming Controls. Fall 2007

Programming in Java

Java Coding 3. Over & over again!

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Lecture #6-7 Methods

Using Classes and Objects. Lecture 7. Midterms Save the Dates! Extra Credit Survey

Zheng-Liang Lu Java Programming 45 / 79

COMP-202: Foundations of Programming. Lecture 4: Methods Jackie Cheung, Winter 2016

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

Transcription:

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Static Methods, Conditionals Lecture 10, Tue Feb 7 2006 based on slides by Kurt Eiselt http://www.cs.ubc.ca/~tmm/courses/cpsc111-06-spr

Reading This week: Chapter 6 all (6.1-6.4)

News Midterm tonight: Tue Feb 7, 18:30-20:00 Geography 100 & 200 Seating by last name A-Kim in 200 Kirtz-Z in 100 Id card face up on desk Every other seat, sit where exam is laid out Closed book/notes/calculator Reminder: no labs or tutorials this week

Recap: Formal vs. Actual Parameters Formal parameter: in declaration of class public class Point { //... public void setposition(int x, int y) { xcoord = x; ycoord = y; Actual parameter: passed in when method is called public class PointTest { public static void main(string [] args) { //... tester.setposition(3,4);

Recap: Scope Variable scope: block of code it's declared in block of code is defined by braces { Class scope: accessible to any class member fields accessed by all class methods Local scope: method parameters and variables declared within method body

Recap: Shorthand Operators Java shorthand count++; // same as count = count + 1; count--; // same as count = count - 1; note no whitespace between variable name and operator Similar shorthand for assignment tigers += 5; // like tigers=tigers+5; lions -= 3; // like lions=lions-3; bunnies *= 2; // like bunnies=bunnies*2; dinos /= 100; // like dinos=dinos/100;

Recap: Data Conversion Math in Java: it depends! int a = 1 / 3; // a is 0 double b = 1 / 3; // b is 0.0 int c = 1.0 / 3.0; // Java s not happy double d = 1.0 / 3.0; // d is 0.333333333

Recap: Data Conversion Casting: explicit data conversion Widening: conversion from one data type to another type with equal or greater amount of space to store value widening conversions safer because don t lose information (except for roundoff) Java will do widening conversions automatically Narrowing: conversion from one type to another type with less space to store value important information may be lost Java will not do narrowing conversions automatically

Recap: Automatic Conversion Done implicitly if widening Assignment conversion: converted because value of one type assigned to variable of other type double b = 1 / 3; Promotion: converted because expression contains mixed types int hours_worked = 40; double pay_rate = 5.25; double total_pay = hours_worked * pay_rate;

Recap: Static Variables Static variable shared among all instances of class "belongs" to class, not instances only one copy of static variable for all objects of class thus changing value of static variable in one object changes it for all others objects too! Memory space for a static variable established first time containing class is referenced in program

Recap: Static Methods Static method "belongs" to the class itself not to objects that are instances of class aka class method Do not have to instantiate object of class in order to invoke static method of that class Can use class name instead of object name to invoke static method

Recap: Static Example public class Giraffe { private static int numgiraffes; private double necklength; public Giraffe(double necklength) { this.necklength = necklength; numgiraffes++; public void sayhowtall() { System.out.println("Neck is " + necklength); public static int getgiraffecount() { return numgiraffes;

Static Example public class Giraffe { private static int numgiraffes; private double necklength; public Giraffe(double necklength) { this.necklength = necklength; numgiraffes++; public void sayhowtall() { System.out.println("Neck is " + necklength); public return numgiraffes; static int getgiraffecount() { using this implicit parameter to disambiguate scope

Calling Static Method Example public class UseGiraffes { public static void main (String[] args) { System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); Giraffe fred = new Giraffe(200); Giraffe bobby = new Giraffe(220); Giraffe ethel = new Giraffe(190); Giraffe hortense = new Giraffe(250); System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); Note that Giraffe is class name, not object name! at first line haven t created any Giraffe objects yet

Static Methods Static methods do not operate in context of particular object cannot reference instance variables because they exist only in an instance of a class compiler will give error if static method attempts to use nonstatic variable Static method can reference static variables because static variables exist independent of specific objects

Static Methods public class UseGiraffes { public static void main (String[] args) { System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); Giraffe fred = new Giraffe(200); Giraffe bobby = new Giraffe(220); Giraffe ethel = new Giraffe(190); Giraffe hortense = new Giraffe(250); System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); Now you know what all these words mean main method can access only static or local variables

Static Methods in java.math Java provides you with many pre-existing static methods Package java.lang.math is part of basic Java environment you can use static methods provided by Math class examples: > Math.sqrt(36) 6.0 > Math.sin(90) 0.8939966636005579 > Math.sin(Math.toRadians(90)) 1.0 > Math.max(54,70) 70 > Math.round(3.14159) 3 > Math.random() 0.7843919693319797 > Math.random() 0.4253202368928023 > Math.pow(2,3) 8.0 > Math.pow(3,2) 9.0 > Math.log(1000) 6.907755278982137 > Math.log10(1000) 3.0

Objectives Understand how static methods work Understand how to use conditionals Understand how boolean operators work

Conditional Statement Boolean expression: test that returns true or false Conditional statement: choose which statement will be executed next based on boolean expression Example if (age < 20) System.out.println("Really, you look like you are " + (age + 5) + ".");

import java.util.scanner; Conditional Example public class Feelgood { public static void main (String[] args) { int age; Scanner scan = new Scanner (System.in); System.out.println ("Enter your age: "); age = scan.nextint(); if (age < 20) System.out.println("Really, you look like you " + "are " + (age + 5) + "."); System.out.println ("You don't look a day over " + (age - 10) + "!");

import java.util.scanner; Conditional Example public class Feelgood { public static void main (String[] args) { int age; Scanner scan = new Scanner (System.in); System.out.println ("Enter your age: "); age = scan.nextint(); if (age < 20) System.out.println("Really, you look like you " + "are " + (age + 5) + "."); if (age >= 20) System.out.println ("You don't look a day over " + (age - 10) + "!");

import java.util.scanner; Conditional Example public class Feelgood { public static void main (String[] args) { int age; Scanner scan = new Scanner (System.in); System.out.println ("Enter your age: "); age = scan.nextint(); if (age < 20) System.out.println("Really, you look like you " + "are " + (age + 5) + "."); else System.out.println ("You don't look a day over " + (age - 10) + "!");

Conditional In Depth Within method, statements usually executed top to bottom one after the other Change control flow with conditional statement if (age < 20) System.out.println("Really, you look like you are " + (age + 5) + "."); Choice hinges on evaluation of boolean operator

Boolean Expressions Boolean expression: test which returns either true or false when evaluated aka conditional Consists of operands and operators, like arithmetic expression but operators only return true or false when applied to operands Two different kinds of operators relational sometime split into relational and equality logical

Relational Operators Tests two values (operands) Operators == equal returns true if they are equal, false otherwise note: do not confuse this with =!= not equal returns true if they are not equal, false otherwise < less than <= less than or equal to > greater than >= greater than or equal to

Equality Example int a = 3; int b = 6; int c = 10; if (a == b) System.out.println( these two values are equal ); if ((b - a) == a) System.out.println( b is the same as a ); if (a!= b) System.out.println( nope! ); Note we can use arithmetic operator inside boolean expression

Logical Operators Way to combine results from relational operators into single test AND, OR, and NOT in terms from math or philosophy class Operators && logical AND logical OR! logical NOT

Logical AND Logical AND of values a and b evaluates to true if both a and b are true false otherwise a b a && b false false false false true false true false false true true true

Logical OR Logical OR of values a and b evaluates to true if either a or b are true true if both are true false otherwise a b a b false false false false true true true false true true true true

Logical NOT Logical NOT of value a evaluates to true if a is false false if a is true a! a false true true false

Logical Operator Examples int a = 3; int b = 6; int c = 10; if ((b > a) && (c == 10)) System.out.println( this should print ); if (!(b > a)) System.out.println( this should not print ); if!(b > a) System.out.println( what happened? );

Logical Operator Examples is (!(b > a)) the same as (a > b) (a >= b) (b < a)

Questions?