Creates a new magazinenode object and adds it to the end

Assignment Help JAVA Programming
Reference no: EM13941372

//********************************************************************
// Magazine.java Author: Lewis/Loftus
//
// Represents a single magazine.
//********************************************************************

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;
}
}

//*******************************************************************
// MagazineList.java Author: Lewis/Loftus
//
// Represents a collection of magazines.
//*******************************************************************

public class MagazineList
{
private MagazineNode list;

//----------------------------------------------------------------
// 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;
}
}

//----------------------------------------------------------------
// Returns this list of magazines as a string.
//----------------------------------------------------------------
public String toString ()
{
String result = "";

MagazineNode current = list;

while (current != null)
{
result += current.magazine + "\n";
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;
}
}
}

//*******************************************************************
// MagazineRack.java Author: Lewis/Loftus
//
// Driver to exercise the MagazineList collection.
//*******************************************************************

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);
}
}

Question: Modify the magazine rack program (from the lecture notes) by adding delete and insert operations into the magazine rack class. The insert method should be based on a compareTo() method in the Magazine class that determines if one magazine title comes before another alphabetically. In the main method, exercise various insertion and deletion operations. Print the magazine rack when complete. Use the code below for your main method:

//*******************************************************************
// MagazineRack.java Author: Lewis/Loftus
//
// Driver to exercise the MagazineList collection.
//*******************************************************************

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.insert (new Magazine("F Title") );
rack.insert (new Magazine("D Title"));
rack.insert (new Magazine("G Title"));
rack.insert (new Magazine("A Title"));
rack.insert (new Magazine("E Title"));
rack.insert (new Magazine("H Title"));

System.out.println( "After inserts:\n" + rack );

rack.delete (new Magazine("A Title"));
rack.delete (new Magazine("H Title"));
rack.delete (new Magazine("G Title"));
rack.delete (new Magazine("E Title"));

System.out.println( "After deletes:\n" + rack );

rack.insert (new Magazine("A Title"));
rack.insert (new Magazine("E Title"));
rack.insert (new Magazine("H Title"));
rack.insert (new Magazine("G Title"));

System.out.println( "After 2nd inserts:\n" + rack );

}
}

Reference no: EM13941372

Questions Cloud

Whittington theories of strategy : Q1- Whittington theories of strategy (Classical, Evolutionary, Processual, Systemic) Provide real life examples for each theory Q2- Disagree with the statement and justify including examples
Discuss small bowel obstruction : Discuss small bowel obstruction to include clinical presentation and physical exam findings, diagnostics, management, complications, indications for referral, and education
Business to business or business to consumer sectors : Select a small firm ‘START UP' of your choice in the business to business or business to consumer sectors' and present it as a case study in the form of a written report in the form of a business plan.
What were the reasons for the early voyages by portuguese : What were the reasons for the early voyages by the Portuguese and Spanish during the late fifteenth and sixteenth century? How were they conducted?
Creates a new magazinenode object and adds it to the end : The public variables are accessed by the MagazineList class.
Socially conscious business : A detailed review of one company claiming to operate ethically. Discuss the approach(es) they have used to become a more socially conscious business, linking this with the ethics and values of the company that you have identified.
What societal changes have affected the cost of health care : According to the Centers for Medicare and Medicaid Services (CMS) the cost of health care increases by approximately 4% each year, exceeding 3 trillion dollars in 2012. What societal changes have affected the cost of health care
Discuss the term rational goal management : Discuss the term rational goal management with reference to management theory. Choose an example of a large organization in the public or private sector and consider how rational goal management is relevant to running of the organization today.
What are the challenges for management : Create a 10- to 15-slide Microsoft PowerPoint presentation that addresses the following: Identify the purpose.  What are the challenges for management

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd