-
Motivation
-
Have seen predefined objects and classes (System.out, String, PrintStream)
-
These are not enough to model all the possible programs
-
c.f., BasicDataReader to make input easier than predefined
-
c.f., Trying to write a program to control airspace around GSP
-
not surprisingly, Java allows us to define our own classes and associated
methods
-
Class definitions
-
Usually, it is necessary to have a methodical process to define classes
-
this will be done later. For now, look at these simple classes
class Student {
public Student () {}
public void ask () {
System.out.println ("I don't understand");
}
}
-
Note:
-
void - method returns nothing
-
public - method is public - for all to see!
-
After defining this class, we can use it:
Student barney = new Student ();
barney.ask ();


-
Parameters
-
Suppose we want the sender of the ask message to ask about a particular
topic
-
provide the topic as an argument to a method...
-
this is additional information, which we are passing as a string
-
to establish this additional information in the method, we declare a parameter,
which acts like a variable declaration
-
the parameter is accessible inside the method only
public void ask (String topic) {
String question = "I don't understand ".concat (topic);
System.out.println (question);
}
-
This method would be placed in the class definition, N.B. overloading
-
Instance variables
-
Suppose that, when a student is created, we wish to define the preamble
for the string, rather than "I don't understand"
Student barney = new Student ("Please, could you explain ");
barney.ask ();
barney.ask ("cereal");
would display
Please could you explain.
Please, could you explain cereal.
There are a number of changes required to the class:
-
The constructor now takes a string, so its signature must contain a String
parameter
-
An instance variable defined in the class
-
ask needs to have access to the
string passed into the constructor, but:
-
Methods cannot access variables in other methods (they are not in the same
context)
-
We need to define it in the context of the whole class, and this is called
an instance variable
-
An instance variable belongs to the entire object, and can be accessed
by all methods
-
For an instance variable, the declaration is the same as a reference variable,
except for the keyword private
added to the beginning.
private String defaultPreamble;
-
Using a class definition
-
How do we use something that is not a predefined class?
-
create a file, Student.java, that contains the class definition
-
don't forget import java.io.*
import java.io.*;
class Student {
public Student (String preamble) {
defaultPreamble = preamble;
}
public Student () {
defaultPreamble = "Excuse me, but can you please explain."
}
public void ask () {
System.out.println (defaultPreamble);
}
public void ask (String topic) {
String question = defaultPreamble.concat (topic);
System.out.println (question);
}
private String defaultPreamble;
}
-
Compile Student.java and, if no errors, Student.class is created.
-
We can now write programs
import java.io.*;
class Professor {
public static void main (String [] arg) {
BasicDataReader input = new BasicDataReader ();
Student bazza = new Student ("Please explain ");
String topic;
System.out.println ("I am listening to the lecture...");
System.out.print ("Please enter a topic:");
topic = input.readLine ();
bazza.ask (topic);
bazza.ask ();
Student boris = new Student ("Yo, describe ");
System.out.println ("I am listening to the lecture...");
System.out.print ("Please enter a topic:");
topic = input.readLine ();
boris.ask (topic);
boris.ask ();
}
}
-
Code
import cucs.*;
class Professor {
public static void main (String [] arg) {
BasicDataReader input = new BasicDataReader ();
BasicDataWriter output = new BasicDataWriter ();
Student bazza = new Student ("Please explain ");
String topic;
output.writeln ("I am listening to the lecture...");
output.write ("Please enter a topic:");
topic = input.readLine ();
bazza.ask (top ic);
bazza.ask ();
Student boris = new Student ("Yo, describe ");
output.writeln ("I am listening to the lecture...");
output.write ("Please enter a topic:");
topic = input.readLine ();
boris.ask (topic);
boris.ask ();
}
}
import cucs.*;
class Student {
public Student (String preamble) {
defaultPreamble = preamble;
}
public void ask () {
System.out.println (defaultPreamble);
}
public void ask (String topic) {
String question = defaultPreamble.concat (topic);
System.out.println (question);
}
private String defaultPreamble;
}