41
Outline Java programming Variables and constants Arithmetic Reflection CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic Deborah Thomas Bethel University CS4HS: Session 2

CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

CS4HS: Session 2

I Basic Java program

I PrintingI Variables

I Creation and assignmentI Variable names

I Arithmetic

Deborah Thomas Bethel University

CS4HS: Session 2

Page 2: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Creating a program: Pick a name that is descriptiveI All programs: End with .java extensionI Example: Example1.java

I First line:

public class Example1

I The class name MUST match the file name

I For now: Body of program will go within a set of curlybrackets

I Indent that “block” one tab in from the curly bracketI Special function: main

I main function header:

public static void main(String [] args)I Body of main function: Enclosed in curly brackets tabbed one

over

Deborah Thomas Bethel University

CS4HS: Session 2

Page 3: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Creating a program: Pick a name that is descriptiveI All programs: End with .java extensionI Example: Example1.java

I First line:

public class Example1

I The class name MUST match the file name

I For now: Body of program will go within a set of curlybrackets

I Indent that “block” one tab in from the curly bracketI Special function: main

I main function header:

public static void main(String [] args)I Body of main function: Enclosed in curly brackets tabbed one

over

Deborah Thomas Bethel University

CS4HS: Session 2

Page 4: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Creating a program: Pick a name that is descriptiveI All programs: End with .java extensionI Example: Example1.java

I First line:

public class Example1

I The class name MUST match the file name

I For now: Body of program will go within a set of curlybrackets

I Indent that “block” one tab in from the curly bracket

I Special function: mainI main function header:

public static void main(String [] args)I Body of main function: Enclosed in curly brackets tabbed one

over

Deborah Thomas Bethel University

CS4HS: Session 2

Page 5: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Creating a program: Pick a name that is descriptiveI All programs: End with .java extensionI Example: Example1.java

I First line:

public class Example1

I The class name MUST match the file name

I For now: Body of program will go within a set of curlybrackets

I Indent that “block” one tab in from the curly bracketI Special function: main

I main function header:

public static void main(String [] args)I Body of main function: Enclosed in curly brackets tabbed one

over

Deborah Thomas Bethel University

CS4HS: Session 2

Page 6: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Output in Java

I Output: What you will see on the screen

I Statement:

System.out.println("String you want to print");

I println: Forces a new line character at the end (print does notinsert a new line)

I print: Does not start a new lineI Double quotes: ”String” that will be displayed on screenI Semicolon: All terminating statements end with a semicolon

Deborah Thomas Bethel University

CS4HS: Session 2

Page 7: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Output in Java

I Output: What you will see on the screen

I Statement:

System.out.println("String you want to print");

I println: Forces a new line character at the end (print does notinsert a new line)

I print: Does not start a new lineI Double quotes: ”String” that will be displayed on screenI Semicolon: All terminating statements end with a semicolon

Deborah Thomas Bethel University

CS4HS: Session 2

Page 8: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Output in Java

I Output: What you will see on the screen

I Statement:

System.out.println("String you want to print");

I println: Forces a new line character at the end (print does notinsert a new line)

I print: Does not start a new line

I Double quotes: ”String” that will be displayed on screenI Semicolon: All terminating statements end with a semicolon

Deborah Thomas Bethel University

CS4HS: Session 2

Page 9: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Output in Java

I Output: What you will see on the screen

I Statement:

System.out.println("String you want to print");

I println: Forces a new line character at the end (print does notinsert a new line)

I print: Does not start a new lineI Double quotes: ”String” that will be displayed on screen

I Semicolon: All terminating statements end with a semicolon

Deborah Thomas Bethel University

CS4HS: Session 2

Page 10: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Output in Java

I Output: What you will see on the screen

I Statement:

System.out.println("String you want to print");

I println: Forces a new line character at the end (print does notinsert a new line)

I print: Does not start a new lineI Double quotes: ”String” that will be displayed on screenI Semicolon: All terminating statements end with a semicolon

Deborah Thomas Bethel University

CS4HS: Session 2

Page 11: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Comments

I Java statements: Not complete sentencesI Sometimes a block of code must be described

I Human readable statements: CommentsI Ignored by compiler: No need to maintain syntaxI Line comment: //

I Everything after two forward slashes ignored

I Block comment: Comment starts with /* and ends with */I Multiline comment

