Ask Java Expert


Home >> Java

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;

}

}

 

 

Java, Programming

  • Category:- Java
  • Reference No.:- M9454207

Have any Question?


Related Questions in Java

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 ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

Assignment game prototypeoverviewfor this assessment task

Assignment: Game Prototype Overview For this assessment task you are expected to construct a prototype level/area as a "proof of concept" for the game that you have designed in Assignment 1. The prototype should function ...

Assignment taskwrite a java console application that allows

Assignment task Write a java console application that allows the user to read, validate, store, display, sort and search data such as flight departure city (String), flight number (integer), flight distance (integer), fl ...

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 ...

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 ...

Fundamentals of operating systems and java

Fundamentals of Operating Systems and Java Programming Purpose of the assessment (with ULO Mapping) This assignment assesses the following Unit Learning Outcomes; students should be able to demonstrate their achievements ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Applied software engineering assignment 1 -learning

Applied Software Engineering Assignment 1 - Learning outcomes - 1. Understand the notion of software engineering and why it is important. 2. Analyse the risk factors associated with phases of the software development lif ...

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. ...

  • 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