CSE 205 Exam 2 Review By Cheri Thompson and Katherine Kincade.

Slides:



Advertisements
Similar presentations
Things to mention public static void main(String [] args) –The starting point for a free-standing Java application (i.e. one not run from the DrJava interactions.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Agenda –interfaces and realization –type hierarchy –introduction to graphics and event handling.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
1 From Yesterday private = accessible only to the class that declares it public = accessible to any class at all protected = accessible to the class and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions CIS 304 Intermediate Java Programming for Business.
Swing CS-328 Dick Steflik John Margulies. Swing vs AWT AWT is Java’s original set of classes for building GUIs Uses peer components of the OS; heavyweight.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Class 8. 2 Chapter Objectives Use Swing components to build the GUI for a Swing program Implement an ActionListener to handle events Add interface components.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
10-1 Exceptions An exception is an object that describes an unusual or erroneous situation Exceptions are thrown by a program, and may be caught and handled.
Chapter 9: Exceptions For error/problem situations Exception classes –ArithmeticException, IOException, etc. –checked exceptions try blocks –catch statements.
1 Event Driven Programming wirh Graphical User Interfaces (GUIs) A Crash Course © Rick Mercer.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Object Oriented Programming Ders 11: Interfaces Mustafa Emre İlal
MIT AITI 2003 Lecture 17. Swing - Part II. The Java Event Model Up until now, we have focused on GUI's to present information (with one exception) Up.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Chapter 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Java GUI. Graphical User Interface (GUI) a list a button a text field a label combo box checkbox.
Java Programming, 2E Introductory Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 24.1 Test-Driving the Enhanced Car Payment.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
1 Event Driven Programs with a Graphical User Interface Rick Mercer.
Chapter 14: Introduction to Swing Components. Objectives Understand Swing components Use the JFrame class Use the JLabel class Use a layout manager Extend.
Creating a GUI Class An example of class design using inheritance and interfaces.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
Lesson 28: More on the GUI button, frame and actions.
Graphical User Interface (GUI)
MIT AITI 2004 Swing Event Model Lecture 17. The Java Event Model In the last lecture, we learned how to construct a GUI to present information to the.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Java Programming Fifth Edition Chapter 13 Introduction to Swing Components.
GUIs and Events Rick Mercer.
CompSci 230 S Programming Techniques
A First Look at GUI Applications
“Form Ever Follows Function” Louis Henri Sullivan
Creating and Modifying Text part 2
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 12 Exception Handling and Text IO Part 1
CiS 260: App Dev I Chapter 6: GUI and OOD.
Presentation transcript:

CSE 205 Exam 2 Review By Cheri Thompson and Katherine Kincade

Interfaces Interfaces are like classes, but they contain groups of methods that have nothing inside. Think of an interface as being a blueprint. Classes implement interfaces. When a class uses an interface, it must declare each method that was outlined in the interface, or the compiler will not allow that class to compile. interface house { //…. void setWindows(int size); //etc... } class Mansion implements House { Int windowSize; //….. void setWindows(int size) { windowSize = size; } //etc…. }

GUI: Your Tools JApplet: “holds” everything desired to be displayed when ran JButton: an object which displays a pressable button JTextField: takes in user input which is typed by user JTextArea: displays plain text JLabel: displays a label message JList: a component used to display an array or vector of Objects JRadioButton: a button which is selected or deselected based on a desired state ButtonGroup: when multiple buttons are in the same ButtonGroup, clicking one button deactivates the others in the group Container: used with getContentPane( ) method of JFrame and JApplet JPanel: Holds other components JTabbedPane: A pane which allows the user to switch between different tabs JScrollPane: Provides a scrollable view to a JTextArea or JTextField ActionEvent: an event that can trigger the ActionListener ActionListener: allows an action to occur when a specific ActionEvent occurs

GUI Graphics: Listening To Buttons ●—When the mouse is inside the window of a GUI program, events occur. ●—If you want your program to react to events, you need to use event listener objects. ●—Event Listeners are instances of classes, they provide instructions in their methods explaining how you want your program to react to events—like button clicks. ●—Without event listeners, your buttons/GUI elements will not respond to user interaction! ○—Users can click all they want, without a listener, your buttons, lists, etc., are not paying attention.

GUI Graphics: Listening To Buttons--Step 1 —You implenent the ActionListener interface to create a listener class that does what you want when the action happens—such as changing text of a JLabel in the example below. —This class will be a nested private class inside the class or code block that contains your elements. In the code to the left, the JLabel named jumpLabel will change text to “Now jumping” when someone interacts with the element that has this listener added to it. private class JumpListener implements ActionListener { public void actionPerformed(ActionEvent event) { jumpLabel.setText(“Now jumping”); } }

GUI Graphics: Listening To Buttons--Step 2 —Just like how you use the Scanner class, after you create your nested class—JumpListener in this case—you must create an object of that class before you can apply it to any elements. —You can create this object any time before its needed. Make sure it’s accessible to the GUI elements that need it. JumpListener jumpListener = new JumpListener(); jumpLabel = new JLabel(“ready to jump”); jumpButton = new JButton(“Click to jump”);

GUI Graphics: Listening To Buttons--Step 3 —GUI elements like Jbutton and Jlabel have an add method so that an ActionListener can pay attention to them. —If you don’t add your ActionListener to your object, they don’t pay any attention to each other. —Make sure to add your ActionListener after your GUI elements have been initialized. jumpButton.addActionListener(jumpListener);

GUI Graphics: Listening To Buttons--Step 4 Test your buttons! —Now you should have everything in place to check if your elements are actually interacting with your ActionListener correctly. —If you get the output you want, congratulations! —If you’re not seeing anything on your screen, check the layouts of your JPanels, and if your elements are all added to your Jpanels.

Exceptions Handling —When your program comes across an error it doesn’t know what to do with, it will stop. —You can avoid the program stopping itself by giving it instructions for when it runs into errors. This is what input/output exception handling is about. —You can even force the program to claim it has found a certain kind of error/exception. —If you want to throw an input/output related exception (like FileNotFoundException), you will need to import java.io.

Throwing Exceptions —If you indicate that your method throws an exception using the throws keyword, the method will end when it runs into that kind of exception. public static void main(String[] args) throws FileNotFoundException { //now main will end rather than giving you an error if a FileNotFoundException happens here }

Catching Exceptions —Use the try and catch keywords. Code inside a try block will attempt to execute as normal, but if an exception detailed in a catch statement is found, the code will stop executing and move to the catch. —If your program throws a FileNotFoundException and has an IOException catch statement, this will execute because a FileNotFoundException is a descendant of IOException. —Catch blocks only execute if the exception identified in the catch statement happens during the try block of code. —The Finally block of code executes after the try and catch statements have executed, regardless of what happened in the try and catch statements.

Checked And Unchecked Exceptions —If you do not indicate how to deal with a checked exception, the program will not compile. Checked exceptions are also sometimes known as compile time exceptions. If you try to read a file, but the file you specified does not exist, the compiler will indicate that you have a FileNotFoundException and refuse to compile. This is a checked exception. An unchecked exception is not caught by the compiler, and will show up while the program is running when unexpected behavior occurs. An example of an unchecked exception is an ArrayIndexOutOfBoundsException. —Want more info? Look to Chapter 7.4 of Java For Everyone 2e.

Common Java Exceptions ArrayIndexOutOfBoundsException: The array index you’re trying to access is either larger than the largest index, or negative. In java.lang. NullPointerException: You’re trying to access an object that holds a null value. In java.lang. StackOverflowError: Your recursive function called itself too many times and the program ran out of room because of it. In java.lang. NumberFormatException: You probably tried to convert a string to a number, but there was a non- number in the string. In java.lang. IllegalArgumentException: An argument you’re trying to pass into your method (the stuff in parenthesis when you call the method) doesn’t match what the method is formatted to receive (the stuff in parenthesis when you create the method). In java.lang.

Text File Read/Write You can read and write files using a BufferedReader and BufferedWriter String str; FileReader fReader = new FileReader(“file.txt”); BufferedReader bReader = new BufferedReader(fReader); while(str = bReader.readLine()) != null){ System.out.println(str); } bReader.close(); fReader.close()

Serialization/ De-Serialization Serialization allows objects to exist outside of a running program Example for serialization. De-serialization is same except you use FileInputStream and ObjectOutputStream Student s = new Student(); FileOutputStream fileout = new FileOutputStream(“students.txt”); ObjectOutputStream output = new ObjectOutputStream(fileout); output.writeObject(s); output.close(); fileout.close();

Algorithms Insertion Sort: Removes one element from the input data and then places it in its correct place within the sorted portion of the list Selection Sort: The algorithm splits an array into two parts: sorted and unsorted. It finds the smallest element in the unsorted portion of the array and then moves it to the end of the sorted portion of the array Merge Sort: Is a recursive algorithm which splits the unsorted array into sublists, each containing one element and then the sublists are merged together until the list is sorted Quick Sort: A pivot is chosen, and then the elements are sorted into two groups: the elements smaller than the pivot and the elements larger than the pivot. This is done recursively until the array is sorted

Sorting —Insertion: O(n 2 ) —Selection: O(n 2 ) —Merge: More efficient than selection sort, O(nlog n ) — Quick: O(n 2 ) —Want more info? Look to Chapter 14 of Java For Everyone 2e.