We will use these classes to load and manipulate data stored in a text file named file50.txt. For the purpose of turning in this lab you should put all three classes into StoreDriver.java - named with your name and period. Ex: P3_Wang_Michael_StoreDriver.java
public class StoreDriver {
public static void main(String[] args) {
Store s = new Store("file50.txt");
s.sort();
s.displayStore();
}
}
class Item implements Comparable<Item> {
private int myId;
private int myInv;
/**
* Constructor for the Item object
*
* @param id id value
* @param inv inventory value
*/
public Item(int id, int inv){
myId = id;
myInv = inv;
}
/**
* Gets the id attribute of the Item object
*
* @return The id value
*/
public int getId(){
// Your code here
}
/**
* Gets the inv attribute of the Item object
*
* @return The inv value
*/
public int getInv(){
// Your code here
}
/**
* Compares this item to another item based on id number. Returns the
* difference between this item's id and the other item's id. A
* difference of zero means the items' ids are equal in value.
*
* @param other Item object to compare to
* @return positive int if myId > other.myId
* 0 if myId == other.myId
* negative int if myId < other.myId
*/
public int compareTo(Item other){
// Your code here
}
/**
* Compares the Item to the specified object
*
* @param otherObject Item object to compare to
* @return true if equal, false otherwise
*/
public boolean equals(Item other){
// Your code here
}
/**
* Overrides the default toString() of Object.
* Returns a String representation of this object. It's up to you
* exactly what this looks like.
*/
public String toString(){
// Your code here
}
}
class Store {
private ArrayList <Item> myStore = new ArrayList <Item>();
/**
* Creates a Store object from data stored in the given file name
*
* @param fName name of the file containing id/inv pairs of data
*/
public Store(String fName){
loadFile(fName);
}
/**
* Reads a file containing id/inv data pairs one pair per line.
*
* @param inFileName name of file containing id/inv pairs of data
*/
private void loadFile(String inFileName){
// Your code here
}
/**
* Prints the store contents in the format shown below
* Line # Id Inv
* 1 184 14
* 2 196 60
*/
public void displayStore(){
// Your code here
}
/**
* Sorts the store ArrayList using recursive mergesort
*/
public void sort(){
// Make a single call to mergeSort to get sorting going
// (If your mergeSort is broken, then use a quadratic sort)
}
private void merge(ArrayList <Item> a, int first, int mid, int last){
// Your code here
}
/**
* Recursive mergesort of an ArrayList of Items
*
* @param a reference to an ArrayList of Items to be sorted
* @param first starting index of range of values to be sorted
* @param last ending index of range of values to be sorted
*/
public void mergeSort(ArrayList <Item> a, int first, int last){
// Your code here
}
}
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)
Complete the Store
class capable of doing each of the following:
Item
types id
number Put all of your classes into one file. Refer to Lab 5.2 Illusions if needed.
The printed output should add a blank line after every 10 items for better readability. Include a line number in the output. For example:
Line # 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
17 9351 8
18 10629 19
19 11485 39
20 11549 88
21 11584 32
22 12024 5
23 12328 63
24 12705 14
25 12917 63
26 13066 8
27 13520 38
28 14088 92
29 14092 48
30 14151 82
31 14896 81
32 15006 53
33 15141 87
34 15320 82
35 15559 33
36 15814 60
37 15917 51
38 17753 70
39 17911 96
40 17914 12
41 18061 3
42 18181 60
43 18410 56
44 18465 27
45 18524 34
46 18618 64
47 18871 69
48 18995 56
49 19623 88
50 19967 45
You will need to complete the getId()
, getInv()
, compareTo()
, equals()
and toString()
methods for the Item
class.
You will need to complete the Store()
constructor, displayStore()
, loadFile()
, mergeSort()
and toString()
methods for the Store
class.
You must Sign In to submit to this assignment
Last modified: January 31, 2024
Back to Recursive Vs....