University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb 8.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Mathematical Operations, Static Methods Lecture 9, Thu Feb 2.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Interfaces, Polymorphism II Lecture 21, Thu Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Data Types, Assignment, Casting, Constants Lecture.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner 1 Advanced Class Design Lecture 19, Thu Mar
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Objects, Methods, Parameters, Input Lecture 5, Thu Jan
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Mathematical Operators, Static Methods Lecture 14,
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Objects, Input Lecture 7, Wed Jan
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CPSC 111 Introduction to Computation October 15 th, 2009 Based on slides by Eiselt, Carter, Murphy, Pottinger.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 20, Fri Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops I Lecture 17, Fri Feb
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops II Lecture 13, Thu Feb
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops III Lecture 19, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Loops Lecture 12, Tue Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Arrays Lecture 21, Mon Mar
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design Lecture 12, Mon Feb
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design II Lecture 31, Wed Mar
University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner Conditionals II Lecture 11, Thu Feb
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Loops II Lecture 18, Mon Mar
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Expressions, Data Conversion, and Input
Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Mathematical Calculations in Java Mrs. G. Chapman.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Operations Making Things Happen (Chap. 3) Expressions.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Structure of programming languages OOP. Programming Paradigm A way of conceptualizing what it means to perform computation and how tasks to be carried.
Mathematical Calculations in Java Mrs. C. Furman.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved5-2 The switch Statement The switch statement provides another way.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Programming Principles Operators and Expressions.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CPSC 233 Tutorial 12 March 4/5 th, TopHat Quiz int[] a = {0}; int[] b = {1}; a = b; What is the value of a[0] i) 0 ii) 1.
Java Primer 1: Types, Classes and Operators
Chapter 6 More Conditionals and Loops
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Creating Objects & String Class
Boolean Expressions and If
User input We’ve seen how to use the standard output buffer
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
OPERATORS (2) CSC 111.
Examples of Primitive Values
Java’s Central Casting
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
Conditionals and Loops
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Presentation transcript:

University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 Static Methods, Conditionals Lecture 15, Mon Feb borrowing from slides by Kurt Eiselt

2 Department of Computer Science Undergraduate Events Events this week Schlumberger Info Session Date: Mon., Feb 8 Time: 5:30 pm Location: HENN Rm 201 Finding a Summer Job or Internship Info Session Date: Wed., Feb 10 Time: 12 pm Location: X836 Masters of Digital Media Program Info Session Date: Thurs., Feb 11 Time: 12:30 – 1:30 pm Location: DMP 201 Reminder: Co-op Deadline Date: Fri., Feb 12 Submit application to Fiona at Rm X241 by 4:30 pm

3 Reminders n midterm tonight, 6:30 FSC 1005 n no labs this week Feb 8-12 n labs resume after break

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

5 Recap: Data Conversion n 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

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

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

8 Static Variables public class Giraffe { private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; } public void sayHowTall() { System.out.println(“Neck is “ + neckLength); }

9 Static Variables public class Giraffe { private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; } public void sayHowTall() { System.out.println(“Neck is “ + neckLength); } n how would we keep track of how many giraffes we’ve made? n need a way to declare variable that "belongs" to class definition itself n as opposed to variable included with every instance (object) of the class

10 Static Variables public class Giraffe { private static int numGiraffes; private double neckLength; public Giraffe(double neckLength) { this.necklength = necklength; } public void sayHowTall() { System.out.println(“Neck is “ + neckLength); } n static variable: variable shared among all instances of class n aka class variable n use "static" as modifier in variable declaration

11 Static Variables 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); } n updating static variable is straightforward n increment in constructor

12 Static Variables n Static variable shared among all instances of class n Only one copy of static variable for all objects of class n Thus changing value of static variable in one object changes it for all others objects too! n Memory space for a static variable established first time containing class is referenced in program

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

14 Static Methods 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; } n static method example

15 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()); } n Note that Giraffe is class name, not object name! n at first line haven’t created any Giraffe objects yet

16 Static Methods n Static methods do not operate in context of particular object n cannot reference instance variables because they exist only in an instance of a class n compiler will give error if static method attempts to use nonstatic variable n Static method can reference static variables n because static variables exist independent of specific objects n Therefore, the main method can access only static or local variables.

17 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()); } n Now you know what all these words mean n main method can access only static or local variables

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

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

20 Conditional Example import java.util.Scanner; 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) + "!"); }

21 Conditional Example import java.util.Scanner; 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) + "!"); }

22 Conditional Example import java.util.Scanner; 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) + "!"); }

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

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

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

26 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!”); n Note we can use arithmetic operator inside boolean expression

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

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

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

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

31 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

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