1 / 2100%
Last Name ______________________ First Name___________________
Please review this handout for exams. You do NOT have to turn it in.
1. In JavaFX, what layout pane could you use to place controls in 4 columns over 7 rows?
_____________________________________
2. What method must be implemented if a class implements EventHandler interface?
__________________________________________
3. If you have a Pane object named topPaneand an inner listener class named ShapeListener, how do you associate
the listener with topPane to listen to the MousePressed event? _________________________________________
4. What happens if you attempt to open a nonexistent file using FileInputStream?
5. What happens if you attempt to open a nonexistent file using FileOutputStream?
6. What happens if you attempt to store an object that is not an instance of Serializable?
7. Assume that you will want to save, using object serialization, a Book object in a file. Write a class definition
(Book.java) for Book class using the following UML class diagram. This class has only one method, a constructor that
assigns values to each instance variable. Then write a complete Java program (driver class with a main () method) to
instantiate a Book object with some title, price and bookId (you can decide on them) and store it in a file called
“book.dat”.
Book
-title : String
-price: doube
-bookId : int
+Book(String, double, int)
Practice – CSE 205
Answers:
1. The Grid Pane layout
2. public void handle (ActionEvent e)
3. topPane. setOnMousePressed (new ShapeListener());
4. FileNotFoundException will be thrown.
5. A file with the name will be created.
6. NotSerializableException will be thrown.
7.
// Book.java
import java.io.Serializable;
public class Book implements Serializable
{
private String title;
private double price;
private int bookId;
public Book(String title, double price, int bookId)
{
this.title = title;
this.price = price;
this.bookId = bookId;
}
}
// Driver class (Driver.java)
import java.io.*;
public class Driver
{
public static void main(String[] args)
{
Book book1 = new Book("Java Programming", 80.50, 12345);
FileOutputStream file =null;
ObjectOutputStream outStream = null;
try
{
file = new FileOutputStream("book.dat");
outStream = new ObjectOutputStream(file);
outStream.writeObject(book1);
}
catch(IOException ex)
{
System.out.println("IOException");
}
//the following is optional
catch(NotSerializableException ex)
{
System.out.println("The object is not serializable");
}
}//end of main method
} //end of Driver class
Powered by TCPDF (www.tcpdf.org)
Students also viewed