CS 112 Lab 01: Java Programming with Eclipse

July 5th, 2017

Problem Zero

For this problem, you must set up your computer with the latest version of Java (1.8) and of Eclipse and configure Eclipse to use the coding style guidelines from Google.

(A) See this link for instructions on how to install the latest version of Java on your machine: PDF

(B) Now go here and download Eclipse for your machine; this should be straightforward if Java has been updated (alternately, go to the link in Problem One and follow the instructions in Topics 3 & 4).

(C) See this link for instructions on how to configure Eclipse to use the Google Coding Style Guidelines: PDF

 

Problem One

Go to this tutorial on Eclipse and read through Topics 5 -- 8. Create the program MyFirstClass.java as instructed. Nothing else to do (you won't have to hand in the program just created).

Problem Two

Go to Homework One on the class web page and read through Appendix One at the end of the page. Follow these instructions to set up your homework one package and create templates for all of the programs you will write in this lab and for homework one. Nothing else to do for this problem!

 

Problem Three

Write a program called Haiku.java which prints out the following haiku:

	Arms folded
	To the moon
	Among the cows.

		-Jack Kerouac (1968)

(there is a tab before each line of the poem, a blank line after the poem, and two tabs before the author's name).

Your program must have the following features:

  1. You may use only three System.out.println(....) statements (so you will need to do two of the lines using a newline character inside a String).
  2. You must start by assigning the author's name to a String variable author and the year to an int variable year, and use these inside the System.out.println(...) statement to print out the last line.
  3. Your program must have an appropriate "header comment" at the top of the file giving at least the name of the file, the date, your name, and the purpose of the program.

You will hand in your program Haiku.java as Problem B.2 in Homework One.

Problem Four

Complete the following template so that the method returns the square root (rounded down to the nearest integer) of its input. Do not use any Java methods that help you compute the square root. Instead, simply use binary search. That is, try a value of the square root, see if it's too big or too small by squaring it, and then decide on the next value to try in the same way as the binary search from class decides on the next place to look.

Here is an example of what the algorithm would do:

Find the square root of 12: you know it is between 1 and 12, so use these
as the bounds in binary search:

    Try  (1+12)/2 = 6  The result is actually 6.5 but rounded down by integer division
        6*6 = 36 
    Too large, must be between 1 and 6    (notice that we don't use 1 and 5)
    Try  (1+6)/2 = 3
        3*3 = 9
    Too small, result must be between 3 and 6      (not 4 and 6)
    Try (3+6)/2 = 4
        4*4 = 16
    Too large, result must be between 3 and 4:
    Since you have two integers next to each other, the result must be 3.
             
    

Note that we are adjusting the bounds each time to include the midpoint (unlike binary search, which excludes the midpoint). You may test for the end of the process by determining when the lower bound and the upper bound are the same, or differ by exactly 1.

Caution: make sure your search ends up rounding down in case the input is not a perfect square, and gives the exact correct answer in case the input is a perfect square.

You may use recursion or iteration to solve this problem.

/*  Your header comment here with name and description of the program
 */ 

public class SquareRoot {


 /*  squareRoot returns the floor of the square root of n
  *  (i.e., rounded down to nearest integer).
  */

  public static int squareRoot(int n) {
		
       // Your code here
		
  }

  public static void main(String[] args) {

      System.out.println( squareRoot(25) );
      System.out.println( squareRoot(256) );
      System.out.println( squareRoot(10) );     // This should print the integer part of the sqrt root, i.e., 3. 

  }
} 
      
      
      

You will hand this in as Problem B.3 in Homework One.