This is for an assignment in my Java class, however, I don\'t seem to understand
ID: 3557037 • Letter: T
Question
This is for an assignment in my Java class, however, I don't seem to understand how it works.
that the teacher let us to Update TextArea with input entered in Textbox
here is the claim for my homework
PURPOSE:
Demonstrate your ability to create a Java applet; demonstrate your ability to use the principles of object-oriented design to create appropriate classes, methods, instance data; demonstrate your ability to use provided sample code in the development of your solution, demonstrate your understanding of list processing and exceptions.
BACKGROUND THOUGHT:
There is a predefined LinkedList class that comes with Java. It is in the java.util package. It normally makes sense to use this predefined class, since it was defined by experts, is well tested, and will save you a lot of work. However, it will not teach you how you can implement linked data structures in Java. To do that you need to build at least one linked data structure as an example. A linked list is both a simple and typical linked data structure.
INSTRUCTIONS / DESIGN:
Create an applet named MagazineView. The applet is a modification the MagazineRack program presented in Chapter 12. The modifications include adding delete and insert operations into the MagazineList class. The insert method will insert each magazine at the beginning of the list (this is a head insert). The deleteAll method will delete all magazines from the list. As this is an applet, you will need to replace the main() method with an init() and paint() method (you may find paint is optional).
Now construct a MagazineView applet that utilizes the Magazine and MagazineList classes from Chapter 12. Your applet must support the following operations:
1) It must provide an editable field of type TextField where a user can enter a magazine title to be added to the magazine list (use the getText() method).
2) It must allow the user to display all the magazines in a TextArea by pressing a print list Button.
Both TextField and TextArea objects use the method setText() to show text in their fields.
3) It must allow the user to clear (set to null) the contents of the magazine list by pressing a delete all Button.
4) Program should not delete an already empty list (if you do extra credit have program throw/catch an Exception if user tries to clear an empty list.)
5) I used the BorderLayout with a Panel containing a Label/Textfield/Button in North region and another panel with same components in South region. The TextArea object was added to Center region (I did not use East/West regions so TextArea object filled the space).
6) You must separate your implementation so that the Magazine and MagazineList classes are in separate file(s) from the file(s) that you use to implement your applet.
NOTE: The front-end GUI file has been provided for you named MagazineView.java
EXTRA CREDIT:
EXTRA CREDIT 1: If you create a separate class LinkedListException in a separate file derived from the Exception class. This will handle empty list problems. The exception error messages can show up in the TextArea.
EXTRA CREDIT 2: Provide a TextField where a use can enter a Magazine to be deleted from the Magazine list. The program will search for that magazine title and remove it from the list.
CONSTRAINTS:
The thrust of this assignment is to develop your skill in designing and implementing list structures. So, you may NOT use any of Java's built-in support for lists, NOR any of the container classes such as Stack, Vector, etc.
You should detect inappropriate activity entered by the user such as deleting an empty list. If you do the extra credit, your program should throw a LinkedListException in this case (you must derive this class from the preexisting Exception class). Your program should catch the Exception and display some kind of "error message" or "warning" to the user. You should use Exceptions to handle any/all other similar misbehavior by either the user or the program.
INPUT: Input for this assignment comes from user typing into text field object.
OUTPUT:
There is no paper output for this assignment. The existence of all the .class files in your public_html subdirectory , a link to your K8.html file, and the successful execution of your applet constitute both the output and execution portions of this assignment. Again, you must email me your Java source file(s) and have an active link on Student Pages in order to receive credit for this assignment.
Here is one example of output, please feel free to modify the GUI MagazineView.java file I provided arranging/adding components to you liking (NOTE: you do NOT need to have the 'Delete Magazine:' textfield UNLESS you do the Extra Credit). You just need the 'Delete All' button to clear the list.
and this is how it supposed to work: http://hills.ccsf.cc.ca.us/~cconner/Java/MagazineView/MagazineView.html
here is my try
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class MagazineView extends Applet
{
private Label addMagazine, deleteMagazine;
private TextField add, delete;
private TextArea show;
private Button list, deleteAll;
private String text;
private MagazineList rack = new MagazineList();
//----------------------------------------------------------------
// Creates a MagazineList object, adds several magazines to the
// list, then shows it in TextArea.
//----------------------------------------------------------------
public void init()
{
addMagazine = new Label("Add Magazine:");
deleteMagazine = new Label("Delete Magazine:");
add = new TextField(20);
add.addActionListener(new AddListener());
delete = new TextField(20);
delete.addActionListener(new DeleteListener());
show = new TextArea(20,50);
list = new Button("List Magazines");
list.addActionListener(new ListListener());
deleteAll = new Button("Delete All");
deleteAll.addActionListener(new DeleteAllListener());
add(addMagazine);
add(add);
add(list);
add(show);
add(deleteMagazine);
add(delete);
add(deleteAll);
setBackground(Color.white);
setSize(400, 400);
}
//----------------------------------------------------------------
// Adds magazine that you type in the "Add Magazine" text field
// to the list.
//----------------------------------------------------------------
private class AddListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text = add.getText();
add.setText("");
rack.insert(new Magazine(text));
}
}
//----------------------------------------------------------------
// Deletes individual magazine that you type in the "Delete Magazine"
// text field from the list, and shows the error message if cannot
// find the magazine.
//----------------------------------------------------------------
private class DeleteListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text = delete.getText();
delete.setText("");
rack.delete(new Magazine(text));
}
}
//----------------------------------------------------------------
// Refresh the list when click the "List Magazines" button.
//----------------------------------------------------------------
private class ListListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
show.setText(rack.toString());
}
}
//----------------------------------------------------------------
// Calls method to delete all the list when clicks the "Delete All"
// button and catch the error if list is already null.
//----------------------------------------------------------------
private class DeleteAllListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
rack.deleteAll();
show.setText(rack.toString());
}
}
}
and this is the MagazineList.java
public class MagazineList
{
private MagazineNode list;
private int listCount;
private MagazineNode head = null;
private MagazineNode next;
//----------------------------------------------------------------
// Sets up an initially empty list of magazines.
//----------------------------------------------------------------
public MagazineList()
{
list = null;
}
//----------------------------------------------------------------
// Creates a new MagazineNode object and adds it to the end of
// the linked list.
//----------------------------------------------------------------
public void add (Magazine mag)
{
MagazineNode node = new MagazineNode (mag);
MagazineNode current;
if (list == null)
list = node;
else
{
current = list;
while (current.next != null)
current = current.next;
current.next = node;
}
}
public
public void insert(Magazine mag) {
MagazineNode node = new MagazineNode(mag);
MagazineNode current;
if (list == null) {
list = node;
}
current = list;
while (current.next != null) {
current = current.next;
}
//set the new node to the Object x, next will be null.
current.next = node;
}
public void deleteAll ()
{
if(list == null){
//nothing to do
} else {
list = null;
}
}
//----------------------------------------------------------------
// Returns this list of magazines as a string.
//----------------------------------------------------------------
public String toString ()
{
String result = "";
MagazineNode current = list;
while (current != null)
{
result += current.magazine + " ";
current = current.next;
}
return result;
}
public void setNextLink( MagazineNode next )
{
this.next = next;
}
public MagazineNode getNextLink()
{
return next;
}
//*****************************************************************
// An inner class that represents a node in the magazine list.
// The public variables are accessed by the MagazineList class.
//*****************************************************************
private class MagazineNode
{
public Magazine magazine;
public MagazineNode next;
public MagazineNode prev;
//--------------------------------------------------------------
// Sets up the node
//--------------------------------------------------------------
public MagazineNode (Magazine mag)
{
magazine = mag;
next = null;
}
}
}
and that can show me the area but can not show up the name of the book
Explanation / Answer
import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MagazineView extends Applet
{
private Label addMagazine, deleteMagazine;
private TextField add, delete;
private TextArea show;
private Button list, deleteAll;
private String text;
private MagazineList rack = new MagazineList();
//----------------------------------------------------------------
// Creates a MagazineList object, adds several magazines to the
// list, then shows it in TextArea.
//----------------------------------------------------------------
public void init()
{
addMagazine = new Label("Add Magazine:");
deleteMagazine = new Label("Delete Magazine:");
add = new TextField(20);
add.addActionListener(new AddListener());
delete = new TextField(20);
delete.addActionListener(new DeleteListener());
show = new TextArea(20,50);
list = new Button("List Magazines");
list.addActionListener(new ListListener());
deleteAll = new Button("Delete All");
deleteAll.addActionListener(new DeleteAllListener());
add(addMagazine);
add(add);
add(list);
add(show);
add(deleteMagazine);
add(delete);
add(deleteAll);
setBackground(Color.white);
setSize(400, 400);
}
//----------------------------------------------------------------
// Adds magazine that you type in the "Add Magazine" text field
// to the list.
//----------------------------------------------------------------
private class AddListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text = add.getText();
add.setText("");
show.setText(text+": Added Successfully");
rack.add(new Magazine(text));
}
}
//----------------------------------------------------------------
// Deletes individual magazine that you type in the "Delete Magazine"
// text field from the list, and shows the error message if cannot
// find the magazine.
//----------------------------------------------------------------
private class DeleteListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
text = delete.getText();
delete.setText("");
Boolean isDeleted;
try {
rack.delete(new Magazine(text));
show.setText(text+" : Deleted Successfully");
} catch (LinkedListException e) {
show.setText(e.getMessage());
}
}
}
//----------------------------------------------------------------
// Refresh the list when click the "List Magazines" button.
//----------------------------------------------------------------
private class ListListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
show.setText(rack.toString());
}
}
//----------------------------------------------------------------
// Calls method to delete all the list when clicks the "Delete All"
// button and catch the error if list is already null.
//----------------------------------------------------------------
private class DeleteAllListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
show.setText("All deleted");
try {
rack.deleteAll();
} catch (LinkedListException e) {
show.setText(e.getMessage());
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.