CS 1110/1111: Introduction to Programming

Lecture 24

Announcements

There is a CS informative meeting for undeclared SEAS students Today, 5-6pm, Olsson Hall, Room 120. College students are welcome, but only the SEAS degrees will be discussed.

Talking Points

One use of a class is to add new types to Java.

Java has only 8 primitive types and arrays built in. Everything else is defined in a class.

Each class can have fields (also called attributes) and methods.

Fields look like variable declarations outside of any method.

Methods that describe the behavior of an object are not static.

Constructors and this

A constructor is a special method used to set up a new object. It always has the same name as the class and has no return type.

Inside any non-static method, there is a special variable named this. this is like a hidden parameter that gets its value from the object on the left of the dot instead of inside the parentheses. So given the code

public class Whatsit {
    public void example(int x, String y) {
        // three names are visible here:
        //     this is a  Whatsit
        //     x    is an int
        //     y    is a  String
    }
}

if we invoke it like a.example(b, c) then inside the example method this gets its value from a, x gets its value from b, and y gets its value from c.

this also exists in a constructor; it is the object currently being set up.

this acts like a final variable: you can't write this = ... because the this variable cannot be assigned to.

UML Class Diagram

We often draw pictures of classes to help design them. Three boxes: the name of the class, its fields, and its methods.

There are lots of details about other pieces, but for this course if it makes sense that's good enough.

String
char[] characters
char charAt(int)
String substring(int)
String substring(int, int)
...

From idea to diagram to code

Suppose we want to have a rectangle type in our language. Java has several built in, but let's make our own. We start by brainstorming what we think is interesting about a rectangle:

Rectangle
double width
double height
double getArea()
double getPerimeter()

Then we turn that into code:

public class Rectangle {
    private double width;  // fields are traditionally private, not public
    private double height;
    
    // TODO: add constructors...
    
    public double getArea() {
        return this.width * this.height;
    }
    public double getPerimeter() {
        return 2 * (this.width + this.height);
    }
}

From Class

Code from 001 (3pm): Spaceship.java, Rectangle.java

Spaceship
terial : String
size : int
String destination
void paint(String)
void fly()
void purchase(double)
Rectangle
double length
double width
Color color
double getArea()
double getPerimeter()
void setArea(double)
void setDimensions(double, double)
boolean isLargerThan(Rectangle)

Code from 002 (1pm): WaterTable.java, Rectangle.java

WaterTable
distanceFromGround
howMuchWater
isgood()
Rectangle
length: double
width: double
color: Color
Rectangle()
Rectangle(double, double)
getArea(): double
getPerimeter(): double
judgeBeauty(): boolean
setLength(double): void
entirelyContains(Rectangle): boolean
Copyright © 2014 by Luther Tychonievich. All rights reserved.