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

I already have the output to have the form of a table but I need the user input

ID: 3570128 • Letter: I

Question

I already have the output to have the form of a table but I need the user input along with the prompted questions to have text boxes for users to enter their info in. Also, the boxes cannot simply just use the JOptionPane.showInputDialog call and the JOptionPane.showMessageDialog call. It must contain some more advanced form of input and output such as a drop down menu, multiple text boxes on one form, a table, tabs, radio buttons, or other method of input/output.

the link to the two files that I have already completed are at        https://www.dropbox.com/s/8rgcetfdrzur69b/Lap%20Times%20Program.zip?dl=0

Explanation / Answer

Please wait, I will update complete program...

import java.text.DecimalFormat;
public class Splits
{
private DecimalFormat df = new DecimalFormat("00.00");
boolean dataOk = false;
String[] timesString;
double[] timesDouble;
double[] singleLaps;
String[] lapChange;
int count = 0;

public Splits(int numLaps)
{
  timesString = new String[numLaps];
  timesDouble = new double[numLaps];
  singleLaps = new double[numLaps];
  lapChange = new String[numLaps];
}

public void addTime(String time)
{
  timesString[count] = time;
  timesDouble[count] = stringToSec(timesString[count]);
  count++;
}

public void execute()
{

  singleLaps[0] = timesDouble[0];
  for(int i = 1; i < timesDouble.length; i++)
  {
   singleLaps[i] = timesDouble[i] - timesDouble[i - 1];
  }

  lapChange[0] = "---";
  for(int i = 1; i < timesDouble.length; i++)
  {
   if(singleLaps[i] >= singleLaps[i - 1])
   {
    lapChange[i] = "+"
      + df.format(singleLaps[i] - singleLaps[i - 1]);
   }
   else
   {
    lapChange[i] = "-"
      + df.format(singleLaps[i - 1] - singleLaps[i]);
   }
  }  
}

public Object[][] fillTable()
{
  Object[][] data = new Object[timesString.length][5];
  int j = 0;
  for(int i = 0; i < timesString.length; i++)
  {

   data[i][j] = i + 1;
   data[i][j + 1] = timesString[i];
   data[i][j + 2] = secToString(singleLaps[i]);
   data[i][j + 3] = lapChange[i];
   data[i][j + 4] = new Boolean(false);
  }
  return data;
}

public double stringToSec(String input)
{
  input = input.trim();
  int colon = input.indexOf(":");
  String temp = input.substring(0, colon);
  int minutes = Integer.parseInt(temp);
  temp = input.substring(colon + 1);
  double seconds = Double.parseDouble(temp);

  seconds = seconds + minutes * 60;

  return seconds;
}

public String secToString(double input)
{
  int minutes = (int) input / 60;
  double seconds = input - (60 * minutes);
  String s = minutes + ":" + df.format(seconds);
  return s;
}

}

-------------------------------------------------------------------------------

import java.text.DecimalFormat;
import java.util.Scanner;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
* TableDemo is just like SimpleTableDemo, except that it uses a custom
* TableModel.
*/

public class TableDemo extends JPanel implements ActionListener
{
private boolean DEBUG = false;
private int laps = 0;
JLabel l1 = new JLabel("Enter the number of laps: ");
JLabel l2 = new JLabel("");
JTextField jtf1 = new JTextField(6);
JTextField jtf2 = new JTextField(6);
JButton jbtn = new JButton("Find Table");
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();

public TableDemo()
{
  setLayout(new GridLayout(2, 1));

  p1.add(l1);
  p1.add(jtf1);
  p1.add(l2);
  p1.add(jtf2);
  p1.add(jbtn);
  
  add(p1);
  add(p2);
  
  jbtn.addActionListener(this);  
}

public class MyTableModel extends AbstractTableModel
{

  private String[] columnNames = {"Lap", "Total Time", "Lap Time",
    "Difference"};
  Object[][] data;

  public MyTableModel(Splits ib)
  {
   super();
   ib.execute();
   data = ib.fillTable();
  }

  public String[] getColumnNames()
  {
   return columnNames;
  }

  public void setColumnNames(String[] columnNames)
  {
   this.columnNames = columnNames;
  }

  public Object[][] getData()
  {
   return data;
  }

  public void setData(Object[][] data)
  {
   this.data = data;
  }

  public int getColumnCount()
  {
   return columnNames.length;
  }

  public int getRowCount()
  {
   return data.length;
  }

  public String getColumnName(int col)
  {
   return columnNames[col];
  }

  public Object getValueAt(int row, int col)
  {
   return data[row][col];
  }

  /*
   * JTable uses this method to determine the default renderer/ editor for
   * each cell. If we didn't implement this method, then the last column
   * would contain text ("true"/"false"), rather than a check box.
   */
  public Class getColumnClass(int c)
  {
   return getValueAt(0, c).getClass();
  }

  /*
   * Don't need to implement this method unless your table's editable.
   */
  public boolean isCellEditable(int row, int col)
  {
   // Note that the data/cell address is constant,
   // no matter where the cell appears onscreen.
   if(col < 2)
   {
    return false;
   }
   else
   {
    return true;
   }
  }

  /*
   * Don't need to implement this method unless your table's data can
   * change.
   */
  public void setValueAt(Object value, int row, int col)
  {
   if(DEBUG)
   {
    System.out.println("Setting value at " + row + "," + col
      + " to " + value + " (an instance of "
      + value.getClass() + ")");
   }

   data[row][col] = value;
   fireTableCellUpdated(row, col);

   if(DEBUG)
   {
    System.out.println("New value of data:");
    printDebugData();
   }
  }

  private void printDebugData()
  {
   int numRows = getRowCount();
   int numCols = getColumnCount();

   for(int i = 0; i < numRows; i++)
   {
    System.out.print(" row " + i + ":");
    for(int j = 0; j < numCols; j++)
    {
     System.out.print(" " + data[i][j]);
    }
    System.out.println();
   }
   System.out.println("--------------------------");
  }
}

/**
* Create the GUI and show it. For thread safety, this method should be
* invoked from the event-dispatching thread.
*/
private static void createAndShowGUI()
{
  // Create and set up the window.
  JFrame frame = new JFrame("TableDemo");
  
  TableDemo newContentPane = new TableDemo();
  newContentPane.setOpaque(true); // content panes must be opaque
  frame.setContentPane(newContentPane);

  // Display the window.
  frame.pack();
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args)
{
  createAndShowGUI();
}

public void actionPerformed(ActionEvent ae)
{
  String str = jtf1.getText();
  laps = Integer.parseInt(str);
  
  Splits ib = new Splits(laps);
  
  for(int i = 0; i < laps; i++)
  {
   l2.setText("Enter time for lap " + (i + 1) + ": ");   
   str = jtf2.getText();
   ib.addTime(str);
  }
  
  JTable table = new JTable(new MyTableModel(ib));
  table.setPreferredScrollableViewportSize(new Dimension(500, 70));
  table.setFillsViewportHeight(true);

  JScrollPane scrollPane = new JScrollPane(table);

  
  p2.add(scrollPane);
}
}