390 likes | 527 Views
Exposure Java. Chapter 13 Slides. Subscripted Variables. PowerPoint Presentation created by: Mr. John L. M. Schram. From Materials Created by Mr. Leon Schram. First Data Structure Definition.
E N D
Exposure Java Chapter 13 Slides Subscripted Variables PowerPoint Presentation created by: Mr. John L. M. Schram From Materials Created by Mr. Leon Schram
First Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types.
Data Structure Starting Point Any data type that can store more than one value is a data structure.
First Array Definition An array is a data structure with one, or more, elements of the same type. A one-dimensional array is frequently also called a vector. A two-dimensional array is frequently also called a matrix.
First Record Definition A record is a data structure with one, or more, elements, called fields, of the same or different data types.
File Definition A file is an internal data structure - with an unspecified number of elements of the same type - assigned to an external file name. The file data structure allows transfer of data between internal and external storage.
Stack Definition A stack is a data structure with elements of the same type. Data elements of the stack data structure can only be accessed (stored or retrieved) at one end of the stack in a LIFO (Last In, First Out) manner.
Improved Data Structure Definition A data structure is a data type whose components are smaller data structures and/or simple data types. The storing and retrieval of the data elements is performed by accessing methods that characterize the data structure.
Improved Array Definition An array is a data structure with a fixed number of elements of the same type. Every element of the array can be accessed directly.
Declaring a 1D Array int list[ ]; // declares the array list identifier list = new int[10]; // allocates memory for 10 integers char names[ ]; // declares the names array identifier names = new char[25]; // allocates memory for 25 characters double grades[ ]; // declares the grades array identifier grades = new double[50]; // allocates memory for 50 doubles
// Java1301.java // This program demonstrates how to declare an array of integers. // Note how each element of the array defaults to zero. // Java allows the display of uninitialized objects because object // elements get assigned values from the default constructor. public class Java1301 { public static void main(String args[]) { int list[]; // declares the array object identifier list = new int[10]; // allocates memory for 10 array elements for (int k = 0; k < 10; k++) System.out.println("list[" + k + "] = " + list[k]); System.out.println(); } } Java1301.java Output List[0] = 0 List[1] = 0 List[2] = 0 List[3] = 0 List[4] = 0 List[5] = 0 List[6] = 0 List[7] = 0 List[8] = 0 List[9] = 0
Array Index Note Java arrays indicate individual elements with an index inside two brackets, following the array identifier, like list[k]. The array index is always an integer and starts at 0. In an array of N elements, the largest index is N-1.
// Java1302.java // This program is almost identical to Java1301. The difference // is that the array declaration and array definition to allocate // memory is done in one program statement. public class Java1302 { public static void main(String args[]) { int list[] = new int[10]; // combined array declaration and definition for (int k = 0; k < 10; k++) System.out.println("list[" + k + "] = " + list[k]); System.out.println(); } } Java1302.java Output List[0] = 0 List[1] = 0 List[2] = 0 List[3] = 0 List[4] = 0 List[5] = 0 List[6] = 0 List[7] = 0 List[8] = 0 List[9] = 0
// Java1303.java // This program demonstrates how to initialize array elements. // The <new> operator is not necessary in this case. public class Java1303 { public static void main(String args[]) { int list[] = {11,22,33,44,55,66,77,88,99}; for (int k = 0; k < 9; k++) System.out.println("list[" + k + "] = " + list[k]); System.out.println(); } } Java1303.java Output List[0] = 11 List[1] = 22 List[2] = 33 List[3] = 44 List[4] = 55 List[5] = 66 List[6] = 77 List[7] = 88 List[8] = 99
// Java1304.java // This program demonstrates how to declare an array of characters. // Are the array elements initialized to any kind of value? public class Java1304 { public static void main(String args[]) { int k; char list1[] = new char[5]; for (k = 0; k < 5; k++) System.out.println("list1[" + k + "] = " + list1[k]); System.out.println(); char list2[] = {'c','h','a','r'}; for (k = 0; k < 4; k++) System.out.println("list2[" + k + "] = " + list2[k]); System.out.println(); } } Java1304.java Output List1[0] = List1[1] = List1[2] = List1[3] = List1[4] = List2[0] = c List2[1] = h List2[2] = a List2[3] = r
// Java1305.java // The purpose of this program is to investigate the type of character that // is used to initialize a character array. // Is it no-character-at-all or a blank space? public class Java1305 { public static void main(String args[]) { char List1[] = new char[10]; System.out.print("Start"); for (int K = 0; K < 10; K++) System.out.print(List1[K]); System.out.println("End"); System.out.println(); } } Java1305.java Output Start End
// Java1306.java // This program demonstrates how String objects are initialized, // both without and with specified array values. public class Java1306 { public static void main(String args[]) { String list1[] = new String[5]; for (int k = 0; k < 5; k++) System.out.println("list[" + k + "] = " + list1[k]); System.out.println(); String list2[] = {"AAA","BBB","CCC","DDD","EEE"}; for (int k = 0; k < 5; k++) System.out.println("list2[" + k + "] = " + list2[k]); System.out.println(); } } Java1306.java Output List[0] = null List[1] = null List[2] = null List[3] = null List[4] = null List2[0] = AAA List2[1] = BBB List2[2] = CCC List2[3] = DDD List2[4] = EEE
// Java1307.java // This program fills an integer array with a random set of numbers. import java.util.Random; public class Java1307 { public static void main(String args[]) { int list[] = new int[20]; Random random = new Random(12345); for (int k = 0; k < 20; k++) list[k] = random.nextInt(900) + 100; for (int k = 0; k < 20; k++) System.out.println("list[" + k + "] = " + list[k]); System.out.println(); } } Java1307.java Output List[0] = 851 List[1] = 680 List[2] = 241 List[3] = 928 List[4] = 458 List[5] = 284 List[6] = 575 List[7] = 802 List[8] = 701 List[9] = 889 List[10] = 717 List[11] = 142 List[12] = 890 List[13] = 206 List[14] = 312 List[15] = 584 List[16] = 687 List[17] = 803 List[18] = 432 List[19] = 775
// Java1308.java // This program introduces the length field to determine the // number of elements in the array. Remove the comments from line 16 // to observe what happens when the length field is altered. public class Java1308 { public static void main(String args[]) { String names[] = {"Joe","Tom","Sue","Meg"}; int n = names.length; // data field access; not a method call System.out.println("There are " + n + " array elements."); for(int k = 0; k < n; k++) System.out.println("names[" + k + "] = " + names[k]); // names.length = 10; System.out.println(); } } Java1308.java Output There are 4 array elements. Names[0] = Joe Names[1] = Tom Names[2] = Sue Names[3] = Meg
// Java1309.java // This program demonstrates how to create an array of random strings. import java.util.Random; public class Java1309 { public static void main(String args[]) { Random random = new Random(12345); int rndInt; String names[] = {"AA","BB","CC","DD","EE","FF","GG","HH","II","JJ"}; for(int k = 1; k < 15; k++) { rndInt = random.nextInt(names.length); System.out.println("names[" + rndInt + "] = " + names[rndInt]); } System.out.println(); } } Java1309.java Output Names[9] = JJ Names[7] = HH Names[9] = JJ Names[8] = II Names[8] = II Names[1] = BB Names[6] = GG Names[0] = AA Names[1] = BB Names[0] = AA Names[7] = HH Names[8] = II Names[0] = AA Names[3] = DD
// Java1310.java // This program is the first stage in the List class. At this stage the // List class only initializes an array and displays its default values. public class Java1310 { public static void main(String args[]) { List1 listA = new List1(10); listA.display(); List1 listB = new List1(15); listB.display(); } } class List1 { private int intArray[]; // stores array elements private int size; // number of elements in the array public List1(int s) { size = s; intArray = new int[size]; } public void display() { for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); } } Java1310.java Output 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
// Java1311.java // This is the second stage of the List class. A method for assigning // random integers has been added. import java.util.Random; public class Java1311 { public static void main(String args[]) { List2 listA = new List2(10); listA.Display(); listA.Assign(); listA.Display(); System.out.println(); } } class List2 // continued on next slide Java1311.java Output CONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES DISPLAYING ARRAY ELEMENTS 0 0 0 0 0 0 0 0 0 0 ASSIGNING RANDOM VALUES TO LIST OBJECT DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389
class List2 { private int intArray[]; // stores array elements private int size; // number of elements in the array public List2(int s) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES"); size = s; intArray = new int[size]; } public void Assign() { System.out.println("\nASSIGNING RANDOM VALUES TO LIST OBJECT"); Random random = new Random(12345); for (int k = 0; k < size; k++) intArray[k] = random.nextInt(9000) + 1000; } public void Display() { System.out.println("\nDISPLAYING ARRAY ELEMENTS"); for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); } }
// Java1312.java // A second "overloaded" constructor has been added to the third stage of the // List class. The second constructor specifies the initial array values. import java.util.Random; public class Java1312 { public static void main(String args[]) { List3 listA = new List3(10); listA.Display(); List3 listB = new List3(10,999); listB.Display(); listB.Assign(); listB.Display(); System.out.println(); } } class List3 // continued on next slide Java1312.java Output CONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES DISPLAYING ARRAY ELEMENTS 0 0 0 0 0 0 0 0 0 0 CONSTRUCTING NEW LIST OBJECT WITH SPECIFIED VALUES DISPLAYING ARRAY ELEMENTS 999 999 999 999 999 999 999 999 999 999 ASSIGNING RANDOM VALUES TO LIST OBJECT DISPLAYING ARRAY ELEMENTS 6251 6080 9241 1828 4055 2084 2375 9802 2501 5389
class List3 { private int intArray[]; // stores array elements private int size; // number of elements in the array public List3(int s) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH DEFAULT VALUES"); size = s; intArray = new int[size]; } public List3(int s, int n) { System.out.println("\nCONSTRUCTING NEW LIST OBJECT WITH SPECIFIED VALUES"); size = s; intArray = new int[size]; for (int k = 0; k < size; k++) intArray[k] = n; } public void Assign() { System.out.println("\nASSIGNING RANDOM VALUES TO LIST OBJECT"); Random random = new Random(12345); for (int k = 0; k < size; k++) intArray[k] = random.nextInt(9000) + 1000; } public void Display() { System.out.println("\nDISPLAYING ARRAY ELEMENTS"); for (int k = 0; k < size; k++) System.out.print(intArray[k] + " "); System.out.println(); } }
// Java1313.java // This program demonstrates how to declare a two-dimensional array. public class Java1313 { public static void main(String args[]) { int matrix[][]; matrix = new int[3][3]; for(int r = 0; r < 3; r++) { for(int c = 0; c < 3; c++) System.out.print(matrix[r][c] + " "); System.out.println(); } System.out.println(); } } Java1313.java Output 0 0 0 0 0 0 0 0 0
// Java1314.java // This program assigns a set of consecutive integers to a two-dimensional // array and displays the values in a matrix format. public class Java1314 { public static void main(String args[]) { int k = 1; int matrix[][]; matrix = new int[7][7]; for(int r = 0; r < 7; r++) for(int c = 0; c < 7; c++) { matrix[r][c] = k; k++; } System.out.println(); for(int r = 0; r < 7; r++) { for(int c = 0; c < 7; c++) System.out.print(matrix[r][c] + " "); System.out.println(); } System.out.println(); } } Java1314.java Output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Java1315.java // This program assigns a set of consecutive integers to a two-dimensional array // and displays the values in a two-digit format using the DecimalFormat class. import java.text.DecimalFormat; public class Java1315 { public static void main(String args[]) { DecimalFormat twoDigits = new DecimalFormat("00"); int k = 1; int matrix[][]; matrix = new int[7][7]; for(int r = 0; r < 7; r++) for(int c = 0; c < 7; c++) { matrix[r][c] = k; k++; } \\ System.out.println(); for(int r = 0; r < 7; r++) { for(int c = 0; c < 7; c++) System.out.print(twoDigits.format(matrix[r][c]) + " "); System.out.println(); } System.out.println() } } Java1315.java Output 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// Java1316.java // This program assigns random values to a matrix and displays all the // random integers in a three-digit format with leading zeroes. import java.text.DecimalFormat; import java.util.Random; public class Java1316 { public static void main(String args[]) { DecimalFormat threeDigits = new DecimalFormat("000"); Random random = new Random(12345); int matrix[][]; matrix = new int[5][4]; for(int r = 0; r < matrix.length; r++) for(int c = 0; c < matrix[r].length; c++) matrix[r][c] = random.nextInt(1000); . for(int r = 0; r < matrix.length; r++) { for(int c = 0; c < matrix[r].length; c++) System.out.print(threeDigits.format(matrix[r][c]) + " "); System.out.println(); } System.out.println(); } } Java1316.java Output 251 080 241 828 055 084 375 802 501 389 517 942 390 806 012 384 787 303 532 175
// Java1317.java // This program assigns random doubles to a matrix and displays all the numbers in a three-digit format with // leading zeroes, and three digits after the decimal point. import java.text.DecimalFormat; public class Java1317 { public static void main(String args[]) { DecimalFormat df = new DecimalFormat("0000.000"); double matrix[][]; matrix = new double[5][5]; double count = Math.PI; for(int r = 0; r < 5; r++) for(int c = 0; c < 5; c++) { matrix[r][c] = count; count += Math.PI; } for(int r = 0; r < 5; r++) { for(int c = 0; c < 5; c++) System.out.print(df.format(matrix[r][c]) + " "); System.out.println(); } System.out.println(); } } Java1317.java Output 0003.142 0006.283 0009.425 0012.566 0015.708 0018.850 0021.991 0025.133 0028.274 0031.416 0034.558 0037.699 0040.841 0043.982 0047.124 0050.265 0053.407 0056.549 0059.690 0062.832 0065.973 0069.115 0072.257 0075.398 0078.540
Decimal Format Class with format Method Objects of DecimalFormat can control numerical output by adding leading zeroes, adding trailing zeroes, rounding numbers after the decimal point, adding commas in large numbers and adding a $ sign. DecimalFormat x = new DecimalFormat("format specification"); System.out.println(x.format(Number)); If "format specification" = "$00,000.00" then the output will display a $ sign followed by five digits with a comma inserted, and a decimal point followed by two digits.
// Java1318.java // This program draws a regular hexagon using the <cos> and <sin> methods of the <Math> class. // This program uses two arrays to enter the polygon coordinate points. import java.awt.*; public class Java1318 extends java.applet.Applet { public void paint(Graphics g) { int radius = 100; int centerX = 200; int centerY = 200; double twoPI = 2 * Math.PI; g.setColor(Color.blue); int x1 = (int) Math.round(Math.cos(twoPI * 1/6) * radius) + centerX; int y1 = (int) Math.round(Math.sin(twoPI * 1/6) * radius) + centerY; int x2 = (int) Math.round(Math.cos(twoPI * 2/6) * radius) + centerX; int y2 = (int) Math.round(Math.sin(twoPI * 2/6) * radius) + centerY; int x3 = (int) Math.round(Math.cos(twoPI * 3/6) * radius) + centerX; int y3 = (int) Math.round(Math.sin(twoPI * 3/6) * radius) + centerY; int x4 = (int) Math.round(Math.cos(twoPI * 4/6) * radius) + centerX; int y4 = (int) Math.round(Math.sin(twoPI * 4/6) * radius) + centerY; int x5 = (int) Math.round(Math.cos(twoPI * 5/6) * radius) + centerX; int y5 = (int) Math.round(Math.sin(twoPI * 5/6) * radius) + centerY; int x6 = (int) Math.round(Math.cos(twoPI) * radius) + centerX; int y6 = (int) Math.round(Math.sin(twoPI) * radius) + centerX; int xCoord[] = {x1,x2,x3,x4,x5,x6}; int yCoord[] = {y1,y2,y3,y4,y5,y6}; Polygon hex = new Polygon(xCoord,yCoord,6); g.fillPolygon(hex); } }
// Java1319.java // This program draws a regular hexagon. This program enters the array values in a convenient loop. import java.awt.*; public class Java1319 extends java.applet.Applet { public void paint(Graphics g) { int xCoord[] = new int[6]; int yCoord[] = new int[6]; int radius = 100; int centerX = 200; int centerY = 200; int k; double twoPI = 2 * Math.PI; g.setColor(Color.blue); for (k = 0; k < 6; k++) { xCoord[k] = (int) Math.round(Math.cos(twoPI * k/6) * radius) + centerX; yCoord[k] = (int) Math.round(Math.sin(twoPI * k/6) * radius) + centerY; } Polygon hex = new Polygon(xCoord,yCoord,6); g.fillPolygon(hex); } }
// Java1320.java // This program demonstrates how an argument can be entered from the // command line using the args[] array. This program expects a // single argument. public class Java1320 { public static void main(String args[]) { System.out.print("Entered at command line: "); System.out.println(args[0]); } } Java1320.java Output C:\Java\labs>java Java1320 "Hello World" Entered at command line: Hello World C:\Java\labs>
// Java1321.java // This program demonstrates how multiple arguments can be entered // from the command line using the args[] array. This program expects // three separate single arguments. public class Java1321 { public static void main(String args[]) { System.out.println("Entered at command line:\n"); System.out.println("args[0]: " + args[0]); System.out.println("args[1]: " + args[1]); System.out.println("args[2]: " + args[2]); } } Java1321.java Output C:\Java\labs>java Java1321 String1 String2 String3 Entered at command line: args[0]: String1 args[1]: String2 args[2]: String3 C:\Java\labs>
// Java1322.java // This program reviews the use of the <Integer> class with the // <parseInt> method to convert strings entered at the command line // prompt to integers. public class Java1322 { public static void main(String args[]) { int Nr1 = Integer.parseInt(args[0]); int Nr2 = Integer.parseInt(args[1]); int Sum = Nr1 + Nr2; System.out.println(Nr1 + " + " + Nr2 + " = " + Sum); } } Java1322.java Output C:\Java\labs>java Java1322 1234 4321 1234 + 4321 = 5555 C:\Java\labs>
Working with args The Java main method always includes (String args[ ]). The args array stores string values entered at the prompt. It is possible to convert entered string values to numerical values by using Integer.parseInt and Double.parseDouble. Note: Every example shown with args used the actual command prompt approach. This approach demonstrates the concept of adding extra information for the main method parameter heading more clearly. If you use JCreator, a small window pops up, which requests the information.