CS II Lesson 9

Professor Abdul-Quader

Designing Classes

Demos

  • Now: project 1 demos
  • After: continue Time class.

Last time

  • Classes are templates for data types.
  • Declare a class: name public class MyClass in file MyClass.java
  • Declare instance variables.
  • Define your constructor(s)
  • Define your instance methods, like toString
  • Outside the class, use it as a data type.

Visibility

Variable declarations in Time class:

    public static final int MIN_HOUR = 1;
    public static final int MAX_HOUR = 12;
    public static final int MIN_MINUTE = 0;
    public static final int MAX_MINUTE = 60;
    public static final String AM = "AM";
    public static final String PM = "PM";

    private int hour;
    private int minute;
    private String amPm;

  • Our constants are public?
  • Our instance variables are private?
  • toString is public?
  • Constructor: public Time(...).
  • Some public / some private?

Public vs private

Java has several different visibility modifiers:

  • public: this item (variable, method, constructor) can be used in code anywhere
  • protected: …
  • default visibility (ie, leave out the words public or private): …
  • private: this item can be used in code only in the same class

Rule of thumb

  • Make everything as private as you can.
  • Restrict how others use your class.
  • That way, you control how they will use it, and can predict / plan for bad behavior.

Bad Example

If the instance variables are public:

Time t = new Time(11, 59, Time.AM); // "11:59 AM"
t.hour = 53;
t.minute = -7;
t.amPm = "Hah! I tricked you!";
System.out.println(t);
  • Got around all of our error-checking!
  • What should we do instead? How would we print out just the hour?

Video

Exam 1

If you are behind, you need to be doing as many exercises as possible. Write code as much as you can.

Find the mistakes

Problem: given a String name, return the number of times the character c appears in it. What are all the mistakes in the following?

public static int numAppearances(String name, char c) {
  String name = "bob";
  char c = "b";
  int n = 0;
  for (int i = 0; i < name.length(); i++) {
    if (name.charAt(i) == c) {
      System.out.println(c);
      n++;
    }
  }
  System.out.println(n);
}

Return types and parameters

  • Pay attention to return types / parameter types
  • Do not set the parameters. (Why?)
  • Read the questions carefully. Understand what is being asked. Understand what you are meant to return.

Put things together

  • What if you blank on one of the parts?
  • The whole point of modularity.
  • You should be able to write displayNumber without knowing how to write getNumber
  • You can even write main without knowing how to write any of the other methods.
  • Create a Scanner, call getNumber four times, output x, output y, multiply (real and imaginary), output the product.

Corrections

If your grade on exam 1 was below 80, I will accept corrections for any problem you wish to correct. For each question, you must:

  1. Write a short explanation of your thinking on the original problem. What did you do wrong? Did you misunderstand what you were supposed to do, did you misunderstand something about the material, or was there another reason for making that mistake?
  2. Write up a complete, corrected solution to the problem.

Extra Credit

You will get up to half the credit lost for each response (up to a max of 80). Please come see me in office hours if you had trouble with any of these! Due: 3/19 (to me, in class, before Spring Break).

If your exam 1 grade is below 50, please see me in office hours, we can discuss further strategies to help catch you up.

Time exercise

Attempt 3: design a program which asks the user to input in the current time and a number of minutes to add (up to a max of 500), and outputs the time after that many minutes.

Modular design

Already implemented for you:

  • Time class (keeps track of hour, minute, AM/PM)
    • toString displays the time correctly
    • timeAfter method, returns a new Time object
  • getInput (gets an integer in a certain range from the user)
  • getAmOrPmInput (asks the user for input until they type in “AM” or “PM”)

What do you need to do?

Exercise

Implement the main method to put everything together.

Starter code

Plato and Aristotle in The School of Athens (Raphael)

Object Oriented Design

  • Classes are templates for data types
  • Represent data and behavior
  • Break your problem up into components: the nouns of your problem.
  • Implement each component in its own class
  • Think about the data required to represnt that component
  • Think about the actions the object should be able to do.

Design discussion

A Gaussian integer is a complex number of the form \(a + bi\), where \(a\) and \(b\) are integers (\(i\) is the imaginary number representing a square root of \(-1\)). How would you design a Gaussian integer? What data is required to represent a Gaussian Integer? What are the actions it should be able to do?

Video

Next Time

  • We will work on the GaussianInteger class next time.
  • Mutable vs immutable versions
  • Quiz 3 (most likely).
  • Project 2 will be assigned later this week, due at the end of Spring Break.
// reveal.js plugins