1

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.
2 / Java classifices instance methods as either mutator or accessor. All accessor methods must specify a return type that is not the type void.
3 / A class can have any number of constructors.
4 / In order to call an instance method of a class, you must do so with a reference object of the class. 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.