Sei sulla pagina 1di 2

CSCI250 Exam 1 – 10:00- 12:00 – Version 1- Solution Summer 2015-2016

Problem #1: [30 pts: 5 pts for each]

1. b. Y = 7
2. c. Y = 1
3. a. a = 8, b = 7
4. c. x3
5. d. 3 x
6. f. A C

Problem #2: [35 pts.]


Note: Marks should be subtracted if the order of the statements is not correct.

import java.util.Scanner; //1 pt


public class Problem2
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in); //1 pt
int adults, children, reservationCost; //1 pt
double serviceCost, totalCost; //1 pt
System.out.print("Enter the number of adults and the number of children: "); //1 pt
adults = input.nextInt(); //1 pt
children = input.nextInt(); //1 pt

if(adults < 0 || children < 0) //3 pt


System.out.println("Wrong inputs. Values cannot be negative."); //1 pt
else if((adults + children) > 20) //3 pt
{
System.out.println("The number of people exceeds 20. ");
System.out.println("The reservation cannot be produced."); //1 pt
}
else //1 pt
{
reservationCost = adults * 30 + children * 15; //3 pt
if (reservationCost <= 150) //3 pt
serviceCost = reservationCost * 0.15; //3 pt
else //1 pt
serviceCost = reservationCost * 0.10; //3 pt
totalCost = reservationCost + serviceCost; //3 pt
System.out.println("The reservation cost is: " +reservationCost); //1 pt
System.out.println("The service cost is: " +serviceCost); //1 pt
System.out.println("The total cost is: " +totalCost); //1 pt

} // end else
} // end main
} // end Problem2

Page 1 of 2
CSCI250 Exam 1 – 10:00- 12:00 – Version 1- Solution Summer 2015-2016

Problem #3: [35 pts]


Note: Marks should be subtracted if the order of the statements is not correct.

import java.util.Scanner; //1 pt


public class Problem3 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in); //1 pt
int nb, d1, d2, d3, d4, sum = 0 , prod = 1; //4 pts
System.out.print("Enter a 4 digit number: "); //1 pt
nb = input.nextInt(); //1 pt
if (nb < 1000 || nb > 9999) //2 pts
System.out.println("You should enter a number between 1111 and 9999!"); //1 pt
else { //1 pt
d1=nb%10; //1 pt
nb /=10; //1 pt
d2=nb%10; //1 pt
nb /=10; //1 pt
d3=nb%10; //1 pt
nb /=10; //1 pt
d4=nb%10; //1 pt
if (d1%2 == 0) //1 pt
sum +=d1; //1 pt
else // ½ pt
prod *=d1; //1 pt
if (d2%2 == 0) //1 pt
sum +=d2; //1 pt
else // ½ pt
prod *=d2; //1 pt
if (d3%2 == 0) //1 pt
sum +=d3; //1 pt
else // ½ pt
prod *=d3; //1 pt
if (d4%2 == 0) //1 pt
sum +=d4; //1 pt
else // ½ pt
prod *=d4; //1 pt
System.out.println("Sum of even digits: " + sum); //1 pt
System.out.println("Product of even digits: " + prod); //1 pt

} // end else
} // end main
} // end Problem3

Page 2 of 2

Potrebbero piacerti anche