Java Methods
Topics Declaring fields vs. local variables Primitive data types Strings Compound Assignment Conversions from one value to another Arithmetic operators
Declaration vs. initialization The following is JUST declaration int sum; double x,y;//note, 2 variables declared Declaration including initialization int mileage = suburban.getMileage(); Declaration and initialization are often separate UrRobot karel; karel = new UrRobot(1,1,East,0);
Declaration vs. initialization Additional notes: int x = 5; //assigning x the value of 5 x is referencing the value of 5 final double gravity = 9.8; The reserved word final makes the variable become a constant Useful for values that should never be changed
Fields vs. Local Variables Fields – declared within a class, but outside of any constructor or method - fields are accessible to the entire class - fields are typically declared private // let us start observing this style rule Local variables – temporary variables declared inside a constructor or method Every time a method is called, that variable is re- initialized Note: your program can have fields and local variables of the same name avoid this!
Parameters Parameters are considered a third type of variable in addition to fields and local variables Parameters – variables passed to constructors or methods - act like local variables public void moveSteps (int step); public static void main(String[] args) { moveSteps(int x = 6);} It is bad convention to declare and initialize when you call a method, but this demonstrates that each method creates a new reference for the parameter, so you can give it a new name
Primitive Data types There are 8 – listed on page 128 We will use: boolean, char, int, double Boolean – true or false int – for use of integers – cuts off any decimal approximations (no rounding! 5.9 = 5) double – for use of decimals char – unicode character set- ASCII code If you had char x = 46, what does System.out.println(x) do?
Unicode values When declaring and initializing a char type variable, use single quotations: char initial = ‘a’; Fact! unicode values are 16 bit To see all unicode values, copy the following: for(x=0; x<26; x++) { for(y=0; y<10; y++) //will do values from 0 to 260 { System.out.printf(" %4d = %4c", 10*x+y, 10*x+y); } System.out.println(); }
Casting Suppose you had the following: double hour = 3.5; int miles = 20; Check to see if the following compiles: double mph = miles/hour; Int and double are apples and oranges! The fix a cast double mph = (double)miles/hour;
Arithmetic Expressions Arithmetic expressions You should know the following arithmetic operators: +,-,*,/ An important one you need to know: modulus - % modulus will give you the remainder of a quotient Ex) 10%3 will give you 1
Arithmetic Expressions You can use modulus as an effective even or odd evaluator If (x%2==0) if (x is even) if( x%2!=0) if(x is odd) The modulus of a number is never larger than it’s divisor i.e. (255%16 = 15)
Compound Assignment When doing arithmetic operations to update a variable, it is recommended to use compound assignment Take x = x +y; x+=y; You can do this for any operator
Pre/Post Increment It is also recommended to use pre and post increment/decrement operators Post increment: operator applied after the expression is used x++ (x= x +1) & x-- (x = x-1) Pre increment: operator applied before ++x (x=x+1) If y = 0, x =3, what is the difference between: y = x++ and y = ++x ? Use a System.out.println(y) to check Can you explain it?
Strings Strings are not primitive data types; they are objects derived from the String class We call them “literals” Ex): String str = “karel”; the variable str references the String “karel” Concatenation: String str = “karel” + “J”; System.out.println(str); karelJ
Escape sequences These are specific character combinations in a String that the compiler recognizes ex) \n newline (check page 131 for the rest) System.out.println(“Hi, how \n are you”); What does that look like?
Converting to a string For primitive values: Just concatenate an empty string! Ex) int amount = 15; System.out.println(“” + amount); Or String amount = “” + amount;
Work Page 143 – finish the Pie Chart program! Exercises: 1,2,4,5,6,7,10,12,13,18,19