CmSc150 Fundamentals of Computing I Homework 06 due 02/17 by 5 pm SOLUTION 1. Programming exercise Download the project HW06-Errors.zip. It contains two classes – Account and Transactions. The Transactions class has several syntax and semantic errors. Your task is to correct them. When compiling the class you will get an error message for each syntax error. For each error message do the following: 1. Write down the error message 2. Write down the reason for the message 3. Write down the necessary corrections 4. Correct the program and compile again. When there are no more error messages, run the program and examine the results. Examine the code and find out the logical (semantic) errors. Write down the semantic errors and explain they can be corrected. Correct the program. Zip the folder, change the extension to .zi and send it back. Errors: 1. amount = scan.nextDouble(); Cannot find symbol “scan” Scanner object not defined import java.util.Scanner; Scanner scan = new Scanner(System.in); 2. int bal = Account.deposit (amount); Non-static method deposit(double) cannot be referenced from a static context int bal = acc1.deposit (amount); 3. int bal = acc1.deposit (amount); possible loss of precision double bal = acc1.deposit (amount); 4. Account.withdraw (acc1); withdraw(double) in Account cannot be applied to Account Account.withdraw (amount); 5. Account.withdraw (amount); Non-static method deposit(double) cannot be referenced from a static context acc1.deposit (amount); Logical error: after withdrawal the balance is not recorded Correction: bal = acc1.withdraw (amount); 1 2. Questions and answers True/False (Write T or F immediately after the number of the question) 1. Java methods can return only primitive types (int, double, float, char, boolean, etc). . Answer: False. Explanation: Java methods can also return (references to) objects, such as a String. 2. Formal parameters are those that appear in the method call and actual parameters are those that appear in the method header Answer: False. Explanation: The question has the two definitions reversed. Formal parameters are those that appear in the method header, actual parameters are the parameters in the method call (those being passed to the method). 3. All Java classes must contain a main method which is the first method executed when the Java class is called upon. Answer: False. Explanation: Only the driver program requires a main method. The driver program is the one that is first executed in any Java program (except for Applets), but it may call upon other classes as needed, and these other classes do not need main methods 4. Java methods can return more than one item Answer: False. Explanation: All Java methods return a single item, whether it is a primitive data type an object, or void. The reserved word continue is used to exit the remainder of a loop and test the condition again. 5. The following method header definition will result in a syntax error: public void aMethod( ); Answer: True. Explanation: The reason for the syntax error is because it ends with a “;” symbol. It instead needs to be followed by { } with 0 or more instructions inside of the brackets. An abstract method will end with a “;” but this header does not define an abstract method. 6. A method defined in a class can access the class’ instance data without needing to pass them as parameters or declare them as local variables Answer: True. Explanation: The instance data are globally available to all of the class’ methods and therefore the methods do not need to receive them as parameters or declare them locally. If variables of the same name as instance data were declared locally inside a method then the instance data would be “hidden” in that method because the references would be to the local variables. 2 7. Defining formal parameters requires including each parameters type Answer: True. Explanation: In order for the compiler to check to see if a method call is correct, the compiler needs to know the types for the parameters being passed. Therefore, all formal parameters (those defined in the method header) must include their type. This is one element that makes Java a Strongly Typed language. 8. While multiple objects of the same class can exist, in a given program there can be only one version of each class Answer: True. Explanation: A class is an abstraction, that is, it exists as a definition, but not as a physical instance. Physical instances are created when an object is instantiated using new. Therefore, there can be many objects of type String, but only one String class. Multiple Choice (use boldface or underline to answer) 1. The behavior of an object is defined by the object’s a) b) c) d) e) instance data constructor visibility modifiers methods all of the above Answer: d. Explanation: The methods dictate how the object reacts when it is passed messages. Each message is implemented as a method, and the method is the code that executes when the message is passed. The constructor is one of these methods but all of the methods combine dictate the behavior. The visibility modifiers do impact the object’s performance indirectly. 2. 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 Answer: b. Classes are definitions of program entities that represent classes of things/entities in the world. Class definitions include instance data and methods. To use a class, it is instantiated. These instances are known as objects. So, objects are instances of classes. Program code directly interacts with objects, not classes. 3 3. Which of the following reserved words in Java is used to create an instance of a class? a. b. c. d. e. class public public or private, either could be used import new Answer: e. Explanation: The reserved word “new” is used to instantiate an object, that is, to create an instance of a class. The statement new is followed by the name of the class. This calls the class’ constructor. Example: Car x = new Car( ); will create a new instance of a Car and set the variable x to it. 4. If a method does not have a return statement, then a. b. c. d. e. it will produce a syntax error when compiled it must be a void method it can not be called from outside the class that defined the method it must be defined to be a public method it must be an int, double, float or String method Answer: b. Explanation: All methods are implied to return something and therefore there must be a return statement. However, if the programmer wishes to write a method that does not return anything, and therefore does not need a return statement, then it must be a void method (a method whose header has “void” as its return type). 5. A class’ constructor usually defines a. b. c. d. e. how an object is initialized how an object is interfaced the number of instance data in the class the number of methods in the class if the instance data are accessible outside of the object directly Answer: a. Explanation: The constructor should be used to “construct” the object, that is, to set up the initial values of the instance data. This is not essential, but is typically done. The interface of an object is dictated by the visibility modifiers used on the instance data and methods. 6. Instance data for a Java class a. b. c. d. e. are limited to primitive types (e.g., int, float, char) are limited to Strings are limited to objects(e.g., Strings, classes defined by other programmers) may be primitive types or objects, but objects must be defined to be private may be primitive types or objects Answer: e. Explanation: The instance data are the entities that make up the class and may be any type available whether primitive or object, and may be public or private. By using objects as instance data, it permits the class to be built upon other classes. This relationship where a class has instance data that are other classes is known as a has-a relationship. 4 7. Consider a method defined with the header: public void foo(int a, int b). following method calls is legal? a. b. c. d. e. Which of the foo(0, 0.1); foo(0 / 1, 2 * 3); foo(0); foo( ); foo(1 + 2, 3 * 0.1); Answer: b. Explanation: The only legal method call is one that passes two int parameters. In the case of answer b, 0 / 1 is an int division (equal to 0) and 2 * 3 is an int multiplication. So this is legal. The answers for a and e contain two parameters, but the second of each is a double. The answers for c and d have the wrong number of parameters. 8. Consider a method defined with the header: public void doublefoo(double x). Which of the following method calls is legal? a. b. c. d. e. doublefoo(0); doublefoo(0.555); doublefoo(0.1 + 0.2); doublefoo(0.1, 0.2); all of the above are legal except for d Answer: e. Explanation: In the case of a, the value 0 (an int) is widened to a double. In the case of c, the addition is performed yielding 0.3 and then doublefoo is called. The parameter list in d is illegal since it contains two double parameters instead of 1. 9. The value of the Java expression 10 - 25 % 4 is: a. 6 b. 5 c. 9 d. 7 e. none of the above 10. If the int variables int1 and int2 contain the values 9 and 5, respectively, then the value of the expression (double)(int1 / int2) is: a. b. c. d. e. 0.8 0 1.8 1.0 1 Answer: d. Explanation: First the integer division is performed, because the expression is in parentheses. The result is 1. Then the type casting is applied and the result is real 1, i.e. 1.0 5 11. If the int variables int1 and int2 contain the values 9 and 5, respectively, then the value of the expression (double)(int1) / int2 is: a. b. c. d. e. 0.8 0.5 1.8 1.0 1 Answer: c. Explanation: First the type casting operation is applied to the value in int1 (note that the contents of int1 is not changed) . As a result the expression now involves a double value, therefore the value in int2 is also transformed to double, and only then the division is performed. 12. If the int variables int1 and int2 contain the values 10 and 5, respectively, then the value of the expression int2 / (double)(int1) is: a. b. c. d. e. 0.8 0.5 1.8 2.0 1 Answer: b. Explanation: First the type casting operation is applied to the value in int1 (note that the contents of int1 is not changed) . As a result the expression now involves a double value, therefore the value in int2 (however not changing the contents of int2) is also transformed to double, and only then the division is performed. How to turn in the assignment: The program: Zip the project and send it as attached file by e-mail. Follow the naming conventions. The questions: write your answers into this document, write your name, then save and send as attached .doc document by e-mail 6