Homework Assignment 1

Announcements

  1. Sun. 1/24, 1pm: The Person class should be declared static, not the Salary class as the write-up originally stated
  2. Sun. 1/24, 1pm: Kristy's homework tips

Submission Instructions

  1. Submit your answers using the 365_submit script.
  2. The answers to questions 1-5 should be in a file named either hw1.txt or hw1.pdf. Do not submit a Word or WordPerfect file because the TAs may not have the software that will allow them to read these files.
  3. The answer to question 6 should be submitted in a jar file that includes both your .class and .java file. See my jar notes on how to create and execute a jar file. I will go over jar files briefly in class on Tuesday--you can prepare your solution to question 6 before then and simply jar it up after my lecture if you're not sure how to do it by looking at my notes.


  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. The following question involves Java's enumerated types:

    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. Declare a variable named year as a ChineseZodiac type and assign it the value Rabbit.

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

  6. Write a Java class named Salary 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 (if you missed the class, read the notes on how to do this or ask someone in the class for their notes).
      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 must declare a class to store a person's information. I don't care what you name it. 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). Since your main method will be static, make sure you declare the Person class to be static or else the compiler will complain.
    4. Store the objects in an array.
    5. You can use the parseDouble method from the Double class to convert a string to a double.