Abstraction Exercises

COM 1201 – Spring 2003 – Jeff Raab

Introduction

In this exercise set you will answer questions about abstraction topics and write code that uses and implements abstractions.  You must follow the course guidelines for type- or handwritten answers, diagrams, and writing code, as posted on the course website.

Written exercises

1.         Obtain two highlighter or equivalent markers of different colors.  Detach the last three pages of this document and write your name at the top of each.

            On the first page, classes are represented by their UML specification.  On the remaining two pages, classes are represented in the Java language.

            On each of the three pages, highlight the public interface for each class in one color, and the hidden data and methods in the other color.  In the box at the top right of each page, marked “Key”, fill in the small boxes next to the two labels with the appropriate colors.

2.         Assume the PhoneBook and PhoneEntry classes specified on pages 11 and 15 of the textbook, respectively.  Write a method named createPhoneBook that creates and returns a new phone book object containing the following entries:

Name

Address

Phone number

Sue Brown

1018 Main Street

(502) 555-1234

Sam Green

987 State Street

(101) 555-4321

Art Black

12 Washington Blvd.

(831) 555-2222

 

3.         Perform exercise 3 on page 35 of the textbook.  Use correct OCL notation to represent resulting values that are sets.  (Hint: If you can’t figure out problem k, graph the functions f(x) = x + x and g(x) = x * x to see where they intersect.)

4.         Perform exercise 4 on page 36 of the textbook.

Programming exercises

To complete this portion of the exercise set you must download the files from the course website.  These files represent a complete CodeWarrior project that will work as-is in the computer labs in Cullinane Hall.  If you wish to work at home, elsewhere, or using an IDE other than CodeWarrior, it is your responsibility to configure the software.

5.         Perform programming exercise 3 on page 37 of the textbook.

CopyMachine

(data definition)

+ CopyMachine()

+ void clear()

+ void plus1()

+ void darker()

+ void lighter()

+ void mode()

+ int getCopies()

+ int getDarkness()

+ int getMode()

(helper methods)

            Complete the class by filling in its definition, which must meet the UML specification at the right.  Left out of the UML specification is the data definition for the class, and any helper methods you need to implement the class correctly.  These are up to you to design as necessary.

            In the class named Tests, write methods that test all of the methods of the CopyMachine class.  You must create several CopyMachine objects, manipulate them by calling update methods, and ensure that the accessor methods return expected values in each case.

            Once you have tested your class, click the ShowCopyMachine button in the application to see your CopyMachine class used in a graphical user interface.

Submission

You must submit the following written and/or printed material:

           Highlighted last three pages of this document

           Type- or handwritten answers to written problems

           Printouts of classes and tests you wrote for programming problems

 

You must submit the following electronic material:

           A folder containing all of the provided files in the exercise

           Classes and tests you wrote for programming problems

 

Details for electronic submission are given on the course website.


Name: ______________________________________________________

Key:

Public       q

Hidden     q

 

NU ID#: ____________________________________________________

 

TimeOfDay

 

+ TimeOfDay(int n, int d)

+ int hour()

+ int minute()

+ boolean isPM()

+ void incHour()

+ void incMinute()

AlarmClock {abstract}

 

+ AlarmClock()

# void viewTimeButtonClick()

# void viewAlarmButtonClick()

# void plusHourTimeButtonClick()

# void plusMinuteTimeButtonClick()

# void plusHourAlarmButtonClick()

# void plusMinuteAlarmButtonClick()

 

 

 

 

 

 

 

BunchOfStars

- Dot[] dots

- int dotCount

+ BunchOfStars()

+ void add(Dot d)

+ void removeRandomStar()

Person {abstract}

# String firstName

# String middleName

# String lastName

+ Person(String fn, String mn, String ln)

+ String preferredFullName() {Abstract}

 

 

Solid {interface}

 

+ double area()

+ double volume()

Cube

- double side

+ Cube(double s)

+ double area()

+ double volume()

+ Object clone()

 


Name: ______________________________________________________

Key:

Public       q

Hidden     q

 

NU ID#: ____________________________________________________

 

/*  Domain

        Every object of type solid is a three-dimensional

        geometric solid with a skin surrounding it */

public interface Solid {

 

    /* post:  result == the total surface area for this solid */

    public double area();

 

    /* post:  result == the volume enclosed by this solid */

    public double volume();

}

 

 

public class Cube implements Solid, Cloneable {

 

    private double side;

 

    /* post:  side == s */

    public Cube(double s) {

        side = s;

    }

 

    /* post:  result == 6 * side * side */

    public double area() {

        return 6 * side * side;

    }

 

    /* post:  result == side * side * side */

    public double volume() {

        return side * side * side;

    }

 

    /* post:  result.equals(this) and result != this */

    protected Object clone() {

        return new Cube(side);

    }

}


Name: ______________________________________________________

Key:

Public       q

Hidden     q

 

NU ID#: ____________________________________________________

import java.awt.Color;

import java.awt.event.*;

import javax.swing.*;

public class NightSkyApp implements ActionListener {

     private JFrame window;

     private JButton addBtn, removeBtn;

     private BunchOfStars stars;

     /* note: This method initiates program execution by

              instantiating this class. */

     public static void main(String args[]) {

           new NightSkyApp();

     }

     /* post: window has been created in black with two buttons

                upon it and stars = set{} */

     public NightSkyApp() {

           stars = new BunchOfStars();

           makeUserInterface();

     }

     /* post: window has been created in black with two buttons

                upon it */

     private void makeUserInterface() {

           window = new JFrame();

           window.getContentPane().setLayout(null);

           window.setBounds(10, 10, 300, 200);

           window.getContentPane().setBackground(Color.black);

           addBtn = new JButton(“Make Star”);

           window.getContentPane().add(addBtn);

           removeBtn = new JButton(“Remove Star”);

          window.getContentPane().add(removeBtn);

           window.show();

     }

     /*  pre:  window != null and stars != null

         post: event e was caused by the add button implies a                  new star is displayed and included in stars

                and event caused by the remove button implies

                one random star from stars@pre has been removed

         note: This method is called in response to all button

               events */

     public void actionPerformed(ActionEvent e) {

           if (e.getSource() == addBtn)

                stars.add(new Dot(150, 250, 3));

           else

                stars.removeRandomStar();

     }

}