Presentation is loading. Please wait.

Presentation is loading. Please wait.

Looking inside classes Fields, Constructors & Methods Week 3.

Similar presentations


Presentation on theme: "Looking inside classes Fields, Constructors & Methods Week 3."— Presentation transcript:

1 Looking inside classes Fields, Constructors & Methods Week 3

2 Fields Constructors Methods Parameters Printing Java Class Definitions CONCEPTS COVERED

3 Exploring the behavior of a ticket machine. Use the naive-ticket-machine project. Machines supply tickets of a fixed price. –How is that price determined? How is money entered into a machine? How does a machine keep track of the money that is entered? Java Class Definitions TICKET MACHINES – AN EXTERNAL VIEW

4 BlueJ Demonstration A first look at the naive-ticket-machine project

5 TicketMachine int price int balance int total int getPrice() int getBalance() void insertMoney(int amount) void printTicket()

6 Interacting with an object gives us clues about its behavior. Looking inside the objects class definition (source code) allows us to determine how that behavior is provided or implemented. All Java classes have a similar-looking internal view/structure. Java Class Definitions SOURCE CODE – AN INTERNAL VIEW

7 Java Class Definitions BASIC STRUCTURE OF A JAVA CLASS public class ClassName { Fields Constructors Methods } public class TicketMachine { Inner part of the class omitted. }

8 Java Class Definitions FIELDS Fields store values for an object. They are also known as instance variables, and/or attributes. Use Inspect in BlueJ to view an objects fields. The values held in an objects fields constitute the state of that object.

9 Java Class Definitions STRUCTURE OF FIELDS IN A JAVA CLASS public class TicketMachine { private int price; private int balance; private int total; Constructor and methods omitted. } private int price; visibility modifier data type variable name

10 Java Class Definitions CONSTRUCTORS Constructors initialize an object They have the same name as their class They do not have a return type They can store initial values into the fields They often receive external parameter values that are used to set initial field values

11 Methods The Java language is built from many classes, each with its own inbuilt methods. Much of programming in Java is about using inbuilt methods as well as being able to create your own methods. The method header or signature describes the methods: –accessibility modifiers, public or private, –whether or not it returns a value and if so what type, –the name of the method, starting with a lowercase letter, –input needed (formal parameters) for the method to fulfill its task.

12 Encapsulation Normally we make fields that hold the objects data private While the methods that can access those fields are made public This way we provide controlled access to the data, thus protecting it This is called encapsulation

13 Java Class Definitions STRUCTURE OF A CLASS CONSTRUCTOR public class TicketMachine { private int price; private int balance; private int total; public TicketMachine(int ticketCost) { price = ticketCost; balance = 0; total = 0; }

14 Methods implement the behavior of objects. Accessors provide information about an object. Methods have a structure consisting of a header and a body. Method header defines the methods access modifier, return type and signature. public int getPrice() Method body encloses a methods statements. Java Class Definitions ACCESSOR METHODS

15 Java Class Definitions ACCESSOR METHODS public int getPrice() { return price; } return type method name parameter list (empty) start and end of method body (block) return statement visibility modifier

16 Mutators have a standard method structure of method header and method body. Mutators are used to mutate (i.e. change) an objects state. Mutators achieve this through changing the value of one or more object fields (attributes). –Typically contain assignment statements. –Typically receive parameters. Java Class Definitions MUTATOR METHODS

17 Java Class Definitions MUTATOR METHODS public void insertMoney(int amount) { balance = balance + amount; } return type ( void ) method name parameter visibility modifier assignment statement field being changed start and end of method body (block)

18 Java Class Definitions PASSING DATA VIA PARAMETERS

19 Java Class Definitions PRINTING FROM METHODS public void printTicket() { // Simulate the printing of a ticket. System.out.println("##################"); System.out.println("# The BlueJ Line"); System.out.println("# Ticket"); System.out.println("# " + price + " cents."); System.out.println("##################"); System.out.println(); // Update the total collected with the balance. total = total + balance; // Clear the balance. balance = 0; }

20 Required Reading Objects First With Java – A Practical Introduction using BlueJ Related reading for this lecture Chapter 2 pp (Constructors & Methods) Reading for next weeks lecture Chapter 2 pp (Choices)


Download ppt "Looking inside classes Fields, Constructors & Methods Week 3."

Similar presentations


Ads by Google