java methods. topics declaring fields vs. local variables primitive data types strings compound...

17
CHAPTER 6 Java Methods

Upload: neil-walton

Post on 30-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

CHAPTER 6Java Methods

Page 2: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

Topics

Declaring fields vs. local variables Primitive data types Strings Compound Assignment Conversions from one value to

another Arithmetic operators

Page 3: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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

Page 4: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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

Page 5: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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!

Page 6: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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

Page 7: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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?

Page 8: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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(); }

Page 9: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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;

Page 10: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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

Page 11: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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)

Page 12: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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

Page 13: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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?

Page 14: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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

Page 15: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

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?

Page 16: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

Converting to a string

For primitive values: Just concatenate an empty string! Ex) int amount = 15; System.out.println(“” + amount); Or String amount = “” + amount;

Page 17: Java Methods. Topics  Declaring fields vs. local variables  Primitive data types  Strings  Compound Assignment  Conversions from one value to another

Work

Page 143 – finish the Pie Chart program!

Exercises: 1,2,4,5,6,7,10,12,13,18,19