Skip to main content
ICT
Lesson A19 - Searches: Sequential & Binary
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

LAB ASSIGNMENT A19.1 page 7 of 9

Store

Background:

We will use these classes to load and manipulate data stored in a text file named file50.txt.

public class Item{
  private int myId;
  private int myInv;

  public Item(int id, int inv){
    myId = id;
    myInv = inv;
  }

  public int getId(){}

  public int getInv(){}

  public int compareTo(Item other){ }

  public boolean equals(Item other){ }

  public String toString(){}
}

public class Store{
  private ArrayList <Item> myStore = new ArrayList <Item>();

  public Store(String fName){ }

  public Store(){}
  private void loadFile(String inFileName){ }
  public void displayStore(){}
  public String toString(){}
  public void Sort(){} //to get recursive sort going
  private void merge(ArrayList <Item> a, int first, int mid, int last){}
  public void mergeSort(ArrayList <Item> a, int first, int last){ }
  private void swap(ArrayList <Item> list, int a, int b){}
}

The file, file50.txt, contains a list of id/inventory integer pairs - one pair per line. The idea behind the data type Item is to simulate an item in a store that is being tracked with a bar code id number (or value) for identification purposes. Each item in the store is tracked with both an id value and an inventory amount. Each id value in file50.txt is unique. So file50.txt looks like this:

3679       87
196        60
17914      12
18618      64
2370       65
... ...
etc. (for 45 more lines)

Assignment:

  1. Write a Store class capable of doing each of the following:

    - load the data file, file50.txt by default, as a collection of Item types
    - sort the data, in increasing order of id number
    - print the data, in order

  2. The printed output should add a blank line after every 10 items for better readability. Include a line number in the output. For example:

     
    Id
    Inv
     1
     184
    14
     2
     196
    60
     3
     206
    31
     4
     584
    85
     5
     768
    85
     6
    2370
    65
     7
    3433
     5
     8
    3679
    87
     9
    4329
    64
    10
    5529
    11
    11
    6265
    58
    12
    6835
    94
    13
    6992
    76
    14
    7282
    73
    15
    8303
    90
    16
    9267
    68
    etc.

  3. You will need to complete the getId(), getInv(), compareTo(), equals() and toString() methods for the Item class.

  4. You will need to complete the Store() constructor, displayStore(), loadFile(), mergeSort() and toString() methods for the Store class.

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.