/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package preptest2may19; import java.util.ArrayList; import java.util.Scanner; /** * * @author jcmtiernan */ public class PrepTest2May19 { /** * @param args the command line arguments */ public static void main(String[] args) { /* Write a method named sumDigits that takes one argument, a string called text, and computes and prints the sum of integers (digits) that appear in text as shown in the sample run. If there is no number in the text, the sum will be 0. When printing the sum 3 spaces must be reserved. See example below. No error checking needed. sumDigits("CSE1310"); sumDigits("Arli4ng6to1n"); sumDigits("999999999999"); sumDigits("UTA"); The sum is: 5 The sum is: 11 The sum is:108 The sum is: 0 */ String inStr = "CSE1310"; int sum = -1; Scanner input = new Scanner(System.in); //System.out.println("Enter a word that may or may not include digits as part of the word:"); //inStr = input.next(); //sum = sumDigits(inStr); //System.out.println("sumDigits of "+inStr+" is "+sum); /* // Selection structure if (sum > 10) { // compound statement System.out.println("sum is greater than 10"); } if (sum > 10) { // compound statement System.out.println("sum is greater than a decade"); } else { System.out.println("sum is less than or equal to a decade"); } if (sum > 10) { // compound statement System.out.println("sum is greater than a decade"); if (sum > 100) System.out.println("a century"); } else if (sum > 2) { System.out.println("sum is greater than 2 and less than a decade"); } else { System.out.println("sum is less than or equal to 2"); } sum = 4; //System.out.println("Before switch sum is "+sum); char ch = 'a'; switch(ch) { // drops through unless stopped by break case 'a': case 'A': System.out.println("a or A");break; case 'B': { System.out.println("B"); } break; case 'z': System.out.println("z"); default: System.out.println("done"); } // Repetition structures //for for (int i=0; i < 10; i++) { System.out.println("i is "+i); } for(int k=0, m=4 ; input.hasNext() ; k++, m += m*k ) { System.out.println("for loop k = "+k+" m = "+m); } //while int i = 0; while (input.hasNext()) // (i <10) { System.out.println("i is "+i); i++; } //do while do { } while (input.hasNext()); */ // recursion //Sequential strcuture // declaration // assignments (initialization ) // method call (print) //ArrayList ArrayList Dates = new ArrayList<>(); int date = 704; while (date != 0) { System.out.println("Please enter MMDD. Enter 0 to stop "); date = input.nextInt(); Dates.add(0, date); } System.out.println(Dates); Dates.add(2, 704); System.out.println(Dates); Dates.remove(1); System.out.println(Dates); } public static int sumDigits(String text) { int sum = 0; char val = ' '; String numStr; for (int i=0; i