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 { String initialChoice = ""; do{ Scanner console = new Scanner(System.in); System.out.println("Would you like to choose a data structure?\nEnter \"Y\" for yes and \"N\" for no."); initialChoice = console.nextLine(); if(initialChoice.equalsIgnoreCase("Y")){ 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()); } }while(!(initialChoice.equalsIgnoreCase("N"))); if(ual != null){ System.out.println(ual.toString()); } if(ull != null){ System.out.println(ull.toString()); } if(uad != null){ System.out.println(uad.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.main */ 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: Scanner console2 = new Scanner(System.in); System.out.println("Would you like to enter your movie randomly, or you you like to see a selection?"); System.out.println("Enter a number value\n1.) Randomly\n2.) Selection"); int menuOrNot = console2.nextInt(); if(menuOrNot == 1){ listCredentials(); }else if(menuOrNot == 2){ Main1.main(arr); getMain1(); }else{ System.out.println("You have entered an invalid selection. Try again."); startProcess(); } 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; } } 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 getMain1(){ addMovie(Main1.getObject()); } 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: Scanner console2 = new Scanner(System.in); System.out.println("Would you like to enter your movie randomly, or you you like to see a selection?"); System.out.println("Enter a number value\n1.) Randomly\n2.) Selection"); int menuOrNot = console2.nextInt(); if(menuOrNot == 1){ listCredentials(); }else if(menuOrNot == 2){ Main1.main(arr); getMain1(); }else{ System.out.println("You have entered an invalid selection. Try again."); startProcess(); } 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; } } 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 getMain1(){ addMovie(Main1.getObject()); } 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: Scanner console2 = new Scanner(System.in); System.out.println("Would you like to enter your movie randomly, or you you like to see a selection?"); System.out.println("Enter a number value\n1.) Randomly\n2.) Selection"); int menuOrNot = console2.nextInt(); if(menuOrNot == 1){ listCredentials(); }else if(menuOrNot == 2){ Main1.main(arr); getMain1(); }else{ System.out.println("You have entered an invalid selection. Try again."); startProcess(); } 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; } } 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 getMain1(){ addMovie(Main1.getObject()); } 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(){ } 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; } }
import java.util.Scanner; public class Main1 extends Movie { private String genre; private String title; private String time; private double cost; private int theConfirmationNo; private int confirmationNo = (int)(Math.random() * 1000) + 135343; private static Scanner console = new Scanner(System.in); private static Main1 obj1 = new Main1(); public void setGenre(String genre){ this.genre = genre.toUpperCase(); } public void setTitle(String title){ this.title = title.toUpperCase(); } public void setTime(String time){ this.time = time; } public void setCost(double cost){ this.cost = cost; } public double getCost(){ return cost; } public String getTitle(){ return title; } public String getGenre(){ return genre; } public String getTime(){ return time; } public int getConfirmationNo(){ theConfirmationNo = confirmationNo; return theConfirmationNo; } @Override public String toString(){ String ticket = String.format("****************************************************************************" + "\nYour ticket purchase\nGenre: " + genre + "\nTitle: " + title + "\nTime: " + time + "\nPrice: " + "$%.2f" + "\nConfirmation Number: " + confirmationNo + "\n" + "****************************************************************************\n", cost); return ticket; } public static void sleepTimer(int timeToSleep){ try{ Thread.sleep(timeToSleep); }catch(InterruptedException e){ e.toString(); } } public static void showEnums(){ System.out.println("Movie Genres:"); for(Prac p : Prac.values()){ sleepTimer(1000); System.out.println(p + " "); sleepTimer(1000); } System.out.println(); for(Prac p : Prac.values()){ System.out.println(p + ": Movie Titles:"); sleepTimer(1000); System.out.println(p.printArr()); } for(Prac p : Prac.values()){ System.out.println(p + ": Movie Times:"); sleepTimer(1000); System.out.println(p.printTimes()); } } public static void initiate(Main1 obj) throws InterruptedException { System.out.println("Would you like to see our movie options?"); System.out.println("Key in \"Y\" for yes and \"N\" for no."); String answer = console.nextLine(); if(answer.equalsIgnoreCase("y")){ showEnums(); System.out.println(); keyTitle(obj); }//if selection equals no tie in the program initially made } public static void keyTitle(Main1 obj) throws InterruptedException { Scanner console = new Scanner(System.in); System.out.println("Type in a Genre:"); String genre = console.nextLine(); boolean isPresent = false; Prac pp = null; for(Prac p : Prac.values()){ if(genre.equalsIgnoreCase(p.name())){ pp = p; isPresent = true; break; } } if(isPresent){ obj.setGenre(genre); validateTitle(pp, obj); }else{ System.out.println("You have entered an invalid entry. Try again."); try{ Thread.sleep(1000); }catch(InterruptedException ie){ ie.toString();//ie.printStackTrace(); } keyTitle(obj); } } public static void validateTitle(Prac p, Main1 obj) throws InterruptedException { Scanner console = new Scanner(System.in); System.out.println("Type in a movie title:"); String title = console.nextLine(); boolean validator = false; validator = p.selectMovie(title); if(validator){ obj.setTitle(title); selectTime(p, obj); } if(validator){ }else{ System.out.println("There is no movie with this title. Try again."); validateTitle(p, obj); } } public static void selectTime(Prac p, Main1 obj) throws InterruptedException { System.out.println(p.printTimes()); Scanner console = new Scanner(System.in); System.out.println("There are six time slots. Please enter a number between 1 & 6 to select the time slot you would like."); int timeSlot = console.nextInt(); if(timeSlot > 0 && timeSlot <= 6){ obj.setTime(p.bookSlot(timeSlot)); selectNumberOfAttendees(p, obj); }else{ System.out.println("You have entered an invalid integer. Try again."); selectTime(p, obj); } } public static void selectNumberOfAttendees(Prac p, Main1 obj) throws InterruptedException { Scanner console = new Scanner(System.in); System.out.println("How many will be attending?"); int numberOfAttentees = console.nextInt(); obj.setCost(p.calculateCost(numberOfAttentees)); System.out.println("Your ticket purchase was successful and your confirmation number for your purchase is on your ticket"); sleepTimer(3000); System.out.print(obj.toString()); obj1 = obj; } public static Main1 getObject(){ return obj1; } public static void main(String[] args) throws InterruptedException { Main1 obj = new Main1(); initiate(obj); } }
public enum Prac { HORROR(){ String[] movies = {"Holloween", "Final Destination", "Resident Evil", "Thirty Days of Night", "The Wicked"}; String[] movieTime = {"2:00pm", "3:00pm", "5:00pm", "7:00pm", "9:00pm", "11:00pm"}; double price = 9.50; public String printArr(){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i] + " "); Main1.sleepTimer(1000); } return ""; } public String printTimes(){ for(int i = 0; i < movieTime.length; i++){ System.out.print(movieTime[i] + " "); Main1.sleepTimer(1000); } return ""; } public boolean selectMovie(String selection){ for(int i = 0; i < movies.length; i++){ if(((String)movies[i]).equalsIgnoreCase(selection)){ return true; } } return false; } public String bookSlot(int timeSlot){ return (String)movieTime[timeSlot - 1]; } public double calculateCost(int numberOfAttentees){ return (double)price * numberOfAttentees; } }, COMEDY(){ String[] movies = {"The Bucket List", "Def Comedy Jam", "Rescue From Gilligan's Island", "Easy Money", "The Hustle"}; String[] movieTime = {"2:30pm", "3:30pm", "5:30pm", "7:30pm", "9:30pm", "11:30pm"}; double price = 9.50; public String printArr(){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i] + " "); Main1.sleepTimer(1000); } return ""; } public String printTimes(){ for(int i = 0; i < movieTime.length; i++){ System.out.print(movieTime[i] + " "); Main1.sleepTimer(1000); } return ""; } public boolean selectMovie(String selection){ for(int i = 0; i < movies.length; i++){ if(((String)movies[i]).equalsIgnoreCase(selection)){ return true; } } return false; } public String bookSlot(int timeSlot){ return (String)movieTime[timeSlot - 1]; } public double calculateCost(int numberOfAttentees){ return (double)price * numberOfAttentees; } }, ROMANCE(){ String[] movies = {"Love Don't Cost a Thing", "Step Up All In", "The Blue Lagoon", "The Choice", "A Spare Room"}; String[] movieTime = {"12:10pm", "2:10pm", "4:10pm", "6:10pm", "8:10pm", "10:10pm"}; double price = 9.50; public String printArr(){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i] + " "); Main1.sleepTimer(1000); } return ""; } public String printTimes(){ for(int i = 0; i < movieTime.length; i++){ System.out.print(movieTime[i] + " "); Main1.sleepTimer(1000); } return ""; } public boolean selectMovie(String selection){ for(int i = 0; i < movies.length; i++){ if(((String)movies[i]).equalsIgnoreCase(selection)){ return true; } } return false; } public String bookSlot(int timeSlot){ return (String)movieTime[timeSlot - 1]; } public double calculateCost(int numberOfAttentees){ return (double)price * numberOfAttentees; } }, ACTION(){ String[] movies = {"Under Siege", "The Long Kiss Goodnight", "Edge of Extinction", "Collateral Damage", "The Legend of Hercules"}; String[] movieTime = {"11:45am", "1:45pm", "3:45pm", "5:45pm", "7:45pm", "9:45pm"}; double price = 9.50; public String printArr(){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i] + " "); Main1.sleepTimer(1000); } return ""; } public String printTimes(){ for(int i = 0; i < movieTime.length; i++){ System.out.print(movieTime[i] + " "); Main1.sleepTimer(1000); } return ""; } public boolean selectMovie(String selection){ for(int i = 0; i < movies.length; i++){ if(((String)movies[i]).equalsIgnoreCase(selection)){ return true; } } return false; } public String bookSlot(int timeSlot){ return (String)movieTime[timeSlot - 1]; } public double calculateCost(int numberOfAttentees){ return (double)price * numberOfAttentees; } }, ADVENTURE(){ String[] movies = {"Gods of Egypt", "The Last Witch Hunter", "The Hunger Games: Mockingja", "10,000 BC", "Lost in Space"}; String[] movieTime = {"12:15pm", "1:15pm", "2:15pm", "3:15pm", "4:15pm", "5:15pm"}; double price = 9.50; public String printArr(){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i] + " "); Main1.sleepTimer(1000); } return ""; } public String printTimes(){ for(int i = 0; i < movieTime.length; i++){ System.out.print(movieTime[i] + " "); Main1.sleepTimer(1000); } return ""; } public boolean selectMovie(String selection){ for(int i = 0; i < movies.length; i++){ if(((String)movies[i]).equalsIgnoreCase(selection)){ return true; } } return false; } public String bookSlot(int timeSlot){ return (String)movieTime[timeSlot - 1]; } public double calculateCost(int numberOfAttentees){ return (double)price * numberOfAttentees; } }, CRIME(){ String[] movies = {"A Man Apart", "New Jack City", "Cradle 2 the Grave", "I Am Wrath", "Murder at 1600"}; String[] movieTime = {"2:40 pm", "3:40 pm", "5:40 pm", "7:40 pm", "9:40 pm", "11:40 pm"}; double price = 9.50; public String printArr(){ for(int i = 0; i < movies.length; i++){ System.out.println(movies[i] + " "); Main1.sleepTimer(1000); } return ""; } public String printTimes(){ for(int i = 0; i < movieTime.length; i++){ System.out.print(movieTime[i] + " "); Main1.sleepTimer(1000); } return ""; } public boolean selectMovie(String selection){ boolean isPresent = false; for(int i = 0; i < movies.length; i++){ if(((String)movies[i]).equalsIgnoreCase(selection)){ isPresent = true; return isPresent; } } return false; } public String bookSlot(int timeSlot){ return (String)movieTime[timeSlot - 1]; } public double calculateCost(int numberOfAttentees){ return (double)price * numberOfAttentees; } }; private String[] movies; public abstract String printArr(); public abstract String printTimes(); public abstract boolean selectMovie(String selection); public abstract String bookSlot(int timeSlot); public abstract double calculateCost(int numberOfAttentees); }

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