I Good programming practiceI Comment at the top with author, date and descriptionI Comment in blocks of code explaining what you are doing

Deborah Thomas Bethel University

CS4HS: Session 2

Page 12: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Comments

I Java statements: Not complete sentencesI Sometimes a block of code must be described

I Human readable statements: CommentsI Ignored by compiler: No need to maintain syntaxI Line comment: //

I Everything after two forward slashes ignored

I Block comment: Comment starts with /* and ends with */I Multiline comment

I Good programming practiceI Comment at the top with author, date and descriptionI Comment in blocks of code explaining what you are doing

Deborah Thomas Bethel University

CS4HS: Session 2

Page 13: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Comments

I Java statements: Not complete sentencesI Sometimes a block of code must be described

I Human readable statements: CommentsI Ignored by compiler: No need to maintain syntaxI Line comment: //

I Everything after two forward slashes ignored

I Block comment: Comment starts with /* and ends with */I Multiline comment

I Good programming practiceI Comment at the top with author, date and descriptionI Comment in blocks of code explaining what you are doing

Deborah Thomas Bethel University

CS4HS: Session 2

Page 14: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Syntax errors

I Some syntax errorsI Not including the semicolonI Calling the class and file different names: Case mattersI Missing a curly bracket

I Compiler will report error: Provides line and approximatelocation of error and type of error

I After every change: Recompile before running

I Some things to try:I Change class nameI Remove semicolon at end of print statementI Change the spelling of a word

Deborah Thomas Bethel University

CS4HS: Session 2

Page 15: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Syntax errors

I Some syntax errorsI Not including the semicolonI Calling the class and file different names: Case mattersI Missing a curly bracket

I Compiler will report error: Provides line and approximatelocation of error and type of error

I After every change: Recompile before running

I Some things to try:I Change class nameI Remove semicolon at end of print statementI Change the spelling of a word

Deborah Thomas Bethel University

CS4HS: Session 2

Page 16: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Syntax errors

I Some syntax errorsI Not including the semicolonI Calling the class and file different names: Case mattersI Missing a curly bracket

I Compiler will report error: Provides line and approximatelocation of error and type of error

I After every change: Recompile before running

I Some things to try:I Change class nameI Remove semicolon at end of print statementI Change the spelling of a word

Deborah Thomas Bethel University

CS4HS: Session 2

Page 17: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Important construct in any programming language

I Variable: Container that stores value used in programI Can contain numbers or string dataI “Variable”: Value can change over time but contains exactly

one value at a given point in time

I Variables: Have an associated typeI Examples: Integer, floating point number, character, true/false

valueI Type affects calculations: Dividing an integer by another

returns integer

Deborah Thomas Bethel University

CS4HS: Session 2

Page 18: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Important construct in any programming languageI Variable: Container that stores value used in program

I Can contain numbers or string dataI “Variable”: Value can change over time but contains exactly

one value at a given point in time

I Variables: Have an associated typeI Examples: Integer, floating point number, character, true/false

valueI Type affects calculations: Dividing an integer by another

returns integer

Deborah Thomas Bethel University

CS4HS: Session 2

Page 19: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Introduction

I Important construct in any programming languageI Variable: Container that stores value used in program

I Can contain numbers or string dataI “Variable”: Value can change over time but contains exactly

one value at a given point in time

I Variables: Have an associated typeI Examples: Integer, floating point number, character, true/false

valueI Type affects calculations: Dividing an integer by another

returns integer

Deborah Thomas Bethel University

CS4HS: Session 2

Page 20: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Creating variables

I Variable declaration: Compiler needs to know that a variableof a particular type is needed

I Compiler: Allocates memory for the variable that will store thevalue

I Name: How we refer to the memory location

I Syntax:

type name;I Some examples

I int: Whole numbersI double: Floating point numbersI char: Single characterI String: Set of characters

Deborah Thomas Bethel University

CS4HS: Session 2

Page 21: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Creating variables

I Variable declaration: Compiler needs to know that a variableof a particular type is needed

I Compiler: Allocates memory for the variable that will store thevalue

I Name: How we refer to the memory location

I Syntax:

type name;

I Some examplesI int: Whole numbersI double: Floating point numbersI char: Single characterI String: Set of characters

Deborah Thomas Bethel University

CS4HS: Session 2

Page 22: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Creating variables

