Complete the method definition below private boolean

This preview shows page 16 - 21 out of 26 pages.

Complete the method definition below: private boolean isWithinRange( int lower, int upper , int value ){ if (value >=lower && value <= upper) { return true; } return false; } (6 marks)
Image of page 16

Subscribe to view the full document.

VERSION 1 - 17 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED 19. The ExamMark class is defined as follows: public class ExamMark { private int mark; public ExamMark(int examMark) { mark = examMark; } public int getMark() { return mark; } public void setMark(int examMark) { mark = examMark; } public boolean getHasPassed() { return mark >= 50; } public String toString() { String markInfo = ""; markInfo = markInfo + "Exam mark: " + mark + "%"; if (getHasPassed()) { markInfo = markInfo + " " + " A PASS"; } else { markInfo = markInfo + " " + " NOT A PASS"; } return markInfo; } } Answer the following questions (a) – (g) which refer to the ExamMark class. a) Write a Java statement which declares an array of ExamMark objects. Call the array marks , i.e. use the identifier marks for the array variable. ExamMark[] marks;
Image of page 17
VERSION 1 - 18 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED b) Write a Java statement which initialises the marks array (that you declared in part (a) above) to be large enough to store exactly 500 ExamMark objects. marks = new ExamMark[500]; c) Write a Java statement which stores the object, goodMark , in index position 0 of the marks array. The goodMark variable has been declared and initialised for you. ExamMark goodMark = new ExamMark(96); marks[0] = goodMark; For parts d) to g) below, assume that the marks array has been created and contains some ExamMark objects. d) Complete the Java statement below which assigns the ExamMark object in index position 2 of the marks array to the variable, markEric . ExamMark markEric = marks[2]; e) Complete the Java statement below which obtains the mark of the ExamMark object in index position 5 of the marks array, and stores it in the variable aMark . int aMark = marks[5].getMark();
Image of page 18

Subscribe to view the full document.

VERSION 1 - 19 - COMPSCI 101 Question and Answer Booklet ID .......................................... CONTINUED f) Complete the single Java statement below which assigns true to the variable hasPassed if the ExamMark object in index position 12 of the marks array is a pass mark and assigns false otherwise. boolean hasPassed = marks[5].getHasPassed(); g) Complete the countNumberPassed() method below which processes the marks array and returns the number of elements of the marks array which are pass marks. Note: the number of ExamMark objects in the marks array is given by the parameter, numberOfMarks . private int countNumberPassed(ExamMark[] marks,int numberOfMarks){ int numberPassed = 0; for(int i=0; i<numberOfMarks; i++) { if (marks[i].getHasPassed() == true) { numberPassed++; } } return numberPassed; } (10 marks)
Image of page 19