Please note that this problem set is intended for practice, and does not fully represent the entire scope covered in the final exam, neither the range of the types of problems that may be included in the final exam. The final exam is a closed-book-closed-notes exam. You may use a crib sheet no more than 2 pages long. No electronic devices of any sort are allowed. Partial credit will be awarded. You can abbreviate System.out.println () with println(). You don t need to include any import statements. You need not use comments and import statements to your code, but otherwise use good style. If anything in the problem definition is unclear, please ask for clarification. Page 1 of 9
Part 1. (10 points) Basics. a. Fill in the blanks 1. Operator is used to create new objects and arrays. 2. When str is a variable of type String, the value of parameter pos in the call str.charat(pos) must be between and (inclusively). 3. Keyword is used to declare methods that do not require a calling object. 4. Suppose class Foo contains a method with the following header: public boolean amethod (double x) o o o What is the return type of this method? Is this an instance method or a static method? Will the Java compiler indicate an error if method with the following header public boolean amethod (int y, String z) was also added to the same class Foo? Include a yes or no answer and an explanation. 5. The name of the constructor method must. Page 2 of 9
Part 2. (10 points) Short answers. b. What will be printed by the following code segment? Note: there are 2 printing statements. Show the intermediate values of participating variables for partial credit. int result = 0, number = 273; int ldigit; do { ldigit = number % 10; System.out.println (ldigit); result = (result * 10) + ldigit; number = number / 10; while (number > 0); System.out.println (result); c. What will be printed by the following code segment (note 2 printing statements)? String [] words = {"one", "three", "four"; System.out.println(words.length); System.out.println(words[1].indexOf('o')); Page 3 of 9
Part 3. (20 points) A gym is offering a yoga class every day, except for Saturdays and Sundays. The gym administration collected attendance data for the past 4 weeks. Complete the program below to visualize the attendance data as follows. The user will enter 200 integers, representing daily attendance of an exercise class. The first number corresponds to a Monday of the first week, the second corresponds to Tuesday and so on with the sixth number representing the Monday of the next week, etc. Print out a diagram showing attendance for every day in the format displayed in the sample interaction below (user input appears in boldface): Please enter attendance data for 200 days: 6 7 21 3 4 5 11 5 3 7 9 8 5 6 2 9 5 6 4 5 Displaying the data week-by-week: Su M T W Th F S - 6 7 21 3 4 - - 5 11 5 3 7 - - 9 8 5 6 2 - - 9 5 6 4 5... In your output, do not worry about perfect alignment of spaces, but make sure to - Print the first row showing the days as shown - Display each week s data on a separate line - Leave a space between each pair of numbers - Put a dash instead of a number for Sunday and Saturday Hints: use Scanner method nextint() to read numbers one by one and store them in an array, before you start printing them out. public class ShowAttendance { public static void main (String []arg){ Scanner kb = new Scanner (System.in); Page 4 of 9
Page 5 of 9
Part 4. (35 points) Consider the definition of class Seat below, representing a seat in Boston s Jordan Hall. The comments in the code define the purpose of each variable or constant. public class Seat { private char row; // character denoting the row private int seatnumber; // seat number in the specified row public static final double STANDARD_PRICE= 45.50; //standard seat price for one performance public Seat (){ this.row = 'A'; this.seatnumber = 1; // Accessors public char getrow() { return this.row; public int getseatnumber() { return this.seatnumber; /* public String sideofhouse(){ Returns a string left or right designating in which side of the hall this seat is located.*/ public String sideofhouse(){ if (seatnumber%2 ==0) return "left"; else return "right"; Define the following methods for the Seat class. Note that parts a,b,c,d can be completed independently of each other. To complete part e you need to at least have completed the headers for methods in a,b,c. a. (5 points) Public instance method tostring () that does not take any parameters and returns a String, containing the row and seat number. For example, if the calling objects represents seat number 56 in row N, the tostring() method should return: Seat N-56 Page 6 of 9
b. (10 points) A constructor, which takes two parameters: a character representing the row, and an integer representing the seat number in that row. The constructor should check that the character parameter stores a letter, using method Character.isLetter() described below, and in such case assign the row instance variable equal to the uppercase version of that letter. Otherwise, the constructor should assign instance variable row to be equal to A. Furthermore, the constructor must check if the parameter denoting the seat number is in the range of 1..60, inclusively, and in such case assign the seatnumber instance variable equal to the value of the parameter. Otherwise, the seatnumber should be assigned value 1. Hint: Static method Character.isLetter(ch) returns true if and only if character ch is a letter. Page 7 of 9
c. (10 points) Public static method numseatsinrow() for class Seat. The method should accept two parameters: an array of Seat objects and a char, designating a row. The method should calculate and return how many of the seats stored in the array that is passed as the first parameter are in the row specified by the second parameter. The check must be case insensitive, i.e. a and A should be treated as the same row. Page 8 of 9
d. (10 points) For this part, you need to at least have completed the headers for methods in a and b. In the unfinished program that follows, add code to 1. Create an object of class Seat based on the data obtained from the user. 2. Print the created object. 3. Call method sideofhouse () for the created object and display the value it returns. 4. Print the standard seat price as defined by the Seat class. The price must be formatted to display exactly 2 digits after the decimal period. public class TestSeat { public static void main(string[] args) { Scanner kb = new Scanner(System.in); System.out.println("Please enter the row"); char row = kb.next().charat(0); System.out.println("Please enter the seat number"); int seatnum = kb.nextint(); Page 9 of 9