/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package finalrevprep1s19; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * * @author jcmtiernan */ public class FinalRevPrep1S19 { /** * @param args the command line arguments */ public static void main(String[] args) { String inputFileName = "catAges.txt"; File nameAgeFile = new File(inputFileName); Scanner inFile = new Scanner(System.in); try { inFile = new Scanner(nameAgeFile); } catch (FileNotFoundException fnfe) { System.out.println("No input data file found"); } ArrayList cats = new ArrayList<>(); cats = catlove(5, inFile); System.out.println("cats is "+cats); for (int i = (cats.size()-1); i >=0; i--) System.out.println(cats.get(i)); int[][] matrix = { { 1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8 , 9, 10}}; System.out.println("int[][] matrix = { { 1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8 , 9, 10}}"); for (int k=0; k < matrix.length; k++) { for (int m=0; m < matrix[k].length; m++) { System.out.printf("%d\t",matrix[k][m]); } System.out.println(""); } ArrayList< ArrayList > pets = new ArrayList<>(); ArrayList apet = new ArrayList<>(); for (int k=0; k < matrix.length; k++) { pets.add(k,apet); for (int m=0; m < matrix[k].length; m++) { pets.get(k).add(matrix[k][m]); //System.out.printf("%d\t",matrix[k][m]); System.out.printf("at pets.get("+k+").get("+m+") %d\n",pets.get(k).get(m)); } //System.out.println(""); } System.out.println("pets is "+pets); // adds everything // candy jar int[][] candyjars = { {1,3,2,0,0}, {0,0,5,1,2}, {0,0,2,1,1}, {2,2,2,2,2}}; conlys(candyjars); // quotes String[] quotes = {"these aren't the droids you're looking for", "the cat and the dog", "hop on pop", "may the force be with you"}; final int MAX = 10; String[] words; for (int i = 0; i < quotes.length; i++) { String line = quotes[i]; words = line.split(" "); System.out.println("\nword results: \n"); for (int j = 0; j < words.length; j++) { System.out.println(words[j]); } quotes[i] = ""; for (int k=0; k < words.length; k++) { if(words[k].equals("the")) { words[k] = "any"; } quotes[i] = quotes[i]+" "+words[k]; } } System.out.println("\nQuote results: \n"); for (int i = 0; i < quotes.length; i++) { System.out.println(quotes[i]); } int bresult; bresult = bill("Sunset"); System.out.println("\nbresult with word Success! is "+bresult); bresult = bill("HELLO"); System.out.println("\nbresult with word HELLO is "+bresult); int[][] transposed = transpose(candyjars); System.out.println("\n\ncandy jars\n"); for (int k=0; k < candyjars.length; k++) for (int m=0; m < candyjars[0].length; m++) System.out.printf("%d%s",candyjars[k][m], ((m == ((candyjars[0].length)-1)) ? "\n" : "\t")); System.out.println(""); System.out.println("\n\ntransposed candy jars\n"); for (int k=0; k < transposed.length; k++) for (int m=0; m < transposed[0].length; m++) System.out.printf("%d%c",transposed[k][m], (m == (transposed[k].length-1) ? '\n' : '\t')); System.out.println(""); int f = 5; boolean vowel = false; System.out.println("Today is "+ (f == 5? "" : "not ")+"Friday"); // ( test ? true_result : false_result ) boolean posVal = true; double res = (posVal? 1 : -1 ) * 42; System.out.println("res = "+res); String[][] characters = {{"Peppa","pig","Pink"}, {"Danny","dog","brown"}, {"Suzy","sheep","white"}}; System.out.println("\n\nanimals original\n"); for (int k=0; k < characters.length; k++) for (int m=0; m < characters[0].length; m++) System.out.printf("%s%c",characters[k][m], (m == (characters[k].length-1) ? '\n' : '\t')); updateVal(characters, "Danny", "black"); System.out.println("\n\nanimals modified\n"); for (int k=0; k < characters.length; k++) for (int m=0; m < characters[0].length; m++) System.out.printf("%s%c",characters[k][m], (m == (characters[k].length-1) ? '\n' : '\t')); Integer intVal = 10; int intVal2 = 10; String numToVal = "53"; int nVal = Integer.parseInt(numToVal); System.out.println("\nnumToVal is "+numToVal +" adding 1 gives "+ (numToVal+1)); System.out.println("nVal = "+nVal +" adding 1 gives "+ (nVal+1)); System.out.printf("\n%c %d\n",'a',(int)('a')); System.out.printf("\n%c %d\n",'j',(int)('j')); System.out.printf("\n%c %d\n",'A',(int)('A')); System.out.printf("\n%c %d\n",'5',(int)('5')); System.out.printf("\n%c %d\n",'3',(int)('3')); String heroName = ""; String[] hName = new String[5]; hName[0] = heroName; if (heroName.isEmpty()) { System.out.printf("\n\nheroName length is "+hName[0].length()+" at location "+hName); } double rt; rt = 1.0/3; System.out.println("\nrt 2/3 is "+rt); System.out.println("\nAnother recursive example - palindrome(string)"); System.out.println("whatIs(receiver) = "+whatIs("receiver")); System.out.println("whatIs(racecar) = "+whatIs("racecar")); System.out.println("whatIs(repair) = "+whatIs("repair")); System.out.println("whatIs(radar) = "+whatIs("radar")); } public static boolean whatIs(String str) { if((str.length() == 0) || (str.length() == 1)) return true; if(str.charAt(0) == str.charAt(str.length()-1)) { return whatIs( str.substring(1, str.length()-1) ); } else return false; } public static void updateVal( String[][] animals, String name, String color ) { // given animal name such as "Candy" and a color such as "red" and update table for (int i = 0; i < animals.length; i++) { if (animals[i][0].equalsIgnoreCase(name)) { animals[i][2] = color; } } } public static int[][] transpose( int[][] matrix ) { int[][] tmat = new int[matrix[0].length][matrix.length]; for( int r = 0; r < matrix.length; r++) for ( int c = 0; c < matrix[0].length; c++) tmat[c][r] = matrix[r][c]; return tmat; } public static int bill(String word) { int k = 0; if (word.equalsIgnoreCase("hello")) /// true or false k = word.length(); else if ( word.compareToIgnoreCase("Success!") >= 0) { k = word.length(); System.out.println("\n"+word+".compareToIgnoreCase(\"Success!\") = "+ word.compareToIgnoreCase("Success!")); } else { System.out.println("\n"+word+".compareToIgnoreCase(\"Success!\") = "+ word.compareToIgnoreCase("Success!")); } System.out.println("the in "); return k; } public static void conlys( int[][] cj ) { int[] totC = new int[cj.length]; // totals each kind of candy int[] totJ = new int[cj[0].length]; // total in each jar int lgC = 0; int lgCloc = -1; int lgJ = 0; int lgJloc = -1; System.out.println(""); for(int r = 0; r < cj.length; r++) { for (int c = 0; c < cj[0].length; c++) { System.out.println("adding in candy element cj["+r+"]["+c+"] of "+cj[r][c]); totC[r] = totC[r] + cj[r][c]; } if (totC[r]>lgC) { lgC = totC[r]; lgCloc = r; } } System.out.println(""); for (int c = 0; c < cj[0].length; c++) { for(int r = 0; r < cj.length; r++) { System.out.println("adding in jar element cj["+r+"]["+c+"] of "+cj[r][c]); totJ[c] = totJ[c] + cj[r][c]; } if (totJ[c]>lgC) { lgJ = totJ[c]; lgJloc = c; } } System.out.println(""); for (int i=0; i < totC.length; i++) { System.out.println("Candy "+i+" has "+totC[i]+" pieces "); } for (int i=0; i < totJ.length; i++) { System.out.println("Jar "+i+" has "+totJ[i]+" pieces "); } System.out.println("Candy "+lgCloc+" has the most pieces = "+totC[lgCloc]+" "); System.out.println("Jar "+lgJloc+" has the most pieces = "+totJ[lgJloc]+" "); } // ArrayList< Class_Type > name // type[] name public static ArrayList catlove(int count, Scanner ifile) { ArrayList fluffies = new ArrayList<>(); int readin; while (ifile.hasNextInt()) { readin = ifile.nextInt(); System.out.println("fluffies next value is "+readin); fluffies.add(readin); System.out.println(fluffies); } return fluffies; } }