Understanding Java Classes: A Deep Dive into the TicketMachine Class in CSIS 110 - Prof. B, Study notes of Javascript programming

An in-depth exploration of the ticketmachine class in csis 110, focusing on its structure, fields, constructors, and methods. Students will learn about the role of constructors in initializing objects and the difference between formal and actual parameters. This document also includes exercises to reinforce the concepts covered.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-s6c
koofers-user-s6c 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 3
Open BlueJ, Open my picture project.
Look at the various Picture classes developed by students and discuss them.
Last time we learned about
- two types of comments: single-line and multi-line
- the state of an object
- source code
- return values and return types
- using objects as parameters
[Ask some questions about the above]
Ticket Machines
Let’s think about a ticket machine that might exist in a subway or light rail station.
Tickets cost a certain amount of money, and you can press a button to get a ticket.
Load the ‘naïve-ticket-machine’ project into BlueJ. Remember to do a ‘save as’ to your
M: drive.
Now, create a ticket machine object. Look at the list of methods. Play around with this
for a few minutes to figure out what it does.
How does this ticket machine object differ from what you would expect a real-world
ticket machine to behave?
By playing with this, we’ve figured out that this Java ticket machine only behaves the
way we would expect it to when we enter the exact amount of money.
Parts of a class
The outer wrapper:
public class TicketMachine
{
...
}
The outer wrappers for most classes look similar – the main purpose is to give the class a
name and to enclose the class elements.
The inner parts
Inside the outer wrapper we define the fields, constructors, and methods that give
instances of the class their behavior.
pf3
pf4

Partial preview of the text

Download Understanding Java Classes: A Deep Dive into the TicketMachine Class in CSIS 110 - Prof. B and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 3

Open BlueJ, Open my picture project. Look at the various Picture classes developed by students and discuss them. Last time we learned about

  • two types of comments: single-line and multi-line
  • the state of an object
  • source code
  • return values and return types
  • using objects as parameters [Ask some questions about the above] Ticket Machines Let’s think about a ticket machine that might exist in a subway or light rail station. Tickets cost a certain amount of money, and you can press a button to get a ticket. Load the ‘naïve-ticket-machine’ project into BlueJ. Remember to do a ‘save as’ to your M: drive. Now, create a ticket machine object. Look at the list of methods. Play around with this for a few minutes to figure out what it does. How does this ticket machine object differ from what you would expect a real-world ticket machine to behave? By playing with this, we’ve figured out that this Java ticket machine only behaves the way we would expect it to when we enter the exact amount of money. Parts of a class The outer wrapper: public class TicketMachine { ... } The outer wrappers for most classes look similar – the main purpose is to give the class a name and to enclose the class elements. The inner parts Inside the outer wrapper we define the fields , constructors , and methods that give instances of the class their behavior.
  • Fields: store data for each object. These are the attributes, and their values as a set make up the object’s state.
  • Constructors: special methods that set up (initialize) an object when it is created
  • Methods: implement the object’s behavior These elements can be in any order in the class – however, I prefer (and the book also does): public class TicketMachine { Fields Constructors Methods } You don’t have to follow this style, but whatever you do: Be Consistent, and don’t mix them up. So, what are the fields, constructors, and methods in the TicketMachine class? Is there anything about the constructor that makes it different from the other methods in the class? Fields This class has three fields (or attributes): price, balance, and total. The fields are defined by declaration statements , such as: private int price; This declaration says that price is a private field – for now, we are going to declare all of our fields private, and defer what that means for a while. This declaration also says that price is an int – that means it can store a single int value. In Java, fields are also called instance variables. Is the order of the words in the declaration statement important? Is the semicolon at the end important? How would I declare an instance variable named numTickets? Notice that the fields are also described by comments – remember, the comments are for human readers only. Constructor

Some terminology:

  • Formal Parameter: The name of the parameter, as defined in the method/constructor signature
  • Actual Parameter: The value that is passed to a method, and used during its execution REMINDERS
  • Continue reading Chapter 2
  • Turn in Homework!