Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple things need determining Without the ability to remember.

Similar presentations


Presentation on theme: "Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple things need determining Without the ability to remember."— Presentation transcript:

1 Week 2 Definitions Assignment Expressions Input

2 Variables Usually when problem solving multiple things need determining Without the ability to remember past calculations, a program would be one giant unbelievably complex and tortured statement Programs can remember the values of calculations using variables

3 Variables When a programmer wants a variable, the programmer typically specify three things with the latter two things separated by an = – The type of value to be determined – The name to be associated with the value – The value to be remembered Together the three things are called a definition In reaction to a variable definition, the computer sets aside some of its memory for the variable While the program runs, the variable has exclusive use of that memory

4 Consider RabbitsRabbitsRabbits The program defines two variables int nbrRabbits = 2; int generation = 1; When executed, the definitions cause two variables to come into existence. The definitions specify – The names of the variables are nbrRabbits and generation – The variables will only hold int values – The variables are initialized to 2 and 1 respectively

5 RabbitsRabbitsRabbits The program defines two variables int nbrRabbits = 2; int generation = 1; I recommend when you analyze one of my examples or when you develop your problem solutions that you draw and label memory boxes to visually understand what is happening – Please believe me taking this advice is invaluable and will save you hours and hours over the semester when doing the homework and help you be speedier when taking tests

6 RabbitsRabbitsRabbits The program defines two variables int nbrRabbits = 2; int generation = 1; I recommend when you analyze one of my examples or when you develop your problem solutions that you draw and label memory boxes to visually understand what is happening – Please believe me taking this advice is invaluable and will save you hours and hours over the semester when doing the homework and help you be speedier when taking tests nbrRabbits2 generation1

7 RabbitsRabbitsRabbits We had the program displays the variables, and saw that they indeed respectively hold a 2 and a 1 int nbrRabbits = 2; int generation = 1;... System.out.println( "Rabbits: " + nbrRabbits ); System.out.println( "Generation: " + generation ); Display Rabbits: 2 Generation: 1 nbrRabbits2 generation1

8 Assignment When problem solving we sometimes determine that the value being stored by a variable needs to be updated The vari in variable was a clue that updates were possible Java allows variable values to be updated through assignment statements. Once updated the previous value is gone, gone, gone The basic assignment statement has two elements separated by an = – The variable to be updated – The value it is to store after the update We call the = an assignment statement the assignment operator

9 RabbitsRabbitsRabbits One of the things we tried with RabbitsRabbitsRabbits was some assignment statements int nbrRabbits = 2; int generation = 1;... nbrRabbits = nbrRabbits * 2; generation = generation + 1; nbrRabbits2 generation1

10 RabbitsRabbitsRabbits One of the things we tried with RabbitsRabbitsRabbits was some assignment statements int nbrRabbits = 2; int generation = 1;... nbrRabbits = nbrRabbits * 2; generation = generation + 1; The execution of an assignment statement begins with an evaluation of the expression to the right of the = The value of the expression nbrRabbits * 2 is the current value of nbrRabbits times 2. A look in the memory box for nbrRabbits shows that it is 2, so the value of the expression is the value of 2 * 2, which is 4 nbrRabbits2 generation1

11 RabbitsRabbitsRabbits One of the things we tried with RabbitsRabbitsRabbits was some assignment statements int nbrRabbits = 2; int generation = 1;... nbrRabbits = nbrRabbits * 2; generation = generation + 1; Because the value of the expression nbrRabbits * 2 is 4, the assignment updates the computer memory for nbrRabbits to 4 To keep our memory box for nbrRabbits current, we replace the 2 with a 4 nbrRabbits2 generation1

12 RabbitsRabbitsRabbits One of the things we tried with RabbitsRabbitsRabbits was some assignment statements int nbrRabbits = 2; int generation = 1;... nbrRabbits = nbrRabbits * 2; generation = generation + 1; Because the value of the expression nbrRabbits * 2 is 4, the assignment updates the computer memory for nbrRabbits to 4 To keep our memory box for nbrRabbits current, we replace the 2 with a 4 nbrRabbits4 generation1

13 RabbitsRabbitsRabbits One of the things we tried with RabbitsRabbitsRabbits was some assignment statements int nbrRabbits = 2; int generation = 1;... nbrRabbits = nbrRabbits * 2; generation = generation + 1; The execution of the next assignment statement is analogous – Determine the value of the expression generation + 1 – Update the computer memory for generation with that value Because the value of generation is currently 1, the value of the expression generation + 1 is 1 + 1, which is 2. So generation is updated to 2 nbrRabbits4 generation1

