0% found this document useful (0 votes)
4 views6 pages

Number Line Jump Algorithm Analysis

The original code analyzes the complexity of an algorithm to determine if two kangaroos will meet based on their positions and speeds. It runs in O(1) time. The proposed solution implements a GUI in Java to allow a user to input kangaroo positions and speeds from a file, runs the algorithm, displays the output, and calculates the CPU time. It initializes GUI components, gets input from a file on button click in O(1) time, runs the algorithm in O(1) time, and displays the output. The overall complexity is O(1).
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Number Line Jump Algorithm Analysis

The original code analyzes the complexity of an algorithm to determine if two kangaroos will meet based on their positions and speeds. It runs in O(1) time. The proposed solution implements a GUI in Java to allow a user to input kangaroo positions and speeds from a file, runs the algorithm, displays the output, and calculates the CPU time. It initializes GUI components, gets input from a file on button click in O(1) time, runs the algorithm in O(1) time, and displays the output. The overall complexity is O(1).
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

VII] ANALYSIS OF COMPLEXITY OF THE ALGORITHM

ORIGINAL CODE
import [Link];

public class Kangaroo {

static String kangaroo(int x1, int v1, int x2, int v2) {

if (v1 > v2) {


int remainder = (x1 - x2) % (v2 - v1); --O(1)
if (remainder == 0) { 2*O(1) O(1)
return "YES"; --O(1)
}
}
return "NO"; --O(1)
}

Cost: 2*O(1)*O(1)
Complexity: O(1)
O(1)
public static void main(String[] args) {

Scanner in = new Scanner([Link]);

int x1 = [Link](); --O(1)


int v1 = [Link](); --O(1)
int x2 = [Link](); --O(1) 6*O(1)
int v2 = [Link](); --O(1)

[Link](kangaroo(x1, v1, x2, v2)); --O(1)


[Link](); --O(1)

}
}
Cost: O(1)*6*(1)
Complexity: O(1)
PROPOSED SOLUTION

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class NumberLineJumpGUI extends JFrame {

private JPanel contentPane;


private JTextField txtx1;
private JTextField txtv1;
private JTextField txtx2;
private JTextField txtv2;
private JTextField txtoutput;
private JTextField txtCputime; --O(1)
private JButton btnAttachFile;
private long startTime = 0;
private long endTime = 0;
static long temp = 0;
static long totalTime = 0;
JFileChooser fileChooser;
int [] item = new int[4];
public static void main(String[] args) {
[Link](new Runnable() {
public void run() {
try {
NumberLineJumpGUI frame = new NumberLineJumpGUI(); --O(1) = 3*O(1)
[Link](true); -- O(1) = O(1)
} catch (Exception e) {
[Link](); --O(1)
}
}
});
}
public NumberLineJumpGUI() {
setTitle("Number Line Jumps");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 718, 432);
setLocationRelativeTo(null);
contentPane = new JPanel(); O(1)
[Link](new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
[Link](null);
fileChooser = new JFileChooser();
btnAttachFile = new JButton("Attach File");
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e) {
totalTime = 0; --O(1)
if ([Link](null)== JFileChooser.APPROVE_OPTION)—O(1)
{
[Link] file = [Link](); --O(1)
Scanner input =null; --O(1)
try {
input = new Scanner(file); -- O(1)
} catch (FileNotFoundException e1) {
[Link](); -- O(1)
}
start(); -- O(1)
while([Link]()) {
item[0] = [Link](); –O(1)
[Link]([Link](item[0])); --0(1)
item[1] = [Link](); -- O(1)
[Link]([Link](item[1])); --0(1)
= 15*O(1) = 8*O(1) item[2] = [Link](); – O(1)
= O(1) [Link]([Link](item[2])); --0(1)
= O(1)
item[3] = [Link](); –O(1)
[Link]([Link](item[3])); -- O(1)

}
[Link](); --O(1)

String s = ((item[1]>item[3]) && ((item[2]-item[0]) % (item[3]-


item[1] )==0))? "YES": "NO"; -- O(1)
end(); --O(1)
[Link](s); --O(1)
temp = getTotalTime(); --O(1)
totalTime += temp; --O(1)
double seconds = (double)totalTime; --O(1)
[Link](seconds / 1000 + "s"); --O(1)
}
}
});
[Link](530, 340, 119, 27); -- O(1)
[Link](btnAttachFile); --O(1)

txtCputime = new JTextField();


[Link](new Font("Dialog", [Link], 15));
[Link](false);
[Link]([Link]); O(1)
[Link](10);
[Link](354, 283, 138, 39);
[Link](txtCputime);

txtoutput = new JTextField();


[Link](new Font("Dialog", [Link], 15));
[Link](false);
[Link]([Link]); O(1)
[Link](10);
[Link](194, 283, 138, 39);
[Link](txtoutput);

txtv2 = new JTextField();


[Link](new Font("Dialog", [Link], 15));
[Link](false);
[Link]([Link]); O(1)
[Link](10);
[Link](512, 159, 138, 39);
[Link](txtv2);

txtx2 = new JTextField();


[Link](new Font("Dialog", [Link], 15));
[Link](false);
[Link]([Link]); O(1)
[Link](10);
[Link](354, 159, 138, 39);
[Link](txtx2);

txtv1 = new JTextField();


[Link](new Font("Dialog", [Link], 15));
[Link](false);
[Link]([Link]); O(1)
[Link](10);
[Link](194, 159, 138, 39);
[Link](txtv1);

txtx1 = new JTextField();


[Link](new Font("Dialog", [Link], 15));
[Link](false);
[Link]([Link]); O(1)
[Link](36, 159, 138, 39);
[Link](txtx1);
[Link](10);

JLabel lblNewLabel = new JLabel("");


[Link](new
ImageIcon([Link]("/images/[Link]"))); O(1)
[Link](0, -11, 719, 408);
[Link](lblNewLabel);
}
public void start() { [Link] = [Link]();} --O(1)
public void end() { [Link] = [Link]();} --O(1)
public long getStartTime() { return [Link]; }--O(1)
public long getEndTime() {return [Link];} --O(1)
public long getTotalTime() { return [Link] - [Link]; } --O(1)
}
Cost:
Complexity:

You might also like