Question

In: Computer Science

B has to be matched with A so please I need both in Java. the previous...

B has to be matched with A so please I need both in Java.

the previous project mean A

A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array.

B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at index i.

Solutions

Expert Solution

/***************** ScoreSingly.java *****************/

public class ScoreSingly {
   Score head;
   Score tail;

   class Score {
       int data;
       Score next;

       Score(int data) {
           this.data = data;
           this.next = null;
       }
   }

   ScoreSingle() {
       head = null;
       tail = null;
   }

   public void add(int data) {
       Score newScore = new Score(data);
       if (head == null) {
           head = newScore;
           tail = newScore;
       } else {
           tail.next = newScore;
           tail = newScore;
       }
   }

   void remove(int position) {

       if (head == null)
           return;
       Score temp = head;
       if (position == 0) {
           head = temp.next;
           return;
       }
       for (int i = 0; temp != null && i < position - 1; i++)
           temp = temp.next;
       if (temp == null || temp.next == null)
           return;
       Score next = temp.next.next;
       temp.next = next;

   }

   public void display() {
       Score current = head;
       if (head == null) {
           System.out.println("List is empty");
           return;
       }
       System.out.println("Nodes of singly linked list: ");
       while (current != null) {
           System.out.print(current.data + " ");
           current = current.next;
       }
       System.out.println();
   }

}

/*************** ScoreDoubly.java *************/

public class ScoreDoubly {
   int ncount;
   Score head;
   Score tail;

   class Score {
       int item;
       Score previous;
       Score next;

       public Score(int item) {
           this.item = item;
       }
   }

   ScoreDouble() {
       ncount = 0;
       head = null;
       tail = null;
   }

   public void add(int item) {
       Score newScore = new Score(item);
       if (head == null) {
           head = tail = newScore;
           head.previous = null;
           tail.next = null;
       } else {
           tail.next = newScore;
           newScore.previous = tail;
           tail = newScore;
           tail.next = null;
       }
       ncount++;
   }

   void remove(int position) {
       if (head == null) {
           return;
       }
       if (ncount - position > position) {
           Score temp = head;
           if (position == 0) {
               head = temp.next;
               ncount--;
               return;
           }
           for (int i = 0; temp != null && i < position - 1; i++) {
               temp = temp.next;
           }
           if (temp == null || temp.next == null)
               return;
           Score next = temp.next.next;
           temp.next = next;
           ncount--;
       } else {
           if (position == ncount - 1) {
               tail = tail.previous;
               tail.next = null;
               ncount--;
               return;
           }
           Score temp = tail;
           for (int i = 0; i < position - 1 && temp != null; i++) {
               temp = temp.previous;
           }
           temp.next.previous = temp.previous;
           temp.previous.next = temp.next;
           ncount--;
       }
   }

   public void display() {
       Score current = head;
       if (head == null) {
           System.out.println("No Scores added yet.");
           return;
       }
       System.out.println("\n Scores are : ");
       while (current != null) {
           System.out.print(current.item + " ");
           current = current.next;
       }
   }
}


Related Solutions

I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B...
I NEED THE ANSWER FOR BOTH THE PARTS PLEASE Part (I) Year Investment A Investment B 0 -$5,000,000 -5,000,000 1 $1,500,000 $1,250,000 2 $1,500,000 $1,250,000 3 $1,500,000 $1,250,000 4 $1,500,000 $1,250,000 5 $1,500,000 $1,250,000 6 $1,500,000 $1,250,000 7 $2,000,000 $1,250,000 8 0 $1,600,000 Calculate Payback period, NPV, IRR and profitability Index for the two projects Part (II) You have been hired as a consultant for Pristine Urban-Tech Zither, Inc. (PUTZ), manufacturers of fine zithers. The market for zithers is growing...
So I need to make a java program that reverse or replace first or lastchar or...
So I need to make a java program that reverse or replace first or lastchar or remove char from user's string input. length, concat, charAt, substring, and equals (or equalsIgnoreCase) thses are the string method that only I can use for example if user put asdf asdf and chose reverse input should be fdsa fdsa if user put asdf asdf and chose replace first a with b input should be bsdf asdf if user put asdf asdf and chose remove...
Please I need this to be done in Java, can I have it answered by anonymous...
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java? Thank you You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention. Algorithm: Inputs: integers m and n , assumes m and n are >= 1 Order is not important for this algorithm Local variables integers: remainder Initialize: No initialization...
I need this in java on textpad. There are two files, both have instructions in them...
I need this in java on textpad. There are two files, both have instructions in them on what to add in the code. They are posted below. public class MyArrayForDouble { double[] nums; int numElements; public MyArrayForDouble() { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[5]; } public MyArrayForDouble(int capacity) { // Constructor. automatically called when creating an instance numElements = 0; nums = new double[capacity]; } public MyArrayForDouble(double[] nums1) { nums =...
In Java please. I put down my code and what I was able to achieve so...
In Java please. I put down my code and what I was able to achieve so far: public class Animal {   private String gender; //stores the gender of the animal    private String type; //stores the type of the animal(bear of fish)    private int strength; //stores the strength of the animal    public Animal() {        gender = "none";        type = "none";        strength = 0;    }        public Animal (String g, String...
I am in beginners java course so using the most simple/basic JAVA please complete the following...
I am in beginners java course so using the most simple/basic JAVA please complete the following CODE SEGMENTS. (2 parts) FIRST PART: Using ITERATIVE create a METHOD MAX that returns the max element in an ArrayList of ints and prints all of the elements. *you have to use an iterative in the method.* (just need to write the METHOD ONLY not a whole program(will do in 2nd part), assume you are given any numbers/integers. SECOND PART: Now write a class...
please I need to correct this essay in both grammar , structur and vocab. it is...
please I need to correct this essay in both grammar , structur and vocab. it is about the connecting between CTE and STEAM. Most countries and societies tend to focus on science, technology, engineering and math education, or STEM or STEAM. Where those thought to believe that it will help improve the results of the outputs of the four disciplines: science, technology, engineering and mathematics. STEAM / STEM is interested in international organizations seeking to develop their human resources in...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods...
I need this in Java please: Behaviors. In the context of OOP, functions are called methods or behaviors because they typically do something. Most often, they read or change the values of one or more variables in the class. For example, you may have a weight variable in a class, and a method called gainWeight( ) that increases the weight variable by a certain amount. For this part of the lab, create class KoalaBear that has a weight attribute (in...
I need this computer typed so I can read it and explained ASAP please!!! In Vienna,...
I need this computer typed so I can read it and explained ASAP please!!! In Vienna, Eliud Kipchoge became the first athlete to run a marathon in less than two hours. Kipchoge wore a special type of shoe developed by Nike called Vaporfly that is different than all previous running shoes. It has a much thicker midsole and has a layer of carbon fiber to bounce back as much energy as possible. Nike engineers claim that Vaporfly is significantly faster...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT