Computer Science 15-100 (Sections T & U), Spring 2008
Homework 2
Due:  by email on Fri 25-Jan-2008 at 10:00am (email copy) and at recitation (physical copy)
(no late submissions accepted).



Sample questions and solutions (study these first!):

  1. [Read/Write from Console] Write a program, Hw2Sample1.java, that reads in two integers (from the console, which is the default unless otherwise stated) and prints out their sum.  This program should work like this (where user input is underlined):
         Enter two integers (separated by a space): 3 7
         3 plus 7 equals 10
    Of course, your program should work for any legal input!
    Remember to exactly match the output!

    Solution:
    // Hw2Sample1.java
    // David Kosbie, 15-100, Section T-or-U
    
    import java.util.Scanner;
    
    public class Hw2Sample1 {
      public static void main(String[] args) {
        // declare local variables
        Scanner scanner = new Scanner(System.in);
        int x, y, sum;
        
        // get the input (with a suitable prompt)
        System.out.print("Enter two integers (separated by a space): ");
        x = scanner.nextInt();
        y = scanner.nextInt();
        
        // compute result and display it
        sum = x + y;
        System.out.println(x + " plus " + y + " equals " + sum);
      }
    }
  2. [Read from a file] Write a program, Hw2Sample2.java, that reads in two integers from the file "in.txt" and prints out their product.  Because this program uses file IO, it should not include any prompts.  This program should work like this:
         in.txt contents:
              5
              6
         Program output:
              30
    Of course, your program should work for any legal input!
    Remember to exactly match the output!

    Solution:

    // Hw2Sample2.java
    // David Kosbie, 15-100, Section T-or-U
    
    import java.util.Scanner;
    import java.io.File;
    
    public class Hw2Sample2 {
      public static void main(String[] args) throws Exception {
        // declare local variables
        Scanner scanner = new Scanner(new File("in.txt"));
        int x, y, product;
        
        // get the input (with no prompt -- it's from a file!)
        x = scanner.nextInt();
        y = scanner.nextInt();
        
        // compute result and display it (with no extra UI -- it's from a file!)
        product = x * y;
        System.out.println(product);
      }
    }
  3. [Write to a file] Write a program, Hw2Sample3.java, that reads in two integers from the file in.txt and outputs their quotient to the file out.txt.  Because this program uses file IO, it should not include any prompts.  This program should work like this:
         in.txt contents:
              24
              6
         Program output:
              <none>
         out.txt contents (after running program):
              4
    Of course, your program should work for any legal input!
    Remember to exactly match the output!

    Solution:

    // Hw2Sample3.java
    // David Kosbie, 15-100, Section T-or-U
    
    import java.util.Scanner;
    import java.io.File;
    import java.io.PrintStream;
    
    public class Hw2Sample3 {
      public static void main(String[] args) throws Exception {
        // declare local variables
        Scanner scanner = new Scanner(new File("in.txt"));
        PrintStream out = new PrintStream(new File("out.text"));
        int x, y, quotient;
        
        // get the input (with no prompt -- it's from a file!)
        x = scanner.nextInt();
        y = scanner.nextInt();
        
        // compute result and display it (with no extra UI -- it's from a file!)
        quotient = x / y;
        out.println(quotient);
      }
    }
  4. [Read from a String]  Repeat problem #1, but read the input from a string literal.  This is useful for debugging purposes.  So, write a program, Hw2Sample4.java, that reads in two integers from a hard-coded string literal and prints out their sum.  This program should work like this (where the values from the string literal are underlined):
         Enter two integers (separated by a space): 3 7
         3 plus 7 equals 10
    Of course, your program should work for any legal input!
    Remember to exactly match the output!

    Solution:
    // Hw2Sample4.java
    // David Kosbie, 15-100, Section T-or-U
    
    import java.util.Scanner;
    
    public class Hw2Sample4 {
      public static void main(String[] args) {
        // declare local variables
        Scanner scanner = new Scanner("3 7"); // hard-coded input values in a string
        int x, y, sum;
        
        // get the input (with a suitable prompt)
        System.out.print("Enter two integers (separated by a space): ");
        x = scanner.nextInt();
        y = scanner.nextInt();
        
        // compute result and display it
        sum = x + y;
        System.out.println(x + " plus " + y + " equals " + sum);
      }
    }

