Homework Assignment 1

Submission Instructions

  1. Submit your answers via Canvas. Please make one submission with two attachments, a pdf file for questions 1-4 and separate jar files for questions 5-8.
  2. The answers to questions 1-4 should be in a pdf file prepared using a word processor. Please do not submit an ascii text file as the formatting you use may get messed up in another editor.
  3. The answers to questions 5-8 should be submitted in separate jar files, each of which includes both your .class and .java file. Your jar files must be executable from the command line. You may either use an IDE to export the jar files or see my jar notes on how to create a jar file from the command line. Before submitting your .jar files, please verify that they run from the command line. You can check my jar.html notes to see how to execute a .jar file from the command line. The TAs do not have time to debug your .jar file or your code to make it run, so please make sure that your code runs on the hydra machines before you submit it.

  1. If you want to mimic in Java the functionality of C++'s destructor method (i.e., it gets called as soon as you are finished using an object), why is the finalize method the wrong way to implement this functionality?

  2. What is a virtual machine? Why are programs executed by virtual machines slower than programs executed by the machine itself?

  3. Write the Java statement that declares MARCH to be an integer constant with a value of 3.

  4. Why is it not possible in Java to write a swap routine that swaps two variables of the same primitive type?

  5. This problem will get you started with Java by having you write a simple Java program. Specifically write a Java class named Min.java that takes an arbitrary number of strings from the command line and prints the first one in alphabetical order. For example, if you invoke the program as
    java Min brad smiley nels yifan alfred michelle
    then the output of the program should be "alfred".

    1. Use the programming style recommended in the notes, which is to put your code in the constructor and simply have main create an instance of the constructor.
    2. Use the "for each" loop construct discussed in the array portion of the Java Basics notes.

  6. Write a Java program named Salary.java that determines whether or not each person in a group of persons is above or below the average salary for the group. The input will be n pairs of strings and salaries representing the names of each person and their salary. For example:
    java Salary nels 105000.80 mary 20000.50 frank 18153.33 nancy 91868.73 michelle 20858
    Your program should print each name and true if the person's salary is above the average salary for the group and false otherwise (if the salary equals the average, then output false). For example:
    average salary =  51,176.27
    nels            105,000.80 true
    mary             20,000.50 false
    frank            18,153.33 false
    nancy            91,868.73 true
    michelle         20,858.00 false
    You should be able to use the array search program covered in class and shown in the example code as a guide. A few notes:

    1. Your error checking should be limited to ensuring that the user enters an even number of arguments (since the arguments come in pairs) and that the user provides at least one pair of arguments. You may assume that the salaries represent valid numbers.
    2. Your output formatting should be as follows:

      1. names left justified in a field 15 characters wide. String objects can be printed using %s.
      2. salaries should be printed right justified in a field 10 characters wide with two decimal points. There should be a thousands separator.
      3. true/false should be left-justified with no field width information provided.
      4. the average salary should be left justified in a field 10 characters wide with two decimal points, and a thousands separator.
    3. You can use System.out.printf to format your output. %b will print a boolean as true/false, %n should be used instead of \n to print a new line character, and %, to start your formatting statement for a number will use a comma as a thousands separator. The rest of printf works just like in C.
    4. You must declare a class to store a person's information. Name this class Person. Since it only needs to be used by your Salary class, you should nest it within the Salary class (that way you also only need to use one file).
    5. Store the objects in an array.
    6. You can use the parseDouble method from the Double class to convert a string to a double.

  7. Modify the above Salary class so that instead of storing each person in an array, it creates a linked list of people with the people sorted in ascending order by salary. When you print out the names, they should now appear in ascending order by salary. Stipulations for this problem include:
    1. Named the new class SalaryList.java
    2. Create a method named createList that takes the command line arguments as a parameter and returns a linked list of Persons. Your constructor should call createList to create a list of Persons and should then operate on this list. I suggest that the return value be a reference to the head of the list. It is okay to use any kind of list you want--singly or doubly linked--and it is okay to use a double node at the front, a circular list, whatever your heart desires. The point of this problem is to give you practice creating a linked structure without having access to C++'s pointers. You may not use Java's collections library to solve this problem. You must create your own linked list.
    3. It is okay to add fields like next and prev to your Person class. If you use next as your link field, then if one person is pointed to by a variable named newPerson and another person is pointed to by a variable named nextPerson, then you can make newPerson point to nextPerson by writing:
      newPerson.next = nextPerson;

  8. The following question involves Java's enumerated types. Put your code in two files, one named ChineseZodiac.java and one named ZodiacTest.java.

    The following code will go in ChineseZodiac.java:

    1. Write a Java statement that declares Rat, Bull, Tiger, Rabbit, Dragon, Snake, Horse, Goat, Monkey, Cock, Dog, and Pig as enumerated constants for the signs of the Chinese Zodiac. You should call your enumerated type ChineseZodiac.

    2. The signs of the Chinese Zodiac are associated with years. For each of the signs, store the most recent year in which that Zodiac sign appeared. For example, 2014 is the year of the Horse. You will need to look online to see the pairing of years with Zodiac signs.
    3. Create a method named getYear() that returns the year associated with that Zodiac sign.

    The following code will go in ZodiacTest.java:

    1. Declare a variable named year as a ChineseZodiac type and assign it the value Rabbit. Then print this variable.
    2. Write a for loop that iterates through the values of the ChineseZodiac and prints the name of each sign and the most recent year in which it occurred.

    I do not care about the order in which you print the animals/years. Here is a sample output from one student's program. You do not have to do it exactly like this, but it should give you an idea about the formatting.

    The last year of the RABBIT occurred in 2011
    The year of the RAT last occurred in 2008
    The year of the BULL last occurred in 2009
    The year of the TIGER last occurred in 2010
    The year of the RABBIT last occurred in 2011
    The year of the DRAGON last occurred in 2012
    The year of the SNAKE last occurred in 2013
    The year of the HORSE last occurred in 2014
    The year of the GOAT last occurred in 2015
    The year of the MONKEY last occurred in 2016
    The year of the COCK last occurred in 2017
    The year of the DOG last occurred in 2006
    The year of the PIG last occurred in 2007