Private void manipulatepoints point p1 new point5 2

• Notes
• UltraLightningCobra8457
• 34

This preview shows page 6 - 13 out of 34 pages.

private void manipulatePoints() { Point p1 = new Point(5, 2); Point p2 = p1; p2 = manipulate(p1); System.out.println(p1.x + " " + p1.y + " " + p2.x + " " + p2.y); } private Point manipulate(Point p) { Point p2 = p; p.x = 1; p.y = 3; return p2; }

Subscribe to view the full document.

Question/Answer Sheet - 7 - CompSci 101 ID: ........................................................... CONTINUED 1 3 1 3 (2 marks) c) Give the output produced when the following manipulatePoints() method is executed. private void manipulatePoints() { Point p1 = new Point(5, 2); Point p2 = p1; p2 = manipulate(p1); System.out.println(p1.x + " " + p1.y + " " + p2.x + " " + p2.y); } private Point manipulate(Point p) { Point p2 = new Point(p); p.x = 1; p.y = 3; return p2; } 1 3 5 2 (2 marks)
Question/Answer Sheet - 8 - CompSci 101 ID: ........................................................... CONTINUED Question 4 (4 marks) Given the getArray() method defined below: private int[] getArray(int[] a, int[] b) { int[] c = new int[a.length + b.length]; for (int i = 0; i < b.length; i++) { c[i] = b[i]; } for (int j = 0; j < a.length; j++) { c[b.length + j] = a[j]; } return c; } what output is produced when the following code is executed? int[] a = {3, 1, 7}; int[] b = {2, 4, 8, 9}; int[] c = getArray(a, b); for (int i = 0; i < c.length; i++) { System.out.print(c[i] + " "); } 2 4 8 9 3 1 7 (4 marks)

Subscribe to view the full document.

Question/Answer Sheet - 9 - CompSci 101 ID: ........................................................... CONTINUED Question 5 (15 marks) a) Complete the getSum() method. This method is passed an array of integer values as a parameter. The method returns the sum of all the elements of the array. NOTE : For this method you MUST use a for loop. private int getSum(int[] numbers) { int sum = 0; for (int i = 0; i < numbers.length; i++) { sum = sum + numbers[i]; } return sum; } (5 marks)
Question/Answer Sheet - 10 - CompSci 101 ID: ........................................................... CONTINUED b) Complete the printNumbersLessThan65() method. This method is passed an array of integer values as a parameter. The method prints each element of the array which has a value less than 65 (printing each number on a new line). NOTE : For this method you MUST use a while loop. private void printNumbersLessThan65(int[] numbers) { int i = 0; while(i < numbers.length) { if (numbers[i] < 65) { System.out.println(numbers[i]); } i++; } } (5 marks)

Subscribe to view the full document.

Question/Answer Sheet - 11 - CompSci 101 ID: ........................................................... CONTINUED c) Complete the printThirdLetters() method. This method is passed an array of String objects as a parameter. The method prints the third letter, i.e., the letter in position 2, of the elements of the array which have more than 2 letters. For example, executing the following code with the completed method: String[] words = {"ant", "it", "preview", "I", "best", "mat"}; printThirdLetters(words); prints the letters: test private void printThirdLetters(String[] words) { for (int i = 0; i < words.length; i++) { if (words[i].length() > 2) { System.out.print(words[i].charAt(2)); } } } (5 marks)
Question/Answer Sheet - 12 - CompSci 101 ID: ........................................................... CONTINUED Question 6 (15 marks) The Book class represents an item in a book store. The instance variables of the Book class are defined as follows. public class Book { private String code; private String title; private int price; ... } a) Define the constructor method for the Book class which can be used to create a Book object with a given code, title and price. For example, Book b = new Book ("1005", "Java Programming", 10); creates a Book object with the code "1005", title "Java Programming" and price \$10.

Subscribe to view the full document.

You've reached the end of this preview.
• Summer '12
• Computer Science, Method, ........., JPanel, Mutator method

{[ snackBarMessage ]}

What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern