CS 187SL Study Guide - Midterm Guide: Linked List, Type System, Class Variable

34 views13 pages
15 Feb 2016
Course
Professor
University of Massachusetts Boston
COMPSCI 187 - Programming w/Data Structures
Spring 2016
Exam 1: 2/17/2016
Exam Guide
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 13 pages and 3 million more documents.

Already have an account? Log in
Topics Included:
1. Classes
2. Interfaces
3. Linked Lists
4. Big O
5. Stacks, Generics, Exceptions
6. Linked Stacks.
Chapter1 Getting Organized
1.1 Classes
1. Class
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.
Example:
public class Date
{
private int year;
private int month;
private int day;
//Constructor
public Date(int newMonth, int newDay, int newYear)
{
month = newMonth;
year = newYear;
day = newDay;
}
//Observers
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
public String toString()
{
return (month + “/” + day + “year”);
}
}
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 13 pages and 3 million more documents.

Already have an account? Log in
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().
2. Objects
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
location.
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.
3. Inheritance
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.
Example:
Unlock document

This preview shows pages 1-3 of the document.
Unlock all 13 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$10 USD/m
Billed $120 USD annually
Homework Help
Class Notes
Textbook Notes
40 Verified Answers
Study Guides
1 Booster Class