CS 1063-001,   Final Exam, Fall 2005, Answers

Directions: Fill in answers on the pages below. Don't spend too much time on any one problem.


  1. if-else: Write a segment of code that uses one or more if-else statements to set the tax rate (variable taxRate) based on income (variable income), using on the table below. Then calculate a value for a double variable named tax which is the income times the tax rate. Finally print either You owe and the tax, in case the tax is greater than zero, or print You owe nothing in case the tax is zero.

    Answers are in red below.

    Income.java One run
    import java.util.Scanner;
    
    public class Income {
    
       public static void main(String[] args) {
          int income;
          double taxRate;
          Scanner in = new Scanner(System.in);
          income = in.nextInt();
          while (income >= 0) {
    
             // put if-else statement(s) here to
             //   give taxRate the proper value
             if (income <= 10000) taxRate = 0.0;
             else if (income <= 50000) taxRate = 0.2;
             else if (income <= 200000) taxRate = 0.35;
             else  taxRate = 0.5;
    
             System.out.println("income: " + income +
                ", taxRate: " + taxRate);
    
             // statement here to give tax the proper value
             double tax = income*taxRate;
    
             // statements here to print the proper message
             if (tax > 0.0)
                System.out.println("You owe " + tax);
             else
                System.out.println("You owe nothing");
             income = in.nextInt();
          }
       }
    }
    
    9999
    income: 9999, taxRate: 0.0
    You owe nothing
    10000
    income: 10000, taxRate: 0.0
    You owe nothing
    10001
    income: 10001, taxRate: 0.2
    You owe 2000.2
    50000
    income: 50000, taxRate: 0.2
    You owe 10000.0
    50001
    income: 50001, taxRate: 0.35
    You owe 17500.35
    100000
    income: 100000, taxRate: 0.35
    You owe 35000.0
    200000
    income: 200000, taxRate: 0.35
    You owe 70000.0
    200001
    income: 200001, taxRate: 0.5
    You owe 100000.5
    0
    income: 0, taxRate: 0.0
    You owe nothing
    -1
    


  2. while loop: Write a while loop (or any other kind of loop, but your answer must use a loop) that will print the even numbers from 1 to 20, with a space after each one, that is, it should print 2 4 6 8 10 12 14 16 18 20.


  3. for loop:

    1. Rewrite the following for loop as a while loop that will print the same thing.

    2. Say exactly what this loop will print.

        for (int i = 1; i < 6; i++) {
           System.out.println(i + " squared = " + i*i);
        }
        
        // for loop as a while below
        int i = 1;
        while (i < 6) {
           System.out.println(i + " squared = " + i*i);
           i++;
        }
        
        // what the loop will print below
        1 squared = 1
        2 squared = 4
        3 squared = 9
        4 squared = 16
        5 squared = 25
        


  4. Sum using a loop: Use a loop to calculate the sum of the numbers from 1 to 100. After the loop, print this sum.


  5. The Student class and StudentTest class: Here are two classes with parts missing and some questions at the end. Answers are in red below.

    1. Fill in a constructor for Student that sets myName equal to the input formal parameter, and sets the other two private data members to 0.
    2. Fill in a public accessor function getName that returns the name.
    3. Fill in a public helper function getGPA that returns the grade point average (GPA) as a double. The GPA is the total number of grade points divided by the number of course hours. In case the number of course hours is 0, you should return -1.0.
    4. Inside the main function give code to do the following:
      1. Give code to create an instance of Student with variable name frank and with the actual parameter "Frank Griswold".
      2. Use the toString method to print all the information about this instance (which will have a GPA of -1.0).
      3. Use the addSemester method to add in 6 credit hours and 15 total grade points.
      4. Again use the toString method to print all the information about this instance.
      5. Use an explicit call to getGPA to print the GPA for Frank.


  6. The Date class and DateTest class:

    1. Write a Date class below from scratch. Date has the following parts.

      1. private data members (also called instance variables in the course) for the day, the month, and the year, all integers.
      2. A constructor that takes in the day, month, and year in that order as formal parameters. In case the day is not in the range from 1 to 31 (inclusive), the constructor should set the variable for day to 0. Similarly, the constructor should check that the month is between 1 and 12 (inclusive), and should check that the year is between 1900 and 2100 (inclusive).
      3. Accessor methods for the three data members.
      4. A toString method that returns the date as a String in the American notation, that is, in the form: mm/dd/year, where mm is one or two digits for the month, dd is one or two digits for the day, and year is 4 digits for the year.
      5. A nextDay method that increases the day by one.
        • If the month number is 4, 6, 9, or 11, and if the day number is 31, set the day to 1 and increase the month number by 1.
        • If the month number is 2, and if the day number is 29, set the day to 1 and increase the month number by 1.
        • Otherwise, if the day number is 32, set the day to 1 and increase the month number by 1.
        • After the above, if the month number is 13, set the month number to 1 and increase the year number by 1.

    2. Write a DateTest class below from scratch. DateTest should the following.

      1. Create an instance of Date with variable name today, and with actual parameter values of 13, 12, and 2005.
      2. Use toString to print today.
      3. Use nextDay to move to the next day.

    Below, I used an extra method daysInMonth that wasn't asked for, but then it is easy to check for incorrect initial dates with myDay < 31. I also put in a leading zero on months or days less than 10, and this wasn't asked for. This does not handle leap year or Feb. 29.

    Date.java DateTest.java, plus run
    // Date: represent dates
    public class Date {
       private int myDay;   // 1-28, 30, 31
       private int myMonth; // 1-12
       private int myYear;  // 1900-2100
    
       public Date(int day, int month, int year) {
          // if error, set field to 0
          if (day < 1 || day > daysInMonth(month))
             day = 0;
          myDay = day;
          if (month < 1 || month > 12) month = 0;
          myMonth = month;
          if (year < 1900 || year > 2100) year = 0;
          myYear = year;
       }
    
       private int daysInMonth(int month) {
          // incorrect on month == 2 in leap year
          if (month == 2) return 28;
          if (month == 4 || month == 6 ||
              month == 9 || month == 11) return 30;
          return 31;
       }
    
       public int getDay()   { return myDay; }
       public int getMonth() { return myMonth; }
       public int getYear()  { return myYear; }
    
       public String toString() {
          String res = "";
          // put in leading 0 on day
          if (myDay < 10) res = res + "0";
          res = res + myDay;
          res = res + "/";
          // put in leading 0 on month
          if (myMonth < 10) res = res + "0";
          res = res + myMonth;
          res = res + "/";
          res = res + myYear;
          return res;
       }
    
       public void nextDay() {
          if (myDay == daysInMonth(myMonth)) {
             myDay = 1;
             myMonth++;
          }
          else myDay++;
          // in case above incremented myMonth
          if (myMonth == 13) {
             myMonth = 1;
             myYear++;
          }
       }
    }
    
    public class DateTest {
    
       public static void main(String[] args) {
          Date today = new Date(13, 12, 2005);
          System.out.println(today);
          today.nextDay();
          System.out.println(today);
          System.out.println();
    
          Date newYearsEve = 
             new Date(31, 12, 2005);
          System.out.println(newYearsEve);
          newYearsEve.nextDay();
          System.out.println(newYearsEve);
          System.out.println();
    
          Date endOfFeb = new Date(28, 2, 1998);
          System.out.println(endOfFeb);
          endOfFeb.nextDay();
          System.out.println(endOfFeb);
          System.out.println();
    
          Date testDate = new Date(29, 2, 2100);
          System.out.println(testDate);
          testDate.nextDay();
          System.out.println(testDate);
          System.out.println();
       }
    }
    // a run of this program
    13/12/2005
    14/12/2005
    
    31/12/2005
    01/01/2006
    
    28/02/1998
    01/03/1998
    
    00/02/2100
    01/02/2100
    

Points for each problem: 1-30, 2-20, 3-25, 4-25, 5-50, 6-50.