CSU Banner

CS 163/164, Spring 2018

Lab 8

Here are the instructions for the lab:
  1. Make sure you are in Workspace not QuizSpace
  2. Create a project called L8 in Eclipse.
  3. Create a class called L8 in the project L8 with a main method.
  4. Copy the L8.java file from the Progress tab into the L8 project source (~/workspace/L8/src).
  5. Copy the data.txt file from the Progress tab into the L8 project (~/workspace/L8).
  6. Write the rest of your code in the main method, below the code shown.
  7. import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    public class L8 {
        public static void main(String[] args) {
            // Declare variables
            int int0=0, int1=0;
            double real0=0.0, real1=0.0;
            char char0=' ', char1=' ', char2=' ';
            String string0="", string1="", string2="";
            try {
                // Open input stream
                Scanner scan = new Scanner(new File("data.txt"));
                // Read contents
                string0 = scan.nextLine();
                string1 = scan.nextLine();
                string2 = scan.nextLine();
                char0 = scan.next().charAt(0);
                char1 = scan.next().charAt(0);
                char2 = scan.next().charAt(0);
                int0 = scan.nextInt();
                int1 = scan.nextInt();
                real0 = scan.nextDouble();
                real1 = scan.nextDouble();
                // Close input stream
            } catch (IOException e) {
                System.out.println("Cannot read file: data.txt");
            // PUT CODE HERE!
  8. We are going to kind of treat this like a programming quiz, except the TA will actually answer your questions!
  9. You may edit the data file to test with different values.
  10. Make sure your L8.java compiles and produces exactly 9 lines of output.
  11. Show your lab to the TA for credit, as usual.
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-I. Do not print anything except what is specified below. You are free to open data.txt in Eclipse to see its contents.

IMPORTANT: Do the steps in order and make sure that each step A-I produces exactly one line of output! Add an empty System.out.println() if you cannot do one of the steps. HINT: Add a comment for each step before coding, and check the output after coding.
A) Print an expression that adds the product of int0 multiplied by 17 to the product of int1 multiplied by 7.
B) Print an expression that subtracts real0 from real1, then divides the result by 4.321.
C) Print real1 to the fifth power using Math.pow() with exactly 6 digits after the decimal.
D) Print the 4th character of string0, the 3rd character of string1, and the last character of string2, separated by commas. The code should work regardless of the length of string2!
E) Print the length of string1 added to the ASCII value of the first character of string1. HINT: Type cast the first character of string1 to get an integer before the addition.
F) Print the product of the ASCII values of char0, char1, and char2.
G) Print the boolean that is returned from using a comparison operator to check whether string2 is equal to "Hello World!".
H) Write a conditional (if/else if/else) statement as follows:
I) Write a switch statement as follows:

© 2017 CS163/CS164 Colorado State University. All Rights Reserved.