CSCI250 - Exam 2 - V1-Solution
CSCI250 - Exam 2 - V1-Solution
Part 2
Problem 1 /35
Problem 2 /35
Instructions:
1. You can solve the questions using the Netbeans IDE if you want.
2. Then upload one file (Word, text, or PDF) containing all answers.
3. For questions 2 and 3 you can upload java files instead if you want.
Page 1 of 6
CSCI250 Exam II Spring 2021-2022
PART 1
Problem #1: [30 pts: 5 pts for each] Answer the following questions.
Answer
1) Which of the following statements a. int c = 20 + (int) (Math.random()* 100);
generates a random integer between
b. int c = 20 + (Math.random()* 100);
20 and 100 both inclusive?
c. int c = 20 + (Math.random()* 81);
d. int c = 20 + (int) (Math.random()* 81);
Page 2 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021
PART 2
Sample run1:
Please enter your 2 items: 1 3
Your purchase is:
bread butter
You should pay: 45.0
Sample run2:
Please enter your 2 items: 2 3
Your purchase is:
flour butter
You should pay: 46.75
Sample run3:
Please enter your 2 items: 1 2
Your purchase is:
bread flour
You should pay: 45.0
Sample run4:
Please enter your 2 items: 2 2
An item can be purchased only 1 time
Sample run5:
Please enter your 2 items: 3 1
Your purchase is:
butter bread
You should pay: 45.0
Page 3 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021
import java.util.Scanner; //1pt
if(item1==item2 ) //2pts
System.out.println("An item can be purchased only 1 time");//1pt
else{//2pts
System.out.println("Your purchase is:"); //1pt
double cost=0; //1pt
if(item1==1&&item2==2) //1pt
{ System.out.println("\tBread Flour");//1pt
cost=45; //1pt
}
else if(item1==1&&item2==3) //1pt
{ System.out.println("\tBread Butter");//1pt
cost=50-50*0.1; //2pts
}
else if(item1==2&&item2==1) //1pt
{ System.out.println("\tFlour Bread");//1pt
cost=45; //1pt
}
else if(item1==2&&item2==3) //1pt
{ System.out.println("\tFlour Butter");//1pt
cost=55-55*0.15; //2pts
}
else if(item1==3&&item2==1) //1pt
{ System.out.println("\tButter Bread");//1pt
cost=50-50*0.1; //2pts
}
else if(item1==3&&item2==2) //1pt
{ System.out.println("\tButter Flour");//1pt
cost=55-55*0.15; //2pts
}
Page 4 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021
PART 2
Problem #2: [35 pts]
Write a java program that helps the Lebanese scout to create an online tombola (lottery). Your program
should generate a random number between 10 and 99 using Math.random(). It should then ask the user to
enter 2 numbers each of 1 digit only (0 to 9) and compares these digits with the random number. If the 2
digits match the same placed digits in the number, your program outputs “Congratulations, You win the
tombola” and the random number. The user is allowed to repeat his guess for 10 times maximum.
For example, if the program generates 73 and the user enters 7 and 3, then the user wins. If the user enters
3 and 7, the user loses the round.
Sample Run 1:
Enter 2 digits: 1 6
Wrong, 9 tries left
Enter 2 digits: 3 5
Wrong, 8 tries left
Enter 2 digits: 1 6
Wrong, 7 tries left
Enter 2 digits: 9 1
Congratulations, You win the tombola, the random number is 91 !
Sample Run 2:
Enter 2 digits: 1 9
Wrong, 9 tries left
Enter 2 digits: 1 2
Wrong, 8 tries left
Enter 2 digits: 2 6
Wrong, 7 tries left
Enter 2 digits: 1 6
Wrong, 6 tries left
Enter 2 digits: 1 9
Wrong, 5 tries left
Enter 2 digits: 1 2
Wrong, 4 tries left
Enter 2 digits: 1 9
Wrong, 3 tries left
Enter 2 digits: 1 3
Wrong, 2 tries left
Enter 2 digits: 9 0
Wrong, 1 tries left
Enter 2 digits: 9 8
Wrong, 0 tries left
You lost, the random number is 45 !
Page 5 of 6
CSCI250 Exam I SOLUTION Spring 2020-2021
import java.util.Scanner; //1pt
public class Program2 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);//1pt
int r=10 + (int) (Math.random()* 90); //3pts
int count=10, d1, d2, n1, n2; //3pts
do{ //1pt
System.out.print("Enter 2 digits: ");//1pt
n1=input.nextInt();//1pt
n2=input.nextInt();//1pt
//extract digits from r
d1=r/10; //3pts
d2=r%10; //3pts
if(n1!=d1 || n2!=d2) //4pts
{ count--;//2pts
System.out.println("Wrong, "+count+" tries left"); //1pt
}
else //1pt
count=0; //1pt
}while(count>0); //3pts
if(d1==n1 && d2==n2) //1pt
{System.out.print ("Congratulations, You win the tombola");//1pt
System.out.println(", the random number is "+r+" !"); //1pt
}
else//1pt
System.out.println("You lost, the random number is "+r+" !");
//1pt
}
}
Page 6 of 6