CS 245 - Programming Languages

Lab 2

Little Java Programs

You many work in pairs on this, and any, lab

In this lab you will write, a four 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 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 that extends ArrayList. Write a main function that just puts the numbers 1 .. 20 into this new class.

Add to you class a new, public method named slicer. Slicer should behave according to the following documentation (I named the class MAL (short for My ArrayList). You may certainly rename.

    /**
     * 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 MAL slicer(int start, int count) {

Factors from Prime Factors

Write a program to answer the following problem: given the set of prime factors of a number, return (or just print) the set of factors. For instance, the prime factors of 300 are 2,2,3,5,5. The factors of 300 are: 1, 2, 3, 4, 5, 6, 10, 15, 20, 25, 30, 50, 60, 100, 150, 300. You can assume that the prime factors are given in a convenient form (for example, an ArrayList) and you may return the factors in a convenient form. Some thoughts: order in the results does not matter (but neatness is nice), start by not worrying about duplication of the factors. However, your final answer should not have duplicates. Efficiency matters (when numbers are large, the number of factors can be large) but having a solution matters more.

What to turn in

Send a single email to gtowell@brynmawr.edu with the all of the code you wrote. You code should be reasonably close to correct. It need not be commented and as above, it need be only minimally exception protected.