Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Data Structures and Big-Oh Performance Estimation Create a class that can b

ID: 3662169 • Letter: J

Question

Java Data Structures and Big-Oh Performance Estimation
Create a class that can be used to test data structure - similar to the StudentA.java example found shown below:

StudentA.java

import java.util.Scanner;
import java.util.ArrayList;

class StudentA implements Comparable <StudentA> {
   static java.util.Random rn = new java.util.Random ();
   static ArrayList <String> firstNames = new ArrayList <>();
   static ArrayList <String> lastNames = new ArrayList <>();
   static SORTBY sortBy = SORTBY.LAST;
   static int nextUID = 1;
  
   String first, last;
   double gpa = 0;
   int credits = 0;
   int uid = 0;
  
   static {
       try {
           java.util.Scanner scFirst = new java.util.Scanner (new java.io.File("firstNames.txt"));
           java.util.Scanner scLast = new java.util.Scanner (new java.io.File("lastNames.txt"));
           while (scFirst.hasNext()) firstNames.add (scFirst.next());
           while ( scLast.hasNext()) lastNames.add ( scLast.next());
          
       }
       catch (java.io.FileNotFoundException e) {
           System.out.println (e);
       }
   }
  
   enum SORTBY {LAST, FIRST, CREDITS, GPA}
  
   public StudentA (String st) {this (new Scanner (st)); }
   public StudentA (Scanner sc) {
       uid = nextUID++;
       first = sc.next();
       last = sc.next();
       credits = sc.nextInt();
       gpa = sc.nextDouble();
   }
  
   public StudentA () {uid = nextUID++;} // no parameter constructor
   public int compareTo (StudentA x) {
       switch (sortBy) {
       case LAST : return last.compareTo (x.last);
       case FIRST : return first.compareTo (x.first);
       case CREDITS: return credits - x.credits;
       case GPA : return (gpa > x.gpa)? 1 : -1;
       } // end switch
       return 0;
   } // end compareTo for Comparable interface
  
   public String toString () {
       return String.format ("%5d %15s, %15s: %5d %10.2f", uid, last, first, credits, gpa);
   } // end method toString
   public static StudentA [] makeRandom (int m) {
       StudentA [] sa = new StudentA [m];
       for (int i = 0; i < sa.length; i++) {
           sa[i] = new StudentA ();
           sa[i].first = firstNames.get (rn.nextInt (firstNames.size()));
           sa[i].last = lastNames.get (rn.nextInt ( lastNames.size()));
           sa[i].credits = rn.nextInt (120);
           sa[i].gpa = rn.nextDouble () * 4.0;
       }
       return sa;
   }
  
   public static void main (String args []) {
       System.out.println (new StudentA ("james robinson 35 3.98"));
       StudentA [] x = makeRandom (10);
       for (StudentA m: x)
           System.out.println (m);
       java.util.Arrays.sort (x);
       System.out.println ("---- SORTED Last -----");
       for (StudentA m: x)
           System.out.println (m);
       System.out.println ("---- SORTED First -----");
       StudentA.sortBy = SORTBY.FIRST;
       java.util.Arrays.sort (x);
       for (StudentA m: x)
           System.out.println (m);
       System.out.println ("---- SORTED Credits -----");
       StudentA.sortBy = SORTBY.CREDITS;
       java.util.Arrays.sort (x);
       for (StudentA m: x)
           System.out.println (m);
       System.out.println ("---- SORTED GPA -----");
       StudentA.sortBy = SORTBY.GPA;
       java.util.Arrays.sort (x);
       for (StudentA m: x)
           System.out.println (m);
   }
} //End

You must make have a firstnames.txt and lastnames.txt files for this sample code to work.

The following are requirements for this homework:
Unique index integer fields
Use at least 5 other fields of various, including at least one of each of String, int and double types.
o assume that the String's have no spaces within them
Write a method that will create an array of N random instances of this class, where N is an integer parameter to this method
Provide a toString method that will format an instance of this class nicely
Provide appropriate constructors
Write methods that will write and read text files of this class
o to make this easy, assume that the first line of the file is the number of elements in the file
o make the file one line per entry
o use spaces as field delimiters in the files
o use the toString method of the class to write to the file
Provide a good GUI interface to control and test this class, at a minimum:
o use buttons to control the functions available
o use JFileChoose for selecting input and output files
o use a JTextArea and JScrollPane to create a scrolling text area for output
o the window should resize nicely - use BorderLayout with the text area in the center

