Questionanswer sheet page 11 compsci 101 surname

This preview shows page 11 - 17 out of 19 pages.

Question/Answer Sheet - Page 11 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... } } (8 marks)
Image of page 11

Subscribe to view the full document.

Question/Answer Sheet - Page 12 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... 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: true e: true f: true Result: true (5 marks)
Image of page 12
Question/Answer Sheet - Page 13 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... 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: 10 Reason for the compile error: The variable ‘discount’ is declared on line 7 inside the if statement. Its scope is inside the if statement (from line 7 to line 9) and it cannot be used outside the if statement . (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

Subscribe to view the full document.

Question/Answer Sheet - Page 14 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... 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 String calculateDiscount( int age, int amount ) { if (age >= 65 && amount > 250) { return "30%"; } else if (age >= 65) { return "10%"; } else if (amount > 250) { return "20%"; } else { return "0%"; }
Image of page 14
Question/Answer Sheet - Page 15 - CompSci 101 SURNAME: ...................................................... FORENAMES: ........................................................... } (9 marks)
Image of page 15

Subscribe to view the full document.

Question/Answer Sheet - Page 16 - CompSci 101 SURNAME: ...................................................... FORENAMES: ...........................................................
Image of page 16
Image of page 17
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