Document 11492575

advertisement
[2013-14 Semester B]
CS2312 Problem Solving and Programming
Quiz 1
Student ID: ______________ (Seat Number: ______)
Please read the questions in this page and complete them in the blanks given in the following pages.
Duration: 40 minutes. Total mark: 40 marks +0.1 for Bonus version of Q2
Q1. Listing of days in a month [10 marks]
Complete the given program which inputs a date from
the user and list all of the days in that month.
Your task: fill in the blanks in the main method and
add the required method in the Day class.
Sample rundown is given on the right.
(Underlined contents are input by the user)
Please enter the date (eg. "2013 12 31"): 2013 2 15 Days in the month: 1 Feb 2013 2 Feb 2013 3 Feb 2013 4 Feb 2013 5 Feb 2013 6 Feb 2013 7 Feb 2013 8 Feb 2013 9 Feb 2013 10 Feb 2013 11 Feb 2013 12 Feb 2013 13 Feb 2013 14 Feb 2013 15 Feb 2013 16 Feb 2013 17 Feb 2013 18 Feb 2013 19 Feb 2013 20 Feb 2013 21 Feb 2013 22 Feb 2013 23 Feb 2013 24 Feb 2013 25 Feb 2013 26 Feb 2013 27 Feb 2013 28 Feb 2013 Q2. Enquiry of helper duties [30 marks]
Complete the given program which checks for helper duties of lab sessions Lab#1, Lab#2 etc..
Your task: fill in the blanks in the main method and complete the
HelperDuties class which encapsulates the schedule in a 2D array and
provides the following methods:
- A constructor: sets up the schedule based on an input file. See the sample 
We assume that there are 5 lab sessions, 2 helpers per session.
Sample input file
(line 1: helpers for Lab#1,
line 2: helpers for Lab#2, etc..):
- A boolean method called hasDuty: tells whether a helper has any duty.
- A void method called showDuties: outputs the duties of a helper
Sample rundowns (Underlined contents are input by the user):
Sample rundown #1
Sample rundown #2
Input the file pathname: c:\sh1.txt Enter the student ID to search: 50123456 Result: Lab #1 Lab #2 Lab #4 Total number of sessions: 3
Input the file pathname: c:\sh1.txt Enter the student ID to search: 50654321 Result: No duty Q2 [Bonus version, 30.1 marks]
Sample input file for the bonus version
This version needs to handle:
- A variable number of lab sessions
- Variable counts of helpers for the sessions.
(Line 1 : the number of lab sessions,
Remaining: pairs of lines which give the count of helpers
of a session and then the helper ids of that session. )
Notes for creating a ragged 2D array of integers:
To create an array of empty 1D arrays: arr2D = new int[size][];
To create each 1D array: arr2D[idx]=new int[count]; Q1.
Day.java
public class Day { private int year; private int month; //List the days in the month private int day; public void listDaysInTheMonth() // Constructor { public Day(int y, int m, int d) {..} // check if a given year is a leap year static public boolean isLeapYear(int y) {..} // check if y,m,d valid static public boolean valid(int y, int m, int d) {..} // Return a string for the day like dd MMM yyyy public String toString() {..} // Accessor methods public int getYear() {return year;} public int getMonth() {return month;} public int getDay() {return day;} // create and return the "next day" of // the current day object public Day next() {..} } }
(8 Marks)
Main.java
import java.util.*; public class Main { public static void main(String[] args) { /* Input */ System.out.print("Please enter the date (eg. \"2013 12 31\"): "); Scanner scannerObj = new Scanner(System.in); int y, m, d; y=scannerObj.nextInt(); m=scannerObj.nextInt(); d=scannerObj.nextInt(); System.out.println("\nDays in the month:"); /* Create the day object and use the .listDaysInTheMonth() method */ scannerObj.close(); (2 Marks)
} }
Student ID: ______________ (Seat Number: ______)
Q2.
HelperDuties.java
import java.util.*; import java.io.*; public class HelperDuties { private int [][] helpers; //The 2D array //Constructor: read file data and setup the helpers array public HelperDuties(String filepathname) throws FileNotFoundException { } Continue on next page Sample input file
(line 1: helpers for Lab#1,
line 2: helpers for Lab#2, etc..):
(12 Marks)
HelperDuties.java
//The showDuties method: Display the duties of a helper _______________________showDuties(________________) { //The hasDuty method: Check if the helper has at least a duty }
________________________hasDuty(__________________) { } (9 Marks)
} (7 Marks)
Main.java
.. public static void main(String[] args) throws FileNotFoundException
{ Scanner in = new Scanner(System.in); System.out.print("Input the file pathname: "); String filepathname = in.next(); HelperDuties hd = new HelperDuties(filepathname); System.out.print("Enter the student ID to search: "); int id=in.nextInt(); System.out.println("\nResult: "); }
} Sample rundown #1
Input the file pathname: c:\sh1.txt Enter the student ID to search: 50123456 Result: Lab #1 Lab #2 Lab #4 Total number of sessions: 3
if (____________________________________________) else System.out.println("No duty"); in.close(); (2 Marks)
Total mark: _____ (/40)
Download