



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Announcements: Read sections 2.5 to 2.9. Discuss how to read/prepare for this class: read assignment, come to lecture, review the reading Turn in Kindergarten Homework Discuss the problem solving homework
What is source code? What is a method signature? Return values Close the picture project and open the lab classes project. This is a simple student database to keep track of students enrolled in lab classes. Comment: The CD from the book includes all the projects that we have looked at so far. It also includes copies of BlueJ and Java. [Create a student. Discuss how I have to provide some parameters when I create the instance] What do I need to do to see what methods can be invoked on a Student object? Invoke the getName method on one of the Students – what happens? A value comes back from this method – when it is done, it returns a result. This is called a return value. What is the signature of this method? String getName() There word String in front of the method name identifies the return type. In this case, it says the when the method is done, it returns a result whose type is String. Look at the method list for a Student object again – we can see several methods that return Strings and one that returns an int. Look at the signature of the print method: void print() The word void indicates that this method does not return a value. We’ve already seen lots of methods like this. So, we can have methods that change the state of an object (e.g., change the color of a square), or get information about the state of an object (e.g., get a student’s name). With UP:
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. Inside the outer wrapper we define the fields , constructors , and methods that give instances of the class their behavior.
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. REMINDERS