10 marks candidates name 8 compsci 101 6 what is the

This preview shows page 7 - 12 out of 16 pages.

} (10 marks)
Image of page 7

Subscribe to view the full document.

Candidate's Name:___________________ 8 COMPSCI 101 6. What is the output when the Q6 application is executed? public class Q6{ public static void main(String[] args){ for(int i=50; i<100; i=i+10){ if (i%20 == 0) System.out.println("i=" + i + ": " + (i+2)); else System.out.println("i=" + i + ": " + (i-2)); } } } >java Q6 i=50: 48 i=60: 62 i=70: 68 i=80: 82 i=90: 88 (8 marks)
Image of page 8
Candidate's Name:___________________ 9 COMPSCI 101 7. Complete a method accumArray() which is passed one parameter, an array of integers. This method returns a new int array which contains the cumulative sum of all the previous elements (including the current element) of the parameter array. For example, if the parameter array contains the integer values: 10 20 30 10 then the accumArray() method will return a new array which contains the integer values: 10 30 60 70. As a further example if the parameter array contains the integer values: 5 20 15 10 5 5 then the accumArray() method will return a new array which contains the integer values: 5 25 40 50 55 60 private static int[] accumArray( int[] intArray ){ int[] returnA = new int[intArray.length]; int upTo = 0; for(int i=0; i<intArray.length; i++){ upTo = upTo + intArray[i]; returnA[i] = upTo; } return returnA; } (12 marks)
Image of page 9

Subscribe to view the full document.

Candidate's Name:___________________ 10 COMPSCI 101 8. Study the definition of the Run class shown below. public class Run{ private static int nextID = 1000; private int runID; //ID number of the run private int numKms; //number of kilometres run private int numMins; //number of minutes public Run(int kms, int mins){ runID = nextID; nextID++; numKms = kms; numMins = mins; } public int getRunID(){ return runID; } public void setNumKms(int kms){ numKms = kms; } public void setNumMins(int mins){ numMins = mins; } public String toString(){ return "Run id: "+runID+ ", Kilometres: "+numKms+ ", Minutes: "+numMins; } public boolean isFasterThan(Run otherR){ return (double)numMins/numKms < (double)otherR.numMins/otherR.numKms; } } Write the code for questions a), b), c) and d) in the spaces provided. a) Write code to create 2 Run instances which look like the following in memory: Run run1, run2; run1 = new Run(10,35); //2 marks run2 = new Run(20,74);
Image of page 10
Candidate's Name:___________________ 11 COMPSCI 101 b) Write code to change [do NOT create new instances] the 2 Run instances so they now look like the following in memory: run1.setNumKms(12); //4 marks run1.setNumMins(38); run2.setNumKms(21); run2.setNumMins(75); c) Write code using the 2 Run instances which will give the following output: Run1: Run id: 1000, Kilometres: 12, Minutes: 38 Run2: Run id: 1001, Kilometres: 21, Minutes: 75 //4 marks System.out.println("Run1: "+run1.toString()); System.out.println("Run2: "+run2.toString()); d) Write code which tests which of the 2 Run instances is faster. If run1 is faster the output will be: Run 1000 is faster If run2 is faster the output will be: Run 1001 is faster Note: The runID value shown in the output to part d) is obtained from the Run instance.
Image of page 11

Subscribe to view the full document.

Image of page 12
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Subroutine, Letter case, ASCII, kilometres

{[ 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