ITK 168 – More Variables 10/13/05
Another example of using instance variables and constants Go through SimpleBot Modify SimpleBot to “teleport”
Accessor Methods get >() Robot examples getStreet() getAvenue() Do BasketballPlayer example
Using accessors to test Creating a test harness main methods Using Becker’s testing scheme BasketballPlayer example
Primitive Types Classes Robot, Thing, BasketballPlayer, String Often custom instantiated with new Primitive types int, double, boolean, char, … Fixed Fundamental types native to the programming language
Integer types byte1 byte -128 to bit short 2 bytes -32,768 to bit int 4 bytes -2,147,483,648 to 2,147,483, bit long 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775, bit
Floating point types float ± E-45 to ± E+38 7 significant digits float f = 0.5f; 32 bit double E-308 to ± E significant digits double d = 56.5E22; 64 bit
Operators 12 – 10 600 * 2 10 / 5 10 / 8 10 / 3.0 10 % 8 + Add – Subtract * Multiply / Divide % Modulus
Converting between types It’s called “casting” int i = 6; double d = i; double d = 55.89; int i = d; ?? int i = (int)d; int i = (int) d / 3.7;
Mathematical functions
Formatting NumberFormat currency = NumberFormat.getCurrancyInstance(); System.out.printf() for printing in columns see java.util.Formatter for use
boolean true or false boolean b = true; boolean b =false; No conversions
char char ‘Я’ or any unicode character Escape sequences \b backspace \f next page \n next line \r carriage return \t tab \\ backslash \’single quote \” double quote \u010E unicode character (®) char c = ‘a’ char registeredTrademark = ‘\u00AE’
String Declaration and assignment String name = “bob”; String name = new String(“bob”); Concatenation String fullName = name + “ “ + “Vila”; BasketballPlayer example
String ELCOWME 156 Welcome Index String welcome = “WELCOME”;
String Methods welcome.length(); //returns 7 welcome.charAt(2); //returns ‘l’ welcome.toLowerCase(); //returns “welcome” welcome.indexOf(“ME”); //returns 5 welcome.equals(“WELCOME”); //returns true DON’T use if(welcome == “welcome”)
toString Robot bob = new Robot(…); System.out.println(bob); BasketballPlayer?
Some examples toUpperCase() indexOf() substring equals
Strings are immutable They can’t be changed. You can only create a new one from an old one
Data Types and Literals Boolean true or false Primitive Integral – has no decimal point byte short int long L char ‘a’ Floating point – has a decimal point float 12.5 (1.25E1) double E200D Reference Objects “Welcome to ITK 168” Welcome to MIS Memory address memory
Primitive vs. Reference Assignment String welcome = “Welcome to ITK 168”; String bob = “Welcome to geekdom”; welcome = bob; bob = null; 156 “Welcome to ITK 168” 156 “Welcome to geekdom” 157 welcome income 758 income = ; income = 5; 157 bob null int income;
Interface Defines how a class should look so it can do specific things. Runnable public void run() public class > implements > Show the ComboLock interface