Docsity

Java Programming: Types, Methods, and Conditionals, Slides of Java Programming

Partial preview of the text

Download Java Programming: Types, Methods, and Conditionals and more Slides Java Programming in PDF only on Docsity!

More types, Methods,

Conditionals

Docsity.com

Outline

�^

Lecture 1 Review

�^

More types

�^

Methods

�^

Conditionals

Docsity.com

Variables

Named location that stores a value Example:

String a = “a”;String b = “letter b”;a = “letter a”;String c = a + “ and “ + b;

Docsity.com

Operators

Symbols that perform simple computations Assignment: =Addition: +Subtraction:Multiplication: *Division: /

Docsity.com

finalPosition

= finalPosition +

initialVelocity * fallingTime;

finalPosition

= finalPosition + initialPosition;

OR

finalPosition

= initialVelocity * fallingTime;

finalPosition

= initialPosition;

Docsity.com

Questions from last lecture?

Docsity.com

Division

Division (“/”) operates differently on

integers and on doubles!

Example:

double a = 5.0/2.0;

a

int b = 4/2;

b

int c = 5/2;

// c = 2

double d = 5/2;

d

Docsity.com

Order of Operations

Precedence like math, left to rightRight hand side of = evaluated firstParenthesis increase precedence double x = 3 / 2 + 1;

// x = 2.

double y = 3 / (2 + 1); // y = 1.

Docsity.com

Conversion by casting

int

a = 2;

// a = 2

double a = 2;

// a = 2.0 (Implicit)

int

a = 18.7;

// ERROR

int

a = (int)18.7;

// a = 18

double a = 2/3;

// a = 0.

double a = (double)2/3;

// a = 0.6666…

Docsity.com

Outline

�^

Lecture 1 Review

�^

More types

�^

Methods

�^

Conditionals

Docsity.com

Adding Methods

public static void

NAME

STATEMENTS

} To call a method: NAME();

Docsity.com

class

NewLine

public static void

newLine

System

. out . println

} public static void

threeLines

newLine

newLine

newLine

} public static void

main

( String

[]

arguments

System

. out . println

( "Line 1"

threeLines

System

. out . println

( "Line 2"

Docsity.com

public static void

main

( String

[]

arguments

System

. out . println

( "Line 1"

threeLines

System

. out . println

( "Line 2"

public static void

threeLines

newLine

newLine

newLine

class

NewLine

public static void

newLine

System

. out . println

(^

Docsity.com

Parameters

public static void

NAME

( TYPE NAME

STATEMENTS

} To call: NAME(

EXPRESSION

Docsity.com