Lab Exercise for Assignment 4
COMP112 2020 Tri 1: Lab Exercise 4
Make sure you have BlueJ version 4.2.2 and Java version 11! This assignment won't work with the old BlueJ and Java 8
This assignment will give you experience in writing programs that get input from files. It will also give you more practice using conditionals and loops.
Resources and links
Download the zip file for lab exercise 4 and extract it to the
folder in your home folder.
It should contain templates for the exercises program.
Look at the model answers to assignment 3, and make sure you understand all the
components of the programs. Also, go over the code examples from the lectures
that used files.
- Files data exercise:
Series of programs to read data from files and print/summarise the results.
Exercise Series: Reading files for Data
The series of exercises is in the
class. It contains
five methods that each involve reading some data from a file and doing something
with the data. (The data files are shown at the end of this page)
printLongestWord should read all the words (tokens) in a file called "text.txt", and print out the longest word in the file.
You will need a Scanner on each line to extract all the tokens.
length() method to find the length of a token.
The longest word should be "differentiated;"
sumNumbers should add up all the numbers in a file of numbers called "numbers.txt", and print the total at the end. There may be any number of numbers on each line.
You will need a Scanner on each line to get the number out of the line.
The total should be 2174.
printFirstWords should read a file called "text.txt" and prints out just the first word on each line.
It will need to use a Scanner on each line in order to extract the first word of the line.
countLargeCourses should read data from a file called "course-counts.txt" that has a coursecode and a course size (the number of students) on each line. The method should count (and print out) how many of the courses have more than 80 students. (The answer should be 13.)
It will need to use a Scanner on each line in order to extract the coursecode and the course size.
computeTotalOrder should read data from a file called "order.txt" that has an item, a count and a unit-price on each line. The method should calculate the cost of each item (count * unit-price) and print the total cost of the order. (The total should be $167.99.)
It will need to use a Scanner on each line in order to extract the count and the unit-price.
These are words you should not be typing out. They
are all the words that appear in the file.
the program is supposed to print nothing but the
first word of each line, not all the
words in the file. After reading the first word
on a line, your program should throw away the rest of the line.
each exercise is slightly differentiated; this one reads by
line as well as by word. You should read the first word
of a line with the next() method on a line scanner. After
this you don't have to read from the rest of the scanner. Writing to a
file isn't done in any of the exercises.
37 36 55 40
1 54 45 67 39 43
64 62 24 45 38
64 63 65 35 96 7
70 39 38 90 21 22 96 94 47 27
52 33 12 10 61 24
57 63 21 49 98 1 8 46 5
Course code and number of students in course
item, count, unit price
bananas 100 0.30
20kgflour 3 16.34
milk 7 2.32
butter 3 4.20
yeast 1 7.89
salt 1 1.43
sugar 2 3.91
raisins 5 7.23
bran 3 2.28