Assigned Questions:

  1. Write a program, Hw2Q1.java, that reads in three integers from the console and prints out their sum.  This program should work like this (where user input is underlined):
         Enter three integers (separated by a space): 3 7 2
         3 + 7 + 2 = 12
    Of course, your program should work for any legal input!  Remember to exactly match the output!
     
  2. Write a program, Hw2Q2.java, that reads in three integers from the file in.txt and writes their sum to the file out.txt.  This program should work like this:
         in.txt contents:
              2 3 5
         Program output:
              <none>
         out.txt contents (after running program):
              10
    Of course, your program should work for any legal input!  Remember to exactly match the output!
     
  3. Write a program, Hw2Q3.java, that reads in two integers, x and y, from the console and prints out the remainder when x is divided by y (hint:  use the modulus operator).  This program should work like this (where user input is underlined):
         Enter two integers (separated by a space): 13 5
         The remainder when 13 is divided by 5 is 3.
    Of course, your program should work for any legal input!  Remember to exactly match the output!   Even the period at the end!

    Hint:  You may assume that the integers are both non-negative (that is, zero or greater), though you may satisfy your curiosity as to what happens when one or both of the numbers are negative.  Also, what happens when one or both of the numbers are 1?  0?  Try these different situations.  Is that what you expected?  While these are thought questions (you only have to submit your program, and not the answers to these questions), you should think about them -- they make great test questions!
     
  4. Write a program, Hw2Q4.java, that reads in two integers, x and y, from the the file in.txt and outputs the remainder when x is divided by y (hint:  use the modulus operator) to the file out.txt.  This program should work like this:
         in.txt contents:
              13 5
         Program output:
              <none>
         out.txt contents (after running program):
              3
    Of course, your program should work for any legal input!  Remember to exactly match the output!
     
  5. Write a program, Hw2Q5.java, that reads in three integers from the file in.txt and outputs the largest value entered to the file out.txt.  This program should work like this:
         in.txt contents:
              13 15 8
         Program output:
              <none>
         out.txt contents (after running program):
              15
    Of course, your program should work for any legal input!  Remember to exactly match the output!

    Hint:  Your program may not use "if" statements (we will cover those soon...).  Instead, you must use the Math.max method.  Here is how you would print out the larger of two values:
         System.out.println(Math.max(x,y));
    Here is how you would assign the larger of two int variables into a third int variable:
         z = Math.max(x,y);
    Note that you may not compute the max of 3 variables like this:
         largest = Math.max(x,y,z);  // Will not work!!!
    So, to find the largest of 3 values, you must use the Math.max method more than once.
     
  6. Write a program, Hw2Q6.java, that reads in three integers from the file in.txt and outputs the median and the mean values (separated by a single space) to the file out.txt.  Note:  the "mean" is just the average, and the "median" of an odd-sized set is the middle number.  Here's one way to find the median:  add the three numbers and then subtract the largest and the smallest values.  This program should work like this:
         Hw2Q6-in.txt contents:
              1 3 14
         Program output:
              <none>
         out.txt contents (after running program):
              3 6
    Of course, your program should work for any legal input!  Remember to exactly match the output!
     
  7. Repeat the previous problem, only with four numbers.  That is, write a program, Hw2Q7.java, that reads in four integers from the file in.txt and outputs the median and the mean values (separated by a single space) to the file out.txt.  Note:  the "mean" is just the average, and the "median" of an even-sized set is the average of the two middle numbers (and this average should be computed using integer division by 2, so no decimals are involved).  This program should work like this:
         in.txt contents:
              1 3 14 6
         Program output:
              <none>
         out.txt contents (after running program):
              4 6
    Of course, your program should work for any legal input!  Remember to exactly match the output!
     
  8. Write a program, Hw2Q8.java, that reads in one non-negative integer from the file in.txt and outputs the one's digit of that number to the file out.txt.
         in.txt contents:
              17
         Program output:
              <none>
         out.txt contents (after running program):
              7
    Of course, your program should work for any legal input!  Remember to exactly match the output!

    Hint:  You may wish to use the modulus operator here.
     
  9. Write a program, Hw2Q9.java, that reads in one non-negative integer in the range [1000,9999] -- that is, between 1000 and 9999 inclusive -- from the file in.txt and outputs each digit of that number, one per line, to out.txt.
         in.txt contents:
              1342
         Program output:
              <none>
         out.txt contents (after running program):
              1
              3
              4
              2

    Of course, your program should work for any legal input!  Remember to exactly match the output!

    Hint:  As with the previous problem, you may wish to use the modulus operator here, but you will also need to use integer division to extract each digit.  Also, as with any assigned program, unless explicitly stated otherwise, you do not have to worry about illegal inputs (in this case, do not worry if the number in "in.txt" is outside of the range [1000,9999]).
     

Carpe diem!