Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

//********************************************************************
// 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 );

}
}

Java, Programming

  • Category:- Java
  • Reference No.:- M91612245
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in Java

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Part a specification - robot simulationpart a

PART A Specification - Robot Simulation PART A Requirements To complete this assignment you will use the supplied eclipse project Robot P1/. It is already set up to execute a simple arm movement loop which you will build ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

Can someone help me please with those question1what is the

Can someone help me please with those question 1:what is the best data type for student id datatime,currency,number,decimal 2:which relationshipis preferable? one to one,one to many,many to many 3:if you add table A's pr ...

Can someone please help me with the following java

can someone please help me with the following java question The input is an N by N matrix of nonnegative integers. Each individual row is a decreasing sequence from left to right. Each individual column is a decreasing s ...

In relation to javaa what is constructor the purpose of

(In relation to Java) A. What is constructor? the purpose of default constructor? B. How do you get a copy of the object but not the reference of the object? C. What are static variables and instance variables? D. Compar ...

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Solving 2nd degree equationsbull write the following java

Solving 2nd degree equations • Write the following Java methods • boolean real-sols(double a, double b, double c): it returns true if the 2nd degree equation ax2 + bx + c has real solutions • double solution1(double a, d ...

Overviewyou are required to use java se 80 and javafx to

Overview You are required to use Java SE 8.0 and JavaFX to develop a Graphical User Interface (GUI) for the FlexiRent rental property management program created in Assignment 1. This assignment is designed to help you: 1 ...

Project descriptionwrite a java program to traverse a

Project Description: Write a java program to traverse a directory structure (DirWalker.java) of csv files that contain csv files with customer info. A simple sample in provided in with the sample code but you MUST will r ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As