Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Java - Understanding class defintions
Description: Java - Understanding class definitions. Notes taken from the programming foundations class lecture, based on lectures from Objects first Java -A practical introduction using BlueJ.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Understanding Class Definitions
Looking at the Java source code inside classes
...

Part 1: BlueJ Chapter 2, Sections 2
...
9

Key concepts to be covered:






fields
constructors
methods
parameters
assignment statements
Objects First with Java - A Practical Introduction using BlueJ, © David J
...

• How is that price determined?

– How is ‘money’ entered into a machine?
– How does a machine keep track of the money that is
entered?

• Internal View – looking inside at the code allows
us to see how that behaviour is implemented in
Java
...
Barnes, Michael Kölling

2

Basic Class Structure
public class TicketMachine
{
Inner part of the class omitted
...
Barnes, Michael Kölling

3

Fields
• Fields store values for
an object
...

• Use the Inspect option to
view an object’s fields
...

visibility modifier

public class TicketMachine
{
private int price;
private int balance;
private int total;
Further details omitted
...
Barnes, Michael Kölling

4

Constructors
• Constructors initialize
an object
...

• They store initial values
into the fields
...


public TicketMachine(int ticketCost)
{
price = ticketCost;
balance = 0;
total = 0;
}

The TicketMachine constructor method
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

6

Assignment
• Values are stored into fields (and other
variables) via assignment statements:
e
...


variable = expression;
price = ticketCost;

• Evaluates the expression on the right-handside and stores the result in the variable on the
left-hand-side
...

7
Objects First with Java - A Practical Introduction using BlueJ, © David J
...

• Accessors provide information about an
object
...

• The header defines the method’s signature
e
...

public int getPrice()
• The body encloses the method’s statements
...
Barnes, Michael Kölling

8

Accessor Methods
visibility modifier

return
type

method name

public int getPrice()
{
return price;
}

parameter
list (empty)
return statement

start and end of method body
(block)
Objects First with Java - A Practical Introduction using BlueJ, © David J
...

• Used to mutate (i
...
, change) an object’s
state
...

– Typically contain assignment statements
...

Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

11

Printing from Methods
Use ‘println’ to print a line
of output

public void printTicket()
{
// Simulate the printing of a ticket
...
out
...
out
...
out
...
out
...
");
System
...
println("##################");
System
...
println();

}

// Update the total collected with the balance
...

// is a ‘comment’
balance = 0;
For humans to read, not Java
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

13

Understanding Class Definitions
Looking at the Java source code inside classes
...

Part 2: BlueJ Chapter 2, Sections 2
...
15

Key concepts to be covered:
• conditional statements
• local variables

Objects First with Java - A Practical Introduction using BlueJ, © David J
...

– No refunds
...


• How can we do better?
– We need more sophisticated behavior
...
Barnes, Michael Kölling

15

Making Choices
public void insertMoney(int amount)
{
if(amount > 0) {
balance = balance + amount;
}
else {
System
...
println("Use a positive amount: "
+ amount);

}
}

Objects First with Java - A Practical Introduction using BlueJ, © David J
...
Barnes, Michael Kölling

17

Alternative Form of If-Statement
‘if’ keyword

boolean condition to be tested
actions if condition is true

if( perform some test ) {
Do these statements if the test gave a true result
}

Note: no ‘else’ keyword and no actions if condition false
...
)
Objects First with Java - A Practical Introduction using BlueJ, © David J
...
g
...

==
!=
<
<=
>
>=

is equal to
is NOT equal to
is less than
is less than or equal to
is greater than
is greater than or equal to

Not for comparing Strings (or objects)
...
Barnes, Michael Kölling

19

Local Variables
• Fields are one kind of variable
...

– They are accessible throughout the class
...

– They exist only as long as the method is
being executed
...

– Called ‘local variables’
...
Barnes, Michael Kölling

20

Local Variables
No visibility
modifier

A local variable

public int refundBalance()
{
int amountToRefund;
amountToRefund = balance;
balance = 0;
return amountToRefund;
}
Local variable only ‘lives’ while method is being executed
...

Objects First with Java - A Practical Introduction using BlueJ, © David J
...
g int
int a;

0
a

Rather than assume initialises to 0, better to explicitly initialise:

int a = 0;

b = a;
a = 20;

0

a

b

14

0
b

14

14

a

a = 14;

0

a

int b;

b

20
a

14
b

Objects First with Java - A Practical Introduction using BlueJ, © David J
...

• Fields store values that determine an object’s
state
...

• Methods implement the behavior of objects
...

• Fields persist for the lifetime of an object
...
Barnes, Michael Kölling

23

Chapter 2 Review
• Parameters are used to receive values into a
constructor or method
...

• Objects can make decisions using conditional
statements – ‘if statement’
...


Objects First with Java - A Practical Introduction using BlueJ, © David J
Title: Java - Understanding class defintions
Description: Java - Understanding class definitions. Notes taken from the programming foundations class lecture, based on lectures from Objects first Java -A practical introduction using BlueJ.