Question

In: Computer Science

*In Java Please! Tasks This assignment has three parts: Create a LinkedList class. Create a BirdSpecies...

*In Java Please!

Tasks

This assignment has three parts:

  1. Create a LinkedList class.
  2. Create a BirdSpecies class.
  3. Print out the results after testing each of the methods in both of the classes and solving a simple problem with them.

Task 1 – LinkedList Class

Create a LinkedList class. It may be singly linked, circularly linked, or doubly linked. A singly linked list may be in either direction, forwards or backwards, while a doubly linked list must be in both directions, forwards and backwards. A circularly linked list will connect the end with the beginning, instead of pointing to null, but otherwise it is a singly linked list. We will need at least one private variable which represents the starting point of our list. The nodes for this list will need the typical private fields of next and data, but also count which will store the number of times a specific species of bird has appeared in a survey.

We must also create a constructor for our LinkedList. Let’s create a default constructor that creates a list of size zero.

Next is to create an Add method. This will add the given BirdSpecies object to the end of the list, if the species is not already in the list. If it is in the list, then the variable count of that species is increased by one (remember that count comes from the node, not the list or BirdSpecies class). Also keep track of the position where a new BirdSpecies was added into the list. This will be returned to the Main method.

Another method called GetCount that returns the count of a given BirdSpecies. If that species is not on the list, return zero.

Finally, a GetReport method that returns the name and count of each BirdSpecies on the list. If the list is empty, return a special message that says the list is empty.

Task 2 – BirdSpecies Class

Create a BirdSpecies class that has private variables for the name of the species. Use an overloaded constructor to create a BridSpecies with a given name. Also use a getter and setter that manipulate the name. Finally, let’s make a ToString method that returns the name of the species.

Task 3 – The Tester Class

            Create a Tester Class, with a Main method. In the Main method, create one LinkedList object. The values will be entered into the LinkedList using user input and a loop. The loop will keep accepting user input until they enter “done” when asked if they wish to enter another bird species. If the user enters a new bird species into the list, use the ToString method of the new BirdSpecies object to display its name along with the position that it now has in the list. If another bird of the same species is added to the list, display the ToString method of the original BirdSpecies object and its count (but not its position). Finally, print out the list using the GetReport method of the LinkedList.

∃ Some Sample Output:

Enter a bird species: Black-chinned hummingbird

Entered bird species: Black-chinned hummingbird at position 0.

Are you done entering birds? no

Enter a bird species: West African swallow

Entered bird species: West African swallow at position 1.

Are you done entering birds? no

Enter a bird species: Black-chinned hummingbird

Entered bird species: Black-chinned hummingbird, count increased to 2.

Are you done entering birds? done

Contents of the list:

Black-chinned hummingbird with count 2

West African swallow with count 1

You need to implement your sourcecode into the given template below:

//Template for Class BirdSpecies------------------------------------------------------------------

package com.company;
public class BirdSpecies
{
private String nameOfSpecies;
public BirdSpecies(String nameOfSpecies)
{
//Initialize the name of the bird.
}
public void SetName(String name)
{
//Change the name of the bird.
}
public String GetName()
{
//Return the name of the bird.
}
@Override
public String toString()
{
//Return the name of the bird.
}
}

// Template for Class LinkedList-------------------------------------------------------------------------------------

package com.company;

public class LinkedList
{
private Node first;

public LinkedList()
{
this.first = null;
}

public int Add(Type data)
{
//CASE 0: List is empty or first is the same species.


//CASE 1: List is not empty, search for species as we move to the end of the list,
// otherwise add at end.
}

public int GetCount(Type data)
{
//Search through the list looking for the value given.
//Otherwise return 0 if we reached the end.
}

public String GetReport()
{
//CASE 0: List is empty, return a special message.

//CASE 1: List is not empty, return the species and count of each bird.
//Hint, use the toString of the bird to do so.
}

//Simple Node Class
class Node
{
E data;
Node next;
int count;

public Node(E data)
{
this.data = data;
this.next = null;
this.count = 1;
}
}
}

//Template for Main Class (No change needed here)-------------------------------------------------------

package com.company;

