CSEN 202: Introduction To Computer Programming Spring Semester 2018
CSEN 202: Introduction To Computer Programming Spring Semester 2018
CSEN 202: Introduction To Computer Programming Spring Semester 2018
Bar Code
Major
Civil
BI
Engineering
4) This exam booklet contains 16 pages, including this one. Three extra sheets of scratch paper are attached
and have to be kept attached. Note that if one or more pages are missing, you will lose their points. Thus,
you must check that your exam booklet is complete.
5) Write your solutions in the space provided. If you need more space, write on the back of the sheet containing
the problem or on the four extra sheets and make an arrow indicating that. Scratch sheets will not be graded
unless an arrow on the problem page indicates that the solution extends to the scratch sheets.
6) When you are told that time is up, stop working on the test.
Good Luck!
a) Write a method sumArray that takes one array of integers and a number n and prints all pairs of elements
in the array whose sum is equal to n.
Suppose that the array stores the following values:
Solution:
b) Write a class called Tester that uses a command-line argument to test the method sumArray imple-
mented above. For example:
Please note that the first element in the command line corresponds to the number n.
Solution:
4 8
Solution:
commonElementsHelper(list1,list2,0,0);
}
commonElementsHelper(list1,list2,++i,j);
else
commonElementsHelper(list1,list2,i,++j);
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 4
1 2
3 4
{
{1},
{2,3},
{4}
}
1 2 3
4 5 6
7 8 9
{
{1},
{2,4},
{3,5,7},
{6,8},
{9}
}
Solution:
a) Implement a class Apartment to define an apartment. Assume that an object of class Apartment has
the following attributes:
• int: apartment number
• int: number of bedrooms
• int: number of bathrooms
• int: floor
• int: rent amount
The class Apartment should implement the following:
1. a constructor for an Apartment that takes as parameters the apartment number, the number of bed-
rooms, the number of bathrooms, the number of the floor, and the rent amount.
2. toString() that returns the apartment number, the number of bedrooms, the number of bathrooms
and the rent amount. This should all be nicely formatted with one attribute on each line using the \n
escape character (see sample output below for an example).
Apartment number: 8
Number of bedrooms: 4
Number of bathrooms: 3
Floor number: 3
Rent amount: 4500 egp
Solution:
b) Implement a Building class that will store information about the building. It should include a name of
the building, an array of Apartments instances called theApartments to hold information about each
apartment. In addition to an integer number representing the maximum number of apartments that could
exist in the building (i.e. the size of the array).
The class Building should implement the following:
• a constructor that takes as parameter a name of the Building and the maximum number of apartments
and creates an array of Apartment objects.
• addApartment(Apartment ap) that takes an apartment object as input and adds it to the build-
ing if it does not exist there already. If the apartment does already exist in the building, then a cor-
responding message should be displayed. Moreover, the apartment is to be added only if the array is
not full. If the array is full, a corresponding message should be displayed. Note: the Apartment
instance should be added to the first available location in the array.
• generateFloor(int floorLevel) that takes an integer number as input and returns an array
of Appartment objects having floor number same as the floorLevel.
• toString() method that prints out the information for the building by calling the toString
method for each Apartment instance.
Solution:
}
}
if (apt[i].floor == level)
c++;
}
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 8
c) Implement a main method that should produce the output below. It should add five apartments with in-
formation as below. The apartments should be added using the addApartment() method. Test the
generateFloor() and toString() methods.
See the sample output below for a sample run of the test program.
Apartment number: 8
Number of bedrooms: 4
Number of bathrooms: 3
Floor number: 3
Rent amount: 4500 egp
Apartment number: 10
Number of bedrooms: 3
Number of bathrooms: 2
Floor number: 4
Rent amount: 3500 egp
Apartment number: 6
Number of bedrooms: 4
Number of bathrooms: 3
Floor number: 2
Rent amount: 4500 egp
Apartment number: 18
Number of bedrooms: 5
Number of bathrooms: 3
Floor number: 6
Rent amount: 5500 egp
Apartment number: 7
Number of bedrooms: 3
Number of bathrooms: 3
Floor number: 3
Rent amount: 4000 egp
_________________________________
Apartments in floor 3:
Apartment number: 8
Number of bedrooms: 4
Number of bathrooms: 3
Floor number: 3
Rent amount: 4500 egp
Apartment number: 7
Number of bedrooms: 3
Number of bathrooms: 3
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 9
Floor number: 3
Rent amount: 4000 egp
Solution:
b.addApartment(t);
b.addApartment(u);
b.addApartment(v);
b.addApartment(w);
b.addApartment(x);
Apartments[] a = b.generateFloor(3);
System.out.println("Apartments in floor:");
a) The following program produces 4 lines of output. Write the output below, as it would appear on the console.
int level;
Solution:
Level 6, 5 hp
Level 6, 10 hp
Level 7, 12 hp
Level 7, 16 hp
Solution:
Compile error. As the object t was not created and thus we can not access i.
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 11
if(obj1 == obj2)
System.out.println("memory address of object1 is same as object2");
if(obj1.equals(obj2))
System.out.println("value of object1 is equal to object2");
}
}
Solution:
d) Does the program compiles? If yes and if it runs state what will be displayed on the console. Otherwise
justify what kind of error will occur.
}
}
Solution:
obj1.a = 4 obj1.b = 3
obj2.a = 4 obj2.b = 3
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 12
Solution:
dogood: do good
f) What is the output of the program? If there is no change, justify why not? If there is a change, how would
you amend the implementation in such a way no change would occur?
String name;
public Subject(String s) {
name = s;
}
}
Subject subj;
String name;
subj.setName("MATH 201");
}
}
Solution:
There is a change in the name of the subject:
Scratch paper
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 15
Scratch paper
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 16
Scratch paper