




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Section A (20 Marks) Answer all 10 parts of section A, This section consists of 10 multiple-choice questions all of which should ideally be attempted. Answers are to be written on the MCQ sheet provided, NOT on the Examination paper. Section B (80 Marks) Answer Any Two Questions
Question 1
Section A (20 Marks) Attempt all 10 questions Mark your answers on the MCQ sheet provided
Question 2
Review the following application and explain the purpose of the application and how it works. Explain in detail, the purpose of each the classes and the workings of their methods, using the line numbers for reference.
1 public class SummerSale 2 { 3 private String name; 4 private double price; 5 6 public SummerSale( ) 7 { 8 name = "No name yet"; 9 price = 0; 10 } 11 12 public SummerSale(String theName, double thePrice) 13 { 14 setName(theName); 15 setPrice(thePrice); 16 } 17 18 public SummerSale(SummerSale originalObject) 19 { 20 if (originalObject == null) 21 { 22 System.out.println("Error: null Sale object."); 23 System.exit(0); 24 } 25 else 26 name = originalObject.name; 27 price = originalObject.price; 28 } 29 30 public static void announcement( ) 31 { 32 System.out.println("This is the Sale class."); 33 } 34 35 public double getPrice( ) 36 { 37 return price; 38 } 39 40 public void setPrice(double newPrice) 41 { 42 if (newPrice >= 0) 43 price = newPrice; 44 else 45 { 46 System.out.println("Error: Negative price."); 47 System.exit(0); 48 } 49 } 50
Section B (80 Marks) Answer Any TWO Questions
51 public String getName( ) 52 { 53 return name; 54 } 55 56 public void setName(String newName) 57 { 58 if (newName != null && newName != "") 59 name = newName; 60 else 61 { 62 System.out.println("Error: Improper name value."); 63 System.exit(0); 64 } 65 } 66 67 public String toString( ) 68 { 69 return (name + " Price and total cost = €" + price); 70 } 71 72 public double bill( ) 73 { 74 return price; 75 } 76 77 public boolean equalDeals(SummerSale otherSale) 78 { 79 if (otherSale == null) 80 return false; 81 else 82 return (name.equals(otherSale.name) 83 && bill( ) == otherSale.bill( )); 84 } 85 86 public boolean lessThan (SummerSale otherSale) 87 { 88 if (otherSale == null) 89 { 90 System.out.println("Error: null Sale object."); 91 System.exit(0); 92 } 93 else 94 return (bill( ) < otherSale.bill( )); 95 } 96 97 public boolean equals(Object otherObject) 98 { 99 if (otherObject == null) 100 return false; 101 else if (getClass( ) != otherObject.getClass( )) 102 return false; 103 else 104 { 105 SummerSale otherSale = (SummerSale)otherObject; 106 return (name.equals(otherSale.name) 107 && (price == otherSale.price)); 108 } 109 } 110
168 public String toString( ) 169 { 170 return (getName( ) + " Price = €" + getPrice( ) 171 + " Discount = " + discount + "%\n" 172 + " Total cost = €" + bill( )); 173 } 174 175 public boolean equals(Object otherObject) 176 { 177 if (otherObject == null) 178 return false; 179 else if (getClass( ) != otherObject.getClass( )) 180 return false; 181 else 182 { 183 SummerDiscountSale otherDiscountSale = 184 (SummerDiscountSale)otherObject; 185 return (super.equals(otherDiscountSale) 186 && discount == otherDiscountSale.discount); 187 } 188 } 189 190 public SummerDiscountSale clone( ) 191 { 192 return new SummerDiscountSale(this ); 193 } 194 } 195 196 public class SummerDemo 197 { 198 public static void main(String[] args) 199 { 200 SummerSale simple = new SummerSale("floor mat", 10.00); 201 SummerDiscountSale discount = new SummerDiscountSale("floor mat", 11.00, 10); 202 System.out.println(simple); 203 System.out.println(discount); 204 205 if (discount.lessThan(simple)) 206 System.out.println("Discounted item is cheaper."); 207 else 208 System.out.println("Discounted item is not cheaper."); 209 210 SummerSale regularPrice = new SummerSale("cup holder", 9.90); 211 SummerDiscountSale specialPrice = new SummerDiscountSale("cup holder", 11.00, 10); 212 System.out.println(regularPrice); 213 System.out.println(specialPrice); 214 215 if (specialPrice.equalDeals(regularPrice)) 216 System.out.println("Deals are equal."); 217 else 218 System.out.println("Deals are not equal."); 219 } 220 } 221
Question 3.