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

Lebanese International University: CSCI 250 - Introduction To Programming - TEST-2: Student Name: Student ID

The document contains details of a test for an Introduction to Programming course, including 4 problems to solve. Problem 1 involves short answer questions about arrays, methods, and logical errors. Problem 2 asks to write a program that reads ordinates from the user and checks which points are above a given line. Problem 3 requires writing a method to print a character pattern and a test program. Problem 4 defines a method to analyze a student ID number and return their campus, and a main method to test it.

Uploaded by

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

Lebanese International University: CSCI 250 - Introduction To Programming - TEST-2: Student Name: Student ID

The document contains details of a test for an Introduction to Programming course, including 4 problems to solve. Problem 1 involves short answer questions about arrays, methods, and logical errors. Problem 2 asks to write a program that reads ordinates from the user and checks which points are above a given line. Problem 3 requires writing a method to print a character pattern and a test program. Problem 4 defines a method to analyze a student ID number and return their campus, and a main method to test it.

Uploaded by

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

Spring 2019-2020

Lebanese International University


CSCI 250 – Introduction to Programming - TEST-2
Exam Date: 25/06/2020 Time: 12:30 p.m -15:30 p.m
Student Name: Albatool Abdul Fattah Student ID: 21730861

Problem 1 (10 points): Short Answer questions

a) Create and initialize an array with five double values of your choice, between 10 and
18.
Answer: double array []={11,12,13,14,15};
b) The area of a cylinder is: Area= 2×π × r (r + h), where r and h are the radius and height
of the cylinder. Complete the header of the method that computes and returns the area of a
cylinder:
Answer: public static double Area(double r, double h){
}

c) Assume having an array of decimal numbers named A. Write the code to print the
multiplication of the second element in A by the last element in A.
Answer: System.out.println(“A[1]*A[A.length-1]”);

d) Correct all the compile and logical errors in this method so it returns true if there
are no two equal consecutive values in the array, otherwise the method returns false.
For example, if the array contains {5, 6, 0, 0, 3} the method returns false. If the array
contains {5, 6, 4, 0, 3} the method returns true.
public static boolean CheckNonConsecutiveDuplicate(int[] A)
{ for(int i=0; i<A.length; i++)
{ if (A[i] = A[i+1])
return true;
else
return false;
}
}
Answer: public static boolean
CheckNonConsecutiveDuplicate(int[] A){
for(int i=0; i<A.length; i++){
if (A[i]==A[i+1])
return false;
}
return true;
}

1|Page
Spring 2019-2020

e) Consider the following two methods and the main. Assume the methods are
implemented. Do NOT write code in the method’s body. Complete the main program
to invoke the two methods:
public static void absolute1(int[] A){…}
public static int[] absolute2(int[] A){…}
public static void main (String[] args){
int[] X={5, -2, 9, 10, -15, -34};
/// add code here to call absolute1 method

Write your answer here: absolute1(X);


/// add code here to call absolute2 method

Write your answer here: int Y []=absolute2(X);


}

Problem 2 (30 points):


Given the line equation: y=2x+1, a point with coordinates (x,y) is
above the line if the value of y is greater than 2x+1. For example,
the point A(4,10) is above the line y=2x+1, because 10 > 2*4+1.
Write a program that reads a set of 5 ordinates y from the user,
stores them in an array of 5 elements. We assume the ordinates have
the same abscissas. The program displays the points that are above
the line Use loop to iterate through the array.
Sample run:
Enter five ordinates y: 2 13 3 5 11
Enter their abscissa x: 4
The following points are above the line: (4, 13) (4,11)

Answer:
import java.util.Scanner;
public class problem2 {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int size=5;
int[]y = new int[size];
int x;
System.out.print("Enter five ordinates y: ");
for(int i=0;i<size;i++){
y[i]=scan.nextInt();
}
System.out.print("Enter their abscissa x: ");
x = scan.nextInt();
System.out.print("The following points are above the
line: ");
for(int i=0; i<size; i++){
if(y[i]>(2*x)+1){
System.out.print("("+x+","+y[i]+")");
}
}
}

2|Page
Spring 2019-2020

}
Problem 3 (30 points)
a) Write a method pattern with the following header:
public static void pattern(char alpha, int n)
This method prints the character alpha n times. So, if pattern('*', 4) is
called, its output is: ****
b) Write a test program that asks the user to enter a character n and an
integer m, then calls the above method. If any of the numbers is
negative, the program prints “wrong entry”.
Sample run1: Sample run2:
Enter a character: @ Enter a character: @
Enter an integer: 3 Enter an integer: -4
The output is @@@ Wrong entry
Answer:
import java.util.Scanner;
public class problem3 {
public static void pattern(char alpha, int n){
for(int i=0; i<n; i++){
System.out.print(alpha);
}
System.out.println();
}
public static void main(String[] args){
Scanner scan= new Scanner(System.in);
System.out.print("Enter a character: ");
char n=scan.next().charAt(0);
System.out.print("Enter an integer: ");
int m =scan.nextInt();
if(m < 0){
System.out.println("Wrong entry");
}
else{
System.out.print("The output is ");
pattern(n, m);

3|Page
Spring 2019-2020

}
}

Problem 4 (30 points)


a) Write a method named analyzeID that accepts as a parameter an integer
value representing a student’s ID. The header of the method is as
follows:
public static String analyzeID(int ID)
The ID is composed of 8 digits. The method returns information about the
campus the student is registered at. For simplicity, we will consider the
below cases regarding the campus:
 If the ID starts with 1, the method returns “Beirut”
 If the ID starts with 2, the method returns “Saida”
 Otherwise, the method returns “Other Campus”
For example: if the ID is 12057346, the call analyzeID(12057346) returns
Beirut.
b) Write a main method that asks the user to enter the number of students
and the list of IDs. If the ID is composed of 8 digits (between 10 8 and
109), then the campus information is displayed by invoking the method
analyzeID. If the ID is not valid, the method is not invoked.
Sample run:
Enter the number of students: 3
Enter ID for student 1: 3457
Enter ID for student 2: 11834725
Campus: Beirut
Enter ID for student 3: 27458259
Campus: Saida
Answer:
import java.util.Scanner;
public class problem4 {
public static String analyzeID(int ID) {
int ch = (int) Math.floor((double) ID / Math.pow(10, 7));

if (ch == 1)
return "Beirut";
if (ch == 2)
return "Saida";
else
return "Other Campus";
}
public static void main(String[] arg) {
int ID, n, i = 1;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number of students: ");
n = in .nextInt();
while (i <= n) {
System.out.print("Enter ID for student " + i + ": ");
ID = in .nextInt();
if (ID >= (int) Math.pow(10, 7) && ID < (int) Math.pow(10, 8))
System.out.println("Campus: " + analyzeID(ID));
i++;
}

4|Page
Spring 2019-2020

}
}

5|Page

You might also like