Docsity

Better Ticket Machine, Conditional Statements, Local Variables | CSIS 110, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 – Lecture 5
Quiz – next Wednesday. Material in Chapters 1 and 2.
Review Labs
- Comments!
- Use the method names that I specify
- Whitespace and indenting
- I will be deducting points for these problems.
Last Time
We spent more time looking at the ticket machine, and discussed the following concepts:
- Assignment statements
otype rules – where else does this rule apply?
- accessor and mutator methods
- return values and the return statement
- printing
- String concatenation
Better Ticket Machine
Let’s go back and think about this ticket machine – it has lots of problems:
- Issues a ticket regardless of the amount of money you have entered
- Doesn’t give change
- Doesn’t check to see if the values entered are sensible – what happens if you enter
a negative amount?
- Doesn’t check to see if the ticket price passed to the constructor is sensible
Load the project better-ticket-machine, and play around with the methods. How is this
version better than the one we were working with?
- It doesn’t print a ticket unless you have inserted enough money.
- It prints an error message if you pass 0 or a negative number to insertMoney.
- It has a method refundBalance
Conditional Statements
How does this version work?
Important thing to notice – some of these methods do different things depending upon the
state of the TicketMachine object or the value of the actual parameter. The methods that
we have seen until now have always done the same thing every time they were invoked.
Let’s look at the source code: the class wrapper, instance variables (fields), constructor,
and accessor methods are the same as in the naïve-ticket-machine.
pf3
pf4

Partial preview of the text

Download Better Ticket Machine, Conditional Statements, Local Variables | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 5

Quiz – next Wednesday. Material in Chapters 1 and 2. Review Labs

  • Comments!
  • Use the method names that I specify
  • Whitespace and indenting
  • I will be deducting points for these problems. Last Time We spent more time looking at the ticket machine, and discussed the following concepts:
  • Assignment statements o type rules – where else does this rule apply?
  • accessor and mutator methods
  • return values and the return statement
  • printing
  • String concatenation Better Ticket Machine Let’s go back and think about this ticket machine – it has lots of problems:
  • Issues a ticket regardless of the amount of money you have entered
  • Doesn’t give change
  • Doesn’t check to see if the values entered are sensible – what happens if you enter a negative amount?
  • Doesn’t check to see if the ticket price passed to the constructor is sensible Load the project better-ticket-machine, and play around with the methods. How is this version better than the one we were working with?
  • It doesn’t print a ticket unless you have inserted enough money.
  • It prints an error message if you pass 0 or a negative number to insertMoney.
  • It has a method refundBalance Conditional Statements How does this version work? Important thing to notice – some of these methods do different things depending upon the state of the TicketMachine object or the value of the actual parameter. The methods that we have seen until now have always done the same thing every time they were invoked. Let’s look at the source code: the class wrapper, instance variables (fields), constructor, and accessor methods are the same as in the naïve-ticket-machine.

Look at insertMoney method – there is a new statement here. if ( amount > 0 ) { balance = balance + amount; } else { System.out.println(“Use a positive amount”); } What does this set of statements do? It compares the value of amount with 0, and executes one of two sets of statements, based on whether the result of the comparison was true or false. Important – only one of the sets will ever be executed – never both. In this case, if amount > 0 then the balance is modified. Otherwise (if amount <= 0 ) an error message is printed. This So, this statement makes sure that the user has passed a reasonable value to the method before modifying the balance in the machine. This statement is called a conditional statement or an if statement. General form: if ( test that evaluates to true or false ) { Statements to execute when expression is true } else { Statements to execute when expression is false } The test in a conditional statement is an example of a boolean expression. Boolean expressions can have only one of two values: true, false. In the above example, either amount was greater than 0 (true) or it was not (false). A conditional statement uses these two values to make a choice about what statements to execute. The statements in each part are enclosed in a block – depending upon the value of the expression, either every statement in the block will be executed, or none will. boolean is also a type – in one of the earlier projects we saw a boolean instance variable – does anyone remember what it was? (isVisible in shapes project). Did it make sense to use a boolean for that instance variable?

We want to return the balance, but also change its value to 0. We need to save the balance in a temporary location, set the balance to 0, and then return the value we put in the temporary place. Look at the refundBalance method. It creates a new variable (amountToRefund) that it uses to store the balance, then sets the balance to 0, then returns the amountToRefund. What kind of variable is amountToRefund?

  • it’s not a class variable – those are declared in the class body, not inside a method
  • it’s not a formal parameter – those are declared in the method header (inside the parentheses).
  • It’s called a local variable – it is local to (known within) the method only. Local variables are declared with a type and a name – there is no private or public. Local variables are similar to formal parameters, in that there lifetime is the same as the lifetime of the method invocation. They are created when the method is invoked, and destroyed when the method completes. We’ve seen 3 types of variables now (What are they?)
  • local variables (LV), fields, formal parameters (FP)
  • All 3 kinds store values of a specified type.
  • Fields are defined at the class level (outside of constructors and methods)
  • Fields are used to store data that has the same lifetime as the object that contains them – they maintain the current state of the object.
  • Fields are accessable in the entire class in which they are declared.
  • FPs and LVs have a lifetime equal to the lifetime of a single method call.
  • FPs are defined in the method or constructor header (the signature)
  • FPs are only accessable in the method in which they are defined.
  • LVs are defined inside of a method.
  • LVs must be defined before they are used.
  • LVs must be assigned a value before they are used.
  • LVs are only accessable in the block in which they are declared. These rules define the scope of the variables. The scope of a variable is the set of statements where the variable may be used. Lab Exercise (IN PAIRS) Do Exercises 2.64 through 2.69 from the book (pages 47 – 49)