In your own words, describe what a Java interface is (this is related to the Java keyword interface), and what a class must do in order to claim that it implements the interface.
The following program simply displays a window containing a button and a field. Show how to edit it so that clicking on the button will clear the text field.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Question3 extends JFrame implements ActionListener {
private JButton button;
private JTextField field;
public Question3() {
button = new JButton("Reset");
field = new JTextField();
getContentPane().add(field, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);
pack();
}
public static void main(String[] args) {
(new Question3()).show();
}
}
Consider the following method.
public class Quadratic {
public static int main(String[] args) {
double a = IO.readDouble();
double b = IO.readDouble();
double c = IO.readDouble();
double disc = b * b - 4 * a * c;
if(disc < 0.0) {
return Double.NaN;
}
if(-b > Math.sqrt(disc)) {
return (-b - Math.sqrt(disc)) / (2 * a);
} else {
return (-b + Math.sqrt(disc)) / (2 * a);
}
}
}
Consider the following method, which finds and removes some duplicated nonzero number in the parameter array, returning true if it found one.
public static boolean removeDuplicate(char[] arr) {
int i, j;
outer:
for(i = 0; i < arr.length; i++) {
for(int j = i + 1; j < arr.length; j++) {
if(arr[i] != 0 && arr[i] == arr[j]) break outer;
}
}
if(i == arr.length { // no duplicate found
return false;
} else { // we found a duplicate; shift elements down over j
for(int k = j + 1; k < arr.length; k++) {
arr[k - 1] = arr[k];
}
arr[arr.length - 1] = 0;
return true;
}
}
arr. In terms
of n, how much time does the above method take? Use Big-O
notation to express your answer, and provide the tightest bound that
you can.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Question3 extends JFrame implements ActionListener {
private JButton button;
private JTextField field;
public Question3() {
button = new JButton("Reset");
field = new JTextField();
getContentPane().add(field, BorderLayout.CENTER);
getContentPane().add(button, BorderLayout.SOUTH);
pack();
button.addActionListener(this);
}
public void actionPerformed(ActionEvent evt) {
field.setText("");
}
public static void main(String[] args) {
(new Question3()).show();
}
}