Group-ID Rule - Petrozavodsk State University

Download Report

Transcript Group-ID Rule - Petrozavodsk State University

20 Oct - Overview
• Homework #1
• Group-Id rule
• Notes on Java text file input/output
– Scanner class
– Printf (like C)
Homework #1
http://pmik.petrsu.ru/pub/ourusoff/Intro to JSE/Home Work/
due noon, Friday, 21 Oct
email solution as (zipped) attachment to:
[email protected]
Please send me:
• 4 .jsp files that show the design of the original output
table and each of the 3 modifications
• Extra credit: 3 .jsp files that show the modification of
the bad multiplication table to produce the 3
modifications
Group-ID Rule
We frequently wish to process groups in a file sorted by
group.
Problem: Census file is sorted on a household number. Data for each
household is to be accumulated.
Detailed problem statement
1.
2.
System diagram
Data structures for:
–
–
3.
4.
5.
6.
7.
input - censusFile.jsp
output - censusReport.jsp
Composite data model showing correspondences
Basic Program Structure - censusBasicStr.jsp
List and allocate operations
Produce elaborated program structure - censusElabStr.jsp
Generate structure text or code
•
•
Structure text
Java code
Group ID Rule
• Group-id Rule: “If a structure contains a component that
is a group of records all having the same value of a
(usually sorted) identifier, then there must be an operation
that stores the value of the identifier. This operation
should be allocated once per group, at the beginning of the
group.”
• Design focuses on:
– a static not a dynamic view of the problem;
– data structures (objects), not on dynamic flow of control
Homework #2 (due 28 October)
The Census household problem is modified as follows:
(The course page has a sample file, censusFile.txt in the HW#2
folder. Test your program with this file.)
•
•
•
The census text file has a city code in positions
8-10. Accumulate the total number of males and
females as well as the average household size in
each city and produce the summary as before.
Put sensible column headings in the report:
(Household Id, # Males, # Females, etc.)
Use Scanner and printf for reading and writing.
Using the Scanner utility class
• Scanner is in the util package, not the lang package so you need to include the
line
import.util.Scanner;
at the top of the class.
• To create a Scanner object and connect it to a file you must create a File object
based on the name of that file. This can be done with one line;
Scanner s = new Scanner( new File(“census.txt"));
Remember all programs are judged assuming the file is in the same directory as
the compiled class file so do not put any path information on the file, just the
file name. To use the file class you must include the line
import.io.File;
at the top of your program.
Using the Scanner utility class
(cont.)
•
Unfortunately instantiating a File object can result in in an IOException and this is a
checked exception so you either have to use a try-catch block or declare that the method
that contains the code that generates the checked exception throws an IOException.
–
–
In actual programming practice it would be much better to include a try-catch block, but for
educational purposes and programming contest purposes it is much easier to simply declare that
main throws an IOException.
Thus the declaration of the main method looks like this:
public static void main(String[] args) throws IOException
•
IOException must also be imported. This can either be done as a separate import
statement:
import java.io.IOException;
or since the program now needs multiple classes from the io package we can use the
wildcard import. Again, not the best programming practice, but for Olympiads, not a bad
idea.
import java.io.*;
Java I/O with Scanner and Formatter
Java documentation
Tutorial on printf
• Printf.demo.java
Tutorial on Using Scanner for Input from Console
• ScanConsoleApp.java