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

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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: Ralf Tamer Student ID: 51930372

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 [ ]a = {10, 13, 15 ,18 , 16.3};

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 n)

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: double multipl = 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: { if (A[i] == A[i+1])
Return false;

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

1|Page
Spring 2019-2020

absolute1(5);
/// add code here to call absolute2 method

Write your answer here: int abs2 = classname.absolute2(5);

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 absciss x: 4
The following points are above the line: (4, 13) (4,11)

Answer:
package scratch;
import java.util.Scanner;
public class Scratch {

public static void main(String[ ] args) {


Scanner input = new Scanner(System.in);
int x;

System.out.print("Enter five ordinates y: ");


int ordinate[] = new int[5];
for (int i = 0 ; i<5 ; i++)
{
ordinate[i] = input.nextInt();
}

System.out.print("Enter their abscissa x: ");


x = input.nextInt();

int y = 2 * x + 1;

System.out.print("The following points are above the


line");
for(int p =0;p<5;p++)
{
if (ordinate[p]>y)
{
System.out.println("("+x+","+ordinate[p]+")");
}
}

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: package scratch;
import java.util.Scanner;
public class NewClass {
public static void main(String[ ] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a character : ");
char alpha = input.next().charAt(0);
System.out.print("Enter an integer : ");
int n = input.nextInt();
if (n < 0 )
{
System.out.print("Wrong entry");
}

pattern(alpha , n);
}
public static void pattern(char alpha, int n){

for (int i = 0 ; i < n ; i++)


{
System.out.print(alpha);

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:
package scratch;
import java.util.Scanner;
public class NewClass1 {
public static void main(String[] args) {
int ID;
Scanner input = new Scanner(System.in);
System.out.print("Please enter the number of students :
");
int n = input.nextInt();
for (int i = 1; i<=n;i++)
{
System.out.print("Enter ID for student "+i+": ");
ID = input.nextInt();
analyzeID(ID);
}

}
public static String analyzeID(int ID){

if (ID/10000000 == 1)
{
System.out.println("Campus : Beirut");

4|Page
Spring 2019-2020

}
else if (ID/10000000 == 2)
{
System.out.println("Campus : Saida");
}
return null;

}
}

5|Page

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