Explanation / Answer

DSBOEstimation.java

// import the required packages
import java.util.*;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.*;
import java.awt.Color;
import java.awt.event.*;
// class for the datastructure big oh estimation
public class DSBOEstimation extends JFrame implements ActionListener
{
   // Declare the required GUI components
   private JTextField txtstuMajor;
   private JTextField txtstuGpa;
   private JTextField txtstuUIN;
   private JTextField txtstuNetID;
   private JTextField txtstuAge;
   private JTextField txtstuGender;
   private StuList sturstr = new StuList();
   private DefaultListModel displyLst = new DefaultListModel();
   private JList rstrLst = new JList(displyLst);
   private JLabel lblInx;

   // main method
   public static void main(String[] args)
   {
       new DSBOEstimation();
   }

   // constructor
   public DSBOEstimation()
   {
       // Add the GUI components into a panel
       this.setSize(800, 600);
       setTitle("Student List");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       getContentPane().setLayout(new BorderLayout(0, 0));
    
       JPanel mainPnl = new JPanel();
       mainPnl.setBorder(new TitledBorder(null, "Students", TitledBorder.LEADING, TitledBorder.TOP, null, null));
       getContentPane().add(mainPnl, BorderLayout.CENTER);
       mainPnl.setLayout(new BorderLayout(0, 0));
    
       JPanel rstrPnl = new JPanel();
       mainPnl.add(rstrPnl, BorderLayout.CENTER);
       rstrPnl.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));    

       rstrLst.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
       rstrPnl.add(rstrLst);
    
       JPanel hdrPnl = new JPanel();
       hdrPnl.setBackground(new Color(255, 255, 255));
       mainPnl.add(hdrPnl, BorderLayout.NORTH);
       hdrPnl.setLayout(new GridLayout(0, 8, 0, 0));
    
       JButton nmFld = new JButton("Name");
       nmFld.addActionListener(this);
    
       lblInx = new JLabel("Index");
       lblInx.setHorizontalAlignment(SwingConstants.CENTER);
       hdrPnl.add(lblInx);
       nmFld.setContentAreaFilled(false);
       nmFld.setDefaultCapable(false);
       nmFld.setToolTipText("Click to sort by name");
       nmFld.setBackground(new Color(255, 255, 255));
       nmFld.setBorder(new LineBorder(new Color(0, 0, 0)));
       hdrPnl.add(nmFld);
    
       txtstuMajor = new JTextField();
       txtstuMajor.setHorizontalAlignment(SwingConstants.CENTER);
       txtstuMajor.setBorder(new LineBorder(new Color(171, 173, 179)));
       txtstuMajor.setText("Major");
       hdrPnl.add(txtstuMajor);
       txtstuMajor.setColumns(10);
    
       txtstuGpa = new JTextField();
       txtstuGpa.setHorizontalAlignment(SwingConstants.CENTER);
       txtstuGpa.setText("GPA");
       hdrPnl.add(txtstuGpa);
       txtstuGpa.setColumns(10);
    
       txtstuUIN = new JTextField();
       txtstuUIN.setHorizontalAlignment(SwingConstants.CENTER);
       txtstuUIN.setText("UIN");
       hdrPnl.add(txtstuUIN);
       txtstuUIN.setColumns(10);
    
       txtstuNetID = new JTextField();
       txtstuNetID.setHorizontalAlignment(SwingConstants.CENTER);
       txtstuNetID.setText("NetID");
       hdrPnl.add(txtstuNetID);
       txtstuNetID.setColumns(10);
    
       txtstuAge = new JTextField();
       txtstuAge.setHorizontalAlignment(SwingConstants.CENTER);
       txtstuAge.setText("Age");
       hdrPnl.add(txtstuAge);
       txtstuAge.setColumns(10);
    
       txtstuGender = new JTextField();
       txtstuGender.setText("Gender");
       txtstuGender.setHorizontalAlignment(SwingConstants.CENTER);
       hdrPnl.add(txtstuGender);
       txtstuGender.setColumns(10);
    
       JButton addStu = new JButton("+");
       addStu.addActionListener(this);
    
       getContentPane().add(addStu, BorderLayout.WEST);
    
       JButton removeStu = new JButton("-");
       removeStu.addActionListener(this);
       getContentPane().add(removeStu, BorderLayout.EAST);
       this.setVisible(true);
    
       lstUpdt();
   }

   // method to perform the updation
   private void lstUpdt()
   {
       displyLst.clear();
       for (Stnt stu : sturstr) {
           String[] prsLst = stu.toString().split("[|]");
           displyLst.addElement(String.format("%-30s%5s, %5s%-25s%-25s%-25s%-25s%-25s%-25s", prsLst[0], prsLst[2], prsLst[1], prsLst[3], prsLst[4], prsLst[5], prsLst[6], prsLst[7], prsLst[8]));
   }
}
   // method to sort the roster
   private static void srtRstr(StuList sturstr)
   {
       Collections.sort(sturstr, new Comparator<Stnt>()
       {
            public int compare(Stnt s1, Stnt s2)
           {
               return s1.getstuLast().compareToIgnoreCase(s2.getstuLast());
            }
        });
   }
    // method overriding to manage the action event
   @Override
   public void actionPerformed(ActionEvent e) {
       switch (e.getActionCommand()) {
           case "+":       new EntryDialog(sturstr);
                           break;
           case "-":       sturstr.removeStu(Integer.parseInt(rstrLst.getSelectedValue().toString().substring(0, 29).trim()));
                           break;
           case "Name":   srtRstr(sturstr);
                           break;
       }
       lstUpdt();
   }
}