14 RabbitsRabbitsRabbits One of the things we tried with RabbitsRabbitsRabbits was some assignment statements int nbrRabbits = 2; int generation = 1;... nbrRabbits = nbrRabbits * 2; generation = generation + 1; The execution of the next assignment statement is analogous – Determine the value of the expression generation + 1 – Update the computer memory for generation with that value Because the value of generation is currently 1, the value of the expression generation + 1 is 1 + 1, which is 2. So generation is updated to 2 nbrRabbits4 generation2

15 RabbitsRabbitsRabbits Another thing we tried with RabbitsRabbitsRabbits were some other assignment statement forms int nbrRabbits = 2; int generation = 1;... ++generation; When the ++generation statement is reached the values of nbrRabbits and generation are respectively 8 and 2 nbrRabbits8 generation2

16 RabbitsRabbitsRabbits Another thing we tried with RabbitsRabbitsRabbits were some other assignment statement forms int nbrRabbits = 2; int generation = 1;... ++generation; The ++ operator is shorthand for increment the value of the variable by 1 You will see that adding 1 to a variable is a common programming task, so this shorthand is much welcomed nbrRabbits8 generation2

17 RabbitsRabbitsRabbits Another thing we tried with RabbitsRabbitsRabbits were some other assignment statement forms int nbrRabbits = 2; int generation = 1;... ++generation; When the ++generation statement is executed, the current value of generation is 2. So the new value of generation is 2 + 1, which is 3 nbrRabbits8 generation2

18 RabbitsRabbitsRabbits Another thing we tried with RabbitsRabbitsRabbits were some other assignment statement forms int nbrRabbits = 2; int generation = 1;... ++generation; When the ++generation statement is executed, the current value of generation is 2. So the new value of generation is 2 + 1, which is 3 nbrRabbits8 generation3

19 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; nbrRabbits8 generation3

20 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; The way to think about *= operator usage is that its shorthand for nbrRabbits = nbrRabbits * 2; nbrRabbits8 generation3

21 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; When nbrRabbits *= 2 is executed, the current value of nbrRabbits is 8 so the value assigned to nbrRabbits is 8 * 2, which is 16 nbrRabbits8 generation3

22 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; When nbrRabbits *= 2 is executed, the current value of nbrRabbits is 8 so the value assigned to nbrRabbits is 8 * 2, which is 16 nbrRabbits16 generation3

23 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; The way to think about += operator usage is that its shorthand for generation = generation + 1; nbrRabbits16 generation3

24 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; When generation += 1 is executed, the current value of generation is 3 so the value assigned to generation is 3 + 1, which is 4 nbrRabbits16 generation3

25 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; When generation += 1 is executed, the current value of generation is 3 so the value assigned to generation is 3 + 1, which is 4 nbrRabbits16 generation4

26 RabbitsRabbitsRabbits We also tried some other assignment statements forms in RabbitsRabbitsRabbits. The new ones below are called compound assignment int nbrRabbits = 2; int generation = 1;... nbrRabbits *= 2; generation += 1; When generation += 1 is executed, the current value of generation is 3 so the value assigned to generation is 3 + 1, which is 4 In practice, when incrementing a variable by 1, the ++ assignment is preferred nbrRabbits16 generation4

27 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; However, this statement does not do what we intend Suppose the current value of variable c is 100. When the definition of f begins, our memory box diagram looks like below – memory has been set aside for f but it is not yet initialized c100 f

28 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; The value to which initialize f is the value of the expression 32 + 9 / 5 * c To evaluate that expression we need to know how Java handles an expression with multiple operators. In programming parlance we need to know the operator precedence rules c100 f

29 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; The Java operator precedence rules for numerical evaluation are what you expect – multiplication and division operations are equally important and are done before addition and subtraction operations with addition and subtraction being equally important c100 f

30 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; The Java operator precedence rules for numerical evaluation are what you expect – multiplication and division operations are equally important and are done before addition and subtraction operations with addition and subtraction being equally important The Java operator precedence rules also specify what to do when there are multiple operators of the same precedence – do them left to right c100 f

31 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; So to evaluate the expression we begin by doing the division. We take the result of that division and multiply it by c. The value of that quantity is added to 32. That value is used to initialize f c100 f

32 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; So to evaluate the expression we begin by doing the division. We take the result of that division and multiply it by c. The value of that quantity is added to 32. That value is used to initialize f So we first need to determine the value of 9 / 5 c100 f

33 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; So to evaluate the expression we begin by doing the division. We take the result of that division and multiply it by c. The value of that quantity is added to 32. That value is used to initialize f So we first need to determine the value of 9 / 5 As 9 and 5 are both int values, int division is performed c100 f

