CS 161 Lab 1
Overview
This lab will familiarize you with the environment we will be using this semester. We will cover:
- Logging into CS Lab machines
- Basic text editing
- Submitting a programming assignment using Checkin
- Basic Eclipse usage
We will also cover basic Java programming from CS 160:
- Reading from files
- Creating and using methods
- for/while loops
- if-statements
- array manipulation
UNIX
Learning some of the basic UNIX commands for working in a UNIX/Linux environment can help you greatly. If you want to learn a lot about programming in the UNIX/Linux environment, you can take the course CS155 (1 credit). We will introduce you to some of the essential and most useful commands this semester.
Type the following commands at a terminal window. The TA will explain what each of them are as you go along.
- ls
- ls -l
- mkdir cs161Lab
- ls
- cd cs161Lab
- pwd
- cd ..
- pwd
Lab Assignment
Please complete this portion of the lab individually. Notify the instructor when you have finished to receive credit. The assignment must be completed in lab.
You will need the following file:
Lab1.java
Please implement the stub methods in Lab1.java.
Part 1 - Checking divisibility
For this part of the lab assignment, you must implement the following methods:
public boolean isDivisibleBy(int x, int y)
: returns true if x is divisible by y
public void divisorsInRange(int begin, int end)
: Populates the instance variable divisibleBySeven with all the numbers that are
divisible by seven in the range of the numbers begin,...,end-1
Part 2 - Extracting positive integers from an array
Implement the method
public int[] extractPositives(int[] values)
Given an array of integers, create and return a new array that contains only the positive elements.
The returned array should be exactly the right size. In other words, if values contains 10 elements, but only 5 elements are positive, the returned array should be of length 5.
For example, if the input array is:
[-1, 0, 3, -5, 6]
The resulting array should be:
[0, 3, 6]
Part 3 - Read input from a file
Create a data file called people.txt that contains a person's last name, a space, and their age. Data for the file is listed below:
Darby 92
Coolie 18
Jorge 25
Jansen 40
Geow 22
Get the file name as a commandline argument; read the data into two arrays, one of strings, and the other, an array of integers.
The output of your program should be composed of lines that have the format:
"Person: %name, Age: %age", where "%name" should be the person's name, and "%age" should be the person's age.
For example, the first line would read "Person: Darby, Age: 92". Then print out all of the last names separated by commas. For our example, the output would be:
"Darby, Coolie, Jorge, Jansen, Geow".
Here's a useful pattern for reading delimited text files:
File file = new File(fileName);
scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] tokens = line.split(delimiter);
//process the information
}
Grading
This lab is worth a total of 2 points broken down as:
Demo/Attendance: 1 point
Effort: 1 point