//Stnt.java public
class Stnt {
// declare the required variables
   private String firstNm, lastNm, stumajor, stuNetID, stugender;
   private Double stuGPA;
   private int stuUIN, stuAge, stuID;
   private static int stunextID = 0;
   // coonsytructor of the class
   public Stnt(String firstNm, String lastNm, String stumajor, Double stuGPA, int stuUIN, String stuNetID, int stuAge, String stugender) {
       this.firstNm = firstNm;
       this.lastNm = lastNm;
       this.stumajor = stumajor;
       this.stuNetID = stuNetID;
       this.stuGPA = stuGPA;
       this.stuUIN = stuUIN;
       this.stuAge = stuAge;
       this.stugender = stugender;
       this.stuID = stunextID++;
   }
    // method to get the student id
   public int getstuID()
   {
       return stuID;
   }

   // method to get the first name
   public String getstuFirst() {
       return firstNm;
   }
// method to set the first name
   public void setstuFirst(String firstNm) {
       this.firstNm = firstNm;
   }
   // method to get the last name
   public String getstuLast() {
       return lastNm;
   }
   // method to set the last name
   public void setstuLast(String lastNm) {
       this.lastNm = lastNm;
   }
  
   // method to get the major
   public String getstuMajor() {
       return stumajor;
   }
  
   // method to set the major
   public void setstuMajor(String stumajor) {
       this.stumajor = stumajor;
   }
  
   // method to get student id
   public String getstuNetID() {
       return stuNetID;
   }
  
   // method to set student id
   public void setstuNetID(String netID) {
       stuNetID = netID;
   }
   // method to get student gender
   public String getstuGender() {
       return stugender;
   }

// method to set student gender
   public void setstuGender(String stugender) {
       this.stugender = stugender;
   }

// method to get student CGPA
   public Double getstuGPA() {
       return stuGPA;
   }
    // method to set student CGPA
   public void setstuGPA(Double gPA) {
       stuGPA = gPA;
   }
    // method to get student UIN
     public int getstuUIN() {
       return stuUIN;
   }
    // method to set student UIN
   public void setstuUIN(int uIN) {
       stuUIN = uIN;
   }
  
   // method to get student age
   public int getstuAge() {
       return stuAge;
   }
    // method to set student age
   public void setstuAge(int stuAge) {
       this.stuAge = stuAge;
   }
  
   // method to print values
   public String toString() {
       return stuID + "|" + lastNm + "|" + firstNm + "|" + stumajor + "|" + stuGPA + "|" + stuUIN + "|" + stuNetID + "|" + stuAge + "|" + stugender;
       }

}

StuList.java
//import required packages
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

// class stuList
public class StuList extends ArrayList<Stnt> {
    // method to remove student
   public Boolean removeStu(int stuID) {
       ListIterator<Stnt> itra = this.listIterator();
       Boolean found = false;
       while(itra.hasNext()){
            if(itra.next().getstuID() == stuID){
                itra.remove();
                found = true;
            }
       }      
       return found;
   }
    // method to remove student
   public Boolean removeStu(Stnt student) {
       ListIterator<Stnt> itra = this.listIterator();
       Boolean found = false;
       while(itra.hasNext()){
            if(itra.next().getstuID() == student.getstuID()){
                itra.remove();
                found = true;
            }
       }      
       return found;
   }
}