34 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; So to evaluate the expression we begin by doing the division. We take the result of that division and multiply it by c. The value of that quantity is added to 32. That value is used to initialize f So we first need to determine the value of 9 / 5 As 9 and 5 are both int values, int division is performed Because 5 goes into 9 one time, the value of 9 / 5 is 1 The 1 is then multiplied by c. which is 100. Because 1 * 100 is 100, the overall value is the value of 32 + 100, which is 132. But the Fahrenheit equivalent is 212 c132 f

35 Consider WhatsUp This program was intended to convert a Celsius value to a Fahrenheit value. It contained the statement int f = 32 + 9 / 5 * c; Generally when you have a mixture of multiplications and divisions, refactor the expression so the multiplications are done first and then the divisions. Your answer will probably be more accurate Program CelsiusToFahrenheit does this refactoring

36 Consider CelsiusToFahrenheit This program does correctly convert a Celsius value to a Fahrenheit value. Its conversion statement is int f = 32 + ( ( 9 * c ) / 5 ); The 9 * c is calculated before the division is performed The program also use parentheses to make the evaluation explicit I think this form with parentheses gives a warmer feeling of correctness and understandability then the also correct int f = 32 + 9 * c / 5;

37 Interactive programs Most problem solving utilizes user input to determine the answer to a particular problem of interest For example program CelsiusToFahrenheit will determine the Fahrenheit equivalent to whatever Celsius temperature we are interested in The easiest way to get and use user input in Java is to make use of its Scanner feature Scanner is a type of Java object Scanner was not part of the original language Java requires that you “ask” for Scanner capabilities to be made available To ask for extra Java capabilities beyond what is normally supplied you need to start your program with import statements. import is is a Java keyword

38 Import statement An import statement tells Java to make available extra features Java has hundreds of libraries that specify additional ways to represent and manipulate values One of those libraries is the util library Scanner is part of the util library. So are many other things we will find important to get a hold of To make all of what the util library has, start your program with the statement import java.util.*; The asterisk is like a “wild card”. It means give it all If you feel the need to be more restrictive start your program with import java.util.Scanner;

39 Scanner basics For a Scanner object to be useful it needs an input source from which it gets values Just like Java has System.out for display interaction, it has System.in to get access to what you enter with your keyboard It may seem strange but it’s true that when your program starts, there are no Scanner objects waiting around to be used

40 Scanner construction You must tell Java to construct a new one for each of your programs requiring input The below expression says produce a new Scanner that gets its input from the keyboard new Scanner( System.in ) To remember that Scanner we need to make it part of a definition The below statement says define a new Scanner variable named reader. When we make use of that variable you will be interacting with the keyboard supplies Scanner reader = new Scanner( System.in )

41 Reconsider CelsiusToFahrenheit Program CelsiusToFahrenheit can convert whatever temperature a user is interested in because of its Scanner usage Method main() begins with a Scanner variable definition Scanner reader = new Scanner( System.in ); It next prompts the user to supply a value. Prompts are helpful because otherwise the program user would not know what to supply Prompts are generally done with System.out.print() statements. A print() is like a println() except it does not move the cursor to the next line System.out.print( "Enter Celsius temperature: " );

42 CelsiusToFahrenheit A Java Scanner has many, many capabilities for detecting and getting input What we are currently interested in is getting numeric data To have a Scanner produce (return) the next input in int form use its method nextInt() To have a Scanner return the next input in double form use its method nextDouble() Program CelsiusToFahrenheit wants to get an int input. To do that it performs a reader.nextInt() int c = reader.nextInt(); The above statement says to define an int variable c and initialize it with the int value produced by having scanner reader return the next input entered by the user in int form

43 Consider PiggyBank Program PiggyBank was another program that used its input to figure out the worth of a pocketful of change Scanner keyboard = new Scanner( System.in ); System.out.print( "Number of quarters, dimes, " + " nickels, & pennies: " ); int quarters = keyboard.nextInt(); int dimes = keyboard.nextInt(); int nickels = keyboard.nextInt(); int pennies = keyboard.nextInt();

44 Consider PiggyBank Program PiggyBank was another program that used its input to figure out the worth of a pocketful of change Scanner keyboard = new Scanner( System.in ); System.out.print( "Number of quarters, dimes, " + " nickels, & pennies: " ); int quarters = keyboard.nextInt(); int dimes = keyboard.nextInt(); int nickels = keyboard.nextInt(); int pennies = keyboard.nextInt(); One of the things this program shows us is that we can name our Scanner as we please

45 Consider PiggyBank Program PiggyBank was another program that used its input to figure out the worth of a pocketful of change Scanner keyboard = new Scanner( System.in ); System.out.print( "Number of quarters, dimes, " + " nickels, & pennies: " ); int quarters = keyboard.nextInt(); int dimes = keyboard.nextInt(); int nickels = keyboard.nextInt(); int pennies = keyboard.nextInt(); Another thing the program shows us is that once we have a Scanner we can use it repeatedly to get more input

