Project 4 Creating a data file:


In jGrasp, you can create a jar file for the source code files in your project.

Start by creating a new project, in the directory of your files is a good place.

Add the source code files to the project which is part of the new project dialog.

Finally, use the

menu and dialog.
In the dialog, you can add any files you wish -


1 // File: CreateDataFile.java
2 // Date: Mar 14, 2016
3 // Author: Nicholas Duchon
4 // Purpose:
5 //   create data files for CMIS 242 Project 4
6 //   entries randomly generated
7 //     id, TNC and TQP using random number generator
8 //     name and major from data files:
9 //       FirstNames.txt
10 //      LastNames.txt
11 //      majors.txt
12 // Input - using JOptionPane for input
13 //   int: number of elements/lines/students
14 //   int: max id
15 //   String: file name used for output
16 // Output format:
17 //   <id> <TNC> <TQP> <name> <major> <gpa>
18 //     id = int, identifier
19 //     TNC = int, total number of credits [0, 120)
20 //     TQP = int, total number of quality points [0, 4*TNC)
21 //     name = String, <last>,<first>, no spaces
22 //     major = String, no spaces
23 //     gpa = double, extra information, not required in Project 4
24 // Note:
25 //   uses HashMap<Integer> to make sure ID's are unique
26
27
// list of majors from:
28 // http://www.nomajordrama.co.nz/Majors
29
30
import java.util.Scanner;
31 import java.util.HashSet;
32 import java.util.ArrayList;
33 import java.util.Random;
34 import java.io.PrintStream;
35 import java.io.File;
36 import java.io.FileNotFoundException;
37 import javax.swing.JOptionPane;
38
39
public class CreateDataFileP4 {
40   static Random rn = new Random ();
41   static ArrayList <String> namesF = new ArrayList <> ();
42   static ArrayList <String> namesL = new ArrayList <> ();
43   static ArrayList <String> majors = new ArrayList <> ();
44
45  
static ArrayList <String> getListID (int num, int maxR) {
46     HashSet   <Integer> hs  = new HashSet <> ();
47     ArrayList <String>  ret = new ArrayList <> ();
48     for (int i = 0; i < num; i++) {
49       if (!(hs.add (rn.nextInt (maxR)))) i--;
50     } // end getting unique id's
51     int digit = (int)(Math.log10(maxR));
52     for (Integer x: hs)
53       ret.add (String.format ("%0" + digit + "d ", x));
54     return ret;
55   } // end method getListID
56
57   static void getNames () {
58     try {
59 // following is for access to jar files from a static method
60 // in instance method, replace mc with getClass()
61 // Class<CreateDataFileP4> mc = CreateDataFileP4.class;
62       Class <?> mc = CreateDataFileP4.class;
63       Scanner sF = new Scanner(mc.getResourceAsStream("FirstNames.txt"));
64       Scanner sL = new Scanner(mc.getResourceAsStream("LastNames.txt"));
65       Scanner sM = new Scanner(mc.getResourceAsStream("majors.txt"));
66
67 // Following can be used for local files
68 // Scanner sF = new Scanner (new File ("FirstNames.txt"));
69 // Scanner sL = new Scanner (new File ("LastNames.txt" ));
70 // Scanner sM = new Scanner (new File ("majors.txt" ));
71
72      
while (sF.hasNext()) namesF.add (sF.next());
73       while (sL.hasNext()) namesL.add (sL.next());
74       while (sM.hasNext()) majors.add (sM.next());
75     } catch (Exception e) {
76       System.out.println ("Data file missing: FirstNames.txt, LastNames.txt or majors.txt");
77     } // end try/catch block to read data files
78   } // end method getNames
79
80  
public static void main (String args []) {
81 // Line from GUI dialog:
82     String inLine = JOptionPane.showInputDialog (
83       "Enter number of elements, max id, and file name: " +
84       "\n <int> <int> <String>"
85     ); // end input dialog
86     Scanner st = new Scanner (inLine);
87 // ELSE input line from console:
88 // Scanner st = new Scanner (System.in);
89     try {
90       int num = st.nextInt();
91       int maxR = st.nextInt();
92       String name = st.next();
93
94
// PrintStream po = System.out;
95 // po.print ("Enter number of elements, max id, and file name: ");
96 // int num = st.nextInt();
97 // int maxR = st.nextInt();
98 // po.printf ("You entered >%d<, >%d< and >%s<\n", num, maxR, name);
99 // po.println ("Elements:");
100
101
      ArrayList <String> ids = getListID (num, maxR);
102       getNames ();
103
104
      PrintStream ps = new PrintStream (new File (name));
105       for (int i = 0; i < ids.size(); i++) {
106         String id = ids.get (i);
107         String nL = namesL.get(rn.nextInt(namesL.size()));
108         String nF = namesF.get(rn.nextInt(namesF.size()));
109         String nM = majors.get(rn.nextInt(majors.size()));
110         int   tnc = rn.nextInt (120);
111         int   tqp = (int)(tnc * (rn.nextDouble() * 3.5 + 0.5));
112
113         String line = String.format (
114           "%s %3d %3d %15s,%-15s %-40s %6.3f",
115           id, tnc, tqp, nL, nF, nM, 1.*tqp/tnc
116         ); // end formatted string
117 // po.println (line);
118         ps.println (line);
119       } // end for as many students as requested
120       ps.close ();
121       JOptionPane.showMessageDialog (null, "Success, Output file: " + name);
122     } catch (Exception e) {
123       JOptionPane.showMessageDialog (null, e);
124 // po.println ("Problem with output file");
125     } // end print try/catch block
126   } // end main
127 } // end class CreateDataFileP4