Even-Driven Programming and basic Graphical User Interface
Exam 2 – Section 02 Avg: 127 (84%)
Exam 2 – Section 01 Avg: (85%)
Exam 2 1. In String class, substring() is a(n) Attribute Method Statement Criteria
Exam 2 2. The following statement: double[] studentGPA = new double[12]; – only declares the array studentGPA and did not allocate memory for this array – only allocates memory for this array studentGPA and did not declare it – creates 12 studentGPA objects – declares the array named studentGPA and allocates memory for this array
Exam 2 3. Suppose an array contains 1024 elements. What are the least and the greatest number of comparisons for a successful search using linear search? – 1 and 10 – 1 and 1024 – 1 and 8 – 1 and 1023
Exam 2 4. Suppose a sorted array contains 1024 elements. What are the least and the greatest number of comparisons for a successful search using binary search? 1 and and 10 1 and 8 1 and 9
Exam 2 5. Which of the followings is the constructor of the class MyString? public String MyString (String str) {…..} public MyString(String str) { ….} public void MyString(String str) {….} public Boolean MyString(String str) {….}
Exam 2 6. The amount of memory allocated to store an array depends on: a. the name of the array b. the size of the array c. the size and type of values in the array d. none of the above
Exam 2 6. The amount of memory allocated to store an array depends on: a. the name of the array b. the size of the array c. the size and type of values in the array d. none of the above
Exam 2 7. How many comparisons we need to perform to find x=10 from the following array with 9 elements using binary search a. 1 b. 2 c. 3 d. 4 Pass 1: (0+8)/2 = 4. 0<10 Pass 2: (5+8)/2 = 6. 20>10 Pass 3: (5+5)/2 = 510=10
Exam 2 8. Array is a collection of data values of the different data types True False
Exam 2 8. Array is a collection of data values of the different data types True False
Exam 2 9. This is a special method that is invoked when a new object of a class is created: a. Main method b. Constructor c. New method d. Void method
Exam In order to run a Java program, this program must include at least one class that has main method a. True b. False
Exam 2 BAD C
E OR C C E D A B Or B A
Exam 2 = AE BDC
CB A D D A
public String ExampleClass(String anExample) { example = new String(anExample); } Constructor doesn’t have a return data type
Exam 2 public class QuestionOne { private double c; public QuestionOne() { c=0; } private void methodOne (int a) { c=a; } public double methodTwo() { return c; } public class QuestionOneMain { public static void main(String args[]) { QuestionOne q1; q1 = new QuestionOne(); q1.c = 12; q1.methodOne( 12); double result = q1.methodTwo(); } C is a private data member and thus can not be assessed outside of QuestionOne Class methodOne is also a private method
Exam obj = new QuestionTwo(); //count = 0 obj.modifyCount(); // count = (0+10)*2 = 20 obj.printCount(); count=20
Exam String resultStr = q3.getSubMessage(16,22);getSubMessage // resultStr = “Easter” System.out.println("A part of this message is "+ resultStr+" length="+resultStr.length()); resultStr = “Easter” and length=6
What is Graphical User Interface (GUI) A graphical user interface is a method of interacting with a computer through a metaphor of direct manipulation of graphical images and widgets in addition to text. interacting with a computer metaphordirect manipulationwidgets GUI display visual elements such as icons, windows, menu, buttons, checkboxes…
Examples
GUI in Java Java.AWT package: GUI objects are implemented by using native GUI objects and may behave differently on different OSs. Javax.swing package (only available from JDK 1.2 onward): GUI objects are implemented fully in Java and therefore behave the same on different OSs. Offers new features compared to Java.AWT package
Event-driven programming An event occurs when the user interacts with a GUI object (click a button, move a mouse, press a key…)
Plan for this week Create a frame Create a button and handling button event
Create a frame
Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Import GUI swing package
Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Create a subclass that inherits JFrame class
JFrame class Type: “JFrame class Java” in Google and choose the link: swing/JFrame.html
Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Constant declarations
Code import javax.swing.*; public class JFrameSubclass extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; public JFrameSubclass () { setTitle("My First Subclass"); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); setDefaultCloseOperation(EXIT_ON_CLOSE); } Constructor for this JFrameSubclass class
Using methods from JFrame class setTitle methods: Description is available at: Title(java.lang.String) setTitle public void setTitle(String title)String – Sets the title for this frame to the specified string. Example: setTitle("My First Subclass”);
Using methods in JFrame setSize(int, int) Avaiable at: nent.html#setSize(int,%20int) public void setSize(int width, int height) – Resizes this component so that it has width width and height height. Example: setSize(FRAME_WIDTH, FRAME_HEIGHT);
Using methods in JFrame setLocation public void setLocation(int x, int y) Moves this component to a new location. The top-left corner of the new location is specified by the x and y parameters in the coordinate space of this component's parent. Example: setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN);
Using methods in JFrame setDefaultCloseOperation: me.html#setDefaultCloseOperation(int) me.html#setDefaultCloseOperation(int) public void setDefaultCloseOperation(int operation) Example: setDefaultCloseOperation(EXIT_ON_CLOSE);
Using public constants in JFrame EXIT_ON_CLOSE /swing/JFrame.html#EXIT_ON_CLOSE public static final int EXIT_ON_CLOSE
Create a button Create a dumb GUI first Add a module to handle event later
Create a dumb GUI
Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton; Packages included for event and GUI objects
Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton;
Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton;
Code import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JButtonFrame extends JFrame { private static final int FRAME_WIDTH=300; private static final int FRAME_HEIGHT=200; private static final int FRAME_X_ORIGIN=150; private static final int FRAME_Y_ORIGIN = 250; private static final int BUTTON_WIDTH=80; private static final int BUTTON_HEIGHT=30; private JButton cancelButton; private JButton okButton;
Constructor for this class public JButtonFrame () { Container contentPane= getContentPane(); setTitle("My Button class"); setResizable(false); setSize(FRAME_WIDTH, FRAME_HEIGHT); setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); contentPane.setLayout(null); contentPane.setBackground(Color.white);
Constructor for this class okButton = new JButton("OK"); okButton.setBounds(70,125,BUTTON_WIDTH,BUTTON_HEIGHT)okButton.setBounds(70,125,BUTTON_WIDTH,BUTTON_HEIGHT); contentPane.add(okButton); cancelButton = new JButton("Cancel"); cancelButton.setBounds(160,125,BUTTON_WIDTH,BUTTON_HEIGHT); contentPane.add(cancelButton); setDefaultCloseOperation(EXIT_ON_CLOSE); }