Question

Please please help me with this code please Purpose: To learn the basics of linked lists...

Please please help me with this code please

Purpose: To learn the basics of linked lists

You are to implement 2 classes, Scene and Movie.

Note: Please dont use inbuild linkedList methods and functions

The Scene class has the following fields and methods: p

rivate String event

private Scene nextScene

Public Scene(String event) - A constructor that creates a Scene object with the event set and nextScene set to null

Public Scene(String event, Scene next) - A constructor that creates a Scene object with the event and the nextScene fields set

public Scene getNextScene() - returns the nextScene after the current one

public void setNextScene(Scene newScene) - changes the nextScene to the Scene passed in

public String getEvent() - returns the event from the current scene

Your Movie class has the following fields and methods:

public Scene script- that stores a linked list of Scenes

Public Movie() - A Movie constructor which initializes the script variable to null

public void addScene(String event) - A method that adds a new Scene to the end of the Movie’s script. If there are no Scenes in the script then the Scene added becomes the first Scene in the movie

public void addScene(String event, int pos) - A method that adds a new Scene to the Movie’s script at the position passed in, if there are not at least pos scenes in the movie return and don’t do anything EX: If i try to addScene(“asdf”,8) but there are only 3 scenes in the movie return nothing and don’t add the string Note: The linked list is indexed starting from 0 so we have 0,1,2,3,4...

public Scene removeScene() - A method that removes a Scene from the end of the Movie’s script, and returns the removed object. If there are no Scenes in the script then don’t do anything and return null

public Scene removeScene(int pos) - A method that removes the Scene at the passed in position from the Movie’s script, and returns the removed object. If there is not a Scene at pos then don’t do anything and return null EX: if I try removeScene(8) but there are only 3 scenes in the movie return null Note: The linked list is indexed starting from 0 so we have 0,1,2,3,4...

public String toString() - returns a string where each Scene’s event is separated by a comma and space. Ex: “event1, event2, event3”

You also need to create a Driver class with a main method.

In main:

Create a Movie object

Add Scenes until the Movie is: “Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Evil Defeated”,”Conclusion”

Print out the script of the Movie object using toString()

Remove the “Conclusion” of the Movie

Print out the script of the Movie object using toString()

Add a scene to the end of the movie called “Sequel”

Print out the script of the Movie object using toString()

Remove “Evil Defeated” from the Movie since the sequel retconned it

Print out the script of the Movie object using toString()

Add a Scene called “Prequel” to the beginning of the Movie

Print out the script of the Movie object using toString()

Example Output:

“Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Evil Defeated”,”Conclusion”

“Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Evil Defeated”

“Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Evil Defeated”,”Sequel”

“Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Sequel”

“Prequel”,“Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Sequel”

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

I wrote the whole code in a single class.

class Movie
{
   //Class variables for the Linked List
   private static Scene script;
   private static int numScenes;
  
   public static void main(String [] args)
   {
       System.out.println("/=/=/=/= TESTING /=/=/=/=");
       Movie mov = new Movie();

       //Add Scenes until the Movie is: “Introduction”,”Magic Man”,”Betrayal”,”Climax”,”Evil Defeated”,”Conclusion”
       String[] events = new String[]{"Introduction","Magic Man","Betrayal","Climax","Evil Defeated","Conclusion"};
       for(int i=0; i< events.length; i++)
       {
           mov.addScene(events[i]);
       }
       System.out.println(mov.toString());

       //Remove the “Conclusion” of the Movie
       mov.removeScene();
       System.out.println(mov.toString());

       // Add a scene to the end of the movie called “Sequel”
       mov.addScene("Sequel");
       System.out.println(mov.toString());

       // Remove “Evil Defeated” from the Movie since the sequel retconned it
       mov.removeScene(4);
       System.out.println(mov.toString());

       // Add a Scene called “Prequel” to the beginning of the Movie
       mov.addScene("Prequel", 0);
       System.out.println(mov.toString());
   }
  
   public Movie()
   {
       script = null;
       numScenes = 0;
   }

   public void addScene(String event)
   {
       Scene newScene = new Scene(event);
       if(numScenes == 0)
       {
           script = newScene;
       }
       else
       {
           Scene tempScene = script;
           while(tempScene.nextScene != null)
           {
               tempScene = tempScene.nextScene;
           }
           tempScene.nextScene = newScene;
       }
       numScenes++;
   }
  
   public void addScene(String event, int pos)
   {
       if(numScenes >= pos && pos >= 0)
       {
           Scene newScene = new Scene(event);
           if(numScenes == 0)
           {
               script = newScene;
           }
           else
           {
               Scene tempScene = script;
               if(pos == 0)
               {
                   newScene.nextScene = script;
                   script = newScene;
               }
               else
               {
                   if(pos == numScenes)
                   {
                       // numScenes will be once incremented by addScene(String event) too
                       // So to normalize we decrease it once here
                       addScene(event);
                       numScenes--;
                   }
                   else
                   {
                       int prevPos = pos-1;
                       while(prevPos > 0)
                       {
                           tempScene = tempScene.nextScene;
                           prevPos--;
                       }
                       Scene prevScene = tempScene;
                       Scene nextScene = tempScene.nextScene;
                       prevScene.nextScene = newScene;
                       newScene.nextScene = nextScene;
                   }
               }
           }
           numScenes++;
       }
       else
       {
           System.out.println("Given position should be non-negative and less than or equal to number of scenes.");
       }
   }
  
   public Scene removeScene()
   {
       Scene removedEvent = null;
       if(numScenes > 0)
       {
           if(numScenes == 1)
           {
               removedEvent = script;
               script = null;
           }
           else
           {
               Scene tempScene = script;
               while(tempScene.nextScene.nextScene != null)
               {
                   tempScene = tempScene.nextScene;
               }
               removedEvent = tempScene.nextScene;
               tempScene.nextScene = null;
           }

           numScenes--;
       }
       else
       {
           System.out.println("No scenes to remove");
       }
       return removedEvent;
   }

   public Scene removeScene(int pos)
   {
       Scene removedEvent = null;
       if(numScenes > pos && pos >= 0)
       {
           if(numScenes > 0)
           {
               if(pos == 1)
               {
                   removedEvent = script;
                   script = null;
               }
               else
               {
                   if(pos == numScenes-1)
                   {
                       // numScenes will be once decremented by removeScene() too
                       // So to normalize we increasing it once here
                       removeScene();
                       numScenes++;
                   }
                   else
                   {
                       Scene tempScene = script;
                       int prevPos = pos-1;
                       while(prevPos > 0)
                       {
                           tempScene = tempScene.nextScene;
                           prevPos--;
                       }
                       removedEvent = tempScene.nextScene;
                       // System.out.println("Removing: "+ removedEvent.event);
                       tempScene.nextScene = tempScene.nextScene.nextScene;
                   }
               }

               numScenes--;
           }
           else
           {
               System.out.println("No scenes to remove");
           }
       }
       else
       {
           System.out.println("Given position should be non-negative and less than number of scenes. (If there are 5 scenes, then pos should be between 0 to 4.");
       }
       return removedEvent;
   }
  
   @Override
public String toString() {
       Scene tempScene = script;
       String str = "";
       while(tempScene != null)
       {
           str += "\""+tempScene.event+"\"";
           tempScene = tempScene.nextScene;
           if(tempScene != null)
           {
               str+=",";
           }
       }
       return str;
   }
  
   class Scene
   {
       //Declare class variables
       private Scene nextScene;
       private String event;
      
       public Scene(String e)
       {
           event = e;
           nextScene = null;
       }

       public Scene(String e, Scene next)
       {
           event = e;
           nextScene = next;
       }
      
       public Object getNextScene()
       {
           return nextScene;
       }

       public void setNextScene(Scene newScene)
       {
           nextScene = newScene;
       }

       public String getEvent()
       {
           return event;
       }
   }
}

Add a comment
Know the answer?
Add Answer to:
Please please help me with this code please Purpose: To learn the basics of linked lists...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Needs Help with Java Programming language! Lights Camera Action Purpose: To learn the basics of linked...

    Needs Help with Java Programming language! Lights Camera Action Purpose: To learn the basics of linked lists You are to implement 2 classes, Scene and Movie. The Scene class has the following fields and methods: private String event private Scene nextScene Public Scene(String event) - A constructor that creates a Scene object with the event set and nextScene set to null Public Scene(String event, Scene next) - A constructor that creates a Scene object with the event and the nextScene...

  • CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to...

    CSBP 319 Data structures - Linked Lists - USE JAVA (NetBeans) A company would like to implement its inventory of computing machines as a linked list, called ComputerList. Write a Computer node class, called ComputerNode, to hold the following information about a Computer: • code (as a String) • brand (as a String) • model (as a String) • price (as double) • quantity (as int) ComputerNode should have constructors and methods (getters, setters, and toString()) to manage the above...

  • Hello, Could you please help me code this program in Java? It is important that all...

    Hello, Could you please help me code this program in Java? It is important that all rules are carefully followed. Using the PairOfDice class from PP 4.9, design and implement a class to play a game called Pig. In this game, the user competes against the computer. On each turn, the current player rolls a pair of dice and accumulates points. The goal is to reach 100 points before your opponent does. If, on any turn, the player rolls a...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

  • Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer)...

    Using Doubly Linked List, and Sorting methods: (In Java) (please attach your output with the answer) (Please answer if it is correct and working) (Have been getting many wrong and spam answers lately) Introduction: In this project, we use the same file structure of Artist and Art to extend the concepts of array and linked list and have them applied to sorting. The input files of p1arts.txt and p1artists.txt have been slightly modified. They are named p7arts.txt and p7artists.txt. Assignments:...

  • Please provide original Answer, I can not turn in the same as my classmate. thanks In...

    Please provide original Answer, I can not turn in the same as my classmate. thanks In this homework, you will implement a single linked list to store a list of computer science textbooks. Every book has a title, author, and an ISBN number. You will create 2 classes: Textbook and Library. Textbook class should have all above attributes and also a “next” pointer. Textbook Type Attribute String title String author String ISBN Textbook* next Textbook Type Attribute String title String...

  • please help!!!! JAVA I done the project expect one part but I still give you all...

    please help!!!! JAVA I done the project expect one part but I still give you all the detail that you needed... and I will post my code please help me fix the CreateGrid() part in main and make GUI works    List Type Data Structures Overview : You will be implementing my version of a linked list. This is a linked list which has possible sublists descending from each node. These sublists are used to group together all nodes which...

  • HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE...

    HELLO, PLEASE TAKE TIME TO ANSWER THIS QUESTION. PLESE ENSURE ITS CORRECT AND SHOW THAT THE PROGRAM RUNS. Task 1: Enforcing const-ness throughout Your first job will be to go through all of the code and decide which functions should be declared const. You should find several places throughout the program where this makes sense. We will also make the id data member in the Customer class const , as once a customer has been created their ID will never...

  • Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In...

    Using Merge Sort: (In Java) (Please screenshot or copy your output file in the answer) In this project, we combine the concepts of Recursion and Merge Sorting. Please note that the focus of this project is on Merging and don't forget the following constraint: Programming Steps: 1) Create a class called Art that implements Comparable interface. 2) Read part of the file and use Merge Sort to sort the array of Art and then write them to a file. 3)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT