Review 1

Advice

  • Solve these questions in a study group.
  • The best way to learn how to program is to program. Work out these problems and others on cs.bridgewater.edu or an AWS Linux VM.

Problems

    1. Create a class named Course that has 2 string fields named catalogNumber, title and an integer field named numberOfCredits. Include in the class a constructor that sets all of the fields as well as getters and setters.
    2. Create a class named TravelCourse that extends Course and includes a string field named destination. Include a getter and setter for the field.  Include in the class a constructor that sets all of the fields as well as getters and setters.
    3. Write a driver named RegistrationApp that creates two objects of type TravelCourse for the following 2 courses:
      1. Course 1
        • Catalog Number: CSCI-200
        • Title: Intermediate Programming
        • Number of Credits: 3
        • Destination: Paris
      2. Course 2
        • Catalog Number: CSCI-200
        • Title: Intermediate Programming
        • Number of Credits: 3
        • Destination: Rome
    4. Write a class named Circle that meets the following specifications.
      • The class has one floating point (decimal number) field named radius.
      • The class has a constructor that sets the radius field equal to 1.
      • The class has a constructor that sets the radius field equal to the value of the argument passed into it.
      • The class has a method named getCircumference that returns the circumference of the circle.
      • The class has a method named getArea that returns the area of the circle.
    5. Create a program named CircleApp that does the following:
      • Get a decimal value from the user.
      • Print out the circumference and area to the screen using the value entered by the user as the radius
    6. Consider the following directory structure.
      /Users/me/work/hr/benefits/EnrollmentApp.java
      /Users/me/work/hr/personel/PersonelManagement.java
      /Users/me/work/hr/personel/Employee.java
      /Users/me/work/research/time_travel/LifeSupportSystems.java
      • List fully qualified names for all 4 classes where all 4 package names have the same root package.
      • List fully qualified names for all 4 classes where only the first 3 package names share the same root package.
      • List fully qualified names for all 4 classes such that there are 3 distinct package roots.
      • List fully qualified names for all 4 classes where all 4 package names have different root packages.
    7. Suppose the Employee class has the following code.
      package hr.personel;
      
      class Employee {
          …
      }
      • Provide code that shows how an Employee object can be instantiated in PersonnellManagement.java. Declare a variable of type Employee, initialize it, and provide any other necessary code.
      • What needs to change in order for the Employee class to be instantiated in a file outside of the hr.personel package?
      • Suppose the change in b. was made. Provide code that shows 2 different ways in which an Employee object can be instantiated in LifeSupportSystems.java.  For each way, declare a variable of type Employee, initialize it, and provide any other necessary code.
      • Suppose the Employee class is in the hr.personel package and the LifeSupportSystems class is in the time_travel package. Suppose also that the LifeSupportSystems class creates an instance of the Employee class and that the working directory is /Users/me/work/research/time_travel/.
      • Give the command to compile LifeSupportSystems.java.
      • Give the command to run LifeSupportSystems.

Review 2

Exam 2 will test you knowledge of everything we’ve discussed in class except Enumerations.  Enumerations will be covered on Exam 3.  Be prepared to answer questions on the material we covered prior to Exam 1 as well as the material we’ve covered after Exam 1.

Below are some problems that will help you prepare for Exam 2. This is not an exhaustive list of problems and may not cover all of the concepts touched upon in Exam 2.


1. Write a class named IllFormedCircleException that satisfies the following specifications.

  1. The class has a private field named reason (String).
  2. The class has a constructor with one String parameter.  When invoked, the constructor sets the field equal to the value passed into the constructor.
  3. The class overrides the toString method which returns “Ill formed circle: ” concatenated with the reason.

2. Write a class named Circle that satisfies the following specifications.

  1. The class has a private field named radius (int).
  2. The class has a constructor with one integer parameter.  When invoked the constructor inspects the value of the argument passed into it.  If the value is less than or equal to zero the constructor throws an IllFormedCircleException object passing its constructor the string “invalid radius”.
  3. The class has a setter method for the radius field.  If the value passed into the method is less than or equal to zero the method throws an IllFormedCircleException object passing its constructor the string “invalid radius”.
  4. The class has a getter method for the radius field.
  5. The class overrides the toString() method and returns “circle: radius=” concatenated with the radius of the circle.

3. Write a class named ColoredCircle that satisfies the following specifications.

  1. The class has a private field named color (String).
  2. The class overrides toString and returns “circle: radius=” and the value of the radius, followed by “color=” and the value of the color.

4. Write a driver named CircleTest that attempts to the following:

  • Create a variable named c1 that holds the reference to a ColoredCircle object with a radius of 1 and a color “red”.
  • Create a variable named c2 that holds the reference to a ColoredCircle object with a radius of 0 and color “blue”.
  • Set the radius of the circle referenced by c1 to 0.
  • Set the radius of the circle referenced by c1 to 1 and set the color to “green”.
  • If any exceptions are caught, the program should simply print the string returned by the exceptions toString method.

5. Write a class named Person with the following specifications.

  • The class has the following members.  Restrict the access of the members to only allow the access specified.
    • name – a string accessible in any class that extends of Person
    • address – a string accessible in any class
    • phone – a string accessible only in the class Person
    • email – a string accessible in any class within the same package
    • spouse – a Person accessible in any subclass of Person, but which can not be modified after construction.
    • averageLifespan – an integer that is accessible in any class and that is shared among all instances of Person.
    • hello() – a method that is accessible in any class and that is shared by all instances of the class.  The method should simply returns the string “hello”.
  • The class should contain an inner class named Builder that uses the Builder Pattern.  The name field is the only required field.  Make up default values for the other fields.

