Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are required, but not limited, to turn in the following source files: Assignment6.java (The Assignment6 class extends JApplet) Book.java Publication.java AddPanel.java - to be

You are required, but not limited, to turn in the following source files:

Assignment6.java (The Assignment6 class extends JApplet) Book.java Publication.java AddPanel.java - to be completed (it extends JPanel and contains ButtonListener nested class)

SelectPanel.java - to be completed (it extends JPanel and contains ButtonListener nested class)

You can download the above files and use them to complete this assignment. You might need to add more methods than the specified ones.

Skills to be Applied:

Swing/AWT, ArrayList (very similar to ArrayList class)

Classes may be needed: JApplet, JButton, JTextField, JTextArea, JLabel, Container, JPanel, JTabbedPane, JCheckBox, , and ActionListener. You may use other classes.

How to run an applet program:

-Create an html file, say "hw6.html" with the following content: --------------------------------------------------------

Assignment 6 Applet

 

------------------------------------------------------ -Compile your java program as usual. -In a console, type: appletviewer hw6.html

(instead of typing "java Assignment6"). -In the TextPad, choose Tool->Run Java Applet

-In the jGrasp, choose Run->Run as Applet.

To test to see if your machine is set to view an applet, please go to the following site:

Click on the following page:

https://courses.eas.asu.edu/cse205/current/assignments/assignment6/AppletExample.h tml

Program Description Suggested Class Diagram:

Write a Java program that constructs an Applet. Your program should provide labels and textfields to a user to enter information regarding books.

The Applet (JApplet) of your program should contain two tabs. The first tab is labeled "Book Addition" and the second tab is labeled "Book Selection".

(The size of the applet here is approximately 800 X 200). The section under the first tab should be divided into two parts:

The left part contains labels, textfields, and a button for a user to enter some book information. The right part shows "No Book" at the beginning (it is done using JTextArea).

A user can enter some book information, and push "Add a book" button.

Then the book information should appear on the right hand side panel (note that the format of the book information can be using toString() method of the Book class). A message "Book added" should also appear with red color at the top of the panel.

Error handling:

If a user forgets to enter some field and pushes "Add a book" button, show a message "Please enter all fields." with red color, and nothing should be added to the right hand side panel.

If a user enters non-numeric value for book version and book price and pushes "Add a book" button, show a message "Please enter a number for book version and price" with red color, and nothing should be added to the right hand side panel.

After entering several books, the applet will have the following appearance. Note that a scroll pane needs to be added to show multiple books.

Under the "Book Selection" tab, a user can choose books from. There should be a JPanel containing all books created by Book Addition panel and a user can choose books by clicking its check box.

The list of books in the Selection Panel should be exactly same as the list under "Book Addition" tab.

After choosing some books by checking some check boxes, it should show the total price of the chosen books in the JTextField below. This total purchase amount should be updated everytime a user check or uncheck each check box.

A user should be able to go back and forth between "Book Addition" tab and "Book Selection" tab, and these two panels need to have consistent information, i.e., the same list of books.

Class description

SelectPanel

SelectPanel class extends JPanel defined in javax.swing package. It should contain at least the following instance variable:

This class should have a constructor:

public SelectPanel(ArrayList bookList)

where the parameter "bookList" is passed from the Assignment6 class. The constructor layouts and organizes components in this panel. You will be adding more variables (components) than what is listed here, including a JLabel, a JTextField,and JCheckBoxs.

public void addCheckBox(Book book1)

Attribute name

Attribute type

Description

bookList

ArrayList

a list of Book objects.

This method create an object of JCheckBox using the toString of the Book object parameter. This JCheckBox object needs to be added to the panel to be displayed. An object of CheckBoxListener class needs to be added to this JCheckBox so that it listens when a user checks or un-checks. This method needs to be called from the actionPerformed of ButtonListener of the AddPanel class when a new Book object is added.

