20 Oct - Overview - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

20 Oct - Overview

Description:

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 ... – PowerPoint PPT presentation

Number of Views:82
Avg rating:3.0/5.0
Slides: 9
Provided by: Nick234
Category:
Tags: class | java | oct | overview | scanner

less

Transcript and Presenter's Notes

Title: 20 Oct - Overview


1
20 Oct - Overview
  • Homework 1
  • Group-Id rule
  • Notes on Java text file input/output
  • Scanner class
  • Printf (like C)

2
Homework 1
  • http//pmik.petrsu.ru/pub/ourusoff/Intro to
    JSE/Home Work/
  • due noon, Friday, 21 Oct
  • email solution as (zipped) attachment to
  • nourusoff_at_psu.karelia.ru
  • 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

3
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
  • System diagram
  • Data structures for
  • 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

4
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

5
Homework 2 (due 28 October)
  • The Census household problem is modified as
    follows
  • (The course page has a sample file,
    censusFile.txt in the HW2 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.

6
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.

7
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 thispublic static void main(String
    args) throws IOException
  • IOException must also be imported. This can
    either be done as a separate import
    statementimport java.io.IOExceptionor 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.

8
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
Write a Comment
User Comments (0)
About PowerShow.com