Public class printyearlyrent public void start final

This preview shows page 18 - 26 out of 26 pages.

public class PrintYearlyRent() { public void start() { final int WEEKS_IN_YEAR = 52; System.out.print("Enter weekly rent: $"); int weeklyRent = Integer.parseInt( Keyboard.readInput()); int yearlyRent = weeklyRent * WEEKS_IN_YEAR; System.out.println("Yearly rent $" + yearlyRent);
Image of page 18

Subscribe to view the full document.

Question/Answer Sheet - Page 19 - CompSci 101 ID: ........................................................... continued (10 marks) } }
Image of page 19
Question/Answer Sheet - Page 20 - CompSci 101 ID: ........................................................... continued Question 9 (12 marks) a) In the start() method below, write Java code which declares and creates an integer array of size 24 and then sets each element inside the array to be two times its index, e.g., the element in position 6 of the array has the value 12, the element at position 7 of the array has the value 14, etc. The name of the array variable should be nums . public class CreateArray () { public void start() { int[] nums = new int[24]; for(int i = 0; i < nums.length; i++) { nums[i] = 2 * i; } (6 marks) } }
Image of page 20

Subscribe to view the full document.

Question/Answer Sheet - Page 21 - CompSci 101 ID: ........................................................... continued b) The getMax() method is passed an integer array as a parameter. The method returns the maximum value of all the elements in the array. Complete the code. Note : you may assume that the parameter array contains at least one element. private int getMax(int[] nums) { int max = nums[0]; for(int i = 1; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; } } return max (6 marks) }
Image of page 21
Question/Answer Sheet - Page 22 - CompSci 101 ID: ........................................................... continued Question 10 (7 marks) Parts a), b) and c) require you to complete the Score class definition below. public class Score { private String captain; private int totalScore; public Score(String captain, int score) { this.captain = captain; totalScore = score; } public String getCaptain() { return captain; } (1 mark) public void setCaptain( String captain ) { this.captain = captain; } (1 mark)) public void addToScore(int score) { totalScore = totalScore + score; } public void setTotalScore( int score ) { totalScore = score; } (1 mark)) public int getTotalScore() { return totalScore; } public String toString() { return captain + ", Score: " + totalScore; } }
Image of page 22

Subscribe to view the full document.

Question/Answer Sheet - Page 23 - CompSci 101 ID: ........................................................... continued a) In the Score class above complete the mutator method, setCaptain() . b) In the Score class above complete the accessor method, getCaptain() . c) In the Score class above complete the mutator method, setTotalScore() . d) Complete the output produced when the following code is executed using the completed Score class. Score s1 = new Score("Ted", 6); Score s2 = new Score("Alice", 8); System.out.println("1. "+ s1.toString()); System.out.println("2. "+ s2.toString()); s1.addToScore(4); s2.addToScore(3); s2.setName("Betty"); s1.addToScore(s1.getTotalScore()); System.out.println("3. "+ s1.toString()); System.out.println("4. "+ s2.toString()); 1: Ted, Score: 6 2: Alice, Score: 8 3: Ted, Score: 20 4: Betty, Score: 11 (4 marks)
Image of page 23
Question/Answer Sheet - Page 24 - CompSci 101 ID: ........................................................... continued OVERFLOW PAGE (Please number the question(s) carefully and indicate clearly under the relevant question that you have overflowed to this page)
Image of page 24

Subscribe to view the full document.

Question/Answer Sheet - Page 25 - CompSci 101 ID: ........................................................... continued ROUGH WORKING (WILL NOT BE MARKED) (You may detach this page from the answer booklet and use it for rough working)
Image of page 25
Question/Answer Sheet - Page 26 - CompSci 101 ID: ........................................................... continued ROUGH WORKING (WILL NOT BE MARKED) (You may detach this page from the answer booklet and use it for rough working)
Image of page 26
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern