+ All Categories
Home > Documents > Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple...

Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple...

Date post: 19-Jan-2016
Category:
Upload: madeline-stewart
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
58
Week 2 Definitions Assignment Expressions Input
Transcript
Page 1: Week 2 Definitions Assignment Expressions Input. Variables Usually when problem solving multiple things need determining Without the ability to remember.

Week 2DefinitionsAssignmentExpressions

Input

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

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

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

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

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

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

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

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

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

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

nbrRabbits 2

generation 1

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

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 );

• DisplayRabbits: 2Generation: 1

nbrRabbits 2

generation 1

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

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

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

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;

nbrRabbits 2

generation 1

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

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

nbrRabbits 2

generation 1

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

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

nbrRabbits 2

generation 1

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

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

nbrRabbits 4

generation 1

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

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

nbrRabbits 4

generation 1

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

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

nbrRabbits 4

generation 2

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

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

nbrRabbits 8

generation 2

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

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

nbrRabbits 8

generation 2

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

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

nbrRabbits 8

generation 2

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

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

nbrRabbits 8

generation 3

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

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;

nbrRabbits 8

generation 3

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

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 fornbrRabbits = nbrRabbits * 2;

nbrRabbits 8

generation 3

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

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

nbrRabbits 8

generation 3

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

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

nbrRabbits 16

generation 3

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

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 forgeneration = generation + 1;

nbrRabbits 16

generation 3

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

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

nbrRabbits 16

generation 3

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

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

nbrRabbits 16

generation 4

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

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

nbrRabbits 16

generation 4

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

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

c 100

f

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

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

c 100

f

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

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

c 100

f

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

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

c 100

f

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

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

c 100

f

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

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

c 100

f

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

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

c 100

f

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

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 c 132

f

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

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

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

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;

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

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

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

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 statementimport 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;

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

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

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

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 )

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

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: " );

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

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

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

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();

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

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

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

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

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

Consider MP3Sizerimport 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" );

}}

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

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 );

...

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

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 calculated0.00471 * nbrSongs

OrGIGABYTES_PER_SONG * nbrSongs

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Consider MP3Sizerimport 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" );}

}


Recommended