introduction to variables assigning the value of one variable to another the right-hand side of an...

25
Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions using Variables Incrementing Values in Variables Accumulating in Variables • Exercises 415.111 Monday 12 th May Java Lecture 4 Overview Explorer’s Guide Chapter 3.

Upload: maria-farmer

Post on 19-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• Introduction to variables

• Assigning the Value of One Variable to Another

• The Right-hand Side of an Assignment Statement Can be an Expression

• Expressions using Variables

• Incrementing Values in Variables

• Accumulating in Variables

• Exercises

415.111 Monday 12th MayJava Lecture 4 Overview

415.111 Monday 12th MayJava Lecture 4 Overview

Explorer’s Guide Chapter 3.

Page 2: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• A place in the computer’s memory

– With a unique name

• It is not the same thing as a variable in algebra or a variable in the social sciences.

• Whenever you see “variable”, think “storage box”.

What is a variable?What is a variable?

Page 3: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

What we can do with variables?

• First we can take a box and give it a name:

• Then we can put a value in the box:

• We can change the value (contents) inside the box:

message

Hi

H i

Bye

message

message

Page 4: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• Declaring is the action of naming a variable and saying what type of data is to be stored

• Examples:– String message;– String mountain;– int height;

• All variables must be declared before they are used

Declaring variablesDeclaring variables

Page 5: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• A variable name must begin with a letter, and then may contain any series of numbers or letters.

• There must be no spaces between the characters of the name.

• It DOES matter whether uppercase or lowercase letters are used. E.g. message is not the same as Message.

• By convention: the first word starts with a small letter and all succeeding words in the name with capital letters. E.g. myMessage.

Naming variablesNaming variables

Page 6: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• message = "Success!";

• mountain = "Everest";

• height = 8848;

Assigning a Value to a VariableAssigning a Value to a Variable

Page 7: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

Displaying the Value of a VariableE. G. Chapter 3 p56

Displaying the Value of a VariableE. G. Chapter 3 p56

String message;String mountain;int height;

message = "Success!";mountain = "Everest";height = 8848;

System.out.println(); System.out.println(message);

System.out.println(mountain); System.out.println("Height: " + height);

message = "Goodbye"; System.out.println(message);

Page 8: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• If we have: int height; and height = “8848”;

• We get:

Incorrect assignmentIncorrect assignment

Page 9: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• Meaningful variable names make programs easier to read, easier to change, and easier to debug.

Although the meaning of a variable name means nothing to the computer we must use meaningful variable names

Although the meaning of a variable name means nothing to the computer we must use meaningful variable names

// A badly written program segment

System.out.println();System.out.println(a);System.out.println(b);System.out.println("Height: " + c);

a = "Goodbye";System.out.println(a);

Page 10: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

Exercise 1 E.G. p58 What is the output?

Exercise 1 E.G. p58 What is the output?

String sort; int four, two;

sort = "chance";four = 4;two = 2;

System.out.println();System.out.println("You only get a few " + sort + "s.");System.out.println("So make the most of it.");System.out.println("What are you waiting " + four + "?");System.out.println("Tomorrow may be " + two + " late.");

Page 11: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• We can assign the result of an expression to a variable.

– height = 20 * 3;

• First the expression is evaluated, – then its value is assigned to the variable on the left-hand side

of the assignment operator.

Right-hand side can be an Expression E.G. 61Right-hand side can be an Expression E.G. 61

Page 12: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• We can assign the result of an expression involving integer variables to an integer variable

• days = weeks * 7;• totalDays= nbrYears *365;

Expressions using Variables E. G p61Expressions using Variables E. G p61

Page 13: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

// Assigning expressions which contain variables E. G. p61

String mountain;int weeks, days, hours

// Assign valuesmountain = "MT COOK";weeks = 3;

// Evaluate expressions and assign to variablesdays = weeks * 7;hours = days * 24;

// Output resultsSystem.out.println();System.out.println(mountain);System.out.println("Weeks: " + weeks);System.out.println("Days: " + days);System.out.println("Hours: " + hours);

Page 14: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

// Assigning expressions which contain variables E. G. p61

String mountain;int weeks, days, hours

// Assign valuesmountain = "MT COOK";weeks = 3;

// Evaluate expressions and assign to variablesdays = weeks * 7;hours = days * 24;

// Output resultsSystem.out.println(); ~~~System.out.println(mountain); MT

COOKSystem.out.println("Weeks: " + weeks);

Weeks: 3System.out.println("Days: " + days); Days:

21System.out.println("Hours: " + hours);

Hours: 504

Page 15: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

Exercise 3 E. G. p62

What is the output of the following program segment?

// Find the area of a tent

// Declare the variablesint length, width, area;

// Assign values to the variableslength = 3;width = 4;

// Assign the result of the expression to a new variablearea = length * width;

// Display the resultSystem.out.println(); System.out.println("Area of tent = " + area + " square metres");

Page 16: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

Exercise 3 E. G. p62

What is the output of the following program segment?

// Find the area of a tent

// Declare the variablesint length, width, area;

// Assign values to the variableslength = 3;width = 4;

// Assign the result of the expression to a new variablearea = length * width;

// Display the resultSystem.out.println(); System.out.println("Area of tent = " + area + " square metres");

Area of tent = 12 square metres

Page 17: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

Incrementing Values in Variables E.G. p66Incrementing Values in Variables E.G. p66

0

0 1+

counter

1

Page 18: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• Giving a variable a value at the beginning of a program(counter = 0;)

(counter = counter + 1;) ----->

• ----------------------------------------------------(counter = 5;) (counter = counter + 1;) ------->

• Java will warn you if you haven’t initialised a variable

Initialisation of Variables E.G. p 66Initialisation of Variables E.G. p 66

Page 19: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

// Count to five the slow way E.G. p65

int counter; counter = 0;

// increment the value in counter counter = counter + 1;

System.out.println(counter); 1

counter = counter + 1;System.out.println(counter);

2

counter = counter + 1;System.out.println(counter);

3

counter = counter + 1; 4

System.out.println(counter); counter = counter + 1;

5System.out.println(counter);

0Counter

Page 20: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

Accumulating in Variables E.G. p71Accumulating in Variables E.G. p71

total

105+

215 +

17

Page 21: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

E.G. p70/*

Program to demonstrate that we canaccumulate values in a variable

*/

int total;

// Initialise to 10 total = 10;

// Accumulate total = total + 5; total = total + 2;

// Display the resultSystem.out.println();System.out.println("Total = " + total);

Page 22: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

E.G. p70/*

Program to demonstrate that we canaccumulate values in a variable

*/

int total;

// Initialise to 10 total = 10;

// Accumulate total = total + 5; total = total + 2;

// Display the resultSystem.out.println(); ~~~System.out.println("Total = " + total); Total = 17

Page 23: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• Work through, and completely understand, every line of every one of the demo programs in Chapter 3.

• Complete as many of the Chapter 3 exercises as you can.

Home work (before your lab)Home work (before your lab)

Page 24: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• Check:

• (i) That both sides of every assignment statement are of the same type:

– both String,– or both int.

• (ii) Check that all variables that need to be initialised are initialised correctly.

Checking Your ProgramChecking Your Program

Page 25: Introduction to variables Assigning the Value of One Variable to Another The Right-hand Side of an Assignment Statement Can be an Expression Expressions

• All of our programs, except in Lab08, will use variables.

• Always use meaningful variable names.

• Read Explorer’s Guide Chapter 3

• Run and modify the Chapter 3 Sample programs.

Conclusion of the Intro to VariablesConclusion of the Intro to Variables