Chapter 2



Tutorial 1 Classes and Objects 3

Introduction 3

Exercise 1 True/False 4

Exercise 2 Fill in the blank 5

Exercise 3 Correct the Code 1 6

Exercise 4 Correct the Code 2 7

Exercise 5 Correct the Code 3 8

Exercise 6 Correct the Code 4 9

Exercise 7 Program Output 1 10

Exercise 8 Program Output 2 11

Exercise 9 Program Output 3 12

Exercise 10 Creating a Complete Program 14

Chapter 2 Classes and Objects

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 re-enforce the information gathered from the lesson.

• To be knowledgeable with the fundamental construct of a Java class.

• To know the basic syntax of Java.

• To view a Java program as a whole/complete thought rather than by parts( method per method).

• To understand how objects interact.

• To be able to use the print() and println() methods to format output effectively.

Exercise 1 True/False

| | | |

|No. |Questions |True/false |

| | | |

|1 |Variables are names representing storage locations of primary memory. | |

| | | |

| |Java classifices instance methods as either mutator or accessor. All accessor methods must specify a | |

|2 |return type that is not the type void. | |

| | | |

|3 |A class can have any number of constructors. | |

| | | |

| |In order to call an instance method of a class, you must do so with a reference object of the class. | |

|4 |Howerver, the same is not true of a class method. You may use the name of the class to call a class | |

| |method. | |

| | | |

|5 |Every method has a formal parameter. | |

| | | |

|6. |A private variable or method is only visible in its own class. | |

| | | |

|7. |A method may have no return statement. | |

| | | |

|8 |An instance variable is static. | |

| | | |

|9 |A class may contain declarations an methods. | |

| | | |

|10 |A Java program may be constructed from several classes. | |

| | | |

|Total | | |

Exercise 2 Fill in the blank

| | | |

|No |Question |Correct |

| | | |

|1 |A ________________________variable does not depend on any instance of the class. | |

| | | |

|2 |A _____________________________is not an executable statement. | |

| | | |

|3 |An object is a(n)______________________________of an entity. | |

| | | |

|4 |The name given to a class, a method, or a variable is called a(n) | |

| | | |

| |__________________________________________ | |

| | | |

|5 |___________________are special forms of methods. They cannot have | |

| |________________________________ type; not even void. | |

| | | |

|6 |A _______________________ method never releases information outside of its class. | |

| | | |

|7 |In order to create an object from a class you must use the operator | |

| |______________________________________ | |

| | | |

|8 |Objects_______________________________ with one another by passing messages in the form | |

| |of data. | |

| | | |

|9 |Variables that do not depend on any particular instance of a class is called a(n) | |

| |______________________________variable. | |

| | | |

|10 |The modifier ____________________________is used to define constants | |

| | | |

|Total | | |

Exercise 3 Correct the Code 1

The following class contains syntax error(s). Identify and correct the error(s). Write the corrected version below.

Exercise 4 Correct the Code 2

The following class contains syntax error(s). Identify and correct the error(s). Write the corrected version below.

Exercise 5 Correct the Code 3

The following class contains syntax error(s). Identify and correct the error(s). Write the corrected version below.

Exercise 6 Correct the Code 4

The following class contains syntax error(s). Identify and correct the error(s). Write the corrected version below.

Exercise 7 Program Output 1

What is outputted by the following program? Write your answer below the program.

Exercise 8 Program Output 2

What is outputted by the following program? Write your answer below the program.

Exercise 9 Program Output 3

What is outputted by the following program? Write your answer below the program.

(Continue)

Question 3 (Continue)

Exercise 10 Creating a Complete Program

The following set of exercises is designed to help you understand how to compose the solution to problems using the object-oriented approach. You may work in pairs or small groups. I would suggest however that each person has a copy of the solution for each part.

a) Refer to Exercises (2a) Question 6.

b) Refer to Exercises (2b) Question 6.

c) Refer to Exercises (2c) Question 5.

d) Refer to Exercises (2d) Question 10.

e) Refer to Exercises (2e) Question 5.

-----------------------

1. class Correct

2. {

3. Integer i;

4. string x;

5.

6. correct(string s, int j)

7. {

8. x = s

9. i = j

10. }

11.

12. void get()

13. {

14. return x;

15. }

16.

17. int change(integer amount)

18. {

19. i = i + amount

20. }

21. }

1. class Correct

