CS 1063-001,   Final Exam, Fall 2005

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.


  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
        
        
        
        
        
        
        // what the loop will print below
        
        
        
        
        
        
        
        


  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.

    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.

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