online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code    Language
import java.util.Scanner; public class Main { private static UseArrayList ual = new UseArrayList(); private static UseLinkedList ull = new UseLinkedList(); private static UseArrayDeque uad = new UseArrayDeque(); private static String[] arr = {"JustAnswer"}; public static void waitTimer(int waitTime) throws Exception { Thread.sleep(waitTime); } public static void selection(int choice) throws Exception { switch(choice){ case 1: ual.startProcess(); break; case 2: ull.startProcess(); break; case 3: uad.startProcess(); break; case 4: System.out.println("Thank you for utilizing our system. Have a wonderful day."); waitTimer(3000); break; default: System.out.println("You have entered an invalid selection. Try again."); waitTimer(3000); main(arr); break; } } public static void main(String[] args) throws Exception { Scanner console = new Scanner(System.in); System.out.println("Please chose the data structure in which you would like to store or remove " + "a movie instances."); //waitTimer(5000); System.out.println("1.) ArrayList \n2.) LinkedList \n3.) ArrayDeque \n4.) Exit"); int choice = console.nextInt(); //waitTimer(5000); selection(choice); System.out.println(ual.toString()); System.out.println("Hello World"); } }
/** * The add operation runs in amortized constant time, * that is, adding n elements requires O(n) time. All * of the other operations run in linear time (roughly speaking). * The constant factor is low compared to that for the LinkedList * implementation. The order of elements added to an ArrayList * are preserved. */ import java.util.ArrayList; import java.util.Scanner; import java.util.Iterator; public class UseArrayList { ArrayList<Movie> al = new ArrayList<>(); String[] arr = {"JustAnswer"}; Scanner console = new Scanner(System.in); public void addMovie(Movie movie){ al.add(movie); } public void startProcess() throws Exception { Scanner console1 = new Scanner(System.in); System.out.println("Let's begin the process of adding or removing a movie to " + "or from the ArrayList.\nYou will be presented with a " + "series of statements."); System.out.println("Would you like to add or remove a movie?\n" + "1.) Add\n2.) Remove"); int choice = console.nextInt(); switch(choice){ case 1: listCredentials(); break; case 2: System.out.println("Key in the title of the movie you would like to remove."); String title = console1.nextLine(); removeItem(title); break; default: System.out.println("You have entered an invalid selection. Try again"); startProcess(); break; } Main.main(arr); } public void listCredentials(){ Scanner console1 = new Scanner(System.in); System.out.println("Enter movie name"); String name = console1.nextLine(); System.out.println("Enter theatre name"); String theatre = console1.nextLine(); System.out.println("Enter location name"); String location = console1.nextLine(); System.out.println("Enter show time"); String showTime = console1.nextLine(); System.out.println("Enter total seats"); int seats = console1.nextInt(); Movie movie = new Movie(name, theatre, location, showTime, seats); addMovie(movie); } public void removeItem(String title) throws Exception { boolean isRemoved = false; Iterator<Movie> iterate = al.iterator(); while(iterate.hasNext()){ if(iterate.next().getTitle().equalsIgnoreCase(title)){ iterate.remove(); isRemoved = true; } } if(isRemoved){ System.out.println("Movie removed successfully"); Main.waitTimer(5000); }else{ System.out.println("No movie with the title of " + title + "was found."); Main.waitTimer(5000); startProcess(); } } public String toString(){ for(Movie movie : al){ return movie.toString(); } return " "; } }
/** * LinkedList is based on a doubly linked list. * Operations that index into the list will traverse the * list from the beginning or the end, whichever is closer * to the specified index. Has a variant called as a * singly-LinkedList, which is normally referred to as a * linked list. Common method calls (get(); add(); remove(); * etc.,) are actually N/2 operations and not even linear. */ import java.util.LinkedList; import java.util.Scanner; import java.util.Iterator; public class UseLinkedList { LinkedList<Movie> ll = new LinkedList<>(); String[] arr = {"JustAnswer"}; Scanner console = new Scanner(System.in); public void addMovie(Movie movie){ ll.add(movie); } public void startProcess() throws Exception { Scanner console1 = new Scanner(System.in); System.out.println("Let's begin the process of adding or removing a movie to\n" + "or from the LinkedList.\nYou will be presented with a\n" + "series of statements."); System.out.println("Would you like to add or remove a movie?\n" + "1.) Add\n2.) Remove"); int choice = console.nextInt(); switch(choice){ case 1: listCredentials(); break; case 2: System.out.println("Key in the title of the movie you would like to remove."); String title = console1.nextLine(); removeItem(title); break; default: System.out.println("You have entered an invalid selection. Try again"); startProcess(); break; } Main.main(arr); } public void listCredentials(){ Scanner console1 = new Scanner(System.in); System.out.println("Enter movie name"); String name = console1.nextLine(); System.out.println("Enter theatre name"); String theatre = console1.nextLine(); System.out.println("Enter location name"); String location = console1.nextLine(); System.out.println("Enter show time"); String showTime = console1.nextLine(); System.out.println("Enter total seats"); int seats = console1.nextInt(); Movie movie = new Movie(name, theatre, location, showTime, seats); addMovie(movie); } public void removeItem(String title) throws Exception { boolean isRemoved = false; Iterator<Movie> iterate = ll.iterator(); while(iterate.hasNext()){ if(iterate.next().getTitle().equalsIgnoreCase(title)){ iterate.remove(); isRemoved = true; } } if(isRemoved){ System.out.println("Movie removed successfully"); Main.waitTimer(5000); }else{ System.out.println("No movie with the title of " + title + "was found."); Main.waitTimer(5000); startProcess(); } } public String toString(){ for(Movie movie : ll){ return movie.toString(); } return " "; } }
/** * Models both FIFO & LIFO. Can be used as both * a Queue as well as a stack. Methods such as * remove(); shifts elements in O(n) time. Has * linear time complexity and it only gets bad * with a large number of elements because we * have more and more element shifts. */ import java.util.ArrayDeque; import java.util.Scanner; import java.util.Iterator; public class UseArrayDeque { ArrayDeque<Movie> ad = new ArrayDeque<>(); String[] arr = {"JustAnswer"}; Scanner console = new Scanner(System.in); public void addMovie(Movie movie){ ad.add(movie); } public void startProcess() throws Exception { Scanner console1 = new Scanner(System.in); System.out.println("Let's begin the process of adding or removing a movie to\n" + "or from the ArrayDeque.\nYou will be presented with a\n" + "series of statements."); System.out.println("Would you like to add or remove a movie?\n" + "1.) Add\n2.) Remove"); int choice = console.nextInt(); switch(choice){ case 1: listCredentials(); break; case 2: System.out.println("Key in the title of the movie you would like to remove."); String title = console1.nextLine(); removeItem(title); break; default: System.out.println("You have entered an invalid selection. Try again"); startProcess(); break; } Main.main(arr); } public void listCredentials(){ Scanner console1 = new Scanner(System.in); System.out.println("Enter movie name"); String name = console1.nextLine(); System.out.println("Enter theatre name"); String theatre = console1.nextLine(); System.out.println("Enter location name"); String location = console1.nextLine(); System.out.println("Enter show time"); String showTime = console1.nextLine(); System.out.println("Enter total seats"); int seats = console1.nextInt(); Movie movie = new Movie(name, theatre, location, showTime, seats); addMovie(movie); } public void removeItem(String title) throws Exception { boolean isRemoved = false; Iterator<Movie> iterate = ad.iterator(); while(iterate.hasNext()){ if(iterate.next().getTitle().equalsIgnoreCase(title)){ iterate.remove(); isRemoved = true; } } if(isRemoved){ System.out.println("Movie removed successfully"); Main.waitTimer(5000); }else{ System.out.println("No movie with the title of " + title + "was found."); Main.waitTimer(5000); startProcess(); } } public String toString(){ for(Movie movie : ad){ return movie.toString(); } return " "; } }
public class Movie { private String title; private String theatre; private String location; private String showTime; private int seats; private int purchased; private double price; public Movie(String title, String theatre, String location, String showTime, int seats) { this.title = title; this.theatre = theatre; this.location = location; this.showTime = showTime; this.seats = seats; purchased = seats; price = seats * 9.50; } public String getTitle() { return title; } public String getTheatre() { return theatre; } public String getLocation() { return location; } public String getShowTime() { return showTime; } public int getSeats() { return seats; } public double getPrice() { return price; } public int getPerchased(){ return purchased; } public void incrementPerchased(){ purchased++; } public void decrementSeats(){ seats--; } @Override public String toString(){ return "Movie details:\nMovie Name: " + title + "\nTheatre: " + theatre + "\nTheatre Location: " + location + "\nShowtime: " + showTime + "\nNumber of seats: " + seats + "\nPrice: " + price; } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue