Download presentation
Presentation is loading. Please wait.
Published byKaren Norton Modified over 8 years ago
1
Even-Driven Programming and basic Graphical User Interface
2
Exam 2 – Section 02 Avg: 127 (84%)
3
Exam 2 – Section 01 Avg: 128.8 (85%)
4
Exam 2 1. In String class, substring() is a(n) Attribute Method Statement Criteria
5
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
6
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
7
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 1024 1 and 10 1 and 8 1 and 9
8
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) {….}
9
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
10
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
11
Exam 2 7. How many comparisons we need to perform to find x=10 from the following array with 9 elements using binary search -100 -80 -20 -10 0 10 20 80 100 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
12
Exam 2 8. Array is a collection of data values of the different data types True False
13
Exam 2 8. Array is a collection of data values of the different data types True False
14
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
15
Exam 2 10. In order to run a Java program, this program must include at least one class that has main method a. True b. False
16
Exam 2 BAD C
17
E OR C C E D A B Or B A
18
Exam 2 = AE BDC
19
CB A D D A
20
public String ExampleClass(String anExample) { example = new String(anExample); } Constructor doesn’t have a return data type
21
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
22
Exam 2 17. obj = new QuestionTwo(); //count = 0 obj.modifyCount(); // count = (0+10)*2 = 20 obj.printCount(); count=20
23
Exam 2 18. 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
24
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…
25
Examples
27
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
28
Event-driven programming An event occurs when the user interacts with a GUI object (click a button, move a mouse, press a key…)
29
Plan for this week Create a frame Create a button and handling button event
30
Create a frame
31
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
32
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
33
JFrame class Type: “JFrame class Java” in Google and choose the link: http://java.sun.com/j2se/1.4.2/docs/api/javax/ swing/JFrame.html
34
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
35
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
36
Using methods from JFrame class setTitle methods: Description is available at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Frame.html#set 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”);
37
Using methods in JFrame setSize(int, int) Avaiable at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Compo 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);
38
Using methods in JFrame setLocation http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Component.html#setLocation(int,%20int) 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);
39
Using methods in JFrame setDefaultCloseOperation: http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFra me.html#setDefaultCloseOperation(int) http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFra me.html#setDefaultCloseOperation(int) public void setDefaultCloseOperation(int operation) Example: setDefaultCloseOperation(EXIT_ON_CLOSE);
40
Using public constants in JFrame EXIT_ON_CLOSE http://java.sun.com/j2se/1.4.2/docs/api/javax /swing/JFrame.html#EXIT_ON_CLOSE public static final int EXIT_ON_CLOSE
41
Create a button Create a dumb GUI first Add a module to handle event later
42
Create a dumb GUI
43
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
44
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;
45
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;
46
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;
47
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);
48
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); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.