Minute Paper 03/30/2009 1. Username, name 2. Status of your homework 3. What is it that you need that you don't have yet (that I could then try to provide for you) to make the most out of this course (and do your assignments) or things to that effect? Assignment Two, problem 1: a) the main method receives an array of strings argument which includes the command line arguments. So in a set up like this: class One { public static void main(String[] args) { } } inside main you can refer to args.length (which will evaluate to the number of elements in the array of strings called args that main receives) and with an index i in between 0 and args.length - 1 you can access any individual string element of the array args that main receives as args[i] class One { public static void main(String[] args) { int count = 1; for (int i = 0; i < args.length; i++) { System.out.println(count + ". " + args[i]); count += 1; } // if we want to store them, where do we store them int[] nums = new int[args.length]; // initialized to 0's // declare nums as an array of ints, allocate that many cells for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " "); } System.out.println(); // so this prints the values in nums, separated by blanks // all of them on the same line // next we set the locations in nums to be the converted // corresponding values in args for (int i = 0; i < args.length; i++) { nums[i] = Integer.parseInt(args[i]); } // print the array, so we can see the numbers we just stored for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " "); } System.out.println(); // change them by adding 2 to each one of them for (int i = 0; i < args.length; i++) { nums[i] += 2; // nums[i] = nums[i] + 2; } // show the array with modified numbers again for (int i = 0; i < args.length; i++) { System.out.print(nums[i] + " "); } System.out.println(); } } So you see how you can read (Strings and convert them into) ints from the command line. Question is now: how do you rearrange them? class One { public static void main(String[] args) { int[] n; // declare variable n as referring to an array of ints n = new int[10]; // creates an array of ten locations and assigns // it to n so you can use n[i] to refer to them n[4] = 5; // n[4] is the 5th location, changes now from 0 to 5 // declare a variable to refer to a two dimensional array int[][] m; // variable to a matrix, two dimensional array of ints // allocate storage m = new int[4][5]; // a 4 by 5 matrix of numbers int size = 12; m = new int[size][size]; // m being an array of arrays m[3][4] = 9; for (int i = 0; i < m.length; i++) { for (int j = 0; j < m[i].length; j++) { System.out.print(m[i][j] + " "); } System.out.println(); } } } So for Wednesday: a) problem one -- you know how to read a sequence of numbers from the command line, that includes some conversions, can you rearrange them in some fashion? b) problem two -- do you understand the rules? can you build a 5x5 magic square by hand? can you do it now that you know how to handle a two dimensional array of integers in Java?