Private string getinitial string name return

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

private String getInitial ( String name ) { return "" + name.charAt(0); } (2 marks)
Image of page 6

Subscribe to view the full document.

Question/Answer Sheet - Page 7 - CompSci 101 ID: ........................................................ CONTINUED k) Complete the swapXAndY() method which swaps the x and y values of the Point object passed as a parameter. private void swapXAndY(Point pt) { int temp; temp = pt.x; pt.x = pt.y; pt.y = temp; (2 marks) } l) Write a Java boolean expression that tests whether the length of the String variable, name , is between 1 and 8 (inclusive). name.length() >= 1 && name.length() <= 8 (2 marks) m) Write a Java boolean expression that tests whether the String variable, name , is equal to "John". (Hint: remember that Strings are objects, not primitive types.) name.equals("John") (2 marks)
Image of page 7
Question/Answer Sheet - Page 8 - CompSci 101 ID: ........................................................ CONTINUED n) What output is produced by the following code? int number = 2; while (number < 5) { System.out.println(number * number); number++; } 4 9 16 (2 marks) o) Write a for loop that prints out each letter (from left to right) of the String variable, name . Each letter should be printed on a separate line. for (int i = 0; i < name.length(); i++) { System.out.println(name.charAt(i)); } (2 marks)
Image of page 8

Subscribe to view the full document.

Question/Answer Sheet - Page 9 - CompSci 101 ID: ........................................................ CONTINUED p) What is the output of the following program? public class Program { public void start() { calculateDiscount(36, 500); } private void calculateDiscount(int age, double amount) { if (age > 50 && amount > 200) { System.out.println("30% Discount"); } else if (age > 50) { System.out.println("10% Discount"); } else if (amount > 200) { System.out.println("20% Discount"); } else { System.out.println("No Discount"); } } } 20% Discount (2 marks) q) Complete the Java code which prompts the user for their age, reads the user's age (use the Keyboard class to do this) and checks if the age entered by the user is greater than or equal to 21. If the age is greater than or equal to 21, the output should be "Age okay", otherwise the output should be "Under age". int age; System.out.print("Enter age: "); age = Integer.parseInt(Keyboard.readInput()); if (age >= 21) { System.out.println("Age okay"); } else { System.out.println("Under age"); } (2 marks)
Image of page 9
Question/Answer Sheet - Page 10 - CompSci 101 ID: ........................................................ CONTINUED Question 1 r) – t) are based on the following class. The Time24Hr class definition below is incomplete (incomplete sections are denoted by ... ). public class Time24Hr { private int hour; private int minutes; private int seconds; public Time24Hr (int h, int m, int s) { hour = h; minutes = m; seconds = s; } public ... getMinutes() { ... } public ... setHour(...) { ... } public ... toString() { ... } } r) Define the accessor (get) method that returns the minutes of the Time24Hr object. public int getMinutes() { return minutes; } (2 marks)
Image of page 10

Subscribe to view the full document.

Question/Answer Sheet - Page 11 - CompSci 101 ID: ........................................................ CONTINUED s) Define the mutator (set) method that updates the hour of the of the Time24Hr object. public void setHour(int h) { hour = h; } (2 marks) t) Define the toString() method that returns the time of the Time24Hr object in the following format: hour:minutes:seconds. For example, "9:45:16", "10:5:8". public String toString() { return hour + ":" + minutes + ":" + seconds; } (2 marks)
Image of page 11
Question/Answer Sheet - Page 12 - CompSci 101 ID: ........................................................
Image of page 12

Subscribe to view the full document.

Image of page 13
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, Subroutine, ... ..., Circle class

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