University of British Columbia Reading CPSC 111, Intro to Computation Jan-Apr 2006 � This week: Chapter 6 all (6.1-6.4) 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 News Recap: Formal vs. Actual Parameters � Midterm tonight: Tue Feb 7, 18:30 - 20:00 � Formal parameter: in declaration of class � Geography 100 & 200 public class Point { //... public void setPosition(int x, int y) { � Seating by last name xCoord = x; yCoord = y; � A-Kim in 200 } � Kirtz-Z in 100 } � Actual parameter: passed in when method is called � Id card face up on desk � Every other seat, sit where exam is laid out public class PointTest { � Closed book/notes/calculator public static void main(String [] args) { //... � Reminder: no labs or tutorials this week tester.setPosition(3,4); Recap: Scope Recap: Shorthand Operators � Variable scope: block of code it's declared in � Java shorthand � count++; // same as count = count + 1; � block of code is defined by braces { } � count--; // same as count = count - 1; � Class scope: accessible to any class member � note no whitespace between variable name � fields accessed by all class methods and operator � Local scope: method parameters and � Similar shorthand for assignment variables declared within method body � 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 Recap: Data Conversion � Casting: explicit data conversion � Math in Java: it depends! int a = 1 / 3; // a is 0 � Widening: conversion from one data type to another type with equal or greater amount of space to store double b = 1 / 3; // b is 0.0 value � widening conversions safer because don’t lose int c = 1.0 / 3.0; // Java’s not happy information (except for roundoff) � Java will do widening conversions automatically double d = 1.0 / 3.0; // d is 0.333333333 � 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 Recap: Static Variables � Done implicitly if widening � Static variable shared among all instances of class � Assignment conversion: converted because value � "belongs" to class, not instances of one type assigned to variable of other type � only one copy of static variable for all objects of class double b = 1 / 3; � thus changing value of static variable in one object changes it for all others objects too! � Promotion: converted because expression contains mixed types � Memory space for a static variable int hours_worked = 40; established first time containing class is double pay_rate = 5.25; referenced in program double total_pay = hours_worked * pay_rate; Recap: Static Methods Recap: Static Example public class Giraffe { � Static method "belongs" to the class itself private static int numGiraffes; � not to objects that are instances of class private double neckLength; public Giraffe(double neckLength) { � aka class method this.neckLength = neckLength; numGiraffes++; � Do not have to instantiate object of class in order to invoke static method of that class } public void sayHowTall() { � Can use class name instead of object name System.out.println("Neck is " + neckLength); to invoke static method } public static int getGiraffeCount() { return numGiraffes; } }
Static Example Calling Static Method Example public class Giraffe { public class UseGiraffes private static int numGiraffes; { private double neckLength; public static void main (String[] args) public Giraffe(double neckLength) { { this.neckLength = neckLength; System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); numGiraffes++; Giraffe fred = new Giraffe(200); Giraffe bobby = new Giraffe(220); } Giraffe ethel = new Giraffe(190); public void sayHowTall() { Giraffe hortense = new Giraffe(250); System.out.println("Neck is " + neckLength); System.out.println("Total Giraffes: " + } Giraffe.getGiraffeCount()); public return numGiraffes; } }static int getGiraffeCount() { } � Note that Giraffe is class name, not object name! } � at first line haven’t created any Giraffe objects yet � using this implicit parameter to disambiguate scope Static Methods Static Methods � Static methods do not operate in context of public class UseGiraffes { particular object public static void main (String[] args) � cannot reference instance variables because they { exist only in an instance of a class System.out.println("Total Giraffes: " + Giraffe.getGiraffeCount()); � compiler will give error if static method attempts to Giraffe fred = new Giraffe(200); use nonstatic variable Giraffe bobby = new Giraffe(220); � Static method can reference static variables Giraffe ethel = new Giraffe(190); � because static variables exist independent of specific Giraffe hortense = new Giraffe(250); objects 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 Objectives � Java provides you with many pre-existing static methods � Understand how static methods work � Package java.lang.Math is part of basic Java environment � Understand how to use conditionals � you can use static methods provided by Math class � examples: � Understand how boolean operators work > Math.random() > Math.sqrt(36) 0.7843919693319797 6.0 > Math.random() > Math.sin(90) 0.4253202368928023 0.8939966636005579 > Math.pow(2,3) > Math.sin(Math.toRadians(90)) 8.0 1.0 > Math.pow(3,2) > Math.max(54,70) 9.0 70 > Math.log(1000) > Math.round(3.14159) 6.907755278982137 3 > Math.log10(1000) 3.0
Conditional Statement Conditional Example � Boolean expression: test that returns true or import java.util.Scanner; false public class Feelgood { � Conditional statement: choose which public static void main (String[] args) statement will be executed next based on { int age; boolean expression Scanner scan = new Scanner (System.in); System.out.println ("Enter your age: "); � Example 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 " System.out.println("Really, you look like you are " + (age - 10) + "!"); + (age + 5) + "."); } } Conditional Example Conditional Example import java.util.Scanner; import java.util.Scanner; public class Feelgood public class Feelgood { { public static void main (String[] args) public static void main (String[] args) { { int age; int age; Scanner scan = new Scanner (System.in); Scanner scan = new Scanner (System.in); System.out.println ("Enter your age: "); System.out.println ("Enter your age: "); age = scan.nextInt(); age = scan.nextInt(); if (age < 20) if (age < 20) System.out.println("Really, you look like you " System.out.println("Really, you look like you " + "are " + (age + 5) + "."); + "are " + (age + 5) + "."); if (age >= 20) else System.out.println ("You don't look a day over " System.out.println ("You don't look a day over " + (age - 10) + "!"); + (age - 10) + "!"); } } } } Conditional In Depth Boolean Expressions � Within method, statements usually executed top to � Boolean expression: test which returns either true bottom or false when evaluated � one after the other � aka conditional � Change control flow with conditional statement � Consists of operands and operators, like arithmetic expression � but operators only return true or false when applied if (age < 20) System.out.println("Really, you look like you are " to operands + (age + 5) + "."); � Two different kinds of operators � relational � Choice hinges on evaluation of boolean operator � sometime split into relational and equality � logical
Recommend
More recommend