Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java HW Please MUST use the design I give Modify the MagazineRack program presented in Chapter 12(13) by reading magazines from a file AND having

Java HW

Please MUST use the design I give

Modify the MagazineRack program presented in Chapter 12(13) by reading magazines from a file AND having the user enter magazines. Also, add delete, deleteAll and insert operations into the MagazineList class. Have theMagazine class implement the Comparable interface, and base the processing of the insert method on calls to thecompareTo method in the Magazine class that determines whether one Magazine title comes before another alphabetically. A) Here are changes you need to make to MagazineRack class: 1) Open and read titles from a text-file mags.dat inserting each into the list. If mags.dat does not exist tell the user. Use a try/catch block for this code. Use classes: File and Scanner

2) Use a loop to prompt and read titles from user input, using user input to create each Magazine (instead of hard-coded arguments) as long as the user wants to continue.

3) Open and write titles to the text-file mags.dat. Put this code in a try/catch block. Use PrintWriter class.

B) Here are the additions you will make to Chapter 12s MagazineList class:

1) Add an insert method/operation into the MagazineList class. Your insert method will insert each magazine lexigraphically into the list (after each insert the list is alphabetized so no need to sort). Base the processing of the insert method on calls to the compareTo method you will add to the Magazine class. Hint: Be careful when inserting at the beginning of the list (the head node).

Note: The add method already included in MagazineList adds magazines to the END of the list (the tail of the list). This method will not change.

2) Add a deleteAll() method that will delete all magazines from the list.

Hint: If the pointer to the beginning (head) of the list is null, the list is empty. This method only needs to be one line long.

3) Add a delete() method that will search for a magazine and remove it from the list. The method should not delete an already empty list. Hint: If user tries to clear an already empty list do nothing (use an if statement).

C) Here is the one change you need to make to Magazine class: Magazine class needs to implement Comparable interface and override compareTo method. The compareTo method determines whether one Magazine object's title comes before another lexicographically (ascii). This is the only change needed.

Note: You need to put the code for opening the external files into try/catch blocks.

Here's the souce code

public class Magazine { private String title; //----------------------------------------------------------------- // Sets up the new magazine with its title. //----------------------------------------------------------------- public Magazine (String newTitle) { title = newTitle; } //----------------------------------------------------------------- // Returns this magazine as a string. //----------------------------------------------------------------- public String toString () { return title; } } 
public class MagazineList { private MagazineNode list; //---------------------------------------------------------------- // Sets up an initially empty list of magazines. //---------------------------------------------------------------- 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; } } //---------------------------------------------------------------- // 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; } //***************************************************************** // 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; //-------------------------------------------------------------- // Sets up the node //-------------------------------------------------------------- public MagazineNode (Magazine mag) { magazine = mag; next = null; } } } 
public class MagazineRack { //---------------------------------------------------------------- // Creates a MagazineList object, adds several magazines to the // list, then prints it. //---------------------------------------------------------------- public static void main (String[] args) { MagazineList rack = new MagazineList(); rack.add (new Magazine("Time")); rack.add (new Magazine("Woodworking Today")); rack.add (new Magazine("Communications of the ACM")); rack.add (new Magazine("House and Garden")); rack.add (new Magazine("GQ")); System.out.println (rack); } } 

Input

Input comes from a text file (mags.dat) if it exists.

Input also comes from user input. Here is what session looks like first time I run the code (user input in bold ):

Sorry no magazines exist in mags.dat

Would you like to enter a title: (y/n): y Enter title: Woodworking Today Would you like to enter a title: (y/n): y Enter title: Time Would you like to enter a title: (y/n): n Here are your magazines: Time Woodworking Today

Enter a title to delete: Time

Here are your magazines: Woodworking Today

NOTE: SAME OUTPUT Woodworking Today WOULD ALSO BE IN mags.dat FILE

HERE IS SECOND RUN showing titles already saved in mags.dat:

Here are your magazines: Woodworking Today

Would you like to enter a title: (y/n): y Enter title: United Way Would you like to enter a title: (y/n): y Enter a title: Wired Would you like to enter a title: (y/n): n

Here are your magazines: United Way Wired Woodworking Today

Enter a title to delete: Wired

Here are your magazines: United Way Woodworking Today

NOTE: SAME OUTPUT United Way Woodworking Today WOULD ALSO BE IN mags.dat FILE

Sample Output

Output will be sent to the text-file mags.dat. Output will also be sent to standard output (the monitor). To see file contents on Unix/Linux use cat command: cat mags.dat or open with text editor of your choice.

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

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago