introduction to variables assigning the value of one variable to another the right-hand side of an...
TRANSCRIPT
• 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.
• 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?
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
• 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
• 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
• message = "Success!";
• mountain = "Everest";
• height = 8848;
Assigning a Value to a VariableAssigning a Value to a Variable
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);
• If we have: int height; and height = “8848”;
• We get:
Incorrect assignmentIncorrect assignment
• 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);
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.");
• 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
• 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
// 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);
// 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
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");
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
Incrementing Values in Variables E.G. p66Incrementing Values in Variables E.G. p66
0
0 1+
counter
1
• 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
// 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
Accumulating in Variables E.G. p71Accumulating in Variables E.G. p71
total
105+
215 +
17
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);
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
• 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)
• 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
• 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