JAVA HW 7.1
Quiz 1
Student Name: Student ID: Grade: /65
Problem 1: True or False. Explain your answer. (2 points per question = 10 total)
1. The Java compiler is able to find all programmer errors.
2. The values of (double) 5 / 2 and (double) (5 / 2) are identical.
3. In a Java program, dividing by 0 is a syntax error.
4. The word "Public" is a reserved word.
5. A java method can only return one item. Explain one way to work around this issue, and allow a
java method to return more than one item.
Problem 2: Multiple Choice. (1 point per question = 4 total)
1. A Java program can handle an exception in several different ways. Which of the following is not a way that a Java program could handle an exception? A) ignore the exception B) handle the exception where it arose using try and catch statements C) propagate the exception to another method where it can be handled D) throw the exception to a pre-defined Exception class to be handled
2. Which of the following would return the last character of the String x? A) x.charAt(0); B) x.charAt(last); C) x.charAt(length(x)); D) x.charAt(x.length( )-1); E) x.charAt(x.length( )); 3. The relationship between a parent class and a child class is referred to as a(n) ________ relationship. A) has-a B) is-a C) was-a D) instance-of E) alias 4. The relationship between a class and an object is best described as A) classes are instances of objects B) objects are instances of classes C) objects and classes are the same thing D) classes are programs while objects are variables E) objects are the instance data of classes Problem 3: Short Answer (5 points per question = 25 total)
a. The main programming mechanisms that constitute object-oriented programming are: Classes, Encapsulation, Inheritance, and Polymorphism. Explain Encapsulation in details.
b. Describe Method Overloading.
c. What is an abstract method?
d. Explain the use of the “this” reserved keyword in Java.
e. Describe a dependency relationship between two classes. Problem 4: Class Definition. (26 points) A Pizza Ordering System keeps track of the orders submitted by users. This system allows users to order three types of pizzas: Cheese, Veggie & Pepperoni, with three different sizes: Small, Medium & Large. Users can only pay via Credit Card. A Java program is used for that system, and it has two classes: A Customer Class and An Order Class.
- The Customer class stores the customer’s ID, name, delivery address, and credit card information.
- The Order class stores the ID of the ordering customer, and an array containing the ordered pizza’s types and sizes. The Order class should have a method for adding another pizza into the order. Assume that the maximum number of ordered pizzas cannot exceed 50.
Write down the class definitions for the two classes described above. (4 points) For each class, ensure that encapsulation is enforced. (8) Include getter and setter methods for all class variables. (8) Finally, include the method definition for the operation described above with the Order class (adding another pizza into an order). Do NOT include the implementation for that method. (6)