Some practice problems for the final

A. The following was a problem sequence proposed by Ori Spector that seemed slightly too long to be on a test. Still, the problems are quite good for practice.

  1. Create an ArrayList<Integer> that contains the numbers 1..100.
  2. Write a method containsSeven(int n) that returns true if any of the digits of the input are a seven.
  3. Write a method deleteSevens(ArrayList<Integer>) that removes all numbers from the ArrayList that either have a seven as a digit or divide seven evenly. Test deleteSevens by passing it the ArrayList you created in problem 1.

 

B. Write the method prepend(String[] strings, String prefix). It does not alter the input array. Rather, it returns an array containing Strings that works like this:

String[] s = { "boil", "head", "y" };
String[] newStrings = prepend(s, "fish");
// newstrings now contains { "fishboil", "fishhead", "fishy" }
	

 

C. Write a class Rectangle that takes four doubles as inputs. The first two numbers are the coordinates of the upper left corner of the Rectangle and the second two numbers are its width and height.

The Rectangle class should have the following methods:

Example:

Rectangle rect = new Rectangle(10, 25, 100, 200);
System.out.println(rect.area());      // prints 20000.0
System.out.println(rect.perimeter()); // prints 600.0
System.out.println(rect);             // prints { 10.0, 25.0, 100.0, 200.0 }