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

An outline for a lecture on java programming, covering various types such as boolean, int, double, and string, as well as variables, operators, methods, and conditionals. It includes examples and explanations of concepts like division, order of operations, and conversion by casting.

Typology: Slides

2012/2013

Uploaded on 04/22/2013

sathaye
sathaye 🇮🇳

4.8

(8)

102 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
2: More types, Methods,
Conditionals
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

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