Colorado State University

Recitation R5 - Interpret Java & Setting up P4
Fall 2013

CS160: Foundations in Programming


The purpose of this lab is to:

Get started on P4

The setup for P4 is complex enough that you won't want to do it on your own, so the TA will help you to get started. P4 requires an additional Java module for the graphical user interface, and an image file. As usual, use Eclipse to create a P4 project and associated P4 class in P4.java. Copy the Chihiro.jpg image files to ../P4/, and the Maze.java source file to ../P4/src/:

A tree view of the P4 directory should look like this:
 P4/
├── Chihiro.jpg
├── bin/
└── src/
         └── Maze.java

After the TA shows you how to copy files, add the following code to the main method in P4.java:
        // Check program arguments
        if (args.length != 2)
        {
            System.out.println("usage: java P4 numberRows numberCols");
            System.exit(-1);
        }
        
        // Store maze size
        int numberRows = Integer.parseInt(args[0]);
        int numberCols = Integer.parseInt(args[1]);

        // Print maze size
        System.err.println("Number of rows: " + numberRows);
        System.err.println("Number of columns: " + numberCols);
        
        // Initial position
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Starting row: ");
        int currentRow = keyboard.nextInt();
        System.out.print("Starting column: ");
        int currentCol = keyboard.nextInt();
        keyboard.close();

        // Create maze
        Maze maze = new Maze(numberRows, numberCols, currentRow, currentCol);

Modify the run configuration to pass the numbers of rows (4) and number of columns (5) to the program. Test your program by making Chihiro move one square to the right. This is done by incrementing the currentCol and calling Maze.moveTo() with the currentRow and currentCol. Now implement down, left, and up in a similar fashion. When complete your program should Move Chihiro right, down, left, then up, returning her to the original starting point.

Interpret Java

What does the following code print out after the prompt(s)?

DO NOT RUN THIS CODE. Try and figure out what it does just by looking at it. You are not graded on correctness, this is purely to help you understand java.
//imports
import java.util.Scanner;

//Interpreting Java 
public class R5 {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter an integer between 0 and 10: ");
        int selection = keyboard.nextInt();
        int number = 5;
        keyboard.nextLine(); // flush scanner

        switch (selection) {

        case 6:
            System.out.println("Case 6");
            number = 1 + 2 * 3;
            break;

        case 2:
            System.out.println("Case 2");
            number = 100;

        case 1:
            System.out.println("Case 1");
            number = 5 * number;
            break;

        case 5:
            System.out.println("Case 5");
            number = 5 / 2;
            break;

        case 4:
            System.out.println("Case 4");
            number = 4;
            break;

        case 3:
            System.out.println("Case 3");
            number = selection % 2;

        default:
            System.out.println("Default");
            number = 1234;
            break;

        }

        System.out.println(number);

        System.out.print("Enter a salary: ");
        double salary = keyboard.nextDouble();
        keyboard.nextLine(); // flush scanner

        if (0.0 <= salary && salary < 1000.0) {
            System.out.println("This salary is within the 0% tax bracket");

        } else if (1000.0 <= salary && salary < 10000.0) {
            System.out.println("This salary is within the 15% tax bracket");

        } else if ((10000.0 <= salary && salary < 50000.0)
                || (100000.0 <= salary && salary < 1000000.0)) {
            System.out.println("This salary is within the 20% tax bracket");

        } else if (50000.0 <= salary && salary < 100000.0) {
            System.out.println("This salary is within the 18% tax bracket");

        } else {
            System.out.println("This salary is within the flat rate tax bracket");

        }

        int x = 2;
        if (x == 1) {
            System.out.println("x == 1");

        } else if (x <= 2) {
            System.out.println("x <= 2");

        } else if (x == 2) {
            System.out.println("x == 2");

        } else if (x >= 2) {
            System.out.println("x >= 2");

        } else {
            System.out.println("None of these");
        }

        System.out.print("Enter an animal: ");
        String input = keyboard.nextLine();

        System.out.println("The animal you entered was: " + input);
        System.out.println("The length of the animal name is: " + input.length());
        System.out.println("The letter of the animal name at index 1 is: " + input.charAt(1));

        System.out.print("Enter an integer: ");
        int inVal = keyboard.nextInt();
        String s = "test";

        System.out.println("The number you entered was: " + inVal);
        System.out.println(inVal + " * 5 = " + (inVal * 5));
        System.out.println("You have " + inVal + " " + input + s.charAt(2));

        keyboard.close();
    }

}

// Example Console Input:
--------------------------------
3
956450.00
Peacock
6
--------------------------------
Write your answer down (you should have 15 lines of output, including prompts) and your TA will go over it once everyone has finished.
Show your interpret Java work and P4.java program to the TA for grading, and submit P4.java to the Checkout tab.
Note that this version of the program is just to get you started, lots more work is needed to complete the assignment.


© 2013 CS160 Colorado State University. All Rights Reserved.