Sie sind auf Seite 1von 4

INFORMATION TECHNOLOGY EDUCATION DEPARTMENT

ITPROG3L
(Object Oriented Programming)

EXERCISE

4
Creating Classes and Objects

<STUDENT NAME>
<SECTION>
<DATE>

I. Objectives:
At the end of the experiment, students must be able to:
Cognitive
a) understand how object-oriented programming and some of its concepts
b) understand the classes and objects
c) understand how instance variables/methods and class(static) variables/methods are being
used
Psychomotor:
a) construct a program using classes and objects
b) compile and debug the error of the program
Affective
a) appreciate the concept behind this experiment
II. BACKGROUND INFORMATION
A class is a considered as the center of Object Oriented Programming methodology and
defines the abstract characteristics of a thing (object), including the thing's characteristics (its
attributes, fields or properties) and the thing's behaviors (the things it can do, or methods,
operations or features). An object is an instantiation of a class.
In a way, a class and its objects have the relationship of a data type and variables. A class is
simply a template for holding objects. It is abstract but objects are real.
Classes are generally declared using the keyword class.
III. EXPERIMENTAL PROCEDURE:
Exercise #01:

(Car.java)

Implement a class Car that has the following characteristics:


a) brandName,
b) priceNew, which represents the price of the car when it was new,
c) color, and
d) odometer.
The class should have:
A. A method getPriceAfterUse() which should return the price of the car after being used
according to the following formula:
odemeter

car price after being used = priceNew ( 1 600,000 )

B. A method updateMilage(double traveledDistance) that changes the current state of the


car by increasing its milage, and
C. A method outputDetails() that will output to the screen all the information of the car, i.e.,
brand name, price new, price used, color, and odometer.
Exercise #02:

(CarTest.java)

Write a test class for the Car class above. You are required to do the followings:
A. Create an object of type Car.
B. Assign any valid values to the instance variables of the object created in A.
C. Use the method getPriceAfterUse on the object created in A then output the result to the
screen.
D. Use the method updateMilage on the object created in A by passing a valid value.
E. Do part C again.
F. Use the method outputDetails on the object created in A.
Exercise #3 (a):

(Student.java)

Implement a Student class with the following fields, constructors and methods:
Fields (All should be private):
name;
totalScore;
numOfQuizzes;
Constructor:
public Student(String name)
Methods:
public String getName()
public double getAverage()

//this should return zero if no quiz has been

taken.

public double getTotalScore()


public void addQuiz(double score)
public void printStudent()
average score

public String toString()

//this

should

print

the

students

name

and

Exercise #03 (b):

(StudentTest.java)

Write an application StudentTest that reads a student name and use the Student
class to create a Student object. Then read the scores of the student in 3 quizzes
and add each to the totalScore of the student using the addQuiz method, then
print the student object.
Sample Output
Enter Student Name: Jared
Enter Quiz 01 Score for Jared: 10
Enter Quiz 02 Score for Jared: 8
Enter Quiz 03 Score for Jared: 9
Student Name : Jared
Average Score: 9.0

V. QUESTION AND ANSWER:


1. What is the difference between classes and objects?
______________________________________________________________________________
______________________________________________________________________________
_____________________________________________________________________________

2. Consider the following class:


public class IdentifyMyParts {
public static int x = 7;
public int y = 3;
}

1. What are the class variables?


2. What are the instance variables?
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
VI. REFLECTIONS:
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________
______________________________________________________________________________

Das könnte Ihnen auch gefallen