Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Even-Driven Programming and basic Graphical User Interface Exam 2 – Section 02 Avg: 127 (84%) Section 02 160 140 120 100 80 60 Series1 40 20 0 0 10 20 30 40 Exam 2 – Section 01 Avg: 128.8 (85%) Section 01 160 140 120 100 80 60 Series1 40 20 0 0 10 20 30 40 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 1024 1 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 -100 -80 -20 -10 0 10 20 80 100 a. 1 Pass 1: (0+8)/2 = 4. 0<10 Pass 2: (5+8)/2 = 6. 20>10 b. 2 Pass 3: (5+5)/2 = 5 10=10 c. 3 d. 4 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 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 Exam 2 B A D C Exam 2 E OR C C OR E D A B Or B Or A Exam 2 A E = B D C Exam 2 C B D A D A Exam 2 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 2 17. obj = new QuestionTwo(); //count = 0 obj.modifyCount(); // count = (0+10)*2 = 20 obj.printCount(); count=20 Exam 2 18. String resultStr = q3.getSubMessage(16,22); // 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. GUI display visual elements such as icons, windows, menu, buttons, checkboxes… Examples 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.*; Import GUI swing package 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); } } Code import javax.swing.*; Create a subclass that inherits JFrame class 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); } } 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 Code import javax.swing.*; Constant declarations 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); } } Code import javax.swing.*; Constructor for this JFrameSubclass class 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); } } 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) – Sets the title for this frame to the specified string. Example: setTitle("My First Subclass”); 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); 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); Using methods in JFrame setDefaultCloseOperation: 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); 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 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.*; Packages included for event and GUI objects 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; 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); contentPane.add(okButton); cancelButton = new JButton("Cancel"); cancelButton.setBounds(160,125,BUTTON_WIDTH,BUTTON_HEIGHT); contentPane.add(cancelButton); setDefaultCloseOperation(EXIT_ON_CLOSE); } }