CSE 205 Exam 2 Review
Download
Report
Transcript CSE 205 Exam 2 Review
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.
● I—
f you want your program to react to events, you need to use event listener
objects.
●E
—vent Listeners are instances of classes, they provide instructions in their
methods explaining how you want your program to react to events—like
button clicks.
●W
—ithout event listeners, your buttons/GUI elements will not respond to user
interaction!
○ U
—sers can click all they want, without a listener, your buttons, lists, etc., are not paying
attention.
GUI Graphics: Listening To Buttons--Step 1
Y
—ou 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.
T
—his class will be a nested private class inside the class or code block that
contains your elements.
private class JumpListener implements ActionListener {
public void actionPerformed(ActionEvent
event) {
jumpLabel.setText(“Now
jumping”);
}
}
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.
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 nonnumber 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(n2)
—
Selection: O(n2)
—
Merge: More efficient than selection sort, O(nlogn)
—
—
Quick: O(n2)
Want more info? Look to Chapter 14 of Java For Everyone
—
2e.