ICS 211 Homework 1

Java Review

0. Goals and Overview

The goals of this assignment are to refresh the Java programming material learned in ICS 111.

This assignment requires you to create three classes that work together.

This assignment also gets you started in using the kind of development environment we will use for the rest of the class -- in particular, the project and the package edu.ics211.h01.

Part of your grade for this assignment will be based on your testing for parameter validity and your choices for appropriate action to take in case of invalid parameters or missing files, as well as your comments and your choice of method and class names and static vs. non-static method. It is to your advantage to create code that is as polished as you know how to create.

The use of Eclipse is highly encouraged. All instructions assume that you are using Eclipse. You may use other IDEs, but if you do, you have to figure out how the instructions for Eclipse apply to you.

Eclipse

Set up your Eclipse by creating a project named with your UH username. For example, the instructor would create a project named "esb". All your ICS 211 assignments will be done as part of this project.

Within the project that you just created, create a new package called edu.ics211.h01. All assignments in ICS 211 this semester will have a similar name (edu.ics211.h02, edu.ics211.h03, etc). The package edu.ics211.h01 is where you will put all the files for homework 1.

1. Square Roots (40%)

Create a class Roots with a method roots that, given an integer (int in Java), returns an array of double-precision floating point numbers (known in Java as double), each of which is a square root of a number between 2 and the parameter to the method. For example, if the parameter is 5, the method should return an array of 4 doubles, with approximate values 1.4142135623, 1.7320508075, 2.0, and 2.2360679774. Your method should check the validity of the parameter, and take appropriate action if the parameter is invalid.

2. Reading Files (40%)

Create a class Reader with a method numChars that, given a string representing a file name, returns an integer with the number of characters in the file. For example, if the file has 10 characters, your method must return the number 10.

3. Main Class (20%)

Create a class HW1.java with a main method that does both of the following:

  1. calls the roots method from part 1 with a random integer between 0 and 10, and prints each of the numbers in the result. The numbers should all be printed on the same line, separated by commas, and with a newline at the end.
  2. calls the numChars method from part 2 with each file name specified in the command-line arguments to the main method (see here if you don't remember how to use command-line arguments in Eclipse). For each file, the file name and the number of characters should be printed, one on each line, as in this example:
    hello.txt 77
    world.txt 199
    

    If a command-line argument names a file that does not exist, your code must print the file name followed by "not found", as in

    f1.txt not found
    f2.txt 88
    f3.txt not found
    f4.txt 10943201
    

Turning in the Assignment

Once you are done, export your code and use Laulima to turn in your files to the TA. Once you log into Laulima and select the ICS 211 site, on the left-hand side will be an assignments tab.