Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lecture Notes on Methods, Mutator Methods, Printing | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-lji-1
koofers-user-lji-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 4
Last Time
We started looking at a ticket machine, and started looking at the parts.
- class definition (outer wrapper)
- instance variables/fields
- declaration statements
- constructor
- parameters
oformal
oactual
Assignment
As we saw, we need to store the short-lived value of the parameter into a field so we can
remember it later.
This is done with an assignment statement. For example,
price = ticketCost;
There are three parts in an assignment statement:
1) The left hand side
2) The right hand side
3) An assignment operator – in this case ‘=’
The right-hand side is an expression. Java evaluates the right-hand side and stores the
result in the variable on left-hand side.
In this case, the expression is just a single variable, so Java copies the value from it and
puts it into price. (In general, the expression can be more complex).
An important rule: The type of the expression must match the type of the variable. What
types do we know about?
So, this means that we CAN NOT assign a String to an int, or an int to a boolean.
The same rule applies for formal and actual parameters – their types must match.
Even though the assignment statement uses the ‘=’, don’t confuse this with mathematical
equality.
Examples:
[Have students take a minute or two to work these out themselves (in pairs)]
pf3
pf4

Partial preview of the text

Download Lecture Notes on Methods, Mutator Methods, Printing | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 4

Last Time We started looking at a ticket machine, and started looking at the parts.

  • class definition (outer wrapper)
  • instance variables/fields
  • declaration statements
  • constructor
  • parameters o formal o actual Assignment As we saw, we need to store the short-lived value of the parameter into a field so we can remember it later. This is done with an assignment statement. For example, price = ticketCost; There are three parts in an assignment statement:
  1. The left hand side
  2. The right hand side
  3. An assignment operator – in this case ‘=’ The right-hand side is an expression. Java evaluates the right-hand side and stores the result in the variable on left-hand side. In this case, the expression is just a single variable, so Java copies the value from it and puts it into price. (In general, the expression can be more complex). An important rule: The type of the expression must match the type of the variable. What types do we know about? So, this means that we CAN NOT assign a String to an int, or an int to a boolean. The same rule applies for formal and actual parameters – their types must match. Even though the assignment statement uses the ‘=’, don’t confuse this with mathematical equality. Examples: [Have students take a minute or two to work these out themselves (in pairs)]

Say we have a class named Counter that has one field: int count. What does the constructor look like? Say we have a class named Animal that has two fields: String variety, and int numLegs. What does the constructor look like? Methods We’ve seen a bunch of methods already. Some of them returned information about the state of the object – others changed the state of the object. Let’s look at the getPrice method (in class TicketMachine). Two parts:

  • method header: comment plus signature. What is one thing that distinguishes a method signature from a instance variable declaration? The (). Also, there is NO ‘;’ at the end of the method signature.
  • method body: sequence of statements enclosed in {}. The method body contains the declarations and statements that define what happens to the object when the method is called Definition: A sequence of statements enclosed in {} is called a block. So, the class body and each method body are blocks. Style – note that the lines inside of a block are indented. This increases readability for people – the computer doesn’t care. Let’s look in more detail at the signature of getPrice(): public int getPrice() This says that when the getPrice method is called, it returns a result when it is done. The type of the return value is an int. Now, let’s look at the body of getPrice(). What does it contain? This is a simple method – its body contains only one statement – return price; In general, methods can have many statements. The return statement has the form: return ; Java evaluates the expression, and that value is returned as a result from the method. When a return statement is executed, the method terminates. If you have a statement immediately after a return statement, it is unreachable:

The first 6 statements print the ticket. They call a special method provided by Java: System.out.println. This method takes the contents of its parameter, and displays it in a text window. The first 3 lines just print a String (followed by a newline – that is, the cursor is positioned at the beginning of the next line after the String is displayed.) The next line is more interesting: System.out.println(“# “ + price + “ cents.”); The ‘+’ operators construct a single String from the parts. When used with a String and anything else (an int, another String, etc.), the + operator generates a new String with the parts concatenated. The last println() doesn’t have a parameter – what does it do? Exercise (IN PAIRS)

  1. Add a showPrice method to the TicketMachine. This method should have a void return type and take no parameters. It should print a message: Tickets cost xxx cents where xxx should be replaced by the value in the price field.
  2. Create a new method named empty that simulates removing all money from the machine. It should set the total field to 0, and should not return any value. (What is its return type?)
  3. Create a new method setPrice that changes the price of a ticket to a new value (which is passed as a parameter). This method should not return any value. What do you need to do to see if your methods work correctly?