public class Main
{
public static void main(String[] args)
{
LinkedList list = new LinkedList<>();

System.out.println("Entered the black chin at: " + list.Add(new BirdSpecies("Black-chinned hummingbird")));
System.out.println("Entered the African swallow at: " + list.Add(new BirdSpecies("West African Swallow")));
System.out.println("Entered the second black chin at: " + list.Add(new BirdSpecies("Black-chinned hummingbird")));
System.out.println("Entered the African swallow at: " + list.Add(new BirdSpecies("West African Swallow")));

System.out.println("Count of black chin is: " + list.GetCount(new BirdSpecies("Black-chinned hummingbird")) );

System.out.println(list.GetReport());
}
}

Solutions

Expert Solution

Below are screenshots of code and output. Your 'Main' class does not work as mentioned in question - it does not have a loop prompting user for new bird species. Thus your code did not produce the same output as in thevquestion. I did implement all the classes and functions as per the skeleton provided, in addition, I added the lines of code required to make it behave in the same way as mentioned in question and generate same output for the input as in the sample provided in question. However commented those lines using muliline comments(/* .... */) . You may uncomment those lines as per your need. Those lines are highlighted in code screenshots attached below:  

1.)LinkedList class(3 lines are highlighted)

2.)BirdSpecies class:

3.) Main class(there is a highlighted code snippet)

*)Output generated by given Main:

*) Output generated by replacing main function's implementation with the commented code snippet and uncommenting the 3 highlighted lines in LinkedList class(matches with sample output provided in question):

Below is the java code for the 3 classes:

// Template for Class LinkedList-------------------------------------------------------------------------------------

public class LinkedList<Type>

{

private Node first;

public LinkedList()

{

this.first = null;

}

public int Add(Type data)

{

//CASE 0: List is empty or first is the same species.

if(first==null){

first = new Node(data);

//for question's implementation of tester

/*System.out.print("Entered bird species: "+((BirdSpecies)(data))+" at position ");*/

return 0;

}

//CASE 1: List is not empty, search for species as we move to the end of the list,

// otherwise add at end.

Node temp = first;

while(temp!=null){

if(((BirdSpecies)(data)).GetName().equals(((BirdSpecies)(temp.data)).GetName())){

temp.count+=1;

//for question's implementation of tester

/*System.out.print("Entered bird species: "+((BirdSpecies)(data))+", count increased to ");*/

return temp.count;

}

temp=temp.next;

}

temp = first;

int index=1;

while(temp.next!=null){

temp=temp.next;

index++;

}

temp.next=new Node(data);

//for question's implementation of tester

/*System.out.print("Entered bird species: "+((BirdSpecies)(data))+" at position ");*/

return index;

}

public int GetCount(Type data)

{

//Search through the list looking for the value given.

//Otherwise return 0 if we reached the end.

Node temp = first;

while(temp!=null){

if(((BirdSpecies)(data)).GetName().equals(((BirdSpecies)(temp.data)).GetName())){

return temp.count;

}

temp=temp.next;

}

return 0;

}

public String GetReport()

{

//CASE 0: List is empty, return a special message.

if(first==null)

return "The list is empty.";

//CASE 1: List is not empty, return the species and count of each bird.

//Hint, use the toString of the bird to do so.

String report = "Contents of the list\n";

Node temp = first;

while(temp!=null){

report+=temp.data+" with count "+temp.count+'\n';

temp=temp.next;

}

return report;

}

//Simple Node Class

class Node<E>

{

E data;

Node next;

int count;

public Node(E data)

{

this.data = data;

this.next = null;

this.count = 1;

}

}

}

public class BirdSpecies

{

private String nameOfSpecies;

public BirdSpecies(String nameOfSpecies){

//Initialize the name of the bird.

this.nameOfSpecies = nameOfSpecies;

}

public void SetName(String name){

//Change the name of the bird.

this.nameOfSpecies=name;

}

public String GetName(){

return nameOfSpecies;

}

@Override

public String toString(){

return nameOfSpecies;

  

}

}

import java.util.Scanner;

//Template for Main Class (No change needed here)-------------------------------------------------------

public class Main

