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 1 Variables that depend on the creation of objects are called instance variables 2 Instance methods were not designed to access instance variables 3 Class variables that do not depend on any particular instance of a class Class variables are sometime prefaced with the keyword static 4 5 Class variables are prefaced with the keyword final. 6. Class methods are designed to access class variables 7. Class methods can access instance variables 8 Instance methods can access class variables 9 Method overloading means that two or more method with the same name but with different parameters is defined in the same class. 10 Constructors cannot be overloaded. 11 The keyword this is used to refer to the current object. 12 In Java we use the modifier final to define constants Total True/false 3 Exercise 2 Fill in the blank No Question 1 A _______ _________________ is a fixed value. 2 __________ __________________ occurs when two or more methods have the same name but have different parameters.. 3 Date d = __________ Date(). 4 The name given to a class, a method, or a variable is called a(n) _______________________ __________________________________________ 5 Class methods must be prefaced with the keyword _____________ 6 DateFormat df = DateFormat._________________________(); 7 The two categories of variables defined in Java are __________ variables and ______________ variables ______________________________________ 8 Class variables require _____________ methods and/or _______ to access them. 9 import java.text.NumberFormat; 10 Overloading requires that the _____________ must be unique. Total Correct 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); } } 1. public class Chain 2. { 3. public Chain(double x) 4. { 5. this("Hello"); 6. } 7. public Chain(String str) 8. { 9. this(); 10. System.out.println(str); 11. } 12. public Chain() 13. { 14. this(200); 15. System.out.println("years"); 16. } 17. 18. public Chain(int x) 19. { 20. System.out.println(x); 21. } 22. }