Upload
daisy-joseph
View
227
Download
0
Embed Size (px)
DESCRIPTION
Accessor Methods get >() Robot examples getStreet() getAvenue() Do BasketballPlayer example
Citation preview
ITK 168 – More Variables10/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
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 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
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
Operators 6 + 6 12 – 10 600 * 2 10 / 5 10 / 8 10 / 3.0 6.6 + 6 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
FormattingNumberFormat currency =
NumberFormat.getCurrancyInstance();
System.out.printf() for printing in columnssee 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
E L C OW M E156
156
Welcome
0 1 2 3 4 5 6
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 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
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
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