Return to the lecture notes index

Lecture #31 (Monday, Dec 4, 2006)

Daily Excercise Three

Today we did our third daily excercise. For this one, all we were asked to do was write a class that models a piece of paper. We defined paper to have four properties: length, width, color, and text. You can't change the color length and width, but you can add on text.

import java.util.*;
import java.io.*;

public class Paper {
	private double length;
	private double width;
	private String color;
	private String text;
	
	//A constructor that takes in some initial text
	public Paper (double length, double width, String color, String text) {
		this.length = length;
		this.width = width;
		this.color = color;
		this.text = text;		
	}
	
	//A constructor with no starting text
	public Paper (double length, double width, String color) {
		this.length = length;
		this.width = width;
		this.color = color;
		this.text = "";
	}
	
	public double getLength() {
		return length;
	}
	
	public double getWidth() {
		return width;
	}
	
	public String getColor() {
		return color;
	}
	
	public String getText() {
		return text;
	}
	
	//Our add text method, notice that it doesn't get rid of the previous text, only adds to it
	public void addText(String input) {
		text += input + "\n";
	}
	
	//Standard equals method
	public boolean equals(Object o) {
		Paper c = (Paper) o;
		
		if (length != c.length)
			return false;
		
		if (width != c.width)
			return false;
		
		if (!color.equals(c.color))
			return false;
		
		if (!text.equals(c.text))
			return false;
		
		return true;
	}
	
	//Nicely formatted toString() method
	public String toString() {
		String list = "Length: " + length + "inches" + "\n" + "Width: " + width + "inches" + "\n" + "Color: " + color + "\n" + "Text: " + text; 
		return list;
	}
	
}