22
ITK 168 – More Variables 10/13/05

ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants Go through SimpleBot Modify SimpleBot to “teleport”

Embed Size (px)

DESCRIPTION

Accessor Methods  get >()  Robot examples getStreet() getAvenue()  Do BasketballPlayer example

Citation preview

Page 1: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

ITK 168 – More Variables10/13/05

Page 2: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Another example of using instance variables and constants Go through SimpleBot Modify SimpleBot to “teleport”

Page 3: 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<<Whatever your getting>>() Robot examples

getStreet() getAvenue()

Do BasketballPlayer example

Page 4: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Using accessors to test Creating a test harness

main methods Using Becker’s testing scheme BasketballPlayer example

Page 5: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

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

Page 6: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Integer types byte 1 byte -128 to 127

8 bit short 2 bytes -32,768 to 32767

16 bit int 4 bytes -2,147,483,648 to 2,147,483,647

32 bit long 8 bytes -9,223,372,036,854,775,808

to 9,223,372,036,854,775,807 64 bit

Page 7: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Floating point types float ±1.40238946E-45 to ± 3.40282347E+38

7 significant digits float f = 0.5f; 32 bit

double 4.94065645841246544E-308 to ± 1.79769313486231570E+308 16 significant digits double d = 56.5E22; 64 bit

Page 8: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Operators 6 + 6 12 – 10 600 * 2 10 / 5 10 / 8 10 / 3.0 6.6 + 6 10 % 8

+ Add – Subtract * Multiply / Divide % Modulus

Page 9: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

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;

Page 10: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Mathematical functions

Page 11: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

FormattingNumberFormat currency =

NumberFormat.getCurrancyInstance();

System.out.printf() for printing in columnssee java.util.Formatter for use

Page 12: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

boolean true or false boolean b = true; boolean b =false; No conversions

Page 13: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

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’

Page 14: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

String Declaration and assignment

String name = “bob”; String name = new String(“bob”);

Concatenation String fullName = name + “ “ +

“Vila”; BasketballPlayer example

Page 15: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

String

E L C OW M E156

156

Welcome

0 1 2 3 4 5 6

Index

String welcome = “WELCOME”;

Page 16: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

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”)

Page 17: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

toString Robot bob = new Robot(…); System.out.println(bob); BasketballPlayer?

Page 18: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Some examples toUpperCase() indexOf() substring equals

Page 19: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Strings are immutable They can’t be changed. You can only create a

new one from an old one

Page 20: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Data Types and Literals Boolean true or false Primitive

Integral – has no decimal point byte short int 25484 long 18131891125484L char ‘a’

Floating point – has a decimal point float 12.5 (1.25E1) double 2.87231651651561E200D

Reference Objects “Welcome to ITK 168”

25484

156 Welcome to MIS 222

156

684Memoryaddress

memory

Page 21: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Primitive vs. Reference AssignmentString 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

0

684

income

758

income = 254848;income = 5;

157

bob

758

157

nullint income;

2548485

Page 22: ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”

Interface Defines how a class should look so it can do

specific things. Runnable

public void run() public class <<name>> implements

<<Interface>> Show the ComboLock interface