CS 160, Spring 2015
Programming Quiz Q1
First Java Midterm

Must be completed during recitation on Feb. 18-20


You have 50 minutes to complete the quiz.

Here are the instructions for the programming quiz:
  1. Create a project called Q1 in Eclipse.
  2. Create a class called Q1 in the project Q1 with a main method.
  3. Put the code shown below in the main method of Q1.java.
  4. Write the rest of your code in the main method, below the code shown.
            // Declare variables
            int int0=0, int1=0, int2=0;
            double real0=0.0, real1=0.0, real2=0.0;
            String string0="", string1="", string2="";
            boolean p = true, q = true, r = false;
    
            try {
    
                // Open input stream
                Scanner scan = new Scanner(new File("data.txt"));
    
                // Read contents
                string0 = scan.nextLine();
                string1 = scan.nextLine();
                string2 = scan.nextLine();
                int0 = scan.nextInt();
                int1 = scan.nextInt();
                int2 = scan.nextInt();
                real0 = scan.nextDouble();
                real1 = scan.nextDouble();
                real2 = scan.nextDouble();
                p = scan.nextBoolean();
                q = scan.nextBoolean();
                r = scan.nextBoolean();
    
                // Close input stream
                scan.close();
    
            } catch (IOException e) {
                System.out.println("Cannot read file: data.txt");
                System.exit(0);
            }
    
  5. Use Eclipse to add the imports needed for the above code.
  6. Copy the data file into data.txt into the Q1 project (~/workspace/Q1).
  7. Follow the instructions from the TA to complete the programming quiz.
  8. Make sure your Q1.java compiles and produces exactly 10 lines of output.
  9. Submit your Q1.java to the RamCT dropbox for this programming quiz.
  10. WARNING: Only one submission is allowed!


Note: The variables have already been defined and read from a file in the provided code, do not redeclare or assign your own values to the variables! Start with the provided code, then add your code below to do steps A-J. Do not print anything except what is specified below, any extra output will cause you to lose points. You are free to open data.txt in Eclipse to see its contents. However, we will test your program using different versions of data.txt too.

IMPORTANT: Do the steps in order and make sure that each step A-J produces exactly one line of output!
A) Print an expression that adds int0, int1, and int2, then multiplies the result by 321. (10 points)
B) Print an expression that subtracts real2 from real1, then divides the result by 5.3. (10 points)
C) Print the result of int2 multiplied by real1 squared with exactly 6 digits after the decimal. (10 points)
D) Print an expression with the concatenation of "Java programming is " and the 14th, 12th, 15th, and 13th character of string0, not separated by any spaces. (10 points)
E) Print the fourth characters of string0, string1, and string2, separated by commas. (10 points)
F) Print the boolean that is returned from a comparison of the contents of string0 and string1 using the appropriate String method. (10 points)
G) Print the result of the boolean expression (p∧q)∨(q∨¬r). (10 points)
H) Write an if/else statement as follows (10 points):
I) Write a conditional statement as follows (10 points):
J)Write a switch statement as follows (10 points):

You must submit your Q1.java program to the RamCT drop box to get graded for Programming Quiz 1.
© 2015 CS160 Colorado State University. All Rights Reserved.