CSEN 202: Introduction To Computer Programming Spring Semester 2018

Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Page 0

German University in Cairo June 6, 2018


Media Engineering and Technology
Prof. Dr. Slim Abdennadher
Dr. Mohammed Abdel Megeed Salem

CSEN 202: Introduction to Computer Programming


Spring Semester 2018
Final Exam

Bar Code

Instructions: Read carefully before proceeding.

1) Please tick your major

Major
Civil
BI
Engineering

2) Duration of the exam: 3 hours (180 minutes).


3) No books or other aids are permitted for this test.

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!

Don’t write anything below ;-)


P
Exercise 1 2 3 4 5
Possible Marks 16 14 16 24 20 90
Final Marks
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 1

Exercise 1 (10+6=16 Marks)

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:

{2, 7, 4, -5, 11, 5, 20}

and n = 15, then the method call would print

Pairs of elements and their sum:


4 + 11 = 15
-5 + 20 = 15

Solution:

public static void sumArray(int [] a, int n) {

for(int i = 0; i < a.length; i++)


{
for(int j = i+1; j < a.length; j++)
{
if(a[i]+a[j] == n)
System.out.println(a[i]+"+"+a[j]+"="+n);
}
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 2

b) Write a class called Tester that uses a command-line argument to test the method sumArray imple-
mented above. For example:

> java Tester 15 2 7 4 -5 11 5 20


Pairs of elements and their sum:
4 + 11 = 15
-5 + 20 = 15

Please note that the first element in the command line corresponds to the number n.

Solution:

public class Tester {

public static void sumArray(int [] a, int n) {

for(int i = 0; i < a.length; i++)


{
for(int j = i+1; j < a.length; j++)
{
if(a[i]+a[j] == n)
System.out.println(a[i]+"+"+a[j]+"="+n);
}
}
}

public static void main(String [] args) {

int len = args.length;


int n = Integer.parseInt(args[0]);
int [] x = new int [len-1];
for(int i = 1;i < args.length; i++)
x[i-1] = Integer.parseInt(args[i]);
sumArray(x,n);
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 3

Exercise 2 (14 Marks)


Write a recursive method commonElements that takes two sorted (in ascending order) array of integers as a
parameter and prints the common elements
For example, if the two arrays are:

int[] list1 = {4,5,6,7,8}


int[] list2 = {2,3,4,8,10,16}

then the method commonElements(list1,list2) would print

4 8

You are not allowed to use any static variables.

Solution:

public static void commonElements(int [] list1, int [] list2) {

commonElementsHelper(list1,list2,0,0);
}

public static void commonElementsHelper(int [] list1, int [] list2, int i, int j) {

if(i == list1.length || j == list2.length)


return;
else{
if(list1[i] == list2[j])
{
System.out.print(list1[i]+" ");
commonElementsHelper(list1,list2,++i,++j);
}
else if(list1[i] < list2[j])

commonElementsHelper(list1,list2,++i,j);

else
commonElementsHelper(list1,list2,i,++j);
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 4

Exercise 3 (16 Marks)


Write a static method named antiDiagonal that accepts a two-dimensional array representing a square matrix
and generates a two-dimensional array representing its anti-diagonals.
For example:

• For the following 2D array:

1 2
3 4

The call of antiDiagonal should return the following two-dimensional array

{
{1},
{2,3},
{4}
}

• For the following 2D array:

1 2 3
4 5 6
7 8 9

The call of antiDiagonal should return the following two-dimensional array

{
{1},
{2,4},
{3,5,7},
{6,8},
{9}
}

Solution:

public static int[][] antiDiagonal(int [][]x)


{
int[][]res = new int[2*x.length-1][];
int s = 0;
for( int i = 0 ; i < res.length ; i++ ) {
if(i<= res.length/2)
s = i+1;
else
s = s -1;
res[i] = new int[s];

for( int j = i, r = 0 ; j >= 0 && r < res[i].length ; j-- ) {


if( (i-j) < x.length && j < x.length ){
res[i][r] = x[i-j][j] ;
r++;
}
}
}
return res;
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 5

Exercise 4 (6+14+4=24 Marks)

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:

public class Apartments {


int aptNum;
int bed;
int bath;
int floor;
int rent;
public Apartments(int aptNum, int bed, int bath, int floor, int rent){
this.aptNum =aptNum;
this.bed = bed;
this.bath = bath;
this.floor = floor;
this.rent = rent;
}

public String toString() {


return "Apartment number: "+ this.aptNum +"\n"
+ "Number of bedrooms: " + this.bed+"\n"+
"Number of bathrooms: " + this.bath + "\n"
+ "Floor number: " + this.floor + "\n"
+ "Rent amount: " + this.rent;
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 6

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:

public class Building {


String name;
Apartments[] apt;
int max;

public Building(String name, int max) {


this.name = name;
this.max = max;
this.apt = new Apartments[max];
}

public void addApartment(Apartments ap) {


if(apt[max-1]!=null){
System.out.println("The array is full");
return;
}

for(int i = 0; i < max; i++) {


if(apt[i] == null) {
apt[i] = ap;
return;
}
if(apt[i].floor == ap.floor && apt[i].aptNum == ap.aptNum){
System.out.println("The apartment is already exists");
return;
}

}
}

public Apartments[] generateFloor(int level) {


int c = 0;
for(int i = 0; i < max; i++) {
if (apt[i] != null) {
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 7

if (apt[i].floor == level)
c++;
}
}

Apartments[] a = new Apartments[c];


int j = 0;

for(int i = 0; i < max; i++) {


if (apt[i] != null) {
if (apt[i].floor == level) {
a[j] = apt[i];
j++;
}
}
}
return a;
}

public String toString() {


String temp = "Building name: " + name + "\n";

for(int i = 0; i < max; i++) {


if (apt[i] != null) {
temp += apt[i] + "\n";
}
}
return temp;
}

}
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.

Assume the building has a total number of 50 apartments.

See the sample output below for a sample run of the test program.

Building name: City Lights

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:

public static void main(String[] args) {


Apartments t = new Apartments(8,4,3,3,4500);
Apartments u = new Apartments(10,3,2,4,3500);
Apartments v = new Apartments(6,4,3,2,4500);
Apartments w = new Apartments(18,5,3,6,5500);
Apartments x = new Apartments(7,3,3,3,4000);

Building b = new Building("City Lights", 50);

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:");

for(int i = 0; i < a.length; i++) {


System.out.println(a[i]);
}
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 10

Exercise 5 (20 Marks)

a) The following program produces 4 lines of output. Write the output below, as it would appear on the console.

public class Pokemon {

int level;

public Pokemon(int level) {


this.level = level;
}
}

public class ReferenceMystery {

public static void main(String[] args) {


int hp = 10;
Pokemon squirtle = new Pokemon(5);
battle(squirtle, hp);
System.out.println("Level " + squirtle.level + ", " + hp + " hp");
hp = hp + squirtle.level;
battle(squirtle, hp + 1);
System.out.println("Level " + squirtle.level + ", " + hp + " hp");
}

public static void battle(Pokemon poke, int hp) {


poke.level++;
hp -= 5;
System.out.println("Level " + poke.level + ", " + hp + " hp");
}
}

Solution:

Level 6, 5 hp
Level 6, 10 hp
Level 7, 12 hp
Level 7, 16 hp

b) Predict the output of following Java program. Justify your answer.

public class Test {


int i;
}
public class Main {
public static void main(String args[]) {
Test t;
System.out.println(t.i);
}
}

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

c) Predict the output of the program.

public class Test


{

public static void main(String[] args)


{
String obj1 = new String("geeks");
String obj2 = new String("geeks");

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:

value of object1 is equal to object2

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.

public class Test


{
int a = 1;
int b = 2;

Test func(Test obj)


{
Test obj3 = new Test();
obj3 = obj;
obj3.a = obj.a++ + ++obj.b;
obj.b = obj3.b;
return obj3;
}

public static void main(String[] args)


{
Test obj1 = new Test();
Test obj2 = obj1.func(obj1);

System.out.println("obj1.a = " + obj1.a + " obj1.b = " + obj1.b);


System.out.println("obj2.a = " + obj2.a + " obj2.b = " + obj2.b);

}
}

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

e) What is the output of the following program?

public class Test {


public static void main(String[] args) {
Test obj = new Test();
obj.start();
}
void start() {
String stra = ”do”;
String strb = method(stra);
System.out.print(“: ”+stra + strb);
}
String method(String stra) {
stra = stra + ”good”;
System.out.print(stra);
return“ good”;
}
}

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?

public class Subject {

String name;

public String getName() {


return name;
}

public void setName(String s) {


name = s;
}

public Subject(String s) {
name = s;
}
}

public class Student {

Subject subj;
String name;

public Subject getSubj() {


return subj;
}

public String getName() {


return name;
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 13

public void setName(String s) {


name = s;
}

public Student(String s, Subject subj) {


name = s;
this.subj = subj;
}

public static void main(String[] args) {


Subject subj = new Subject("CSEN 202");
Student stud = new Student("John", subj);

System.out.println("Before: " + stud.getName() + " - "


+ stud.getSubj().getName());

subj.setName("MATH 201");

System.out.println("After: " + stud.getName() + " - "


+ stud.getSubj().getName());

}
}

Solution:
There is a change in the name of the subject:

Before: John - CSEN 202


After: John - MATH 201

The amendment would be to change the constructor as follows:

public Student(String s, Subject subj) {


name = s;
this.subj = new Subject(subj.name);
}
CSEN 202: Introduction to Computer Programming, Final Exam, June 6, 2018 Page 14

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy