1 Tutorial Classes and Objects - Program Development ................................................................................... 1 Introduction ............................................................................................................................................... 1 Objective.................................................................................................................................................... 1 Exercise 1 True/False ............................................................................................................................ 2 Exercise 2 Fill in the blank ........................................................................................................ 3 Exercise 3 Correct the Code 1 ............................................................................................................... 4 Exercise 4 .............................................................................................................................................. 5 Exercise 5 .............................................................................................................................................. 5 Exercise 6 .............................................................................................................................................. 6 Exercise 7 .............................................................................................................................................. 6 Exercise 8 Program Output 1 ................................................................................................................ 7 Tutorial Classes and Objects - Program Development Introduction These exercises are geared to help the student to learn: (a) The Java programming language (b) Object Oriented Programming (c) How to solve problems using the object oriented approach. Students should attempt these exercises only after they have read the chapter, and at least do the end of section exercises. I suggest that you do this tutorial material before you attempt a major programming assignment. Do not skip any of the exercises, since they are arranged with incremented difficulty. I would further suggest that you do these exercises before you do the closed lab session. Objective To know the basic syntax of Java. To know what are instance variables and class variables. To know when to use instance variables as oppose to when to use class variables. To know what are instance methods and class methods. 2 To differentiate how to call instance methods as opposed to calling class methods. To know about the object reference object this, and be able to use it. To develop problem solving strategies ( method per method). Exercise 1 True/False No. Questions True/false 1 Variables that depend on the creation of objects are called instance variables True 2 Instance methods were not designed to access instance variables 3 Class variables that do not depend on any particular instance of a class true Class variables are sometime prefaced with the keyword static False (always) 5 Class variables are prefaced with the keyword final. false 6. Class methods are designed to access class variables true 7. Class methods can access instance variables false 8 Instance methods can access class variables true 9 Method overloading means that two or more method with the same name but with different parameters is defined in the same class. true 10 Constructors cannot be overloaded. false 11 The keyword this is used to refer to the current object. true 12 In Java we use the modifier final to define constants true false 4 Total 3 Exercise 2 No Fill in the blank Question Correct 1 A _______constant_________________ is a fixed value. 2 _______Overloading______________________ is when two or more methods have the same name but have different parameters.. 3 Date d = _new__________ Date(). 4 The name given to a class, a method, or a variable is called a(n) __ identifier__ __________________________________________ 5 Class methods must be prefaced with the keyword _____________ 6 DateFormat df = DateFormat._________________________(); 7 The two categories of variables defined in Java are – _instance__________ variables and __class______________ variables ______________________________________ 8 Class variables require ___class___ methods__________________ to access them. 9 import java.text.NumberFormat; 10 In overloading, the _parameters_____________ must be unique. Total methods and/or ___ instance 4 Exercise 3 Correct the Code 1 The following class contains syntax error(s). Identify and correct the error(s). Write the corrected version below. 1. class A 2. { 3. static int count; 4. int weight; 5. A(int d) 6. { 7. weight = d; 8. } 9. static int getWeight() 10. { 11. 12. 13. } return weight; } 5 Exercise 4 What will happen if you attempt to compile the following Java code? 1. class A 2. { 3. static int count; 4. int weight; 5. 6. A(int d) 7. { 8. count = d; 9. } 10. 11. static int getCount() 12. { 13. return count; 14. } 15. } See Line 11 Exercise 5 Given the following situations, identify the constants, if any. (a) The sales tax on consumer goods in Miami is 7%. (Constant) (b) The room has a seating capacity of 35. (Constant) (c) The capacity of the petrol tank is 20 gallons. (Constant) (d) The speed of the car is 25 miles per hour. 6 Exercise 6 Given the following variable declarations, identify those that are class variables from those that are instance variables. (a) private double money; (Instant variable) (b) public static double rate; (Class variable) (c) private String word; (Instance variavble) (d) private static int capacity = 35; (Class variable) (e) public int noOfChildren; (Instance variable) Exercise 7 What do you think will happen if you attempt to compile the following code? Book b1 = Book(“("OOP using Java", "000-000-00001", 895, 85.95); The code will not compile, because it is lacking the operator new to create the object. 7 It should have been: Book b1 = new Book(“("OOP using Java", "000-000-00001", 895, 85.95); Exercise 8 Program Output 1 What is outputted by the following program? Write your answer below the program. 1. 2. 3. 4. 5. 6. 7. public class TestChain { public static void main(String arg[]) { Chain ch = new Chain(100.0); } } Output: 200 years Hello 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. public class Chain { public Chain(double x) { this("Hello"); } public Chain(String str) { this(); System.out.println(str); } public Chain() { this(200); System.out.println("years"); } public Chain(int x) { System.out.println(x); } }