0% found this document useful (0 votes)
10 views5 pages

ITCS114 Midterm Sem2 2022 2023 Key Answer

Uploaded by

danah.m.alkhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

ITCS114 Midterm Sem2 2022 2023 Key Answer

Uploaded by

danah.m.alkhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

SERIAL NO:

University of Bahrain
College of Information Technology
Department of Computer Science
Second Semester, 2022-2023
ITCS 114 - Computer Programming II

Midterm
Date: 4-May-2023 Duration: 1 Hour & 15 minutes

STUDENT NAME

STUDENT ID # SECTION #

Question # MARKS COMMENTS

1 8

2 12

3 15

4 15

TOTAL 50

Notes:
1. Please write only one answer for each question. Only the first answer will be graded.
2. You may use the back pages to complete your answer if needed.

Page 1 of 5
Question (1) Find the output of the following program:

class Simple public class simpleDemo{


{ public static void main(String[] args)
private static int a = 5; {
private int b; Simple r = new Simple();
r.incr();
public Simple( ) r.display();
{ b=4; a++; b++; }
Simple s = new Simple(3);
public Simple(int x) s.display();
{ b=x; a+= x; b--; } s.incr(10);

public void incr( ) Simple t = new Simple(r.getValue());


{ a++; } t.display();

public void incr(int x) if (r.getValue() == t.getValue())


{ a += x; } System.out.println("Dark");
else
public static int getValue() System.out.println("Light");
{ return a; } }
}
public void display()
{ System.out.println(a+","+b); }

Output:

// Each line 2 pts


7,5
10,2
40,19
Dark

Page 2 of 5
Question (2) Consider the following class:

public class Triangle {


private int a; private int b; private int c;
public Triangle() {a = 0; b = 0; c = 0;}
public Triangle(int newA, int newB, int newC) { a = newA; b = newB; c = newC;}
public int getA( ) {return a;}
public int getB( ) {return b;}
public int getC( ) {return c;}
}

Write a static method named checkTriangle that takes as a parameter an array of type Triangle named list. The method
should check the array and output the count of equilateral, isosceles and scalene Triangles. A triangle is said to be
equilateral triangle if all the sides are equal, a triangle is said to be an isosceles triangle if any of its two sides are equal,
and a triangle is said Scalene Triangle if none of its sides is equal. Note: assume the array list is declared and populated
with data in the main method

public static void checkTriangle(Triangle[] list) {

int E=0, I=0, S=0; // 1 pts

for (int i=0; i<list.length; i++) // 1pts


{
// Check for equilateral triangle
if(list[i].getA() == list[i].getB() && list[i].getB()==list[i].getC() ) // 3ps
E++; // 0.5 pt

// Check for isosceles triangle


else if (list[i].getA() == list[i].getB() ||list[i].getB()==list[i].getC() ||
list[i].getC() == list[i].getA() ) // 3pts
I++; // 0.5 pts
// Otherwise scalene triangle
else // 1 pts
S++; // 0.5 pt
}

System.out.println("Number of Equilateral Triangles = "+ E); // 0.5 pt


System.out.println("Isosceles Triangles = "+I);// 0.5 pt
System.out.println("Isosceles Triangle = "+S);// 0.5 pt

} // end of method

Page 3 of 5
Question (3)
Write the definition of a class named Workshop with four private data members:
- title: representing the title of the workshop.
- capacity: representing the length of the array list.
- list: an array of size capacity to hold CPR numbers (int) of registered persons in the workshop.
- size: representing the number of used elements in the array list.
Additionally, the class should include the following public methods:
a. A constructor that accepts title and capacity as input parameters to initialize the corresponding data members,
create a new array list of length capacity, and initializes size to 0.
b. A method named register: that takes as parameter (newCPR) to be added at the end of list (if list is not full) and
increment size.
c. A method named show( ) to display the data of the workshop object. See Sample Below:

Title: An introduction to Java programming


Registered Persons:
770771110
81654231
965234511

public class Workshop {

// private memebrs each 1 pt - Toal 4 pts


private long [] list; private int size;
private String title; private int capacity;

// Total = 3 pts
public Workshop(String t, int C) // 0.5 pt
{
list = new long[C]; // 1 pt size = 0; // 0.5 pt
capacity = C; // 0.5 pt title = t; // 0.5 pt
}

// Total = 4pts
public void register(long newCPR) // 0.5 pt
{
if (size == list.length) // 1 pt
System.out.println("The list is full");
else // 0.5 pt
{
list[size] = newCPR; // 1 pt
size++; // 1 pt
}
}

// Total = 4 pts
public void show() { // 1 pt
System.out.println("Title : "+ title); // 0.5 pt
System.out.println("Registered Persons : "); // 0.5 pt

for (int i = 0; i < size; i++) // 1 pt


System.out.print(list[i]+","); // 1 pt

}
} // end of class

Page 4 of 5
Question (4) Write a Java program (One class with a main method) to do the following:
1- Declare an array named batteryList of size 5 of type integer to represent battery capacity in milliamp-hours
(mAh) and prompt the users to enter the values of the array.
2- Ask the user to enter double value for typical consumption (mA).
3- Output the estimated battery life and index of batteries that will have estimated life less than 5 hours, using
the following formula:
𝐵𝑎𝑡𝑡𝑒𝑟𝑦𝐶𝑎𝑝𝑎𝑐𝑖𝑡𝑦(𝑚𝐴ℎ)
𝐸𝑠𝑡𝑖𝑚𝑎𝑡𝑒𝑑𝐵𝑎𝑡𝑡𝑒𝑟𝑦𝐿𝑖𝑓𝑒(ℎ𝑜𝑢𝑟𝑠) =
𝐶𝑜𝑛𝑠𝑢𝑚𝑝𝑡𝑖𝑜𝑛(𝑚𝐴)
Sample Input/output:
Enter Battery Capacity 0: 100
Enter Battery Capacity 1: 20
Enter Battery Capacity 2: 300
Enter Battery Capacity 3: 30
Enter Battery Capacity 4: 400
Enter typical consumption (mA) : 50.50
Batteries that will have estimated battery life less than 5 hours Life :
Battery Located in index = 0 with Estimated Life : 1.98
Battery Located in index = 1 with Estimated Life : 0.39
Battery Located in index = 3 with Estimated Life : 0.59

import java.util.Scanner;
public class Battery{
public static void main(String[] args) {

Scanner input = new Scanner(System.in);


int [] batteryList = new int[5]; // 2 pts

for(int i=0;i<batteryList.length;i++) // 1 pt
{
System.out.print("Enter Battery Capacity " + i + ":"); // 1 pt
batteryList[i] = input.nextInt(); // 1 pt
}

System.out.print("Enter typical consumption (mA) : "); // 1 pt


double cons = input.nextDouble(); // 1 pt

// 1 pt
System.out.println("Batteries that will have estimated battery life less than 5 hours Life : ");
for(int i=0;i<batteryList.length;i++)// 1 pt
{
double life = batteryList[i]/cons; // 2 pt
if(life<5) // 2 pt
{
//2 pt
System.out.println("Battry Located in index = "+ i +" with Estimated Life : "+life);

}
}

} // end of main
} // end of class

Page 5 of 5

You might also like