1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Java Past Paper Question!

Discussion in 'HTML, Graphics & Programming' started by Darr2cool78, 8 May 2010.

  1. Darr2cool78

    Gangster

    Joined: 28 May 2008

    Posts: 346

    Hi i am currently studying for my Year 1 Java exam by going over past papers! I am stuck on one of the questions and was wondering if any of you programming whizzs could give me a hand :p the question is -

    Question 8.
    Consider the following classes Date and LibraryBook:

    class Date {
    private int day, month, year;

    public Date(int aDay, int aMonth, int aYear) {
    //assigns values to the three properties
    }

    public Date (Date copyDate) {
    //creates a date identical to copyDate
    }

    public boolean isAfter (Date d) {
    //returns true if the date of the object calling the
    //method is after d
    }

    public void addWeeks(int numWeeks){
    //adds numWeeks to a date
    }
    }// Date



    class LibraryBook {

    private String title, author, code;
    private String borrowerName;
    //additional properties required

    public LibraryBook(String title, String author,
    String code) {
    //assigns values to corresponding properties
    }

    public void createLoan(int day, int month, int year,
    String name, int numWeeks){
    //creates and sets appropriate values for loanDate
    //and dueDate and sets values of borrowerName and
    //onLoan
    }

    public boolean isOverdue(Date d) {
    //determines whether the book is overdue
    }

    public boolean isOnLoan(){
    //determines whether the book is on loan
    }

    public String getBorrowerName(){
    //returns the name of the borrower
    }
    }// LibraryBook

    (a) Provide the following for the class LibraryBook:

    (i) declare additional properties, one to represent whether the library book is on loan, and two instances of the class Date, one for the date on which the loan starts and one for the date on which the book is due to be returned;
    (3 marks)

    (ii) code for the methods createLoan, isOverdue, isOnLoan and getBorrowerName.
    (12 marks)

    (b) Write fragments of code which could be included in an application class to:
    (i) create an instance of the class LibraryBook (with suitable values);
    (2 marks)

    (ii) create a loan for this book for 12 weeks for a borrower called Jones (you will need to specify suitable values for the day, month and year);
    (2 marks)

    (iii) creates an instance of the class Date for today’s date and then checks whether the book is on loan and, if so, whether it is overdue. If it is, the borrower’s name should be printed out.
    (6 marks)

    (c) Write another version of the method createLoan in the class LibraryBook which replaces the three parameters for the day, month and year with a single parameter which is an instance of the class Date and show how this method could be used to provide an alternative answer to b(ii).
    (5 marks)

    Any help would be greatly aprecicated ;)
     
  2. jack.mitchell

    Wise Guy

    Joined: 15 Apr 2008

    Posts: 1,016

    Location: West Didsbury, Manchester

    Do you need help for a specific question? I can start you off..

    a)i)

    startDate Date = new Date(8,5,2010);
    endDate Date = new Date(15,5,2010);

    book LibraryBook = new LibraryBook("Book name","Author","1043857");
    book.createLoan(8,5,2010,"SomeName",1);
     
  3. Darr2cool78

    Gangster

    Joined: 28 May 2008

    Posts: 346

    Thanks for the start much appreicated!

    Part A I think I can do but part B is the bit that is confusing me! :confused:
     
  4. jack.mitchell

    Wise Guy

    Joined: 15 Apr 2008

    Posts: 1,016

    Location: West Didsbury, Manchester

    I don't think I read the question properly, i'm not quite sure what they want for a)i) because they seem to be asking for the same thing in b) anyway its fairly simple if i'm understanding it correctly.

    (b) Write fragments of code which could be included in an application class to:
    (i) create an instance of the class LibraryBook (with suitable values);
    (2 marks)

    book libraryBook = new libraryBook("book name", "book author", "book code");

    (ii) create a loan for this book for 12 weeks for a borrower called Jones (you will need to specify suitable values for the day, month and year);
    (2 marks)

    book.createLoan(8,5,2010,"Jones", 12);

    (iii) creates an instance of the class Date for today’s date and then checks whether the book is on loan and, if so, whether it is overdue. If it is, the borrower’s name should be printed out.
    (6 marks)

    todaysDate Date = new Date(8,5,2010);

    if( book.isOnLoan() ) {

    if (book.isOverdue(todaysDate)) {

    System.out.println(book.getBorrowersName());

    }

    }

    Pretty standard stuff, try doing a few basic exercises involving creating and using classes. There will be loads on the web then you'll get the hang of it in no time.
     
  5. Ladforce

    Hitman

    Joined: 17 Feb 2006

    Posts: 898

    Location: Fleet

    youve got the class name and variable name the wrong way round...should be Date todaysDate = new Date(8,5,2010) etc....
     
  6. Darr2cool78

    Gangster

    Joined: 28 May 2008

    Posts: 346

    Thanks for all the help its been very helpful indeed :)

    I have another small question if thats alright, i have it mostly done but am unsure about the last bit! This is for a student class were there is an array of 10 students with values of Name and Mark!

    (i) declares and creates an array called studentArray containing 10 instances of Student; (3marks)

    I have.. Student [] list = new Student [10];
    for (int i = 0; i < 10; i ++)
    list = new Student();

    (ii) reads in the name and mark for each element of the array by calling the readStudent method specified in part (a);

    I have... for (int i = 0; i < 10; i ++)
    list.readDetails();

    (iii) determines how many students obtained a mark above 60 and prints out the answer; (6 marks)

    for this.. for (int i = 1; i < 10; i++) {
    if (list.getMark > 60).......then im stuck i no how to display names but not how many :(


    (iv) prompts for and reads in a name from the keyboard, searches the array studentArray for the name entered, and prints the mark obtained by the student if the name is found (or the mark for each student with this name).

    this one i dont no :(
     
  7. Kua

    Hitman

    Joined: 21 Jul 2008

    Posts: 512

    Location: Lancaster

    Where are you doing your degree btw? I'm a first year CS student at Lancaster.
     
  8. Zitron

    Gangster

    Joined: 15 Jun 2009

    Posts: 149

    Location: Bucks



    Just use a int variable within your if statement and increment it.

    Code:
    int count;
    for (int i = 0; i < 10; i++)  {
        if (list[i].getMark > 60){
           count++;
    }
    }
    

    You could just use a for loop as you have in the other ones. Once you've read the name in use the String compareTo method to see if they match, compareTo returns a 0 (int) if they match or 1 if they don't. (Check the API)
    Something like:
    Code:
    for (int i = 0; i < 10; i++){
    
           if (list[i].getName.compareToIgnoreCase(input)==0){
               System.out.println(list[i].getName +list[i].getMark);
           }