EntryDialog.java
// import the required packages
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// class for entry dialog
public class EntryDialog extends JDialog implements ActionListener {
   private JTextField txtFirstNm;
   private JTextField txtLastNm;
   private JTextField txtstuMajor;
   private JTextField txtstuGPA;
   private JTextField txtstuUIN;
   private JTextField txtstuNetID;
   private JTextField txtstuAge;
   private JTextField txtstuGender;
   private StuList sturstr;

// constructor of a class
   public EntryDialog(StuList sturstr) {
       this.sturstr = sturstr;
       setTitle("New Entry");
       setAlwaysOnTop(true);
       setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
       setResizable(false);
       setModalityType(ModalityType.APPLICATION_MODAL);
    
       JPanel mainPnl = new JPanel();
       getContentPane().add(mainPnl, BorderLayout.NORTH);
       mainPnl.setLayout(new BorderLayout(0, 0));
    
       JPanel entryPanel = new JPanel();
       mainPnl.add(entryPanel, BorderLayout.CENTER);
       entryPanel.setLayout(new GridLayout(0, 2, 0, 0));
    
       JLabel lblNewLabel = new JLabel("First Name");
       entryPanel.add(lblNewLabel);
    
       txtFirstNm = new JTextField();
       entryPanel.add(txtFirstNm);
       txtFirstNm.setColumns(10);
    
       JLabel lblLastName = new JLabel("Last Name");
       entryPanel.add(lblLastName);
    
       txtLastNm = new JTextField();
       txtLastNm.setColumns(10);
       entryPanel.add(txtLastNm);
    
       JLabel lblMajor = new JLabel("Major");
       entryPanel.add(lblMajor);
    
       txtstuMajor = new JTextField();
       txtstuMajor.setColumns(10);
       entryPanel.add(txtstuMajor);
    
       JLabel lblGpa = new JLabel("GPA");
       entryPanel.add(lblGpa);
    
       txtstuGPA = new JTextField();
       txtstuGPA.setColumns(10);
       entryPanel.add(txtstuGPA);
    
       JLabel lblUin = new JLabel("UIN");
       entryPanel.add(lblUin);
    
       txtstuUIN = new JTextField();
       txtstuUIN.setColumns(10);
       entryPanel.add(txtstuUIN);
    
       JLabel lblNetId = new JLabel("Net stuID");
       entryPanel.add(lblNetId);
    
       txtstuNetID = new JTextField();
       txtstuNetID.setColumns(10);
       entryPanel.add(txtstuNetID);
    
       JLabel lblAge = new JLabel("Age");
       entryPanel.add(lblAge);
    
       txtstuAge = new JTextField();
       txtstuAge.setColumns(10);
       entryPanel.add(txtstuAge);
    
       JLabel lblNewLabel_1 = new JLabel("Gender");
       entryPanel.add(lblNewLabel_1);
    
       txtstuGender = new JTextField();
       txtstuGender.setColumns(10);
       entryPanel.add(txtstuGender);
    
       JPanel buttonPanel = new JPanel();
       mainPnl.add(buttonPanel, BorderLayout.SOUTH);
    
       JButton okButton = new JButton("OK");
       okButton.addActionListener(this);
       buttonPanel.add(okButton);
    
       JButton cancelButton = new JButton("Cancel");
       cancelButton.addActionListener(this);
       buttonPanel.add(cancelButton);
       this.setSize(400,300);
       this.setVisible(true);
   }

   // method to handle the events
   @Override
   public void actionPerformed(ActionEvent e) {
       switch (e.getActionCommand()) {
       case "Cancel":       this.dispose();
                           break;
       case "OK":           sturstr.add(createStudent());
                           this.dispose();
                           break;
       }
   }
    // method to create student dialog
   private Stnt createStudent() {
       return new Stnt(txtLastNm.getText(),txtFirstNm.getText(), txtstuMajor.getText(), Double.parseDouble(txtstuGPA.getText()), Integer.parseInt(txtstuUIN.getText()), txtstuNetID.getText(), Integer.parseInt(txtstuAge.getText()), txtstuGender.getText());
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote