Create a project using the classes in the doc sharing area

Assignment Help JAVA Programming
Reference no: EM13164088

create a project using the classes in the Doc Sharing area labeled A Simple LinkedList class. Compile it, run it, and review the code that is given carefully. This code tests the LinkedList class provided in the lecture.

Exercise 2: Implementing a Linked List

Back to Top

Modify the class LinkedList given in the lecture by adding the functions listed below for Exercise 2. In each case, the appropriate error message should be generated if an invalid condition occurs. For example, an error message should be generated when trying to replace the item at a given location in the list and the location is out of range. Create a main class to test your LinkedList class.

a. String toString(): Modify the display method to override the toString method of the object class. This method returns a string representation of the linked list elements.
b. int getLength(): Create this method to return the number of items in the list (accessor method).
c. void clear(): Create this method to remove all of the items from the list. After this operation, the length of the list is zero.
d. void addEnd(int item): Create this method to add the item to the end of the list.
e. void replace(int location, int item): Create this method to replace the item in the list at the position specified by location. The item should be replaced with the item.
f. int get(int location): Create a method that returns the element at location.

In the Doc Sharing document labeled Implementing a LinkedList class, you will find the basic framework of the LinkedList class you need to use to implement your solution.

Exercise 3: Using a Linked List

Back to Top

This exercise is similar to Exercise 3 in Lab 1 but uses the LinkedList class implemented in Exercise 2 above. That is, using the class LinkedList completed in the previous exercise, write a program to store the first 30 Fibonacci numbers in a LinkedList object.

Exercise 4: Implementing a Bag Class

Back to Top

Create a Bag class (multiset) that uses a linked list to store the bag items. The class should have the methods listed below. Create a main class to test your Bag class. This main class should fill a bag of integers with 10 random numbers, each in the interval [0, 15], and print how many times each integer in the interval [0, 15] appears in the bag.

a. Bag(): default constructor that creates an empty bag
b. boolean isEmpty(): determines whether the bag is empty
c. String toString(): returns a string representation of the linked list elements
d. int getLength(): returns the number of items in the bag
e. void clear(): removes all of the items from the bag
f. void add(int item): adds an item to the bag
g. void remove(int item): removes an item from the bag, all occurrences of item in the bag should be removed
h. int count(int item): counts the number of occurrences of item in the bag

(Note that you can reuse the code completed in Exercise 2 for the LinkedList class to create your Bag class. It will help you to save development time.)

 

 

/******************************

* Week 2 lab - exercise 1: *

* a simple LinkedList class *

*******************************/

 

/**

* Class implementing a linked list.

*/

public class LinkedList

{

 

private Node first; //dummy header node

 

/**

* Initializes the list to empty creating a dummy header node.

*/

public LinkedList()

{

first = new Node();

}

 

/**

* Determines whether the list is empty

*

* @return true if the list is empty, false otherwise

*/

public boolean isEmpty()

{

return (first.getNext() == null);

}

 

/**

* Prints the list elements.

*/

public void display()

{

Node current = first.getNext();

 

while (current != null)

{

System.out.print(current.getInfo() + " ");

current = current.getNext();

}

 

System.out.println();

}

 

/**

* Adds the element x to the beginning of the list.

*

* @param x element to be added to the list

*/

public void add(int x)

{

Node p = new Node();

 

p.setInfo(x);

p.setNext(first.getNext());

 

first.setNext(p);

}

 

/**

* Deletes an item from the list. Only the first occurrence of the item in

* the list will be removed.

*

* @param x element to be removed.

*/

public void remove(int x)

{

Node old = first.getNext(),

p = first;

 

//Finding the reference to the node before the one to be deleted

boolean found = false;

while (old != null && !found)

{

if (old.getInfo() == x)

{

found = true;

} else

{

p = old;

old = p.getNext();

}

}

 

//if x is in the list, remove it.

if (found)

{

p.setNext(old.getNext());

}

 

}

}

 

/******************************

* Week 2 lab - exercise 1: *

* a simple LinkedList class *

*******************************/

 

public class Main

