CS 245 - Programming Languages

Lab 2

Little Java Programs

You many work in pairs on this, and any, lab. If you work in pair, trade off who does they typing.

Work on these for about 80 minutes (ie the time for the lab). I do not expect you to do them all in 80 minutes. I doubt anyone will get to the all subsets problem.

In this lab you will write, a several small programs in Java. The intent here is simply to get you some practice with Java before writing some of these same small programs in a new language, Go. Your programs need not be hugely protected against failure (ie minimal, or no, exception handing is fine) but they should actually run and produce valid output on any valid input.

Fizz Buzz

Write a program that prints the numbers 1 .. 100 except: if the number is divisible by 3 print "Fizz", if the number is divisible by 5 print "Buzz", if the number is divisible by 3 and 5 print "FizzBuzz".

K to M conversion

Write a program that takes input from the command line to convert miles to kilometers or kilometers to miles. (1 mile equals 1.609km) For instance, if your command line was
        java KM 10 k
    
then the output would be "6.14 miles". Conversely, if your command line was
        java KM 5 m
    
then the output should be "8.05 k"

Slicing ArrayList

Start by creating a Java class named ExtArrayList that extends ArrayList. Write a main function that just puts the numbers 1 .. 20 into this new class.

Add to your class a new, public method named slicer. Slicer should behave according to the following documentation

    /**
     * This function takes two parameters, the start and the count. 
     * It returns a new instance of the class that contains the items
     * starting at the start index and with the given count. For example
     * if the current instance contains [0,1,2,3,4,5,6,7,8,9] and this 
     * method was called with the the parameters 1,3 it would return
     * a new instance of the class with [1,2,3]. With parameters
     * 4,5 it would return [4,5,6,7,8]
     * If the first number is less than 0, count backwards from the end.
     * For example -3,2 would give [7,8].
     * @param start the index (the first item is at index 0) 
     * of the first item in the current list to 
     * be copied into the new list. If start index is less than 0, 
     * start at length-start index.
     * @param count the number of items to be copied. If count is too large
     * return as many as are available.   If count is less than 0, return all items
     * to the end of the current list.
     * @return a new class containing a subset of the objects in the 
     * source class
     */
    public ExtArrayList slicer(int start, int count) {

Chen Primes

Write a program to find all of the chen prime numbers less than 10,000. (Chen primes are numbers p, such that p is prime and p+2 is either a prime or semiprime (a product of two primes). Your function may assume it has access to a list of the first several chen prime numbers. Indeed, you might just put the following code into yours ... Suggested approach: write a function to find prime numbers, then filter the result down to chen primes.
    import java.util.Arrays;

    public class ChPr {
        public int[] chPrimes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 47, 53, 59, 67, 71, 83, 89, 101, 107, 109 };
    
        public void printer() {
            System.out.println(Arrays.toString(chPrimes));
        }
        public static void main(String[] args) {
            (new ChPr()).printer();
        }
    }

All Subsets

Write a program that returns all proper subsets of a provided set. The set will be given as a array list of generic objects. For instance, given an array list of integers [1,2,3,4], your program should return [[1],[2],[3],[4],[1,2],[1,3],[1,4], [2,3], [2,4], [3,4], [1,2,3],[1,2,4],[1,3,4],[2,3,4],[1,2,3,4]]. The signature of the Java function to do this should be
    public ArrayList<ArrayList<T>> allSubsets(ArrayList<T>)

Some thoughts:

What to turn in

Send a single email to gtowell@brynmawr.edu with the all of the code you wrote. Your code should be reasonably close to correct. It need not be commented and as above, it need be only minimally exception protected. If you worked in a pair, one email will be enough, just be clear on who worked.