I Variable declaration: Compiler needs to know that a variableof a particular type is needed

I Compiler: Allocates memory for the variable that will store thevalue

I Name: How we refer to the memory location

I Syntax:

type name;I Some examples

I int: Whole numbersI double: Floating point numbersI char: Single characterI String: Set of characters

Deborah Thomas Bethel University

CS4HS: Session 2

Page 23: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Variable assignment

I Declaration: Compiler sets aside memory for it

I “Variable assignment”: Assigning a value to the variableI Example:

int x = 5;

I Syntax of variable assignment

type name = value;

I Value: Can be another variable, a value or an expressioninvolving other variables

I Type of value: Must match type of variableI Variable being assigned on left of equal, value on right

I What ”=” does not mean: Left hand side is equal to righthand side

I It is set equal to right hand side at that time

Deborah Thomas Bethel University

CS4HS: Session 2

Page 24: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Variable assignment

I Declaration: Compiler sets aside memory for itI “Variable assignment”: Assigning a value to the variable

I Example:

int x = 5;

I Syntax of variable assignment

type name = value;

I Value: Can be another variable, a value or an expressioninvolving other variables

I Type of value: Must match type of variableI Variable being assigned on left of equal, value on right

I What ”=” does not mean: Left hand side is equal to righthand side

I It is set equal to right hand side at that time

Deborah Thomas Bethel University

CS4HS: Session 2

Page 25: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Variable assignment

I Declaration: Compiler sets aside memory for itI “Variable assignment”: Assigning a value to the variable

I Example:

int x = 5;

I Syntax of variable assignment

type name = value;

I Value: Can be another variable, a value or an expressioninvolving other variables

I Type of value: Must match type of variableI Variable being assigned on left of equal, value on right

I What ”=” does not mean: Left hand side is equal to righthand side

I It is set equal to right hand side at that time

Deborah Thomas Bethel University

CS4HS: Session 2

Page 26: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Variable assignment

I Declaration: Compiler sets aside memory for itI “Variable assignment”: Assigning a value to the variable

I Example:

int x = 5;

I Syntax of variable assignment

type name = value;

I Value: Can be another variable, a value or an expressioninvolving other variables

I Type of value: Must match type of variableI Variable being assigned on left of equal, value on right

I What ”=” does not mean: Left hand side is equal to righthand side

I It is set equal to right hand side at that time

Deborah Thomas Bethel University

CS4HS: Session 2

Page 27: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Picking the name

I Every variable has a name: Identifier

I Rules for variablesI Sequence of characters: Letters, digits, underscores, dollar

signsI No spaces, other punctuationI Cannot start with a digitI Cannot be a reserved wordI Case sensitive

I Guidelines for picking namesI Use descriptive namesI camelCase: First word is all lower case, capitalize first letter of

subsequent words

Deborah Thomas Bethel University

CS4HS: Session 2

Page 28: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Picking the name

I Every variable has a name: IdentifierI Rules for variables

I Sequence of characters: Letters, digits, underscores, dollarsigns

I No spaces, other punctuationI Cannot start with a digitI Cannot be a reserved wordI Case sensitive

I Guidelines for picking namesI Use descriptive namesI camelCase: First word is all lower case, capitalize first letter of

subsequent words

Deborah Thomas Bethel University

CS4HS: Session 2

Page 29: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Picking the name

I Every variable has a name: IdentifierI Rules for variables

I Sequence of characters: Letters, digits, underscores, dollarsigns

I No spaces, other punctuationI Cannot start with a digitI Cannot be a reserved wordI Case sensitive

I Guidelines for picking namesI Use descriptive namesI camelCase: First word is all lower case, capitalize first letter of

subsequent words

Deborah Thomas Bethel University

CS4HS: Session 2

Page 30: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Arithmetic

I Binary operations: Two variablesI Operations: Addition, subtraction, multiplication, division,

modulusI Symbol for multiplication: *

I Modulus operator: Returns the remainder of an operationI Symbol: %I Example: 14 % 3 = 2, 19%4 = 3, 9%2 = 1

I Use of modulus operator: Determining if one number is afactor of another

I If x is a factor of y , what is the remainder of dividing y by x?

Deborah Thomas Bethel University

CS4HS: Session 2

Page 31: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Arithmetic

I Binary operations: Two variablesI Operations: Addition, subtraction, multiplication, division,

