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.


Download the zip file for lab exercise 4 and extract it to the COMP112-2020T1-Lab4 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:
    right 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 FilesDataExercise 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.
    Use the 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.

Data Files


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.


28 19
37 36 55 40
1 54 45 67 39 43
64 62 24 45 38
57 6
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
ACCY111	200
ARCH251	34
ARTH112	180 
BBSC251	52
BIOL227	72 
BIOL329	61 
BMSC117	90 
CHEM425	6  
CLAS310	24 
COML303	52 
COMP312	6  
COMP442	4  
CRIM318	62 
ECON140	372 
ECON405	10 
ECON413	6  
ENGL402	8  
ENVI214	143 
ESCI303	23 
FHSS111	674 
FREN403	7  
GEOG314	51 
GPHS441	1  
IBUS212	72 
INTP113	118 
INTP417	4  
ITAL306	12 
LATI331	10 
LAWS121	552 
LAWS303	84 
LAWS334	43 
LING221	43 
MAOR216	48 
MARK203	53 
MARK313	46 
MATH433	3  
MBUS401	4  
MGMT101	718 
MMAF522	17 
MMBA553	19 
MOFI202	121 
MOFI409	9  
NZSM436	3  
PASI202	20 
PHIL225	24 
PHIL325	17 
PHYS131	100 
PHYS215	39 
PHYS414	6  
POLS112	252 
POLS417	15 
PSYC338	68 
PUBL205	23 
QUAN111	15 
SAMO102	11 
SPAN316	22 
STAT193	52 
STAT338	55 
TECH423	2  


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