This class contains a nested class called CheckBoxListener class that implements ItemListener interface. Thus you need to define its itemStateChanged method that is supposed to verify which check boxes are checked and add the price of the corresponding books and display the total purchase amount to the JLabel using the dollar format (Please use NumberFormat class).

AddPanel

AddPanel extends JPanel defined in the javax.swing package. It should contain at least the following instance variable:

This class should have a constructor:

public AddPanel(ArrayList bookList, SelectPanel selectPanel)

where the parameter "bookList" is passed from the Assignment6 class and the second parameter is an object of SelectPanel. The constructor layouts and organizes components in this panel. You will be adding more variables (components) than what is listed here, including labels, textfields, a button, and a text area.

This class contains a nested class called ButtonListener class that implements ActionListener interface. Thus the ButtonListener needs to have a definition for actionPerformed method that adds some book information to the list and does error handling. See the UML class diagram for the parameter and return type of this method. In the actionPerformed, you need to extract the information from the textfields for book information. Then you can instantiate an object of the Book class using the information. You can use the toString( ) method of the Book object to display the information on the textarea on the right hand side and also add the Book object to the "bookList". This is also where addCheckBox( ) method of the

Attribute name

Attribute type

Description

bookList

ArrayList

a list of Book objects.

selectPanel

SelectPanel

an object of SelectPanel.

SelectPanel class needs to be called with its object "selectPanel". so that a new book is added to the AddPanel, the same one is added to the SelectPanel.

Assignment6 class

Assignment6 class extends JApplet defined in javax.swing package. It contains at least init() method (see UML diagram for its return type) to instantiate all instance variables and adds its components to itself. It also sets its size. It contains at least following instance variables:

Attribute name

Attribute type

Description

bookList

ArrayList

a list of Book objects. It will be used in both AddPanel and SelectPanel.

selectPanel

SelectPanel

an object of SelectPanel.

AddPanel

AddPanel

an object of AddPanel.

tPane

JTabbedPane

an object of JTabbedPane. It will contain AddPanel and selectPanel under each tab.

Grading Policy:

submit assignment on time

indicate assignment number, name, lecture number, and description of each class clearly

in each submitted java file

your program minimally has the following functionalities:

3 points: Appropriate components such as textfields, labels, etc. are shown under the "Book Addition" and "Book Selection" tabs. JScrollPane is added to display the entire information on each Panel.

1 point: When the "Add a book" button is pushed, the book information from textfields is added on the right panel in the correct order and the message of "Book added" shows up.

1 point: Error handing in case some field is not filled.

1 point: Error handing in case non-numerica value is entered for book version or

price.

1 point: The same list of books is shown under the "Book Addition" tab and under

the "Book Selection" tab.

1 point: When a user checks or unchecks each check box in SelectPanel, it

updates the total purchase amount in the textfield.

This is the code that I have as of right now.

import javax.swing.*; import java.util.*;

public class Assignment6 extends JApplet {

private int APPLET_WIDTH = 800, APPLET_HEIGHT = 300;

private JTabbedPane tPane; private AddPanel addPanel; private SelectPanel selectPanel; private ArrayList bookList;

//The method init initializes the Applet with a Pane with two tabs public void init() { //list of books to be used in every panel bookList = new ArrayList();

//customer selection panel uses the list of books selectPanel = new SelectPanel(bookList);

addPanel = new AddPanel(bookList, selectPanel);

//create a tabbed pane with two tabs tPane = new JTabbedPane(); tPane.addTab("Book Addition", addPanel); tPane.addTab("Book Selection", selectPanel);

getContentPane().add(tPane); setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size } }

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.ArrayList; import java.text.NumberFormat;