modulusI Symbol for multiplication: *

I Modulus operator: Returns the remainder of an operationI Symbol: %I Example: 14 % 3 = 2, 19%4 = 3, 9%2 = 1

I Use of modulus operator: Determining if one number is afactor of another

I If x is a factor of y , what is the remainder of dividing y by x?

Deborah Thomas Bethel University

CS4HS: Session 2

Page 32: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Arithmetic

I Binary operations: Two variablesI Operations: Addition, subtraction, multiplication, division,

modulusI Symbol for multiplication: *

I Modulus operator: Returns the remainder of an operationI Symbol: %I Example: 14 % 3 = 2, 19%4 = 3, 9%2 = 1

I Use of modulus operator: Determining if one number is afactor of another

I If x is a factor of y , what is the remainder of dividing y by x?

Deborah Thomas Bethel University

CS4HS: Session 2

Page 33: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

More arithmetic

I Updating:

x = x + 5;

I Valid operation: Works right to leftI Step 1: Evaluation of right hand sideI Step 2: Assignment to variable on left hand side

I Power of operator: Use

Math.pow(x, y)

Deborah Thomas Bethel University

CS4HS: Session 2

Page 34: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

More arithmetic

I Updating:

x = x + 5;

I Valid operation: Works right to leftI Step 1: Evaluation of right hand sideI Step 2: Assignment to variable on left hand side

I Power of operator: Use

Math.pow(x, y)

Deborah Thomas Bethel University

CS4HS: Session 2

Page 35: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Programming and Math

I SimilaritiesI “Variable”: Container that holds a single valueI “Function”: Transforms an input value

I main method is our function

I DifferencesI Assignment operator: Not universal truth, but rather

assignmentI If x changes later, y hasn’t change unless function is called

again

Deborah Thomas Bethel University

CS4HS: Session 2

Page 36: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Programming and Math

I SimilaritiesI “Variable”: Container that holds a single valueI “Function”: Transforms an input value

I main method is our function

I DifferencesI Assignment operator: Not universal truth, but rather

assignmentI If x changes later, y hasn’t change unless function is called

again

Deborah Thomas Bethel University

CS4HS: Session 2

Page 37: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

More on expressions

I Variable datatype: Affects result of operationI Example: 4/3 = 1, 5/8 = 0I However: 5.0/8 = 0.625

I CAREFUL: Results not rounded but truncated

I Mixed expressions: Result temporarily converted to highestprecision variable

I Operator precedence: Following mathematical precedence

Deborah Thomas Bethel University

CS4HS: Session 2

Page 38: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

More on expressions

I Variable datatype: Affects result of operationI Example: 4/3 = 1, 5/8 = 0I However: 5.0/8 = 0.625

I CAREFUL: Results not rounded but truncated

I Mixed expressions: Result temporarily converted to highestprecision variable

I Operator precedence: Following mathematical precedence

Deborah Thomas Bethel University

CS4HS: Session 2

Page 39: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

More on expressions

I Variable datatype: Affects result of operationI Example: 4/3 = 1, 5/8 = 0I However: 5.0/8 = 0.625

I CAREFUL: Results not rounded but truncated

I Mixed expressions: Result temporarily converted to highestprecision variable

I Operator precedence: Following mathematical precedence

Deborah Thomas Bethel University

CS4HS: Session 2

Page 40: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

More on expressions

I Variable datatype: Affects result of operationI Example: 4/3 = 1, 5/8 = 0I However: 5.0/8 = 0.625

I CAREFUL: Results not rounded but truncated

I Mixed expressions: Result temporarily converted to highestprecision variable

I Operator precedence: Following mathematical precedence

Deborah Thomas Bethel University

CS4HS: Session 2

Page 41: CS4HS: Session 2 - mathcs.bethel.eduthodeb/sessions/Session2.pdf · CS4HS: Session 2 I Basic Java program I Printing I Variables I Creation and assignment I Variable names I Arithmetic

Outline Java programming Variables and constants Arithmetic Reflection

Reflection

I Work through questions 1 through 4 in the workbookI For the problems you cam up with in Session 1, answer:

I What are the variables you would need and what are theirtypes?

I What are the expressions you would use?

I What examples can you come up with that uses the modulusoperator?

Deborah Thomas Bethel University

CS4HS: Session 2