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

  Tracer for java

Implementation of a tracer of Java objects and the tracer can be invoked from any point of a Java program, accepting an object as argument.

  Program that allows two players to play tic-tac-toe

Implement a program that allows two players to play tic-tac-toe. Draw the game grid and an indication of whose turn it is (X or O). Upon the next click, check that the mouse click falls into an empty location,

  Write a recursive method to reverse a string.

write a recursive method to reverse a string. Explain why you would not normally use recursion to solve this problem?

  Create a gui client side application that is a java version

q1. create a gui client side application that is a java version of notepad giving the ability to open modify and save

  Wrappershallow and wrapperdeep

Each class is simply a wrapper class to hold a private array variable. int [] a; The default constructor for each class should initialize â??aâ??. Each class should have a toString() and equals(). Each class should have a setArray method that allows ..

  Implement program that prompts for number of students

A group of students have been told to get into teams of a specific size for their class work. Design and implement a program that prompts for the number of students in the group and the size of the teams to be formed, and display how many teams ca..

  Create a splash screen for the tgc application

To complete your splash screen please use the information below: Create a splash screen for the TGC application that displays: the application's name.

  Program that decrypts the file produced by above program

Write another Java program that decrypts the file produced by above program. The decryption program should read the contents of the coded file, restore the data to its original state, and write it to another file.

  Methods

Assignment describe the basic working of methods. How to write a method in java and how to call a method.

  Write a script that reads a four-digit integer

Write a script that reads a four-digit integer entered by the user in a prompt dialog and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10.

  Benefits of using java as a programming language

What are the benefits of using Java as a programming language? Discuss how the use of an integrated development environment (IDE) such as Eclipse enhances programmer productivity

  Inheritancedemo

Create a New Project named InheritanceDemo . 2. Create a new class called Person and cut and paste the Person.java file from Blackboard into the class. Create a new class called Student and cut and paste the Student.java file from Blackboard into the..

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