ITCS114 Midterm Sem2 2022 2023 Key Answer
ITCS114 Midterm Sem2 2022 2023 Key Answer
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 #
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:
Output:
Page 2 of 5
Question (2) Consider the following class:
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
} // 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:
// 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
}
} // 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) {
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
}
// 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