2. {

3. String x;

4. int i;

5.

6. Correct(int p, String q)

7. {

8. x = p;

9. i = q;

10. }

11.

12. String getI()

13. {

14. return i;

15. }

16.

17. int getX()

18. {

19. return x;

20. }

21. }

1. class Correct

2. {

3. final int MAX = 25;

4. static int count;

5. static String str;

6. int value;

7.

8. void Correct(int x)

9. {

10. count = 2 * MAX;

11. value = x;

12. }

13.

14. void Correct(int x, String s)

15. {

16. str = s;

17. this(x);

18. }

19.

20. static int getValue()

21. {

22. return value;

23. }

24.

25. void change()

26. {

27. MAX = value;

28. }

29.

30. void calculate()

31. {

32. value = MAX + count;

33. }

34.

35. getStr()

36. {

37. return str;

38. }

39. }

1. class TestCorrect

2. {

3. public static void Main(String[] arg)

4. {

5. correct c = new correct("Hi there", 10);

6.

7. System.out.println(getX());

8. System.out.println(getI());

9. }

10. }

1. class Correct

2. {

3. String x;

4. int i;

5.

6. Correct(int p, String q)

7. {

8. i = p;

9. x = q;

10. }

11.

12. String getX()

13. {

14. return x;

15. }

16.

17. int getI()

18. {

19. return i;

20. }

21. }

1. class TestCorrect

2. {

3. public static void main(String[] arg)

4. {

5. System.out.println(Correct.getStr());

6. System.out.println(Correct.getValue());

7. Correct c1 = new Correct(10, "Hello");

8. Correct.calculate();

9. System.out.println(c1.getValue());

10.

11. Correct c2 = new Correct(20);

12. c2 = changeValue();

13. System.out.println(c2.getValue());

14. System.out.println(Correct.getValue());

15. }

16. }

1. class Trace

2. {

3. int x;

4. String str;

5.

6. Trace()

7. {

8. x = 25;

9. str = "Hello";

10. }

11.

12. int getX()

13. {

14. return x;

15. }

16.

17. String getStr()

18. {

i. return str;

19. }

20.

21. void change()

22. {

23. str = "I shall return ";

24. }

25. }

1. class TestTrace

2. {

3. public static void main(String[] arg)

4. {

5. Trace t = new Trace();

6.

7. System.out.println( t.getX() );

8. System.out.println( t.getStr() );

9. t.change();

10. System.out.println( t.getX() );

11. System.out.println( t.getStr() );

12. }

13. }

14. class TestTrace

15. {

16. public static void main(String[] arg)

17. {

18. Trace t = new Trace();

19.

20. display(t);

21. t.change();

22. display();

23. }

24.

25. static void display(Trace t)

26. {

27. System.out.println( t.getX() );

28. System.out.println( t.getStr() );

29. }

30. }

26. class Trace

27. {

28. int x;

29. String str;

30.

31. Trace()

32. {

33. x = 25;

34. str = "Hello";

35. }

36.

37. int getX()

38. {

39. return x;

40. }

41.

42. String getStr()

43. {

i. return str;

44. }

45.

46. void change()

47. {

48. str = "I have returned ";

49. }

50. }

1. public class TestDeptStore

2. {

3. public static void main(String[] arg)

4. {

5. DeptStore customer1 = new DeptStore("Jerry");

6. customer1.addSales(45);

7. display(customer1);

8. DeptStore customer2 = new DeptStore("Mary");

9. customer2.addSales(600);

10. customer2.addSales(30);

11. display(customer2);

12. customer1.addSales(120);

13. display(customer1);

14. customer2.addSales(120);

15. display(customer2);

16. customer1.addSales(100);

17. customer1.addSales(200);

18. display(customer1);

19.

20. }

21. static void display(DeptStore s)

22. {

23. System.out.println(s.getName() + ": Total $" + s.getCustomerBill());

24. System.out.println("Store total $" + DeptStore.getStoreTotal());

25. System.out.println();

26. }

27. }

1. public class DeptStore

2. {

3. private double customerTotal;

4. private static double departmentTotal = 0;

5. private String name;

6.

7. DeptStore(String n)

8. {

9. name = n;

10. customerTotal = 0;

11. }

12.

13. void addSales(double price)

14. {

15. customerTotal = customerTotal + price;

16. departmentTotal = departmentTotal + price;

17. }

18.

19. String getName()

20. {

21. return name;

22. }

23.

24. public double getCustomerBill()

25. {

26. return customerTotal;

27. }

28.

29. public static double getStoreTotal()

30. {

31. return departmentTotal;

32. }

33. }

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download