THIRD RESUBMISSION BECAUSE PEOPLE GIVE ME INCOMPLETE PROGRAMS/SPAM need to edit
ID: 3829420 • Letter: T
Question
THIRD RESUBMISSION BECAUSE PEOPLE GIVE ME INCOMPLETE PROGRAMS/SPAM
need to edit a Java Cosole app to a Java GUI application. Specifications...
Use imported Swing class to create Java GUI form.
Add controls to a Java GUI form.
Modify form control properties with Java code.
Attach Java code to form controls so application is user eventdriven.
please see the code so far...
import java.text.SimpleDateFormat;
import java.util.Date;
public class CompareLoops {
//Declare & Initialize Variables
static double forLoopElapsedTime = 0;
static double whileLoopElapsedTime = 0;
static double doWhileLoopElapsedTime = 0;
//For Loop - Calculate Elapsed Time
public static void calculateForLoopElapsedTime()
{
System.out.println(" ==== For Loop Execution Time ======");
System.out.println("Start Time = " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
double startTime = System.currentTimeMillis();
long total = 0;
for (int i = 0; i < 10000000; i++)
{
total += i;
}
System.out.println("End Time = " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
double stopTime = System.currentTimeMillis();
forLoopElapsedTime = (stopTime - startTime)/1000;
System.out.println("While loop average elapsed time = " + forLoopElapsedTime + " seconds");
System.out.println("Completed Loops = " + total);
}
//While Loop - Calculate Elapsed Time
public static void calculateWhileLoopElapesdTime()
{
System.out.println(" ==== While Loop Execution Time ======");
System.out.println("Start Time = " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
double startTime = System.currentTimeMillis();
long total = 0;
long i = 0;
while(i < 10000000){
total += i;
i++;
}
System.out.println("End Time = " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
double stopTime = System.currentTimeMillis();
whileLoopElapsedTime = (stopTime - startTime)/1000;
System.out.println("Do While loop average elapsed time = " + whileLoopElapsedTime + " seconds");
System.out.println("Completed Loops = " + total);
}
//Do While Loop - Calculate Elapsed Time
public static void calculateDoWhileLoopElapsedTime()
{
System.out.println(" ==== Do While Loop Execution Time ======");
System.out.println("Start Time = " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
double startTime = System.currentTimeMillis();
long total = 0;
long i = 0;
do{
total += i;
i++;
}while(i < 10000000);
System.out.println("End Time = " + new SimpleDateFormat("hh:mm:ss a").format(new Date()));
double stopTime = System.currentTimeMillis();
doWhileLoopElapsedTime = (stopTime - startTime)/1000;
System.out.println("For loop average elapsed time = " + doWhileLoopElapsedTime + " seconds");
System.out.println("Completed Loops = " + total);
}
//Compare Elapsed Time
public static void compareElaspedTime()
{
System.out.print(" ");
if(forLoopElapsedTime < whileLoopElapsedTime){
if(forLoopElapsedTime < doWhileLoopElapsedTime){
System.out.println("Fastest execution time : For Loop " +forLoopElapsedTime+ " seconds");
}else{
System.out.println("Fastest execution time : Do While Loop " +doWhileLoopElapsedTime+ " seconds");
}
}else{
if(whileLoopElapsedTime < doWhileLoopElapsedTime){
System.out.println("Fastest execution time : While Loop " +whileLoopElapsedTime+ " seconds");
}else{
System.out.println("Fastest execution time : Do While Loop " +doWhileLoopElapsedTime+ " seconds");
}
}
}
public static void main(String[] args)
{
System.out.println("Average Elapsed Time for 10,000,000 loops in seconds");
System.out.println(" _________________________________ ");
calculateForLoopElapsedTime();
calculateWhileLoopElapesdTime();
calculateDoWhileLoopElapsedTime();
compareElaspedTime();
}
}
*****Must Include These Events
Form Load event
While Loop button Click event
For Loop button Click event
Do While Loop button Click event
Clear All button Click event
************Please model the GUI after after these C# outputs with buttons, etc.
Use imported Swing class to create Java GUI form.
Add controls to a Java GUI form.
Modify form control properties with Java code.
Attach Java code to form controls so application is user eventdriven.
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
public class AWTAccumulator extends Frame implements ActionListener {
private Label lblInput;
private Label lblOutput;
private TextField tfInput;
private TextField tfOutput;
private int sum = 0;
public AWTAccumulator() {
setLayout(new FlowLayout());
lblInput = new Label("Enter an Integer: ");
add(lblInput);
tfInput = new TextField(10);
add(tfInput);
tfInput.addActionListener(this);
lblOutput = new Label("The Accumulated Sum is: ");
tfOutput = new TextField(10);
tfOutput.setEditable(false);
add(tfOutput);
setTitle("AWT Accumulator");
setSize(350, 120);
setVisible(true);
}
public static void main(String[] args) {
new AWTAccumulator();
@Override
public void actionPerformed(ActionEvent evt) {
int numberIn = Integer.parseInt(tfInput.getText());
sum += numberIn;
tfInput.setText("");
tfOutput.setText(sum + "");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.