0% found this document useful (0 votes)
40 views6 pages

ES234211 - Programming - Fundamental - MT - Paper 2024

Uploaded by

Ahmad
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)
40 views6 pages

ES234211 - Programming - Fundamental - MT - Paper 2024

Uploaded by

Ahmad
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

INSTITUT TEKNOLOGI SEPULUH NOPEMBER

Faculty of Intelligent Electrical, Electronic, and Informatics Engineering


Department of Information Systems
Bachelor of Computer Science Program in Information Systems
SEMESTER II, MIDTERM EXAMINATION, 2023/2024

ES234211 – Programming Fundamental

Course Convenors
Faizal Johan Atletiko
Renny Pradina Kusumawardani
Ahmad Muklason

25 April 2024 TIME ALLOWED: 2.5 HOURS

INSTRUCTIONS TO CANDIDATES

1. This examination paper contains SEVEN (7) questions

2. Answer strictly FIVE (5) questions only, i.e. questions numbers 1,


either 2 or 3, 4, 5, and either 6 or 7. The marks for each question are
indicated at the beginning of each question.

3. Answer the questions in any order.

4. This is CLOSED BOOK exam.

5. When justification is required, you MUST write down systematically


the steps in the workings.

6. Please work independently, and Good Luck!


ES234211

Question 1. (15 marks)


The following Java code is intended to calculate the factorial of a given
number. However, there are several errors in the code provided. Correct
these errors to calculate the factorial of 5 using the provided code. The
output should print: The factorial of 5 is: 120.0
public class FactorialCalculator {
public static void main(String[] args) {
int num = _____; // (1)
_____ factorial = _____; // (2)
for (int i = 1; _____; i++) { // (3)
factorial = _____ // (4)
}
System.out.println("The factorial of "
+ _____ + " is: " + _____); // (5)
}
}

Question 2. (15 marks)


Suppose a method is defined as follow:
public static void mystery(int n){
int c=n/4+2;
int d=c%5;
int arr [] = {3,5,7,9};
boolean b = n >=(2*d);
double s= 10;
if (!b){
for (int i=2;i<n;i++){
s+=arr[d];
i+=2;
}
}
System.out.print(s+d+"$"+c+d+(n-d));
}
What is the output from the following method call?Briefly justify your an-
swer!

2
ES234211

(a) mystery(5)

(b) mystery(7)

Question 3. (15 marks)


Suppose a method is defined as follow:

public static void mystery(int n){


char c []={’*’,’#’};

for(int i=1;i<n;i++){
int y=i;
do{
int x=i%2;
System.out.print(c[x]);
y++;
}while(y<5);
System.out.println();
}
}

What is the output from the following method call?Briefly justify your an-
swer!

(a) mystery(5)

(b) mystery(7)

Question 4. (20 marks)


Complete the following method definition:

public static void squareStars(int n){


//your code goes here

In order to generate expected output with its respective method call as follow:

3
ES234211

Please note that your code has to work with parameter n, any integers greater
than 0.

Question 5. (20 marks)


Given an input file namely input.txt, containing a single line string of exactly
five integer numbers separated by commas, complete the following java code
to calculate the highest and the average of these five numbers respectively
separated by a comma.
Example:
Input (input.txt) | expected output
-----------------------------------------
5,7,4,2,2 | 7,4.0
6,8,8,3,1 | 8,5.2

You may use branching, looping, array, and the following string manipulation
methods:
-----------------------------------------------------------
return type | method and description
-----------------------------------------------------------
String[] | split(String regex)
| Splits this string around
| matches of the given regular expression.
-----------------------------------------------------------

4
ES234211

Main.java:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner read = new Scanner(Main.class.getResourceAsStream
("input.txt"));
String txt = read.nextLine();
String [] numbers = txt.split(",");
//your code here
}
}

Question 6. (30 marks)


Write a complete Java program named Guess.java for a simple number guess-
ing game using do-while. The program should generate a random number
between 0 and 99 and then ask the user to input a guess. If the user’s guess
is incorrect, the program should provide feedback (whether the guess is too
high or too low) and ask the user to guess again. The game should continue
until the user correctly guesses the number. Use a do-while loop to handle
the game logic. More specifically, your code should:

a. Generate a random number between 0 and 99 using (int) (100*Math.random())

b. Ask the user to enter their guess:

i. Print ”Welcome to the Number Guessing Game!”


ii. Print ”Guess a number between 0 and 99.”
iii. Get the user’s guess

c. Compare the user’s guess with the random number.

d. If the guess is correct:

i. Print ”Congratulations! You guessed the correct number.”


ii. Print ”Guess a number between 0 and 99.”
iii. End the program

5
ES234211

e. If the guess is incorrect:


i. If the guess is too low, print: ”Too low! Try guessing higher.”
ii. If the guess is too high, print: ”Too high! Try guessing lower.”
iii. End the program
f. Continue this process until the user guesses correctly.
g. Use a do-while loop to repeatedly ask the user for guesses until the correct
number is guessed.
Please note that you should ask the user input from console (System.in).

Question 7. (30 marks)


A ferry company operates X ferries every day. Each ferry can carry up to 200
passengers. One day, N passengers would like to travel to the same island
destination. What is the minimum number of new ferries that the company
must buy to carry all N passengers?
Given an input file, named ferry.txt with the following format:
input format: (ferry.txt)
• The first line of input will contain a single integer T, denoting the
number of test cases.
• Each test case consists of a single line containing two space-separated
integers X and N — the number of ferries the ferry company owns and
the number of passengers travelling, respectively.
Sample Input:
3
4 600
3 523
8 245
Sample expected Output:
2
3
0

Create a complete java program, namely Ferry.java. to solve the problem

You might also like