Big Java / Java Concepts Lab 14

northcarolinawryΛογισμικό & κατασκευή λογ/κού

3 Νοε 2013 (πριν από 3 χρόνια και 7 μήνες)

90 εμφανίσεις

Big Java / Java Concepts Lab 14

Buttons

1.

All problems in this lab are based on the
HelloTester

program:


/** File HelloTester.java */
import javax.swing.JFrame;
public class
HelloTester
{


public static void main(String args[])


{




J
Frame frame = new HelloFrame();




frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




frame.setTitle("HelloTester");




frame.setVisible(true);


}
}
/** File HelloFrame.java */
import javax.swing.JFrame;
import
javax.swing.
JLabel;
import java.awt.BorderLayout;
import
java.awt.Font;
public class HelloFrame extends JFrame
{


public
HelloFrame()


{




message = "Hello, World!";




label
= new JLabel(message);




label.setFont(new Font("serif",
Fon
t.PLAIN, DEFAULT_SIZE));




add(label, BorderLayout.CENTER);






setSize(FRAME_WIDTH, FRAME_HEIGHT);



}




private String message;



private JLabel label;




private static int FRAME_WIDTH = 300;



private static int FRAME_HEIGHT = 3
00;



private static int DEFAULT_SIZE = 20;

}


In this problem, you need to add two buttons to the bottom of the frame, like this:


Follow these steps:

1. Make two instance variables of the class
JButton
:

private JButton smallerButton;

private JButton l
argerButton;

2. Create a method called
createSouthPanel

and place the following two lines
inside the method to initialize the variables with
JButton

objects:

smallerButton = new JButton("Smaller");

largerButton = new JButton("Larger");




3. Place the bu
ttons into a panel:

JPanel southPanel = new JPanel();
southPanel.add(smallerButton);
southPanel.add(largerButton);

4. Add the south panel to the frame:

add(southPanel, BorderLayout.SOUTH);

5. In the frame constructor, call the method
createSouthPanel
:

cr
eateSouthPanel();

Make these changes in your program. Run the program to confirm that the buttons
appear.

(a) What happens when you click the buttons? Why?

(b) Provide the source code for your createSouthPanel method
.



2.

Next, you need to connect the bu
ttons to actions. For each button, carry out these
steps:

1. Create a class that implements the
ActionListener

interface and override the
actionPerformed

method.

2. Make an object of that class.

3. Install that object as the
action listener

of the button.

Perform these steps one at a time.

When the "Larger" button is clicked, we want to increase the font size by 25
percent. When the "Smaller" button is clicked, we want to decrease it. To avoid
integer rounding errors, keep a floating
-
point instance variable

fontSizeFactor

that is initialized with 1. Clicking on the buttons multiplies by 1.25 or 0.8 (because
0.8 = 1/ 1.25).

private double fontSizeFactor = 1;

Here is an appropriate action listener for the "Larger" button.

class LargerFontListener implements Ac
tionListener

{



public void actionPerformed(ActionEvent event)



{



fontSizeFactor = 1.25 * fontSizeFactor;



label.setFont(new Font("serif", Font.PLAIN,









(int) (DEFAULT_SIZE * fontSizeFactor)));



label.repaint();



}

}


Place this class inside the
createSouthPanel

method, since
actionPerformed

needs to access the
panel

and
fontSizeFactor

variables.

To connect it to the "Larger" button, you need to create an object of this class and
set it as an action listener of the b
utton. Place the following instructions into the
frame constructor:

ActionListener largerListener = new LargerFontListener();

largerButton.addActionListener(largerListener);


Add the code, compile the program, and run it. Don't add the code for the
"Small
er" button yet.

Click on both buttons several times. What happens?


3.

Repeat the steps above for the "Smaller" button. Make an action listener class and
add an instance of that class as an action listener of the
smallerButton
. Run your
program. Both butt
ons should work.

Provide the complete code for the
createSouthPanel

method.

Layout Management

4.

In the preceding program, the buttons were placed on the bottom of the frame, next
to each other. Change your program so that they are placed on the
right

of
the
frame, one
above

the other other.


What is the code for the
HelloFrame

class now?


Menus

5.

In this program, we will use a menu instead of a set of buttons to increase and
decrease the font size.


As with buttons, you need to solve two unrelated iss
ues:



How to place the menu items



How to connect actions to the menu items.

In Java, there is a three
-
level hierarchy for menus.

1.



A
menu bar

is attached to a window.

2.



The menu bar contains
menus
, rectangular panels with menu strings inside
them.

3.



Men
us contain
submenus

and
menu items.


Only menu items have actions associated with them.

Here are the instructions to build the menu for this program.

JMenuBar menuBar = new JMenuBar();

setJMenuBar(menuBar);


JMenu fontMenu = new JMenu("Font");

menuBar.a
dd(fontMenu);


JMenuItem largerItem = new JMenuItem("Larger");

fontMenu.add(largerItem);


JMenuItem smallerItem = new JMenuItem("Smaller");

fontMenu.add(smallerItem);

Start again with the
HelloFrame

class.

Create a method called
createMenu
, and place
these instructions inside this
method.

Run y
or. (a)

Run your program and check that the menu works. Don't forget to call
createMenu

from
within the constructor. (a) What is the code for your
HelloFrame

class? (b) W
hat
happens when you select a menu item?


6.

Next, you need to attach actions to the menu items. This process is
identical

to
attaching actions to buttons.

Attach the actions to the two menu items and run your program. Check that
selecting the menu items respectively increases and decreases the

font size.

What is the complete source code for your frame class now?

Combo Boxes

7.

In this exercise, you will see how to offer a user a selection among multiple
choices. You can place all choices into a
combo box
.



To build such a combo box, you add
items to it:

final JComboBox comboBox = new JComboBox();

comboBox.addItem("Small");

comboBox.addItem("Medium");

comboBox.addItem("Large");

comboBox.addItem("Extra Large");

Then place the combo box in the south end of the content pane.

add(comboBox, Bo
rderLayout.SOUTH);

Add a
createSouthPanel

method and paste the lines indicated above inside the
method. Try it out. Don't forget to call the
createSouthPanel

method from within
the constructor. Run the program and compare it against the figure.

The progra
m looks different. Why?



8.

The border layout grows all items to their maximum size. To avoid this, enclose the
combo box inside a panel, even though it is a single item.

JPanel southPanel = new JPanel();

southPanel.add(comboBox);