public class SelectPanel extends JPanel { private ArrayList bookList; private JPanel panelList; private ArrayList checkBoxes; private JTextField textField;

public SelectPanel(ArrayList bookList) { this.bookList = bookList; // orgranize components for purchase panel

} //end of constructor

/** This method add checkBox for a given book object to the **/ /** left panel. It also creates a check box listener and **/ /** add it to the checkbox **/ public void addCheckBox(Book book1) { //TO BE COMPLETED }

/** CheckBoxListener defines an action when a checkbox is **/ /** selected or un-selected. It computes the total purchase **/ /** amount based on the new selection **/ private class CheckBoxListener implements ItemListener { public void itemStateChanged(ItemEvent event) { // compute the total purchase amount when a check box is // checked or unchecked. } } }

import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*;

public class AddPanel extends JPanel { private ArrayList bookList; private JButton button1; private SelectPanel selectPanel;

//Constructor initializes components and organize them using certain layouts public AddPanel(ArrayList bookList, SelectPanel selectPanel) { this.bookList = bookList; this.selectPanel = selectPanel;

//organize components here button1 = new JButton("Add a book"); add(button1); }

//ButtonListener is a listener class that listens to //see if the button "Add a book" is pushed. //When the event occurs, it adds a book using the information //entered by a user. //It also performs error checking. private class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { //TO BE COMPLETED } //end of actionPerformed method } //end of ButtonListener class }

import java.text.NumberFormat;

public class Book { private String title; private String author; private Publication pub; /************************************************************************ Constructor method to initialize each variable. ************************************************************************/ public Book() { title = "?"; author = "?"; pub = new Publication(); }

/************************************************************************ Accessor method: This method returns the title of a book ************************************************************************/ public String getTitle() { return title; }

/************************************************************************ Accessor method: This method returns the author of a book ************************************************************************/ public String getAuthor() { return author; } /************************************************************************ Accessor method: This method returns the publication info of a book ************************************************************************/ public Publication getPublication() { return pub; } /************************************************************************ Mutator method: This method sets the title of a book ************************************************************************/ public void setTitle(String newTitle) { title = newTitle; }

/************************************************************************ Mutator method: This method sets the author of a book ************************************************************************/ public void setAuthor(String newAuthor) { author = newAuthor; }

/************************************************************************ Mutator method: This method sets the publication info of a book ************************************************************************/ public void setPublication(int newVersion, String newISBN, double newPrice) { pub.setVersion(newVersion); pub.setISBN(newISBN); pub.setPrice(newPrice); }

/************************************************************************ This method returns a String containing attribute(variable) values of a book. ************************************************************************/ public String toString() { String result = " Title: \t" + getTitle()+" " + "Author: \t"+getAuthor()+" " + pub.toString()+" ";

return result; } }//end of book class

import java.text.NumberFormat;

public class Publication { private String ISBN; private int version; private double price; /************************************************************************ Constructor is used to initialize instance variables. ************************************************************************/ public Publication() { ISBN = new String("?"); version = 1; price = 0.0; }

/************************************************************************ Accessor method: This method returns the price of a book. ************************************************************************/ public double getPrice() { return price; }

/************************************************************************ Accessor method: This method returns the version of a book. ************************************************************************/ public int getVersion() { return version; }

/************************************************************************ Accessor method: This method returns the ISBN of a book. ************************************************************************/ public String getISBN() { return ISBN; }

/************************************************************************ Modifier method: This method sets the price of a book. ************************************************************************/ public void setPrice(double newPrice) { price = newPrice; }

/************************************************************************ Modifier method: This method sets the new version of a book. ************************************************************************/ public void setVersion(int newVersion) { version = newVersion; }

/************************************************************************ Modifier method: This method sets the ISBN of a book. ************************************************************************/ public void setISBN(String newISBN) { ISBN = newISBN; }

/***************************************************************************** This method return a string containing the publication information of a book. *****************************************************************************/ public String toString() { String result; NumberFormat money = NumberFormat.getCurrencyInstance(); result = "Edition: \t" + version + " ISBN: \t" + ISBN + " Price: \t" + money.format(price); return result; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Next Generation Databases NoSQLand Big Data

Authors: Guy Harrison

1st Edition

1484213300, 978-1484213308

More Books

Students also viewed these Databases questions

Question

13. You always should try to make a good first impression.

Answered: 1 week ago