Guide, Week 3 PDF
Guide, Week 3 PDF
In Java, you will usually engage in object-oriented programming. In object-oriented programming, you
view a programming problem in terms of the objects involved. You analyze the problem, decide what
different types of objects are involved, decide on what attributes each type has and decide what
behaviours each type exhibits. Then you write a blueprint called a class for each object type, create the
objects (instances of the class) from the main program and in some cases (like the Pong example) you
may even be able to exit the main method and let the objects interact with one another on their own.
b. Creating Objects
Java Memory Diagram Notes
Circle c = new Circle(); c: radius: 10.0 Variables c and d contain
x: 100.0 references (memory addresses)
y: 100.0 that allow access to the objects
stored in RAM. The “.” operator
Circle d = new Circle(); d: “dereferences” the variable to get
radius: 25.5
d.radius = 25.5; to the object.
x: 100.0
y: 100.0
System.out.println(c)
will show the memory address.
print(addup(1,2,3)) System.out.println(addup(1,2,3));
Notes
• A method is a function that is declared inside a class definition.
• In Java, all statements (except variable declarations) must be inside a method, and all methods
must be inside a class.
• When you run a Java class, the main method executes.
• The declaration above starts with the type of the method.
o This is the type of value that the method must return
o If you try to return the wrong type, you’ll get a syntax error
o The type can also be void, which means the method does not return anything
• Parameters and local variables must be declared using types
• When you call a method, arguments are matched to parameters by position
o No keyword or optional parameters like Python
Arguments provide all information necessary to Arguments provide new information not already in the
perform the subtask. object’s instance variables.
Return values communicate the result of the Return values send back information from the object. If a
subtask. behaviour changes an instance variable, we might not need
a return value at all.
Java Diagram & Notes
public class Circle2 {
double getArea() {
return Math.PI * radius * radius; getArea has no parameters because all the
} information is already in the object. Returns
the result of its computation.
Note
The textbook (e.g. pages 272, 328, 342) shows a style of UML class diagram in which the parameter
types appear before the variable names instead of after them. This is not standard, so in this course we
will stick to the more standard UML style of paramName: paramType as shown above.
3. Encapsulation (Read Section 5.2)
It’s not always a good idea to let other programmers
access your instance variables directly:
Accessor methods (get methods) are a standard way to access a private instance variable.
Mutator methods (set methods) are a standard way to set the value of a private instance variable.
In UML class diagrams, “–” means private and “+” means public, as shown above.
See Circle3.java on eLearn for the code that matches this diagram.
• Notice that setRadius() and setLocation() check the values being passed to them and
issue warnings or errors. We wouldn’t be able to do that if radius, x and y were public.
• Notice that x and y are “write only”. You can change them with setLocation but you can’t
access their values. We also wouldn’t be able to do that if x and y were public.
toString() is a kind of “magic” method. It will automatically be called whenever you print or
concatenate your variable. So you never have to actually call toString(). The system does it for you.
(Of course, there’s nothing really magic about toString(). We’ll explain in a later class how this
method is really getting called.)
The interface, or API (Application Programming Interface) tells the programmer everything they need to
know to use your class.
Implementation
The implementation of a class is its private stuff + method bodies + comments inside methods.
The programmer should not have to know any of this in order to use your class.
You can use the JavaDoc compiler (from the Tools menu on IntelliJ) to generate HTML from your
JavaDoc. Feel free to embed HTML tags to make the result look nicer.
EXTRA: Partial Class Diagrams for Common Classes
Diagrams like these will be made available on your tests. More to come later!