46 Consider MP3Sizer import java.util.*; public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) { Scanner stdin = new Scanner( System.in ); System.out.print( "Enter number of songs: " ); int nbrSongs = stdin.nextInt(); double gigaBytes = GIGABYTES_PER_SONG * nbrSongs; int storage = (int) ( gigaBytes ); System.out.println( ”Storage necessary for " + nbrSongs + " songs is: ” GBs" ); }

47 MP3Sizer There are three things to point out regarding this program – The definition and subsequent use of constant GIGA_BYTES_PER_SONG, and the (int) cast operator to convert a double value to an int value public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) {... double gigaBytes = GIGABYTES_PER_SONG * nbrSongs; int storage = (int) ( gigaBytes );...

48 MP3Sizer Programmers use constants to make code more readable We saw constants before when we used Math.PI and Math.E For a program like MP3Sizer what tells you more about what is being calculated 0.00471 * nbrSongs Or GIGABYTES_PER_SONG * nbrSongs

49 MP3Sizer We will define our constants immediately before the start of method main() public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) {... You should remember that the public static at the start of method main() told you two things of how Java treats the method – public: other Java components can make use of the method – static: the method is performing a service. I usually call such methods as a function or library method. It is more commonly called a class method. A non-static method describes an object behavior. Such methods are called message methods. We will talk about that a lot more message methods later in the course

50 MP3Sizer We will define our constants immediately before the start of method main() public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) {... The keywords public static at the start of the definition of GIGA_BYTES_PER_SONG also tells us two things – public: other Java components can make use of it – static: it is not an object attribute – it’s just a named value

51 MP3Sizer We will define our constants immediately before the start of method main() public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) { The keyword final in the definition of tells us that the value of GIGA_BYTES_PER_SONG is not allowed to be changed by the program; that is, it is a constant The keyword double in the definition tells us that the value of the constant is decimal

52 MP3Sizer We will define our constants immediately before the start of method main() public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) { It is also worth noting that we named the constant GIGA_BYTES_PER_SONG per song rather than gigaBytesPerSong. It is a programming convention (i.e., its not a rule of Java) to name constants using upper case letters and to use underscores (_) to make the individual words stand out

53 MP3Sizer With the GIGA_BYTES_PER_SONG in effect the definition the assignment to gigaBytes later in the program tell makes a lot of sense double gigaBytes = GIGABYTES_PER_SONG * nbrSongs; When we examine the statement, the capital letters in GIGA_BYTES_PER_SONG is an indicator where to find its definition – at the start of the program Because GIGA_BYTES_PER_SONG represents a ratio in storage units per song and nbrSongs is a count, the product represents the amount of storage needed for that many songs

54 MP3Sizer When you tell a person the temperature you would not say it’s 23.257 degrees. Instead you would say it’s 23 degrees The.257 would be considered superfluous by most people. The accuracy of the additional.257 is not something we can distinguish If you were a witch or wizard and wanted to turn someone into a newt you would cast a spell to make the change to the person Java supplies the cast operator (int) to get an int value from a decimal value The cast operator does not round it just ignores the fractional amount to get the int value

55 MP3Sizer The use of the (int) cast operator is in the definition of int variable storage int storage = (int) ( gigaBytes ); The parentheses around int is a Java requirement to make the cast. The parentheses around the value to be converted is an extremely important programming practice

56 MP3Sizer Some might be tempted to get rid of the definition of gigaBytes and directly assign storage by int storage = (int) GIGA_BYTES_PER_SONG * nbrSongs; This definition does not do what you want. The rules of operator precedence makes the cast operator more important than the multiplication, so the cast gets done first Using parentheses to show how Java evaluates the above statement we get int storage = ( (int) GIGA_BYTES_PER_SONG ) * nbrSongs; This cast evaluates to 0 and because 0 times anything is 0, storage is initialize to 0 The moral to this example is be explicit when casting

57

58 Consider MP3Sizer import java.util.*; public class MP3Sizer { public static final double GIGABYTES_PER_SONG = 0.00471; public static void main( String[] args ) { Scanner stdin = new Scanner( System.in ); System.out.print( "Enter number of songs: " ); int nbrSongs = stdin.nextInt(); double gigaBytes = GIGABYTES_PER_SONG * nbrSongs; int storage = (int) ( gigaBytes ); System.out.println( ”Storage necessary for " + nbrSongs + " songs is: ” GBs" ); }


Download ppt "Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple things need determining Without the ability to remember."

Similar presentations


Ads by Google