Download presentation
Presentation is loading. Please wait.
Published byNeil Walton Modified over 9 years ago
1
Java Methods
2
Topics Declaring fields vs. local variables Primitive data types Strings Compound Assignment Conversions from one value to another Arithmetic operators
3
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);
4
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
5
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!
6
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
7
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?
8
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(); }
9
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;
10
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
11
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)
12
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
13
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?
14
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
15
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?
16
Converting to a string For primitive values: Just concatenate an empty string! Ex) int amount = 15; System.out.println(“” + amount); Or String amount = “” + amount;
17
Work Page 143 – finish the Pie Chart program! Exercises: 1,2,4,5,6,7,10,12,13,18,19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.