Continued question 5 10 marks a complete the output

This preview shows page 12 - 18 out of 23 pages.

CONTINUED Question 5 (10 marks) a) Complete the output produced by the following code. boolean result; boolean a = true; boolean b = false; boolean c = true; boolean d = (a && b) || (a && c); boolean e = a || (!c); boolean f = (c || b) && (c || a); System.out.println(“d: + d + “ e: “ + e + “ f: “ + f); result = (!a && !b) || ((c && !b) || (!e || f)); System.out.println(“Result: ” + result); d: e: f: Result: (5 marks)
Image of page 12

Subscribe to view the full document.

Question/Answer Sheet - Page 13 - CompSci 101 ID: ........................................................... CONTINUED b) The following program does not compile. Give the line number of the statement which causes the compile error and state the reason for the compile error. public class Example { public void start() { double cost = 53.0; int age = 68; final double GST = 0.125; if (age <= 12 || age >= 65){ double discount = 0.15; System.out.println("Eligible for discount"); } double price = cost * (1 + GST) * (1 - discount); System.out.println(price); int limit = (int)(Math.random() * 999 + 1); if (price / limit > 1) { System.out.println("Too expensive"); } else { System.out.println("Affordable"); } } } line number: Reason for the compile error: (5 marks) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Image of page 13
Question/Answer Sheet - Page 14 - CompSci 101 ID: ........................................................... CONTINUED Question 6 (9 marks) Complete the definition of the calculateDiscount() method in the program below so that a String containing the appropriate percentage is returned by the method. The format of the String which is returned by the calculateDiscount() method is a number followed by the ‘%’ symbol, e.g., “0%”, “10%, “20%” or “30%”. A discount is given based on the purchase amount and the age of the customer as described below: 30% discount for a customer who is 65 years of age or over and whose purchase amount is over 250, 20% discount for a customer whose purchase amount is over 250 but whose age is younger than 65, 10% discount for any customer whose age is 65 or over, and whose purchase amount is not over 250, 0% discount in all other cases. public class Program { public void start() { System.out.print(“Enter amount of purchase:”); int amount = Integer.parseInt(Keyboard.readInput()); System.out.print(“Enter age of customer:”); int age = Integer.parseInt(Keyboard.readInput()); String discount = calculateDiscount(age, amount); System.out.println(“You have a ” + discount + ” discount!”); } private ________ calculateDiscount(_____________ __________________) {
Image of page 14

Subscribe to view the full document.

Question/Answer Sheet - Page 15 - CompSci 101 ID: ........................................................... CONTINUED } } (9 marks)
Image of page 15
Question/Answer Sheet - Page 16 - CompSci 101 ID: ........................................................... CONTINUED Question 7 (10 marks) Give the output when the following program is executed. public class MyProgram { public void start() { System.out.print("1 "); methodOne(); System.out.print("2 "); methodTwo(); System.out.println("3 "); } private void methodOne() { methodTwo(); System.out.print("4 "); } private void methodTwo() { System.out.print("5 "); methodThree(); } private void methodThree() { System.out.print("6 "); } } //Show output here (10 marks)
Image of page 16

Subscribe to view the full document.

Question/Answer Sheet - Page 17 - CompSci 101 ID: ...........................................................
Image of page 17
Image of page 18
You've reached the end of this preview.
  • Summer '12
  • AdrianaFerraro
  • Computer Science, English-language films, .........

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