6.Write a class named PeopleApp that tests the Person class.

 

Review 3

Write one or more drivers that test the implementations of the classes and interfaces described below.

Create an interface named Tuple that requires the following methods:

  1. a method named getFirst that has no parameters and returns an Object
  2. a method named getLast that has no parameters and returns an Object
  3. a method named getElements that returns an array of Objects

Create an class named Pair that implements the Tuple interface and meets the following requirements:

  • The class has two fields, named first and second, that hold Objects.
  • The class should have a constructor that has 2 Object parameters whose values are stored in the fields.
  • The getFirst method returns the value stored in the field named first.
  • The getLast method returns the value stored in the field named second.
  • The getElements method returns an array of length 2 that holds the values stored in the two fields.
  • The class overrides toString and returns a string of the form “(x,y)” where x and y are the string representations of the values stored in first and second respectively.
  • The class overrides equals assuming both fields are significant.
  • The class overrides hashCode.

Create a class named Triple that implements the Tuple interface and meets the following requirements:

  • The class has one field named elements that holds an array of 3 Objects.
  • The class should have a constructor that has 3 Object parameters whose values are stored in the fields.
  • The getFirst method returns the first element in the array.
  • The getLast method returns the last element in the array.
  • The getElements method returns the array field.
  • The class overrides toString and returns a string of the form “(x,y,z)” where x,y, and z are the string representations of the values of the first, second, and third elements in the elements array.
  • The class overrides equals assuming all elements in the array are significant and order matters.
  • The class overrides hashCode.

Write a class named PositivePair that extends Pair and meets the following requirements:

  • The class has one constructor that takes two Objects as parameters.  If either object is not an Integer or is negative, the constructor throws an IllegalArgumentException exception.  Otherwise, the constructor sets the fields with the values passed in.

Write a class named GreekGod that meets the following requirements:

  • The class has 3 named instances: APOLLO, ARES, and DIONYSUS.
  • Each instance has two String fields named mother and father which are set via a constructor.  Use Wikipedia to find the parents of these gods.
  • The class has getters for the mother and father fields.

Write a class named JigglyPuff that meets the following requirements:

  • The class allows only one instance to be created but is not an enumerations.
  • The one instance is accessible only via a method named getInstance.
  • The instance has an readable field named HP that is initialized to 5.

Recite and use Bloch’s advice for designing methods.

Write a function named foo that takes 0 or more integers as arguments and returns the average of the integers that are passed into the function.

Write a function named bar that takes an instance of Tuple as an argument and returns a string representation of the first and last element of the object passed into the function by calling the object’s getFirst and getLast methods.  Demonstrate that you can pass an instance of Pair and an instance of Triple to bar.

Review 4

After writing each of the classes below, and before moving onto the next class, write a driver that tests your code.  And stress test it.  Don’t just write a few lines to test it, try to break your own code.

1.  Write a class named Stack that implements the StackInterface interface as described in Exam 3.  Feel free to use the code in the Exam 3 answer key.  If you use my code, you’ll need to implement the FullStackException class.  A quick solution is provided below.  Write a driver and test that the Stack class works properly before continuing.

public class FullStackException extends RuntimeException { }

Then augment the Stack class so that it implements the Iterable<Object> interface.  The iterator method should return an Iterator<Object> object that iterates over the stack’s array, from the last object added to the first object added.

2. Write a class named Matrix that satisfies the following requirements:

  • The class should store integers (primitive) as an array of arrays.
  • The class should have a constructor that has 2 integer parameters representing the number of rows and columns (m,n) of the matrix respectively.
  • The class should have have a method named set that takes a variable number of int[] arguments which hold the rows of the matrix.  If the number of arguments passed into the method is not equal to m, an IllegalArgumentException exception should be thrown.  If the lengths of each of the arrays that are passed into the method is not equal to n, an IllegalArgumentException exception should be thrown.
  • The class should implement the Iterable<Integer> interface. The iterator method should return an Iterator<Integer> object that can be used to iterate over all of the elements in the matrix, row by row, from left to right.
  • The class should have a method named getDiagonal that returns an Iterator<Integer> object that can be used to iterate over the elements on the diagonal from (0,0) to the (m,n).

3. Write a class named Student that satisfies the following requirements.

  • The class should have a constructor that takes a first name (String) and a last name (String).
  • The class should override toString to print all of the values of the fields.
  • The class should implement the Comparable<Student> interface.   The compareTo method should order the Students objects as follows.
    • For all Students a and b:
      • If a.lastName comes before b.lastName alphabetically, then a < b.
      • If a.lastName and b.lastName are the same and a.firstName comes before b.firstName alphabetically, then a < b.
      • Otherwise a and b are equal.

4. Write a class named Person that satisfies the following requirements.

  • The class should have a constructor that takes a first name (String) and a last name (String).
  • The class should override toString to print all of the values of the fields.
  • The class should implement the Comparable<Person> interface.   The compareTo method should order the Person objects as follows.
    • For all Students a and b:
      • If the total number of letters in a’s first and last name is less than the total number of letter’s in b’s first and last name, then a < b.
      • Otherwise a and b are equal.

5. Augment the Person class so that it implements Serializable.  Set serialVersionUID to 1000L.  Write a driver that creates a serialized instance of the class.  Then email the instance to a peer in class.  Write a second driver that reads a serialized instance of the Person class and prints to the screen the name of the person.