{

public static void main(String[] args)

{

LinkedList list = new LinkedList<BirdSpecies>();

System.out.println("Entered the black chin at: " + list.Add(new BirdSpecies("Black-chinned hummingbird")));

System.out.println("Entered the African swallow at: " + list.Add(new BirdSpecies("West African Swallow")));

System.out.println("Entered the second black chin at: " + list.Add(new BirdSpecies("Black-chinned hummingbird")));

System.out.println("Entered the African swallow at: " + list.Add(new BirdSpecies("West African Swallow")));

System.out.println("Count of black chin is: " + list.GetCount(new BirdSpecies("Black-chinned hummingbird")) );

System.out.println(list.GetReport());

//for question's implementation of Tester

/*

LinkedList List = new LinkedList<BirdSpecies>();

boolean done = false;

Scanner s =new Scanner(System.in);

while (!done){

System.out.print("Enter a bird species: ");

String name=s.nextLine();

System.out.println(List.Add(new BirdSpecies(name)));

System.out.print("Are you done entering birds? ");

done=s.nextLine().equals("done");

}

System.out.println(List.GetReport());

*/

  

}

}


Related Solutions

Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp
Laboratory Tasks public class LinkedList {              Node head;               class Node {                       int data;                     Node next;                     Node(int d) {                             data = d;                             next = null;                     }                 } } Complete the above java program by adding the following methods: Part1: isEmpty() checks if the linked list is empty or not. (return type is boolean) printList() prints all data in the linked list. (void method) insertFirst(int newData) add newData at the head of the linked list. (void method) insertLasL(int newData) add newData at...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin...
Java Project Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print...
Use LinkedList build-in class (java.util.LinkedList) to write a Java program that has: A. Method to print all elements of a linked list in order. B. Method to print all elements of a linked list in reverse order. C. Method to print all elements of a linked list in order starting from specific position. D. Method to join two linked lists into the first list in the parameters. E. Method to clone a linked list. The copy list has to be...
Assignment Tasks This assignment consists of three parts as follows (100 marks): Part 1(30 marks): Write...
Assignment Tasks This assignment consists of three parts as follows : Part 1: Write a report of 300 words that covers the following:  Fixed Cost  Variable Cost  Profit Part 2 : Solve the following questions by completing the required calculations:  A factory has fixed cost of RO 10,000 and produces 500 units of a product at a variable cost of RO 75 per unit. Calculate Total Cost of the factory  Calculate variable cost per unit...
java For this assignment, you will create a Time class that holds an hour value and...
java For this assignment, you will create a Time class that holds an hour value and a minute value to represent a time. We will be using "military time", so 12:01 AM is 0001 and 1 PM is 1300. For this assignment, you may assume valid military times range from 0000 to 2359. Valid standard times range from 12:00 AM to 11:59 PM. In previous assignments, we had a requirement that your class be named Main. In this assignment, the...
create a java class with name ModernArt: For the first part of the assignment, you will...
create a java class with name ModernArt: For the first part of the assignment, you will be extending the JApplet class and creating a work of modern using the Graphics class to draw various shapes to the applet window. As part of your masterpiece, you should incorporate the following elements: At least 1 non-solid rectangle At least 1 non-solid oval At least 1 solid oval At least 1 non-solid Polygon At least 1 solid Polygon At least 3 lines At...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE...
THE QUESTION IS OF JAVA LANGUAGE. ANSWER IS REQUIRED IN THREE PARTS (THREE JAVA FILES). PLEASE DIFFERENTIATE FILES SO I CAN UNDERSTAND BETTER. NOTE - Submission in parts. Parts required - Dog Class Code, Dog Manager Class Code and the main code. Please differentiate all three in the answer. This Assignment is designed to take you through the process of creating basic classes, aggregation and manipulating arrays of objects. Scenario: A dog shelter would like a simple system to keep...
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3....
Java File I/O Assignment: 1. Write a generic LinkedList class referencing page 2 and page 3. a. The class must be named LinkedList. b. The class must provide the methods listed above for constructing, accessing, and manipulating LinkedList objects for a generic type. c. Other than for testing purposes, the LinkedList class should do no input or output. d. The package must enable the provided tests. 2. Test this class using JUnit. a. A suite of JUnit tests have been...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT