You are on page 1of 2

COMSATS University Islamabad, Lahore Campus

Sessional-I Lab Examination – Spring 2021

Course Title: Object Oriented Programing Course Code: CSC241 Credit Hours: 4(3,1)
Course Dr. Shahbaz Akhtar Abid Programme Name: BS Computer Science
Instructor/s:
Semester: Batch: BCS Section: A Date: 30/03/2021
Time Allowed: 45, minutes Maximum Marks: 20
Student’s Name: Reg. No. CUI - - /LHR
Important Instructions / Guidelines:

• Submissions at Teams will be acceptable Only


• No Extra time will be given

Question 1: Encircle the correct option

i. when implementing composition, the outer class can change the private inner class instance
variable by:
a. Calling setter function without creating the inner class object
b. Changing field directly without creating the inner class object
c. Both direct field access and calling setter function by creating an inner class object
d. ONLY calling setter function by creating an inner class object, direct access is not possible
ii. The following statement can be used to include a class from java class library into your program.
a. include
b. add
c. import
d. get
iii. The constructor of a class can be called from ANY OTHER METHOD using the “this” reference.
a. true
b. false
iv. The following statement creates an empty array list of integers: ArrayList<int> integerAL = new
ArrayList<int>();
a. true
b. false
v. A static method staticFunc() is defined in the class named sourceClass. This method CANNOT be
called as:
a. sourceClass.staticFunc() inside the main method
b. staticFunc() inside the main method
c. staticFunc() inside other static methods of sourceClass
d. staticFunc() inside other non-static methods of sourceClass
vi. If a reference variable is not initialized to any value, the default value will be:
a. nothing
b. 0
c. Null
d. It is an error to leave a reference variable uninitialized

Question 2: Consider a java class named as City with the following encapsulated attributes: -

City class’s private attributes:


• cityName
• cityPopulation
PTO
• cityRegisteredVoters
• isCapital //true, false
• totalCities

The detail of City class is given below:-

Detail of Attributes:

• cityName :- represents the name of the city that must be provided by the user every time a new
object is being created.
• cityPopulation :- Provided by the user when a new object is being created and if the user does not
enter any value, default value is set to be 1 million. Larger value must be stored in this attribute. If
the new value is smaller than already existing one, it will keep previous value. Else, it will be
updated with that new value.
Note: - Valid entry in this attribute must be positive value.
• cityRegisteredVoters :- total population that is eligible to cast vote is 15% of the total population..
• isCapitle :- is a variable that can only store true or false. If the city is capital city then store true
else false. By default initialize it with false.
• totalCities :- the number of cities that has been created by the user so far. Whenever a new city is
being created the totalCities attribute must be incremented by one. Assume the initial value is 100
when you run the program. Use static block to initialize totalCities.

Detail of Methods:

1) Constructors two parameter:


o Accept name and city population as parameter.
2) Getter and Setter method for each of the instance variable.
3) A method toString, to return the String that can also be used to print the detail of the city in the
following format
One Sample run:
Name = Lahore, Population = 40000, Registered Voters = 0, Capital = false, No. of Cities:
101

TestCity Class:

Consider a TestCity class that mainly includes the main method which provides the menu based interface
to the user to create and manipulate the City class objects as follows:-

1) Add new city


2) Print detail of current city
3) Print maximum population encountered
4) Exit
:>

Explanation of menu Options

1. When the user selects an option 1, ask him to take input as the city name and city population
and create a new object. Note that every newly created object will become the current object as
it will overwrite the previous one because we are not storing the objects in the array.
2. When the user selects this option, print the detail of the current city by returning values using
method toString i.e. the City object created lastly.
3. When the user selects this option, print the value of maxPopulation entered so far.
4. When the user enters 4, exit the software. //System.exit(0);

Please write the code for the following:

• Constructor for the ‘City’ class


• ‘main’ method of ‘TestCity’ class
Note: Do not create City and TestCity classes. Only provide the code of the above required.

You might also like