You are on page 1of 3

COLLEGE OF ST.

CATHERINE OF QUEZON CITY

WEEK 4: User-Defined Classes and ADTs


Exercises:
I. Mark the following statements as true or false
1. The instance variables of a class must be of the same type. False
2. The methods of a class must be public False
3. A class can have more than one constructors. True
4. A constructor can return a value of the int type. True
5. An accessor method of a class accesses and modifies the data members of the
class. False
II. Given the UML diagram below. Answer the following questions:
Province

- Name: String
- Population: int
- Area: double

+ Province ()

+ Province (String, int, double)

+ setName(String): void

+ getName():String

+ setPopulation (int): void

+ getPopulation (): int

+ setArea(double): void

+ getArea(): double

+ toString(): String

+ makeCopy(Province): void

+ getCopy(): Province

1. What is the name of the class? Province


2. What are the instance variables? Name: String , Population :int, Area:double
3. What are the methods?
Province() ,
Province(String,int,double)
setName(String):void

Computer Programming 2
Activity Sheet 1
COLLEGE OF ST. CATHERINE OF QUEZON CITY

getName():String
setPopulation(int):void
setArea(double):void
getArea():double
toString(): String
makeCopy(Province): void
getCopy(): Province
4. What are the private members? Name:String , Population: int, Area: double
What are the public members?
Province() ,
Province(String,int,double)
setName(String):void
getName():String
setPopulation(int):void
setArea(double):void
getArea():double
toString(): String
makeCopy(Province): void
getCopy(): Province

Suppose that myProvince is a reference variable of the class type given in this figure
accomplish the following tasks:
5. Write a statement to instantiate the object myProvice with the values
“Rizal”, 626932, 5867639.00, respectively.
myProvince=new Province();
OurProvince= new Province( 626932, 5867639.00)
6. Write a statement that outputs the value of this instance variable name of the
object myProvince.
Public myProvince();

7. Write a statement that changes the value of the instance variable population
of the object myProvince to 672000.
public void myProvince (Name:String,Population: int,Area:double)
III. Programming Exercise:
1. Write a program that converts a number entered in Roman numerals to decimal.
Your program should consist of a class say, Roman. An object of type Roman should
do the following:
a. Store the number as a Roman numeral
b. Convert and store the number into decimal number as requested by the user.
The decimal values of Roman numerals are:
M 1000
D 500
C 100
L 50

Computer Programming 2
Activity Sheet 2
COLLEGE OF ST. CATHERINE OF QUEZON CITY

X 10
V 5
I 1
c. Your class must contain the method romanToDecimal to convert a Roman
numeral into its equivalent decimal number.
d. Test your program using the following Roman numerals: MCXIV, CCCLIX,
and MDCLXVI.
Rubric for grading programming exercise.
Grading Rubric
Criteria Performance Indicators Points
Correctness The code produces the expected results 5
Logic The code meets the specifications of the problem 5
Efficiency The code is concise without sacrificing correctness and logic. 5
Syntax The code adheres to the rules of the programming language. 5
Total 20

Computer Programming 2
Activity Sheet 3

You might also like