CSCI325: Data Structures                    Name________________________________

Practice Test #1

Java classes

 

1. This problem does not involve creating a separate class. Assume that you have a 2D array called sales (in your main program) that keeps track of the sales for a number of salespeople for four quarters. The sales data will be stored in a two-dimensional array with four columns (one for each quarter) and an unknown number of rows (one row per salesperson). Each salesperson is assigned an ID number in the range 1 to n (where n is the number of rows in the array). To locate a salesperson's data in the array, subtract 1 from his ID number. The quarters are numbered 1-4. To determine a quarter's column in the array, subtract 1 from the quarter number. The entries in the array are of type double and represent each salesperson's sales for the given quarter.

 

NOTE: In both functions below, the salesperson number that is passed in to the function will be in the range 1 to n and the quarter number that is passed in to the function will be in the range 1-4. It is the responsibility of each function to subtract 1 from these numbers to make sure they access the appropriate row and column in the sales array.

 

[10] Write a function called getSalesTotal that accepts two arguments: (1) the sales array, and (2) the ID number of a salesperson (an integer from 1 to 100). It returns the total sales (a double) for that salesperson for all four quarters. You may assume that the ID number is always in the range from 1-100.

2. Write a Java class called BCUTime that represents the time of day in hours and minutes. Write the class with the following methods:

 

[10] The class has one constructor with the following signature:

BCUTime (int hours, int minutes)

You do NOT need to write any error-checking code and you may assume that the parameter values are valid.

 

[10] Write a method called advanceTime that accepts two integers, one representing a number of hours and one representing a number of minutes to advance the time by. The number of hours could be any non-negative integer. The number of minutes must be between 0 and 59. For example, if the current time is 8 hours and 15 minutes and we call advanceTime with 1 hour and 30 minutes, the new time would be 9 hours and 45 minutes. Note that a "carry" could be involved, as in the following example. The current time is 8 hours and 45 minutes and we call advanceTime with 4 hours and 45 minutes. The new time is 12 hours and 90 minutes. But since 90 minutes is more than an hour, we need to change it to 1 hour and 30 minutes, giving us a time of 13 hours and 30 minutes. If the new time exceeds 23 hours and 59 minutes, you must reduce it to a number that is less than or equal to 23 hours and 59 minutes. For example, if the current time is 23 hours and 45 minutes, and we call advanceTime with 48 hours and 40 minutes, the new time would be 23+48 = 71 hours and 45+40 = 85. 85 minutes is 1 hour and 25 minutes, so our new time is 72 hours and 25 minutes. 72 hours, when converted to a 24-hour clock becomes 0 hours (72 mod 24 = 0). So the final value is 0 hours and 25 minutes (12:25 a.m.).

 

[10] A toString() method that returns a string representing the time in a 12-hour format, with time values less than 12 hours and 00 minutes labeled "a.m." and values greater than or equal to 12 hours and 00 minutes labeled "p.m.". Note that minutes less than 10 must be preceded by a 0 (e.g. "9:05 a.m.", not "9:5 a.m.". The method's signature is:

public String toString()

 

Examples:

// Create a time representing 18:15, or 6:15 p.m.

BCUTime myTime = new BCUTime(18,15);

// Return the time as a string: "6:15 p.m."

System.out.println (myTime.toString());

// Advance by 36 hours and 50 minutes:

myTime.advanceTime(36, 50);

// New time is "7:05 a.m."

System.out.println (myTime.toString());

 

3. Create a new class called PhoneBook which keeps track of many names and phone numbers. Each entry in the phone book consists of (1) a first name, (2) a last name, (3) a phone number. All three are Strings. The first name, last name, and phone number are kept in parallel arrays. There is room for 500 entries in the phone book.

 

[10] Create a default constructor that creates a first name array with 500 entries, a last name array with 500 entries, and a phone number array with 500 entries. The constructor should allocate memory for all three arrays and initialize each array entry to the empty string ("").

 

[10] Write a void method called setEntry that will accept (1) an index number in the range 0-499 representing a position in the arrays, (2) a first name, (3) a last name, and (4) a phone number. The method should put the first name in the given position in the first name array, the last name in the same position in the last name array, and the phone number in the same position in the phone number array.

 

[10] Write a String method called toString that will accept an integer in the range 0-499 representing a position in the arrays. The method will return the last name, followed by a comma and a blank, followed by the first name, followed by a colon and a blank, followed by the phone number.

 

 

[10] Write a function called getQuarterTotal that accepts two arguments: (1) the sales array, and (2) the number of a quarter (an integer from 1 to 4). It returns the total sales for all 100 salespeople for the quarter (a double).

 

Test Code:

// Test code for the Sales 2D array
double[][] sales = {
        {
1, 2, 3, 4},
        {
2, 3, 4, 5},
        {
3, 4, 5, 6}
};

System.out.println(
"Sales for #1 should be 10: " + getSalesTotal(sales, 1));
System.out.println(
"Sales for #2 should be 14: " + getSalesTotal(sales, 2));
System.out.println(
"Sales for #3 should be 18: " + getSalesTotal(sales, 3));
System.out.println(
"Sales for Q1 should be  6: " + getQuarterTotal(sales, 1));
System.out.println(
"Sales for Q2 should be  9: " + getQuarterTotal(sales, 2));
System.out.println(
"Sales for Q2 should be 12: " + getQuarterTotal(sales, 3));
System.out.println(
"Sales for Q2 should be 15: " + getQuarterTotal(sales, 4));


// Test code for the BCUTime class
// Create a time representing 18:15, or 6:15 p.m.
BCUTime myTime = new BCUTime(18,15);
// Return the time as a string: "6:15 p.m."
System.out.println ("Should be 6:15 p.m.: " + myTime.toString());
// Advance by 36 hours and 50 minutes:
myTime.advanceTime(36, 50);
// New time is "7:05 a.m."
System.out.println ("Should be 7:05 a.m.: " + myTime.toString());


// Test code for the PhoneBook class
PhoneBook book = new PhoneBook();
book.setEntry(
0, "George", "Washington", "555-555-1234");
System.out.println(
"Should be George Washington: " + book.toString(0));