JMU JMU - Department of Computer Science
Help Tools
Computer-Based Programming Portion of Exam 1 - Sample


This is an "open book" examination but you must work entirely on your own.

The source code you submit must be entirely your work and must be written entirely during the exam period. The use of any pre-existing code (other than that provided as part of the exam) is prohibited.

Your code need not comply with the course style guidelines and need not include comments. You should not submit any tests.

No limit will be placed on the number of submissions but, obviously, you must complete the exam during the exam period and submissions waste time. Autolab will not provide hints. Your last submission is the one that will be graded.

For question 1, you must submit a .zip file named e1q1.zip that contains just the file Game.java.

For question 2, you must submit a .zip file named e1q2.zip that contains just the file Inspector.java.

  1. (25 points) Complete the following Game class which must contain a method named isUnlucky() and a method named movePieceBy().

    movePieceBy() is passed an int board position (there are 32 such positions and they are 0-based) and an int representing the number on one die (which can have an arbitrary number of sides, not just six, and, hence, have any value). It must return the board position that corresponds to advancing the current board position by the number of positions on the die. So, for example, if the current position is 18 and the number on the die is 22, it must return 8.

    isUnlucky() is passed two int values representing the numbers on two dice. isUnlucky() must return true if the sum of the two dice (each of which can again have an arbitrary number of sides) is a multiple of 13 and must return false otherwise. So, for example, it must return true if it is passed 39 and must return false if it is passed 40.

    Your solution must not print to the console/display and must not read from the console/keyboard. You may write a main class (with a main() method) for testing purposes, but you must not submit it.

    Note: The elegance of your solution matters. That is, there are more and less elegant solutions to this problem. The more elegant your solution, the more points you will receive. For example, it is very inelegant to use multiple if statements, and it is inelegant to use an if statement, switch statement, or ternary operator.

    public class Game {
    
        public int movePieceBy(int position, int number) {
    
    
    
    
    
    
        }
    
    
    
    
        public boolean isUnlucky(int die1, int die2) {
    
    
    
    
    
        }
    }
    
  2. (25 points) Create a class named Inspector that contains a public static method named isCleanEnough() that is passed an int named age, a double named co, and a double named particulates, and returns a boolean.

    This method must:

    1. Have a double variable named ageFactor.
    2. Assign the value 2.0 to ageFactor if age is between 10 and 20 (inclusive) and assign the value 1.0 to ageFactor otherwise.
    3. Have a double variable named adjusted.
    4. Calculate the age adjusted carbon monoxide emssions by multiplying co by ageFactor and assign the result to adjusted
    5. Return false if the age adjusted carbon monoxide emissions value is greater than 200.0.
    6. Return false if the particulates value is greater than 50.0.
    7. Return false if both the age adjusted carbon monoxide emissions value and the particulates value are greater than 20.
    8. Return true otherwise.
    9. Have exactly one return statement.

    Your solution must not print to the console/display and must not read from the console/keyboard. You may write a main class (with a main() method) for testing purposes, but you must not submit it.

    Note: The elegance of your solution matters. That is, there are more and less elegant solutions to this problem. The more elegant your solution, the more points you will receive. For example, it is very inelegant to use multiple if statements, and it is inelegant to use an if statement, switch statement, or ternary operator.

Copyright 2020