add(southPanel, Borde
rLayout.SOUTH);


Try it out. Add the combo box to the frame of the
HelloTester

program. Compile
and run the program. Pull down the item list of the combo box to see the items.

What happens if you select one of the items?



9.

To activate the combo box, yo
u need to attach an action listener. The
actionPerformed
method needs to determine which item the user selected.

class ComboListener implements ActionListener

{



public void actionPerformed(ActionEvent event)



{



String item = (String) comboBo
x.getSelectedItem();



. . .



}

}


You need to cast the return value of the
getSelectedItem

method to a
String

because it is possible to put other objects, such as icons, into a combo box.

Now you simply set the correct font size, depending on the

item string.

int messageSize = DEFAULT_SIZE;

if (item.equals("Small")) { messageSize = SMALL_SIZE; }

else if (item.equals("Medium")) { messageSize = MEDIUM_SIZE; }

else if (item.equals("Large")) { messageSize = LARGE_SIZE; }

else if (item.equals("Extr
a Large")) { messageSize =
EXTRA_LARGE_SIZE; }

label.setFont(new Font("serif", Font.PLAIN, messageSize));

label.repaint();


Define the size constants as follows:

public static final int SMALL_SIZE = 12;

public static final int MEDIUM_SIZE = 18;

public
static final int LARGE_SIZE = 24;

public static final int EXTRA_LARGE_SIZE = 36;

Add the combo action listener and try out your program. When you select an item
from the combo box, the message should be displayed in the appropriate size.

Provide the comp
lete source code for your
createSouthPanel

method.


Radio Buttons and Check Boxes

10.

Radio buttons are another way to offer the user a selection among multiple
alternatives.


These buttons are called radio buttons because they work like the channel but
tons
on a radio. When you select a radio button, the previously selected radio button is
turned off.

However, radio buttons take up more screen "real estate" than combo boxes and
therefore are best used when you need to offer users only a small selection.

Here are the instructions to construct two radio buttons:

helloButton = new JRadioButton("Hello");

goodbyeButton = new JRadioButton("Goodbye")

;


To get the "radio button" effect, where all other buttons are turned off when one of
the buttons is clicked,
you need to place the buttons in a button group:


ButtonGroup group = new ButtonGroup();

group.add(helloButton);

group.add(goodbyeButton);

Finally, you want to turn the first button on:


helloButton.setSelected(true);


The button group is a
logical

grou
ping of the buttons. You still need to physically
lay out the buttons on the screen:

JPanel southPanel = new JPanel();

southPanel.add(helloButton);

southPanel.add(goodbyeButton);

add(southPanel, BorderLayout.SOUTH);



Add the two radio buttons to the f
rame of the
HelloTester

program. Add a
createSouthPanel

method, just like you did in the previous problems. (a) What is the
code for your
HelloFrame

class? (b) Run the program. What happens when you click the
radio buttons?


11.

Now change the message text

when the user clicks a radio button. To assemble the
correct text, simply check which button is currently selected.

class MessageListener implements ActionListener

{



public void actionPerformed(ActionEvent event)



{



String message = "";




if (helloButton.isSelected()) message = "Hello";



else if (goodbyeButton.isSelected()) message = "Goodbye";



message = message + ", World!";



label.setText(message);



}

}

Note that you can attach the
same

action object to both bu
ttons.

Add the action listener and attach it to the radio buttons. Run the program. Observe
how clicking the buttons changes the message text.

What is the complete source code for your
HelloFrame
class?



12.

Check boxes are used to allow users to select
a single logical alternative, typically
of the form "do X" or "do nothing". Here, a check box is used to allow the user to
specify whether the word "cruel" should be added to the message text:


Follow these steps to add the check box to the program.

1. Co
nstruct the check box.

cruelCheckBox = new JCheckBox("Cruel");

2. Attach the
same

action listener to the check box and the radio buttons.

cruelCheckBox.addActionListener(listener);

3. Add the check box to the south panel.

southPanel.add(cruelCheckBox);

4.
In the
actionPerformed

method of the
MessageAction

class, check

if (cruelCheckBox.isSelected()) . . .

Run the program. Observe how clicking the check box changes the message text.

What is the complete source code for your
HelloFrame

class now?









Big

Java / Java Concepts Lab 15

Throwing Exceptions

1.

The
BankAccount

class has three methods with preconditions:

1. the constructor (initial balance must not be negative)

2. the
withdraw

method (withdrawal amount must not be greater than the balance)

3. th
e
deposit

method (deposit amount must not be negative)

Modify the code for the
BankAccount

class so that each of the three methods
throws an
IllegalArgumentException

if the precondition is violated.

What is the code for your modified
BankAccount

class?


2
.

Write a tester program that asks the user for:



the initial balance



an amount to deposit



an amount to withdraw

Have the program construct a
BankAccount

object and make the deposit and
withdrawal.

What is your tester program?


3.

What happens when you
make the following errors?

a.

Construct an account with a negative balance

b.

Withdraw more money than the account balance

c.

Enter an illegal input (such as "ten" instead of "10")


Custom Exceptions

4.

It is often a good idea to define your own exception class

rather than rely on a
predefined class.


Define a class
BankAccountException

that can be used by the
BankAccount

class.


Your exception should be an unchecked exception.


What is the code for your exception class?


5.

Why does it make sense to define the

BankAccountException

class as an
unchecked exception class?


6.

Now modify the
BankAccount

class to throw
BankAccountException

objects
instead of
IllegalArgumentException

objects.


What is the code for your modified class?



7.

If the
BankAccountExcepti
on

class had been a checked exception, then what
changes would you need to make


a.

in the definition of the exception class?

b.

in the
BankAccount

methods?

Propagating Checked Exceptions

8.

The
DateFormat

class of the standard Java library has a method

Date
parse(String source)

that converts a string such as

"2/27/05 8:00 AM"

into an object of type
Date
:

DateFormat formatter = DateFormat.getDateTimeInstance(


DateFormat.SHORT, DateFormat.SHORT);


String source = . . .;


Date d = formatter.parse(source);


If t
he source string is not in the correct format, then the
parse

method throws a
ParseException
, which is a checked exception.

In this exercise, you will implement a class
Appointment

that stores the date and
description of an appointment:

public class Appoin
tment

{



. . .



public String toString()



{





return "Appointment[date="









+ formatter.format(date)









+ "description=" + description + "; "









+ "]";



}




private Date date;



private String descriptio
n;



private static DateFormat formatter =
DateFormat.getDateTimeInstance(







DateFormat.SHORT, DateFormat.SHORT);

}


Supply a constructor

Appointment(String aDate, String aDescription)

that constructs an
Appointment

object from two strings. If t
he first string is not in a
legal format, your constructor should throw a
ParseException
.



9.

Now you will create a class
AppointmentBook

that keeps an array list of
appointments. Part of the class has been provided for you:


import java.util.ArrayList;

import java.text.ParseException;


public class AppointmentBook

{



public AppointmentBook()



{





book = new ArrayList<Appointment>();



}




. . .





public void addAll(ArrayList<Appointment> list)




{






book.addAll(list);




}





public int getNumAppointments()




{






return book.size();




}




public Appointment getAppointment(int i)




{






return book.get(i);




}





public String toString()




{






String out = "";






for (Appointment a : book)







{








out = out + a.toString() + "
\
n";






}






return out;




}




private ArrayList<Appointment> book;


}


Add a method

add
, that adds a new appointment to the book. The method should
not catch the
ParseException
, but propagate it to the

calling method.

public void add(String aDate, String aDescription)

Catching Exceptions

10.

Write a program
AppointmentBookTester

whose
main

method asks the user to
enter a series of appointments. Add the appointments into an appointment book
object. If a

parse error occurs in the add method, have the program instruct the user
to reenter the appointment.

Here is an outline of the
main

method, which does not yet catch any exceptions.

boolean done = false;

while (!done)

{




System.out.println("Next date
(or
-
1 when done):");




String input1 = in.nextLine();




if (input1.equals("
-
1"))




done = true;



else



{





System.out.println("Description:");






String input2 = in.nextLine();





book.add(input1, input2);



}

}

System.out.pri
ntln(book);

Add the appropriate
try/catch

block and test your program.

What is the complete code for your
AppointmentBookTester

class?


11.

The preceding program is tedious to use because the user must type all
appointment data on the command
-
line. Improv
e the method so that the data can be
stored in a file.

Put the data in a text file, say,
appts.txt
, and then provide this file name when the
programs asks you to.

Create an
AppointmentBookReader

class that will read appointments stored in a
file. Use the f
ollowing class:


import java.util.Scanner;

import java.text.ParseException;

import java.io.FileReader;

import java.io.IOException;


public class AppointmentBookReader

{



public AppointmentBookReader()



{





book = new AppointmentBook();



}




public AppointmentBook read(String filename)







throws IOException, ParseException



{





FileReader reader = new FileReader(filename);





Scanner in = new Scanner(reader);





while (in.hasNextLine())





{







String i
nput1 = in.nextLine();







String input2 = in.nextLine();







book.add(input1, input2);



}




reader.close();



return book;



}



private AppointmentBook book;

}


To read from an appointment book, use the following code:


Ap
pointmentBookReader bookReader = new AppointmentBookReader();


AppointmentBook book = bookReader.read(filename);


System.out.println(book);


Now change the
AppointmentBookTester

program so that it uses an
AppointmentBookReader
.

If there is any exception, d
escribe the nature of the exception in English. Print the
contents of the appointment book at the end of your program, whether or not there
is any exception.

What is the complete code for your
AppointmentBookTester

class?


12.

Give an example of an input
file that causes the
AppointmentBookReader

to throw
a
ParseException
.


13.

Give an example of an input file that causes the
AppointmentBookReader

to throw
an
NoSuchElementException
.

The finally Clause

14.

When there is an input error and the
AppointmentB
ookReader

class exits with an
exception, it should close its
FileReader
object by calling
reader.close()
.

Modify the
read

method of the
AppointmentBookReader

class to close
reader

in
a
finally

clause.

What is the code for your modified
read
method?
























Big Java / Java Concepts Lab 16

Reading and Writing Text Files

1.

Writ
e a program to store multiple memos in a file. Allow a user to enter a topic and
the text of a memo (the text of the memo is stored as a single line a
nd thus cannot
contain a return character). Store the topic, the date stamp of the memo, and the
memo message.

Use a text editor to view the contents of the "memo.dat" file and to check that the
information is stored correctly.

Creating a

java.util.Date

ob
ject with no arguments will initialize the
Date

object to the current time and date. A date stamp is obtained by calling the
Date.toString()

method.

Part of the code of the program has been provided for you:

import . . .
public class MemoPadCreator
{



public static void
main(String args[])


{




Date now;




Scanner console =
new Scanner(System.in);




System.out.print("Output file: ");




String filename = console.nextLine();




try




{






PrintWriter out = . . .;






boolean done = false;






while (!done)






{








System.out.println("Memo topic (enter
-
1 to end):");








String topic = console.nextLine();








if (topic.equals("
-
1"))










done = true;








else








{










System.out.println("Memo text:");










String message = console.nextLine();










/** Create the
new date object and obtain a dateStamp */










out.println(topic + "
\
n" + dateStamp
+ "
\
n" + message);








}






}






/** Close the output file*/


}




catch (. . .)




{






. . .




}

}
}
What is the code
for your program?


2.

Modify your memo writing program to read multiple memos s
tored in a text file.
Memos are stored in three lines. The first line is the memo topic, the second line is
the date stamp, and the third line is the memo message. Display the memos one at a
time, and allow the user to choose to view the next memo (if ther
e is one).

Part of the program code has been provided for you:


. . .



public class MemoPadReader

{



public static void main(String[] args)



{





Scanner console = new Scanner(System.in);





System.out.print("Input file: ");





String i
nputFileName = console.nextLine();






try





{







FileReader reader = . . .;







Scanner in = new Scanner(reader);








boolean done = false;







while (in.hasNextLine() && !done)







{









String topic = . .

.;









String dateStamp = . . .;









String message = . . .;









System.out.println(topic + "
\
n" + dateStamp + "
\
n" +
message);










if (. . .) // You should only ask to display the next
memo if there are more memos in t
he file









{











System.out.println("Do you want to read the next
memo (y/n)?");











String ans = console.nextLine();











if (ans.equalsIgnoreCase("n"))











done = true;









}







}








reader.close();





}





catch (. . .)





{







. . .





}



}

}


What is the code for your program?



3.

Modify your simple memo reader program. Use the
JFileChooser

to allow the
user to choose the file from which the me
mos will be read.

You can use the
showOpenDialog

method to enable the user to select a file to
"open". This method returns either
JFileChooser.APPROVE_OPTION
, if the user
has chosen a file, or
JFileChooser.CANCEL_OPTION
, if the user canceled the
selection
.

If a file was chosen, then you call the
getSelectedFile

method to obtain a
File

object that describes the file. Here is a complete example:

JFileChooser chooser = new JFileChooser();

FileReader in = null;

if (chooser.showOpenDialog(null) == JFileChoose
r.APPROVE_OPTION)

{



File selectedFile = chooser.getSelectedFile();



reader = new FileReader(selectedFile);



. . .

}


What is the code for your program?

File Encryption


4
.

Write a program that encrypts (or decrypts) a file using an XOR cipher.

An XOR
cipher encrypts a byte by performing an XOR bitwise operation with the byte to be
encrypted and a key. In Java, you can perform bitwise XOR using the
^

operator.

First, write a class
XorEncryptor

that encrypts or decrypts a file using an XOR
cipher
. Part of the code of the class has been provided for you:

import . . .


/**



An encryptor encrypts files using a "XOR" cipher.

*/

public class XorEncryptor

{



/**





Constructs an encryptor.





@param aKey the encryption key



*/



p
ublic XorEncryptor(byte aKey)



{





key = aKey;



}




/**





Encrypts the contents of a file.





@param inFile the input file





@param outFile the output file



*/



public void encryptFile(String inFile, String outFile)





throws . . .



{





InputStream in = null;





OutputStream out = null;






try





{







in = . . .;







out = . . .;







encryptStream(in, out);





}





finally





{







/** Close the files */





}




}




/**





Encrypts the contents of a stream.





@param in the input stream





@param out the output stream



*/



public void encryptStream(InputStream in, OutputStream out)





throws . . .



{





boolean done = false;






while (!done)





{







int next = . . .;







if (next ==
-
1) done = true;







else








{









byte b = (byte) next;









byte c = encrypt(b);









out.write(c);







}





}



}




/**





Encrypts a byte.





@param b the byte to encrypt





@return the encrypted byte



*/



public byte encrypt(byte b)



{





return . . .;



}




private byte key;

}


What is the complete code for the
XorEncryptor

class?



5.

Writ
e a tester program that asks the user for the name of a file to encrypt (or
decrypt) and the name of the output file, and encrypts (or decrypts) the file. Note
that both operations are performed in exactly the same way. Your program should
also ask the use
r for the encryption key to use.

What is the complete code for your tester class?


6.

Test your program. Create a file with the following content and encrypt it. Use "a"
as the encryption key.

This is a test for the XOR encryptor.

Open the encrypted file
with a text editor. What are the contents of the encrypted
file?


7.

Decrypt the file you just encrypted using your program. Don't forget to use "a" as
the decryption key. What is the output?

Command Line Arguments


8.

Modify your testing program so that

the input file, output file, and encryption key
are supplied on the command line.

What is the code for your modified tester class?


9.

What command do you use to run your program to encrypt the file
message.txt

into the file
message.enc
, using
a

as the

encryption key? What command do you
use to decrypt the
message.enc

file?

Random Access Files

10.

Create a program that reads and stores memos in a
RandomAccessFile
. To be able
to read and write memos in a random access file, the topic, date stamp and me
ssage
should each have a fixed size.

First, complete the code for the class
MemoPad
. You will write the tester program in
the next problem.


public class MemoPad

{



public MemoPad()



{





file = null;



}




public void open(String filename)
throws IOException



{





. . .



}




/**





Gets the number of memos in the file.





@return the number of memos



*/



public int size() throws IOException



{





. . .



}




public void close() throws IOException



{






if (file != null) file.close();





file = null;



}




/**





Reads a memo record. The values of the last record read can
be obtained





with the getTopic, getDateStamp and getMessage methods.





@param n the index of the memo in t
he file



*/



public void read(int n) throws IOException



{





file.seek(. . .);





byte[] topic = new byte[MAX_CHARS_TOPIC];





byte[] date = new byte[MAX_CHARS_DATE];





byte[] message = new byte[MAX_CHARS_MSG];





/* Read the

topic, date and message from the file (use the
method "read(byte[])".







Then, convert the byte arrays to strings and store them
in the variables currentTopic,







currentDateStamp and currentMessage





*/



}




public String getTopic
()



{





return currentTopic; // returns the topic of the last memo
read



}




public String getDateStamp()



{





return currentDateStamp;



public String getDateStamp()



{





return currentDateStamp;



}




public String

getMessage()



{





return currentMessage;



}




public void write(int n, String topic, String dateStamp, String
message) throws IOException



{





file.seek(. . .);






file.writeBytes(. . .); // "topic" should have a fixed size






file.writeBytes(. . .); // "dateStamp" should have a fixed size





file.writeBytes(. . .); // "message" should have a fixed size



}




/**





Adds white spaces to a string or cuts the string, so the string
has a length of exactly "size".





@param str the input string





@param size the desired size





@return the new string



*/



private String padOrTrim(String str, int size)



{





if (str.length() < size)





{







String pad = "";







for (int i = str.
length(); i < size; i++)







pad = pad + " ";







return str + pad;





}





else







return str.substring(0, size);



}




private RandomAccessFile file;



private String currentTopic;



private String currentDateStamp;



private String currentMessage;




public static final int MAX_CHARS_TOPIC = 25;



public static final int MAX_CHARS_DATE = 40;



public static final int MAX_CHARS_MSG = 250;



public static final int RECORD_SIZE = MAX_CHARS_TOPIC +
MAX_CHARS_DATE





+ MAX_CHARS_MSG;

}


What is the complete code for this class?


11.

Write a tester program that allows the user to add or display a memo. Memos
should be added to the end of the file. If the user wants to display a memo, a list of
the existing topics

should be provided, so that the user can select which one he/she
wants to see. Complete the following program:


public class MemoPadTester

{



public static void main(String args[])



{





Scanner console = new Scanner(System.in);





System.o
ut.println("Input file:");





String filename = console.nextLine();






try






{







MemoPad memoPad = new MemoPad();







memoPad.open(. . .);








boolean done = false;







while (!done)







{









System.out
.print("Enter A to add a memo, D to display
an existing memo or Q to quit: ");









String choice = console.nextLine();










if (choice.equalsIgnoreCase("A"))









{











System.out.println("Topic (max 25 characters):");












String topic = console.nextLine();











String dateStamp = (new Date()).toString();











System.out.println("Message (max 250
characters):");











String message = console.nextLine();











memoPad.w
rite(. . ., topic, dateStamp, message);









}









else if (choice.equalsIgnoreCase("D"))









{











if (memoPad.size() > 0)











{













// Display the list of topics













for (int
i = 0; i < . . .; i++)













{















memoPad.read(. . .);















System.out.println(i + " " + . . .);













}














System.out.println("Enter the number of the memo
to display:");














int n = Integer.parseInt(console.nextLine());













memoPad.read(. . .);













System.out.println(. . .); // topic













System.out.println(. . .); // date stamp













System.out.println(. .
.); // message











}











else











{













System.out.println("There are no memos in the
file.");










}









}









else if (choice.equalsIgnoreCase("Q"))











done = tr
ue;







}








memoPad.close();





}





catch (IOException exception)





{







System.out.println("Input/output error: " + exception);





}



}

}


What is the complete code for this class?

Object Streams

12.

Write a pr
ogram that writes memos to a file as object streams.The
Memo

class
should have fields to store a topic, a date stamp and the memo message. Allow a
user to enter multiple memos, supplying the topic and message. Your program
should provide the date stamp usi
ng the
java.util.Date
object. (Creating a
Date

object with no arguments initializes the object to the current time and date).

Let the user choose the file name to which the memos are saved.

What is the code for your
Memo

class? Remember to implement the
Se
rializable

interface!



13.

Provide a tester program for your
Memo

class. Allow a user to enter multiple
memos, supplying the topic and message. Your program should provide the date
stamp using the
java.util.Date
object (creating a
Date

object with no
arg
uments initializes the object to the current time and date). Let the user choose
the file name to which the memos are saved.


After saving the file, your tester program should open the file and print out all
memos.

You can modify the program you created i
n problem 1.

What is the code for your tester program?










Big Java / Java Concepts Lab 17

Discovering Classes


1.

You have been given an assignment to write a program to schedule classrooms in
your college's building. Classrooms can be reserved for
classes, seminars, guest
speakers, and for meetings for clubs (such as the Java Users Group). Clubs can
meet only after 5 p.m. and only if the room is not needed by any other group. Each
group needing a classroom is given a priority. Classes in your colleg
e have the
highest priority, with clubs having the lowest priority. Some classes in your college
have higher priority over others, especially if the instructor has tenure. Classrooms
are characterized by size, amenities (overhead projectors, etc.) and comf
ortable
seats. Groups can specifically request a room by room number or simply give a
description of what they need. Groups also may request specific time slots or give a
range of acceptable time slots. Rooms are not available after 10 p.m. and before 5
a.
m.

Make a list of classes that you may need for your program.


2.

From your list of potential classes in your classroom
-
scheduling program, pick
three potential classes and identify their main responsibilities.


3.

A CRC card describes a class, its respo
nsibilities, and its collaborating classes. You
already identified the classes and responsibilities in the classroom
-
scheduling
program. Now you will identify the collaborating classes.


From your list of potential classes in your classroom
-
scheduling prog
ram, pick
three potential classes and identify their collaborators.

Relationships Between Classes

4.

Using the list of classes for your classroom
-
scheduling program, identify any
objects that may use inheritance. List the superclasses (if any) followed by

their
subclasses. If you see no need for inheritance, explain.


5.

Inheritance can be described as an
"is
-
a"

relationship between classes. Aggregation
can be described as a
"has
-
a"

relationship. Identify any aggregation relations in the
classroom schedul
ing problem.


6.

Identify the relationships between classes i
n the following code:

class GroceryBag extends BasicContainer

{



private Fruit[] fruits;



private Vegetable[] veggies;


. . .




public void addItem(Food item)



{



. . .



}




public Money computeTotal()



{



. . .



}




. . .


}

Inheritance

From which classes does
GroceryBag

inherit
?

Aggregation

Identify any
aggregation

relationships (
has
-
a

relationship).

Dependency

On which classes does
GroceryBa
g

depend

(
uses

relationship)?

Case Study

7.

In this case study, you will develop a program to be used for ordering from an
online pizza restaurant. Allow the user the following choices: pizza size (small,
medium, or large), pizza crust type (thin or thick
), pizza toppings, as well as small,
medium, and large drinks. The user may select specific toppings. A user can order
multiple pizzas and drinks. The order should include the customer's delivery
address. After taking the order, print an order slip that sh
ows the order information
and total price.

Each topping has a price (for example, onions $1 and pepperoni $2). The price of a
pizza is the sum of the base price ($10 for a small pizza, $13 for a medium, and
$16 for a large pizza), plus the sum of the toppi
ng prices. The crust type doesn't
affect the price.

The price of a drink is the base price of the drink ($1 for soda, $2 for beer)
multiplied by a size factor (0.8 for small, 1.0 for medium, 1.2 for large drinks).

The delivery charge is $3. It is waived
for pickup orders.

What classes can you identify using the "nouns from the requirements
specification" technique?


8.

Not all of the classes are immediately visible in the requirements specification. In
our example, a pizza and a drink have common proper
ties:



the price



the description that is printed on the order slip

In fact, if the restaurant were to start serving other items (such as bread sticks or
sandwiches), those items also would have a price and a description.


Define a suitable class that cap
tures this commonality, and indicate how the classes
Pizza

and
Drink

are related to this class.


9.

What are the responsibilities and collaborators of the
Order

class?




10.

You probably decided you need a class
Topping
. What is the code for your
Toppin
g

class? If you think you do not need a class
Topping
, explain why.


11.

A very important class in your program is probably going to be called
Pizza
. What
is the code for your
Pizza

class. If you think you need superclasses and/or
subclasses of
Pizza
, sup
ply the code for these classes too.


12.

Supply the code for your
Drink

class.


13.

An order should contain a list of items (pizzas or drinks), so that the total price can
be calculated and the order can be formatted to be printed. Supply the code for yo
ur
Order

class here.


14.

Supply the code of any other classes you need to complete your program.





























Big Java / Java Concepts Lab 18

Recursion

1.

The factorial of a positive integer is the product of all nonnegative integers
less than or
equal to that number. (The symbol for factorial is "!"
-

the exclamation mark.)

The formula for factorials for all nonnegative integers is:

n! = n * (n
-
1)! for n > 0;

Zero factorial is a special case and 0! = 1

From this definition, 5! is 120
:
1! = 1 * 0! = 1
2! = 2 * 1! = 2
3! = 3 * 2! = 6
4!
= 4 * 3! = 24
5! = 5 * 4! = 120

Write a recursive factorial method

long factorial(int n)


2.

You can also compute the factorial values iteratively, without using recursion. Supply a
non
-
recursive f
actorial method.


3.

Write a tester class that tests both solutions.


4.

A formula for finding the greatest common divisor (GCD) of two numbers was
formulated by the mathematician Euclid around 300 BCE. The GCD of two
numbers is the largest number that
will divide into both numbers without any
remainder. For example, the GCD of 12 and 16 is 4, the GCD of 18 and 12 is 6.

The basic algorithm is as follows:

Let
a

be the larger,
b

the smaller number. (We will assume that both values are
positive.)

Let
c

be
the remainder that is obtained after dividing
a

through
b
.

If
c

is zero, then the GCD is
b
.

Otherwise, the GCD of
a

and
b

is the same as the GCD of
b

and
c
.

Recall that in Java, the
%

operator computes the remainder. For example, to
compute the GCD of 12

and 16, you compute

16 % 12 = 4

Now you need to compute the GCD of 12 and 4.

12 % 4 = 0

Therefore, the GCD is 4.

Write a recursive method that finds the GCD of two numbers using Euclid's
algorithm. (You may assume
--
or, better, assert
--
that both inputs ar
e positive.)


5.

Write a tester program for your GCD class.

Recursive Helper Methods

6.

Write a program to draw the logarithmic spiral recursively.

The logarithmic spiral or Fibonacci spiral is a shape often found in nature. The nautilus
shell is the cl
assic example of the logarithmic spiral. Seed patterns in sunflowers and
pinecones also demonstrate the spiral.


To construct a logarithmic spiral, begin with a "golden" rectangle. A golden rectangle
is a rectangle with sides of length A and φA, where φ (Phi) is the golden mean or the
value ( 1 + sqrt(5) ) / 2 or 1.61803 . . . A square with length equal to the smalle
st side
of the rectangle is drawn inside the rectangle. A quarter arc (
arcAngle = 90

degrees)
is drawn within this square. The remaining rectangle is also a golden rectangle. This
remaining rectangle is divided into a square and another smaller golden rect
angle. A
quarter arc connected to the previous arc is drawn inside the square, and the remaining
rectangle is once again subdivided with an arc drawn in it.

Create a panel in which to draw the spiral:

public class LogSpiralPanel extends JPanel



Given the
dimensions of the panel, draw the largest golden rectangle that will fit within
the panel. Part of the code of the class has been provided for you:

import java.awt.Graphics2D;

import java.awt.Graphics;

import java.awt.Rectangle;

import java.awt.geom.Arc
2D;

import javax.swing.JPanel;


public class LogSpiralPanel extends JPanel

{



public void paintComponent(Graphics g)



{

Graphics2D g2 = (Graphics2D) g;







/*









Your code goes here.



1. create the goldenRectangle (you can use
getHeight() to
obtain the side size





2. draw the golden rectangle







3. call the recursive helper method "recursiveDraw" which will
draw the spiral







*/



}




/**





Method that recursively draws a logarithmic spiral.





@
param x The upper left corner x
-
coordinate of the golden
rectangle







@param y The upper left corner y
-
coordinate of the golden
rectangle



@param side the smallest side size of the golden rectangle





@param angle the angle (0, 90, 180 or 270
) where the top of the
golden rectangle is located.







For the outermost golden rectangle, the angle is 90.

*/



private void recursiveDraw(Graphics2D g2, double x, double y,
double side, int angle)



{







// we'll do this in the next secti
on. . .



}







private static final double GOLDEN_MEAN = (1 + Math.sqrt(5)) / 2;

}


What is the code of your completed

paintComponent
method?



7.

Now you will complete the code of the recursive helper method
recursiveDraw
.
To recursively draw
the logarithmic spiral, you first need to draw the square. The
square's upper
-
left corner is at position (
x
,
y
), and its side size is
side
. Then you
need to draw the arc. You can use the method
drawArc
that has been provided for
you. Then you need to recur
sively call
recursiveDraw
to continue drawing the
spiral recursively. Before making the recursive call you need to calculate the new
side size, the new x
-
coordinate, the new y
-
coordinate and the new angle. Two
methods
calculateNewX
and
calculateNewY
have b
een provided for you. You
can use these methods to calculate the new x and y coordinate, but you need to
calculate the new side size and the new angle yourself. Remember that the new
side size is the difference between the sizes of the two sides of the cur
rent
rectangle. The new angle is given by rotating the current angle 90 degrees in clock
-
wise direction.


public class LogSpiralPanel extends JPanel

{



public void paintComponent(Graphics g)



{



. . .



}




/**





Method that recu
rsively draws a logarithmic spiral.





@param x The upper left corner x
-
coordinate of the golden
rectangle





@param y The upper left corder y
-
coordinate of the golden
rectangle





@param side the smallest side size of the golden rectangl
e





@param angle The angle (0, 90, 180 or 270) where the top
of the




current golden rectangle is located.





For the outermost golden rectangle, the angle is 90.



*/



private void recursiveDraw(Graphics2D g2, double x, double y
,
double side, int angle)



{





/* Recursion ending condition: when the side is very small
*/





. . .






/* Draw the current square and arc */





. . .






/* Continue drawing the spiral recursively: calculate the
new si
de size, calculate the new







(x, y) coordinates and the new angle. Then, call
"recursiveDraw" again. */





. . .



}



/**





Draws the arc of the current iteration.





@param x The upper
-
left corner x
-
coordinate of the s
quare





@param y The upper
-
left corner y
-
coordinate of the square





@param side The size of the side of the square (or the
arc's radius)





@param angle The angle (0, 90, 180 or 270) where the top
of the current golden rectangle is loca
ted.





For the outermost golden rectangle, the angle is 90.

*/



private void drawArc(Graphics2D g2, double x, double y, double
side, int angle)



{





double auxX = x;





double auxY = y;





if (angle == 0 || angle == 270)







auxX = x
-

side;





if (angle == 270 || angle == 180)







auxY = y
-

side;





Rectangle2D.Double boundingRectangle =

new Rectangle2D.Double(auxX, auxY, side * 2, side * 2);





Arc2D.Double arc = new Arc2D.Double(bou
ndingRectangle, angle,
90, Arc2D.OPEN);





g2.draw(arc);



}




private double calculateNewX(double x, double angle, double side,
double newSide)



{





if (angle == 0)







x = x + side
-

newSide;





else if (angle == 90
)







x = x + side;





else if (angle == 270)







x = x
-

newSide;





return x;



}




private double calculateNewY(double y, double angle, double side,
double newSide)



{





if (angle == 0)







y = y
+ side;





else if (angle == 180)







y = y
-

newSide;





else if (angle == 270)







y = y + side
-

newSide;





return y;



}




private static final double GOLDEN_MEAN = (1 + Math.sqrt(5)) / 2;

}


What is the cod
e of your

recursiveDraw
method?


8.

Create a
LogSpiralFrame

class that will contain the
LogSpiralPanel
. What is the
code of your class?


9.

What is the code of your viewer class?

The Efficiency of Recursion

10.

The golden mean, often referred to as the g
olden ratio, divine proportion, or the
golden number is represented by the Greek letter phi (φ). The value of the golden
mean is (1 + sqrt(5)) / 2 or 1.61803 . . . The golden mean is found in nature and art
and tends to yield the most "aesthetically pleasi
ng" arrangements of objects.

Fibonacci numbers are directly related to the golden mean. The definition of
Fibonacci numbers is

fib(1) = 1

fib(2) = 1

fib(n) = (fib(n
-
1) + fib(n
-
2))

After the first two numbers, each of which has the value of 1, the next numb
er in
the Fibonacci sequence is the sum of the two preceding numbers.

Computing ratios of Fibonacci numbers can approximate the value of the golden
mean. The value of.

g(n) = fib(n) / fib(n
-
1)

approximates the golden mean as n goes to infinity.


These val
ues can be computed recursively as well. Note that

g(n) = (fib(n
-

1) + fib(n
-

2)) / fib (n
-

1)

Simplify this expression so that
g(n)

is expressed in terms of
g(n
-

1)
.

(a) What is your recursive definition for
g(n)
?

(b) When does the recursion stop?


11.

Write a recursive method that computes the
n
th iteration of the golden ratio. What
is the code of your method?


12.

What is the tenth approximation of the golden ratio?

13.

As you learned in the textbook, the recursive computation of
fib(n)

can be
i
nefficient. Add trace statements to verify that your method for computing
g(n)

does not calculate each number more than once. What is the output of your trace
messages?

Mutual Recursion (and More Efficiency)

14.

In one variant of the game of Nim, two play
ers alternately take marbles from a pile.
In each move, a player chooses how many marbles to take. The player must take at
least one but at most half of the marbles. Then the other player takes a turn. The
player who is left with one marble loses.

Clearly
, if you start with two marbles, you are going to win. Take one marble, and
your opponent loses right away. We therefore say that 2 is a
winning position
.

In general, a winning position is a position in which you can force a win. That is,
there is at leas
t one move that leads to a losing position.

Conversely, a
losing position

is a position that is either an outright loss, or in which
every move leads to a winning position.

Following these definitions, write mutually recursive methods

public class NimCa
lculator

{



public boolean isWinningPosition(int n)



{



for (int i = 1; i <= n / 2; i++)



. . .



}



public boolean isLosingPosition(int n) { . . . }

}


What is the code for your methods?


15.

Run a test program.Which values between
2 and 100 are losing positions?


16.

Why does this recursion run so slowly? Add trace messages to find out.


17.

You can speed up the methods by remembering for which numbers you have
already determined the answers. Use an array to store known position
s.


public class NimCalculator

{



public boolean isWinningPosition(int n) { . . . }



public boolean isLosingPosition(int n) { . . . }



private int[] knownPositions = new int[10000];

}

Here,
knownPosition[i]

should be 1 if
i

is known to be a winning p
osition, and
-
1 if
i

is known to be a losing position, and 0 if it is not known to be either.

What is the code for your modified
isWinningPosition

and
isLosingPosition

methods?


18.

Now run your program again. Just print out the losing positions < 10000





























Big Java / Java Concepts Lab 19

Sorting

1.

Add a counter to the
MergeSorter

that counts how many times the algorithm
compares two array elements. Supply a method
getCounter

to retrieve the
counter. Then run the following exp
eriment. For
n

= 10000, 20000, . . ., 100000,
prepare an array with descending integer values
n
,
n

-

1
,

n

-

2, . . .,3, 2, 1. Pass it to
the
MergeSorter
, then print out how many comparisons were made.


What results did you get?


2.

Implement the bubble s
ort algorithm.

The bubble sort is so called because it compares adjacent items, "bubbling" the
smaller one up toward the beginning of the list. By comparing all pairs of adjacent
items starting at the end of the list, the smallest item is guaranteed to rea
ch the
beginning of the list at the end of the first pass.

The second pass begins again at the end of the list, ultimately placing the second
smallest item in the second position in the list. During the second pass, there is no
need to compare the first an
d second items, since the smallest element is
guaranteed to be in the first position.

Bubble sort takes at most

n
-
1 passes for a list of
n

items. During the first pass,
n
-
1
pairs need to be compared. During the second pass,
n
-
2 pairs need to be compared.
D
uring the ith pass,
n
-
i

pairs need to be compared. During the last pass,
n
-
(
n
-
1) or
one pair needs to be compared. If, during any pass, no two adjacent items need to
be interchanged, the list is in order and the sort can terminate. If it continues, no
furt
her interchanges will occur.

A variation on the bubble sort starts each pass at the beginning of the list,
interchanging items as needed so that a larger item "sinks" below a smaller item
following each comparison. Such a sort might be called a "rock sort,
" but this term
is not in common usage.

What is the code of your
BubbleSorter

class?


3.

Describe the efficiency of the bubble sort algorithm and represent it in Big
-
Oh
notation.


4.

A variation on the bubble sort starts each pass at the beginning of th
e list,
interchanging items as needed so that a larger item "sinks" below a smaller item
following each comparison. (Such a sort might be called a "rock sort," but this term
is not in common usage.)

Implement this variation. What is the code for your
sort

method?

Comparable

and
Comparator

5.

We want to sort a list of cities by name; however, if there are two cities with the
same name, then we use the state name as tie breaker. Implement a
City

class and
a constructor

public City(String name, String state)

Also supply a
toString

method
--
we will need it for testing.

Have your
City

object implement the
Comparable<City>

interface.

What is the code of your
City
class?


6.

Write a tester class that populates an
ArrayList<City>

with cities and then sorts
it. Wh
at is the code of your tester class? Remember that you can use
Collections.sort

to sort an array list.


7.

Now we want to sort lists of cities first by state name, then by city name,
without
modifying the implementation of the
City

class
. That means, you
cannot redefine
the
compareTo

method. Instead, define a
CityComparator

class that implements
the
Comparator
<City>

interface.

public class CityComparator implements Comparator<City>

{



public int compare(City a, City b)



{



. . .



}

}


What is the code of your
CityComparator

class?


8.

Write a tester class that populates an
ArrayList<City>

with cities and then sorts
it, using a
CityComparator
. What is the code of your tester class?



Binary Search

9.

We will implement a "guess your nam
e" game. The computer will guess your last
name:

This program tries to guess your last name, but you have to give
some hints.

Does your name come before "MYERSCOUGH" in the dictionary? (Y/N)
Y

Does your name come before "ISENBERG" in the dictionary? (Y/N)
Y

Does your name come before "DIMAGGIO" in the dictionary? (Y/N)
N

Does your name come before "GALLUP" in the dictionary? (Y/N)
N


. . .

Is your name "HORSTMANN"? (Y/N)
Y

Start out with a program that reads all names from the "last names" file at
http://www.census.gov/genealogy/names/
. Discard the statistical information. Sort
the names, using
Collections.sort
.


What is the code for reading all names and sorting them?



10.

At the beginning of the sea
rch,


set
low

to 0 and
high

to the maximum index of the
names list. Set
mid

to the average of
low

and
high
, then ask the user if the name
comes before the list entry at index
mid
. Depending on the answer, update
low

or
high
. If
low == high
, ask if the name

matches.

What is the code for your program?


11.

How many entries does your names list have? How many guesses does your
program need to make until it has guessed the user's name or given up?

















Big Java / Java Concepts Lab 20

Using Linked
Lists

1.

Using the "cursor in a word processor" notation from the book, indicate the
contents of the list and the iterator position after each step in the code below.

LinkedList<String> letters = new LinkedList<String>();

ListIterator<String> iterator =
letters.listIterator();

iterator.add("A");

iterator.add("C");

iterator.add("D");

iterator =
letters.listIterator();

iterator.next();

iterator.next();

iterator.remove();

Implementing Linked Lists

2.

The
LinkedList

class in the standard Java library

has a
get(int index)

method
that allows you to retrieve arbitrary list elements, just like you can in an
ArrayList
. In this lab, we want to analyze why this operation is inefficient.

Add a method

Object get(int index)

to the
LinkedList

implementation of C
hapter 20. What is the code for your
method?


3.

What does your method do if
index

is < 0 or >= the size of the linked list?


4.

Write a tester program that adds ten items to a
LinkedList

and then executes the
loop

for (int i = 0; i < list.size(); i++)



System.out.println(list.get(i));

What is the code for your tester program?


5.

How many times does the tester program look up the
next

value of a list node?
How many lookups would it make if there were 100 elements in the list?


6.

It is possible to o
ptimize the behavior of the
get

method in some common cases.
You can
cache

the last visited position and its
Node

pointer. Then you can start
from that position instead of the start of the linked list.

public Object get(int index)

{


Node current;


if

(lastVisitedPosition > 0 && lastVisitedPosition < index)


{


current = lastVisitedNode;


while (. . .)


current = current.next;


}


else


{


current = first;


while (. . .)


current = curren
t.next;


}


// remember for next time


lastVisitedPosition = index;


lastVisitedNode = current;


. . .

}

Finish the implementation of the method, and test it with the same tester program.

What is the code of the
get

method?


7.

With this
modification, how many times does the tester program look up the
next

value of a list node?


8.

This is an ingenious trick, but it has limitations. If another method mutates the
linked list, then the cached position may no longer be accurate, and the cac
he
should be invalidated by setting
lastVisitedPosition

to
-
1. Which methods of
the
LinkedList

and
LinkedListIterator

class need to be modified?


9.

The standard library does not use this trick in its implementation of
get
, but it uses
another optimizati
on: if
index

is >
size() / 2
, then the search starts at the end of
the list, not at the beginning. With that optimization, how many lookups of the
next

or
previous

value would the tester program make if it looped through a list
with 100 elements?

Abstract

and Concrete Data Types

10.

You are assigned to write a program to schedule appointments for a doctor's office.
An appointment is scheduled every half an hour from 8 a.m. to 4 p.m., with no
appointments between 12 p.m. and 1 p.m. Not all times may be sch
eduled, and
some times may be double
-
booked. You must be able to delete and add
appointments at anytime. Would you use a linked list or an array to store
appointments? Why?


11.

When performing a binary search, is it more appropriate to use an array or a
linked
list? Why?

Stacks and Queues

12.

A
deque

is a double
-
ended stack or a double
-
ended queue. Items can be inserted or
removed from either end. Write an implementation of a deque of strings using a
LinkedList
. No other type of element access should be

permitted (e.g. get element
at position
i
).

public class Deque

{



. . .




public void addFirst(Object str){ . . . }



public Object removeFirst(){ . . . }



public void addLast(Object str){ . . . }



public Object removeLast(){ . . . }





private LinkedList<Object> list;

}



13.

Suppose you make your previous deque implementation iterable by modifying it as
follows:


public class Deque implements Iterable<Object>


{



. . .




public Iterator<Object> iterator()




{




return
list.iterator();




}





private LinkedList<Object> list;


}


Now you can iterate through the deque using the "for each" loop. Adding this
functionality is very useful because it allows us to iterate through the elements of
the queue and print them.

Write a test program that adds elements to a deque and then prints them out with a
"for each" loop. What is your test program?


14.

The problem is that by returning an iterator, we would be allowing an illegal
operation to the deque: to be able remove an
element at an arbitrary position, by
calling
iterator.remove()
.

Write a tester program that shows that if you change your queue implementation as
suggested above, you can now remove an element in the middle of the list.


15.

You can solve the problem by d
isabling the
remove()

method for your
Deque

iterator You can do that by creating your own iterator class which just calls the
methods of the list iterator, except for the
remove

method. If another class calls the
iterator's
remove

method, the method should

not perform the remove operation;
instead, it should throw an
UnsupportedOperationException
.

Part of the code of the class has been provided for you:

public class Deque implements Iterable<Object>

{




public Iterator<Object> iterator() { return new De
queIterator(); }




private class DequeIterator implements Iterator<Object>



{





public DequeIterator() { . . . }





public boolean hasNext() { . . . }





public Object next() { . . . }