/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package lab4t1part1adone; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.NoSuchElementException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Lab4T1Part1aDone { /** * @param args the command line arguments */ public static void main(String[] args) { /* Replace the sets of variables with arrays for each kind of data. Ex. An array of Strings for the superhero names and an array of ints for the publication years. Make seperate arrays for each kind of data. Arrays should hold at least 20 values each. */ /* String superHero1="", superHero2="", superHero3="", superHero4=""; int shYear1=0, shYear2=0, shYear3=0, shYear4=0; String shPub1="", shPub2="", shPub3="",shPub4=""; int shIssue1=0, shIssue2=0, shIssue3=0, shIssue4=0; int shMovie1=0, shMovie2=0, shMovie3=0, shMovie4=0; */ final int SHMAX = 20; String[] superHero = new String[SHMAX]; int[] shYear = new int[SHMAX]; String[] shPub = new String[SHMAX]; int[] shIssue = new int[SHMAX]; int[] shMovie = new int[SHMAX]; for (int i = 0; i < SHMAX; i++) { superHero[i] = ""; shPub[i] = ""; } Scanner input =new Scanner(System.in); int select = 0; File inFile = new File("L4T1dataErr1.txt"); Scanner inputFile = new Scanner(System.in); try { inputFile = new Scanner(inFile); } catch (FileNotFoundException fnfe) { System.out.println("Input file "+"L4T1dataErr1.txt"+"not found"); } String fileLine; Scanner inLine = new Scanner(System.in); int counter = 0; int lineCount = 1; int minMovYr = 2020; int maxMovYr = 1900; int earlyYear = 2020; int latestYear = 1900; int shYr = 0, shIss = 0, shMov = 0; String shNm = "", shPb = "", shIssStr = "", shMovStr = ""; boolean valid = true; while (inputFile.hasNextLine() && (counter < SHMAX)) { valid = true; fileLine = inputFile.nextLine(); //System.out.println("File line "+lineCount+" "+fileLine); inLine = new Scanner(fileLine); inLine.useDelimiter("-*\\d"); //inLine.useDelimiter("[aeiou]"); shNm = inLine.next(); //System.out.println("Print the word after looking for vowels = *"+shNm+"*"); shNm = shNm.trim(); inLine.reset(); try { shYr = inLine.nextInt(); } catch (InputMismatchException ime) { valid = false; System.out.println(">> Invalid publication year data on line "+lineCount); } catch (NoSuchElementException nsee) { valid = false; System.out.println(">> Missing publication year data on line "+lineCount); } inLine.useDelimiter("-*[0-9]"); shPb = inLine.next(); shPb = shPb.trim(); inLine.reset(); try { shIssStr = inLine.next(); shMovStr = inLine.next(); } catch (NoSuchElementException nsee) { valid = false; System.out.println(">> Missing issue number or movie year data on line "+lineCount); } try { shIss = Integer.parseInt(shIssStr); } catch (NumberFormatException nfe) { valid = false; System.out.println(">> Invalid issue year data on line "+lineCount); } try { shMov = Integer.valueOf(shMovStr); } catch (NumberFormatException nfe) { valid = false; System.out.println(">> Invalid movie year data on line "+lineCount); } if (valid) // error checking values { if ((shYr < 1837) || (shYr > 2021)) { valid = false; System.out.println(">> Issue year out of range on line "+lineCount); } if ((shMov < 1888) || (shMov > 2025)) { valid = false; System.out.println(">> Movie year out of range on line "+lineCount); } if (shIss < 0) { valid = false; System.out.println(">> Issue number out of range on line "+lineCount); } } if (valid) { superHero[counter] = shNm; shYear[counter] = shYr; shPub[counter] = shPb; shIssue[counter] = shIss; shMovie[counter] = shMov; /* find earliest and latest publication years and movie years */ if (earlyYear > shYear[counter]) earlyYear = shYear[counter]; if (minMovYr > shMovie[counter]) minMovYr = shMovie[counter]; if (latestYear < shYear[counter]) latestYear = shYear[counter]; if (maxMovYr < shMovie[counter]) maxMovYr = shMovie[counter]; } else { counter--; //reset the counter for bad data } counter++; lineCount++; } System.out.println("\nThe following valid information was read in from the file: "); System.out.printf(" %-23s %-6s %-20s%6s%6s\n","Superhero","Comic","Publisher","Iss.","Movie"); for (int i = 0; (i < SHMAX)&& (i < counter) ; i++) { System.out.printf("%3d %-22s%7d %-20s%6s%6s\n",(i+1), superHero[i],shYear[i],shPub[i],shIssue[i],shMovie[i]); } System.out.println(""); /* Combine the if (earlyYear and if (select structures to use only one selection structure that tests for earlyYear, sets some values, and then prints the message: The earliest listed superhero comic featured SuperHero and was published in XXXX by Pub in comic issue #XX where Superhero is the name of the superhero with the earliest publication year in the file data, XXXX is the year number, Pub is the name of the publisher, ex. Marvel, and XX is the number of the comic issue where Superhero first appears. The print statement for the message should be AFTER the selection structure so think about what info is needed to print the correct data after the end of the selection structure. */ for (int i = 0; (i < SHMAX)&& (i < counter) ; i++) if (earlyYear == shYear[i]) { select = i; } System.out.println("[Early year found in while loop is "+earlyYear+"]"); System.out.println("\nThe earliest listed superhero comic featured "+superHero[select] +" and \nwas published in "+shYear[select]+" by "+shPub[select] +" in comic issue #"+shIssue[select]+"\n"); // sout SH1 /* Alternate way to find earliest: Write method to take in array, find smallest and return index of smallest value. */ earlyYear = findEarliestYear(shYear); int earlyIndex = findEarly(shYear); /* Modify the earlyYear test in the loop followed by output so that the print statement is the body of the if inside the while loop. Change the tests and/or the indices of the array appropriately. */ System.out.println("[Early year found by method call is "+earlyYear+"]"); for (int i = 0; (i < SHMAX)&& (i < counter) ; i++) { if (earlyYear == shYear[i]) System.out.println("\nThe earliest listed superhero comic featured "+superHero[i] +" and \nwas published in "+shYear[i]+" by "+shPub[i] +" in comic issue #"+shIssue[i]+"\n"); // sout SH1 } System.out.println("\n[Alternate output using index found by findEarly method call]"); System.out.println("\nThe earliest listed superhero comic featured " +superHero[earlyIndex]+" and \nwas published in " +shYear[earlyIndex]+" by "+shPub[earlyIndex] +" in comic issue #"+shIssue[earlyIndex]+"\n"); /* Find most recent movie year and then use that to select a message to print of the form: "The most recent listed superhero movie was about Superhero in 20XX" where Superhero is the name of the superhero with the most recent movie date and the 20XX is the year of that movie. */ System.out.println("----------------"); for (int i = 0; (i < SHMAX)&& (i < counter) ; i++) { if (maxMovYr == shMovie[i]) System.out.println("The most recent listed superhero movie was about " + superHero[i]+ " in "+ shMovie[i]); } System.out.println("----------------\n"); /* 4.a) Create an shAge array whose values are calculated as the absolute value of the movie year minus the publication year of the comic. */ int[] shAge = new int[SHMAX]; for (int k = 0; k < counter; k++) { shAge[k] = Math.abs(shMovie[k] - shYear[k]); if (shMovie[k] == 0) { shAge[k] = 2019 - shYear[k]; } } System.out.println("shAge array:"); for (int k = 0; k < counter; k++) System.out.println("shAge["+k+"] = "+shAge[k]); /* 4.b) Create a duplicate array called secondAge that has the same values as shAge. In the main method, print the secondAge array, bubble sort the secondAge array, and then print the values again. */ int[] secondAge = new int[SHMAX]; secondAge = shAge.clone(); // make a copy of an array with .clone() System.out.println("\nsecondAge array cloned from shAge:"); for (int k = 0; k < counter; k++) System.out.println("secondAge["+k+"] = "+secondAge[k]); //OR for (int k = 0; k < counter; k++) secondAge[k] = shAge[k]; System.out.println("\nsecondAge array copied from shAge:"); for (int k = 0; k < counter; k++) System.out.println("secondAge["+k+"] = "+secondAge[k]); int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < counter; pass++) // sz would list.length() { for (int comp = 0; comp < counter -1 ; comp++) { if (secondAge[comp] > secondAge[comp+1]) { // swap temp = secondAge[comp]; secondAge[comp] = secondAge[comp+1]; secondAge[comp+1] = temp; countswaps++; } } } System.out.println("\nsecondAge array after bubble sort:"); for (int k = 0; k < counter; k++) System.out.println("secondAge["+k+"] = "+secondAge[k]); /* 4.c) Create a duplicate array called secondPub that has the same values as shPub. Write a new method, called bubbleSortArray, that takes in the secondPub array and a number of elements in secondPub. Use the method to bubble sort the secondPub array. In the main method, print the secondPub array, call the bubble sort method with the secondPub array, and then print the values again. */ for (int k = 0; k < counter; k++) secondAge[k] = shAge[k]; System.out.println("\nsecondAge array re-copied from shAge:"); for (int k = 0; k < counter; k++) System.out.println(secondAge[k]+" is secondAge["+k+"]"); bubbleSortArray(secondAge,counter); System.out.println("\nsecondAge array after bubble sort method call:"); for (int k = 0; k < counter; k++) System.out.println(secondAge[k]+" is secondAge["+k+"]"); /* 4.e) Write a new method, called bubbleSortMultiArrays, that takes in the arrays shPub, superHeros, shYear, shIssue, shMovie and the number of elements in the arrays. Sort the superHeros array by name and keep all the other arrays in the same order with the superhero name array. In the main method, print the arrays, call the bubble sort method with the 5 arrays, and then print the values again. */ bubbleSortMultiArrays(superHero,shYear, shPub, shIssue, shMovie, counter); System.out.println("\nAfter multi-array bubble sort"); System.out.printf(" %-23s %-6s %-20s%6s%6s\n","Superhero","Comic","Publisher","Iss.","Movie"); for (int i = 0; (i < SHMAX)&& (i < counter) ; i++) { System.out.printf("%3d %-22s%7d %-20s%6s%6s\n",(i+1), superHero[i],shYear[i],shPub[i],shIssue[i],shMovie[i]); } System.out.println(""); /* Write code to ask user for earliest movie year, test their answer against earliest year, and tell if right or wrong. Let user guess again if desired. (Must find earliest movie year earlier in the program.) Your output to the user should include the following info. Your message can be different as long as it tells the user if they are correct and includes the required information. For incorrect guess of a year where there was a superhero movie then include the year that was guessed and name of superhero in the movie that year: “Rats. Your year 1964 had a movie about Dr. Strangelove but it was not the earliest movie” For incorrect guess where there was not a movie include year guessed: “Bummer! No superhero movies were premiered in 2002” For correct guess include year guessed and name of superhero that year: “You got it! The earliest superhero movie was in 1954 and was about Donald Duck!” */ Scanner userIn = new Scanner(System.in); int guess = 0; int guessIndex = -1; boolean guessValid = true; boolean guessAgain = true; String anotherGuess = "N"; System.out.println("\nWe're going to play a game."); while (guessAgain) { guessAgain = false; guessIndex = -1; while (guessValid) { guessValid = false; System.out.println("Please guess the earliest superhero movie year: "); try { guess = userIn.nextInt(); } catch (InputMismatchException ime) { System.out.println("I'm sorry. I couldn't understand your guess."); userIn.next(); guessValid = true; } } guessIndex = retrieveIndexMovie(guess, shMovie); //System.out.println("guessIndex is "+guessIndex+" minMovYr is "+minMovYr); if (guess == minMovYr) { System.out.println("You got it! The earliest superhero movie was in " + shMovie[guessIndex] + " and was about "+ superHero[guessIndex]+ "!"); for (int g=guessIndex+1; g < shMovie.length; g++) { if (shMovie[g] == minMovYr) System.out.println("There was also a movie about "+superHero[g] +" in "+shMovie[g]+"!"); } } else { boolean movMatch = false; int movCount = -1; //System.out.println("in else"); for (int i = 0; i < shMovie.length; i++) { //System.out.println("in for"); if (guess == shMovie[i]) { movMatch = true; movCount++; System.out.println((movCount == 0?"Rats. Your year ":" and your year ") + shMovie[i]+ " had a movie about " + superHero[i]+ " but it was not the earliest movie"); } } if (!movMatch) System.out.println("Bummer! No superhero movies were premiered in " + guess); } //System.out.println("End of guessing loop"); if (guess != minMovYr) { System.out.println("Would you like to try another guess? Enter Y or N: "); anotherGuess = input.next(); if (anotherGuess.equalsIgnoreCase("Y")) guessAgain = true; guessValid = true; } } // end of guessing game System.out.print("\nEnter the initial of your favorite superhero: "); char firstLet = input.next().charAt(0); String superH = ""; int charCnt = -1; System.out.print("You have entered the letter "+firstLet); for (int i = 0; (i < SHMAX)&& (i < counter) ; i++) { if (Character.toUpperCase(firstLet) == superHero[i].toUpperCase().charAt(0)) { superH = superHero[i]; if (superH.compareTo("") == 0) { System.out.println(" but no superhero matches your letter. "); } else { System.out.println(" so your favorite superhero is "+superH+"."); } } } /* if (superH.compareTo("") == 0) { System.out.println(" but no superhero matches your letter. "); } else { System.out.println(" so your favorite superhero is "+superH+"."); } */ } public static int findEarly(int[] shYear) { int early = 3000; int earlyIndex = -1; for (int i = 0; i < shYear.length; i++) if ((shYear[i] < early) && (shYear[i] != 0)) { early = shYear[i]; earlyIndex = i; } return earlyIndex; } public static int findEarliestYear(int[] shYear) { int early = 3000; for (int i = 0; i < shYear.length; i++) if ((shYear[i] < early) && (shYear[i] != 0)) early = shYear[i]; return early; } public static int retrieveIndexMovie(int MvYr, int[] shMovie) { for (int i = 0; i < shMovie.length; i++) if (MvYr == shMovie[i]) return i; return -1; } public static int bubbleSortArray(int[] secondAge,int counter) { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < counter; pass++) // sz would list.length() { for (int comp = 0; comp < counter -1 ; comp++) { if (secondAge[comp] > secondAge[comp+1]) { // swap temp = secondAge[comp]; secondAge[comp] = secondAge[comp+1]; secondAge[comp+1] = temp; countswaps++; } } } return countswaps; } public static int bubbleSortMultiArrays(String[] superHero,int[] shYear, String[] shPub, int[] shIssue, int[] shMovie, int sz) // superHero[i],shYear[i],shPub[i],shIssue[i],shMovie[i] { int countswaps = 0; int temp; String tempStr; for (int pass = 0; pass < sz; pass++) { for (int comp = 0; comp < sz -1 ; comp++) { //do you need to check for blank strings? if ( (superHero[comp].compareToIgnoreCase(superHero[comp+1])) > 0 ) { // swap tempStr = superHero[comp]; superHero[comp] = superHero[comp+1]; superHero[comp+1] = tempStr; temp = shYear[comp]; shYear[comp] = shYear[comp+1]; shYear[comp+1] = temp; tempStr = shPub[comp]; shPub[comp] = shPub[comp+1]; shPub[comp+1] = tempStr; temp = shIssue[comp]; shIssue[comp] = shIssue[comp+1]; shIssue[comp+1] = temp; temp = shMovie[comp]; shMovie[comp] = shMovie[comp+1]; shMovie[comp+1] = temp; countswaps++; } } } sz = 0; return countswaps; } }