{

public static void main(String args[])

{

LinkedList intList = new LinkedList();

 

System.out.print("List of numbers before list creation: ");

for (int i =0; i < 10; i++)

{

int info = (int)(Math.random()*10);

System.out.print(info + " ");

 

intList.add(info);

}

 

System.out.print("\nList of numbers after list creation: ");

 

intList.display();

}

}

 

/******************************

* Week 2 lab - exercise 1: *

* a simple LinkedList class *

*******************************/

 

/**

* Linked list node.

*/

public class Node

{

 

private int info; //element stored in this node

private Node next; //link to next node

 

/**

* Initializes this node setting info to 0 and next to null

*/

public Node()

{

info = 0;

next = null;

}

 

/**

* Sets the value for this node

*

* @param i the desired value for this node

*/

public void setInfo(int i)

{

info = i;

}

 

/**

* Sets the link to the next node

*

* @param l node reference

*/

public void setNext(Node l)

{

next = l;

}

 

/**

* Returns the value in this node

*

* @return the value in this node

*/

public int getInfo()

{

return info;

}

 

/**

* Returns the link to the next node

*

* @return link to the next node

*/

public Node getNext()

{

return next;

}

}

 

 

Reference no: EM13164088

Questions Cloud

Write a program that contains a main function : Write a program that contains a main function and three other functions that will return various attribute information about an array of floating point
Create a time trial program to compare the average execution : Create a time trial program to compare the average execution times of the Formula Node and the native LabVIEW Math Functions. This program will rquire a For Loop, a Flat Sequence structure, and a Case structure. The For Loop is required to run the ti..
Write a c program to compute dr if f1=50 lb and f2 = 4000 : make a program for:A hydraulic lift as shown below from fluid mechanics, it can be shown that a small force F1     acting over a piston of diameter D 1    can be multiplied into a large force   F 2    acting over a piston of diameter D 2  .
Create a project using the classes in the doc sharing area : create a project using the classes in the Doc Sharing area labeled A Simple LinkedList class. Compile it, run it, and review the code that is given carefully. This code tests the LinkedList class provided in the lecture.
Tenure of office act attempted : The Tenure of Office Act attempted to:
Write a method, to be included in a template unsorted list : Write a metho, to be included in a template unsorted list class, called replace_item, that will receive two xType parameters, one called olditem, the other called newitem.
Fourteenth amendment : Which statement about the Fourteenth Amendment is NOT true?
A string is valid windows filename. : writing a function in python that verifies whether a string is valid windows filename.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Cascading style sheet to a website

Compare and contrast the process of adding JavaScript and a Cascading Style Sheet to a Website. Determine if they can be used simultaneously in a page.

  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.

  Write java code to read integers from an input file

write java code to read integers from an input file and write only the odd numbers to an output file. the two file names will be provided on the command line as the input file followed by the output file.

  Compute the temperature in centigrade

Compute the temperature in Centigrade - Display the temperatures in both Centrigrade and Fahrenheit with appropriate labels, using the + operator to concatenate the labels with the variables

  Implement a shopping cart class with user interface

project will be to implement a shopping cart class with user interface (UI) that contains main() in Net Beans. The UI class will be used to perform user input/output and to invoke the appropriate methods of shopping cart class. When your program star..

  Presell a limited number of cinema tickets

Write an application to presell a limited number of cinema tickets. each buyer can buy as many as 4 tickets. No more than 100 tickets can be sold. Implement a program called TicketSeller that prompts the user for the desired number of tickets and the..

  What makes a program easy to modify

Describe the order of magnitude of the code section using Big(O) notation and Explain the relationship between dynamic storage allocation and recursion.

  Create java application which creates random phone number

Create and implement Java application which creates and prints a random phone number of the form xxx-xxx-xxxx. Include the dashes in the output.

  Write java program to print strings given at command line

Write a program Average.java which just prints strings that it is given at command line, one per line. If nothing is given at command line, print "No arguments".

  A mini game made in java using zen graphics

a mini game made in Java using Zen graphics .

  Create an abstract class named element

Part 1: Create an abstract class named Element that holds properties of elements, including their symbol, atomic number, and atomic weight. Include a constructor that requires values for all three properties and a get method for each value.

  Determine the type of moped

Write a driver class called MopedRental. This class should perform the following: asks the user to enter the size of the moped, the day of the week and the number of hours rented, creates the Moped object, based on the size, and displays the input..

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