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

  Write a java program to read the numbers from the disk file

Write a Java program to read the numbers from the disk file and store them in an integer array ( not an ArrayList ).

  Java program to compare two variables if they are equal

Write down program which will ask user to initialize two character arrays, program must compare two variables if they are equal.

  Write a program in java that reads a file

Write a program in Java that reads a file of numbers of type int and outputs all the numbers to another file, but without there being any duplicate numbers.

  Write and run a java program

Write and run a Java program which allows the user to input two doubles and outputs the greater of the two?

  Create an array to hold four account objects

Write Mybank class in Mybank.java. Create an array to hold four Account objects. For each account, randomly set the balance in the range of $0-$1000.00; print out the balance of each account

  String as its parameter and returns the length

Write a method named longestWord that accepts a String as its parameter and returns the length of the longest word in the string.A word is a sequence of one or more non-space characters (any character other than the space character).

  Write a program that draws a picture of a house

Write a program that draws a picture of a house. It could be as simple as the accompanying figure, or if you like, make it more elaborate

  How do you figure out how to declare a variable

How do you figure out how to declare a variable? How do you know what type of variable to use? What is the best way to name a variable and why? What are the differences between an integer variable and a floating point variable? When and why do dat..

  Write a program that starts a player off with a bank of $15

Write a program that starts a player off with a bank of $15.00.

  You are to implement an intrusion detection system in java

you are to implement an intrusion detection system in java. we are assuming all activities are associated with the same

  How large a value can be stored in an integer variable

Most programming languages have a built-in integer data type. Normally this representation has a fixed size, thus placing a limit on how large a value can be stored in an integer variable

  Create a java class namedtextprocessorgui

Create a Java class namedTextProcessorGUI. Provide a main() function that creates and displays an instance ofTextProcessorGUI. I recommend you write this class as a subclass ofJFrame.

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