CS 187SL Study Guide - Midterm Guide: Linked List, Type System, Class Variable
34 views13 pages
University of Massachusetts – Boston
COMPSCI 187 - Programming w/Data Structures
Exam 1: 2/17/2016
3. Linked Lists
4. Big O
5. Stacks, Generics, Exceptions
6. Linked Stacks.
Chapter1 – Getting Organized
A java class defines the structure of an object or a set of objects. A class defines variables (data) and
methods (actions) that determine the behavior of an object.
public class Date
private int year;
private int month;
private int day;
public Date(int newMonth, int newDay, int newYear)
month = newMonth;
year = newYear;
day = newDay;
public int getYear()
public int getMonth()
public int getDay()
public String toString()
return (month + “/” + day + “year”);
The Date class provides an example of how a class would be structured. The instance variables are date,
month and year. These variables represent attributes of the class. The methods, or actions of this class
are getYear(), getMonth(), getDay() and toString().
Objects are created from classes at runtime. They can contain and manipulate data and are created
using the constructor of the objects class.
To create an Object:
Date myDate = new Date(6, 24, 1997);
In the example above, Date is the name of the class, myDate is the name of the object created using the
class and new is a keyword used to create objects. The values in the () are the parameters that are taken
in when creating the object.
When an object (instance) is created, memory is allocated for it at a particular address or
Apple apple = new Apple();
The memory location is called a “pointer” or “reference” to the object.
Here variable apple holds the reference (or pointer) to the newly created Apple object.
Assigning an object variable to another variable does NOT allocate new memory — it merely
copies the pointer from the first variable to the second:
Thus both variables reference the same object, and we say variables hi and message are ‘alias’
of each other.
You can define a class by inheriting from a parent class (aka super-class).
The inherited class contains all variables and methods from the parent class, and may have
additional variables and methods.