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

Put the following code\'s output so that it can fit in with the next set of code

ID: 3569699 • Letter: P

Question

Put the following code's output so that it can fit in with the next set of code which creates a sample table as an output (basically the output of the first set of code should be in table form using the next set of code as a base). Or if it's easier I can give you a different code that has more return statements for the first set of code.



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

public class Splits
{
private static DecimalFormat df = new DecimalFormat("00.00");

{
Scanner keyboard = new Scanner(System.in);
boolean dataOk=false;
int numLaps = 0;

System.out.println("Enter the number or laps.");


numLaps=keyboard.nextInt();
keyboard.nextLine();


String[] timesString = new String[numLaps];
double[] timesDouble = new double[numLaps];
for(int i=0;i<timesString.length;i++)
{
System.out.println("Enter time for lap "+(i+1));
timesString[i]=keyboard.

nextLine();
timesDouble[i]=stringToSec(timesString[i]);
}

double[] singleLaps = new double[numLaps];


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


String[] lapChange = new String[numLaps];
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]);
}
}

System.out.println("Lap Total Time Lap Time Difference");



for(int i=0;i<timesString.length;i++)
{
System.out.println("Lap "+(i+1) + " " +timesString[i] +
" " +secToString(singleLaps[i]) + " " +lapChange[i]);
}
System.out.println(timesString[1]);
}



public static 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 static String secToString(double input)
{
int minutes = (int)input/60;
double seconds = input - (60*minutes);

//DecimalFormat df = new DecimalFormat("00.00");

String s = minutes+":"+df.format(seconds);
return s;
}


}







NEXT SET OF CODE:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;

/**
* TableDemo is just like SimpleTableDemo, except that it
* uses a custom TableModel.
*/
public class TableDemo extends JPanel {
private boolean DEBUG = false;

public TableDemo() {
super(new GridLayout(1,0));

JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
add(scrollPane);
}

class MyTableModel extends AbstractTableModel {
private String[] columnNames = {"Lap",
"Total Time",
"Lap Time",
"Difference",};
private Object[][] data = {
{"Kathy", "Smith",
"Snowboarding", new Integer(5), new Boolean(false)},
{"John", "Doe",
"Rowing", new Integer(3), new Boolean(true)},
{"Sue", "Black",
"Knitting", new Integer(2), new Boolean(false)},
{"Jane", "White",
"Speed reading", new Integer(20), new Boolean(true)},
{"Joe", "Brown",
"Pool", new Integer(10), new Boolean(false)}
};

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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

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

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

Explanation / Answer

Code 1:

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

public class Splits
{
private DecimalFormat df = new DecimalFormat("00.00");
Scanner keyboard = new Scanner(System.in);
boolean dataOk=false;
int numLaps = 0;
String[] timesString;
double[] timesDouble;
double[] singleLaps ;
String[] lapChange;
public void execute()
{
  
  System.out.println("Enter the number or laps.");

  numLaps=keyboard.nextInt();
  keyboard.nextLine();
  timesString = new String[numLaps];
  timesDouble = new double[numLaps];
  
  for(int i=0;i<timesString.length;i++)
  {
   System.out.println("Enter time for lap "+(i+1));
   timesString[i]=keyboard.

   nextLine();
   timesDouble[i]=stringToSec(timesString[i]);
  }

  singleLaps= new double[numLaps];


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


  lapChange = new String[numLaps];
  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]);
   }
  }

  System.out.println("Lap Total Time Lap Time Difference");

  for(int i=0;i<timesString.length;i++)
  {
   System.out.println("Lap "+(i+1) + " " +timesString[i] +
     " " +secToString(singleLaps[i]) + " " +lapChange[i]);
  }
  System.out.println(timesString[1]);
}

public void fillTable(Object[][] data)
{
  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);
  }
}  


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);

//DecimalFormat df = new DecimalFormat("00.00");

String s = minutes+":"+df.format(seconds);
return s;
}


}

Code 2:

import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import java.awt.Dimension;
import java.awt.GridLayout;

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

class TableDemo extends JPanel {
private boolean DEBUG = false;

public TableDemo() {
super(new GridLayout(1,0));

JTable table = new JTable(new MyTableModel());
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
table.setFillsViewportHeight(true);

//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);

//Add the scroll pane to this panel.
add(scrollPane);
}

public class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"Lap",
"Total Time",
"Lap Time",
"Difference","true/false"};
Object[][] data;
Splits ib = new Splits();
ib.execute();
ib.fillTable(data);

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");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
TableDemo newContentPane = new TableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

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

public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run() {
createAndShowGUI();
}
});
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote