Exposure Java Slides

Download Report

Transcript Exposure Java Slides

Review of
Simple/Primitive Data Types
A simple/primitive data type can store only one
single value.
This means an int can only store one integer.
A double can only store one real number.
A char can only store one character.
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 1-dimensional array is frequently called a vector.
A 2-dimensional array is frequently called a matrix.
The array is the first historical data structure which
was introduced in the language FORTRAN.
Record Definition
A record is a data structure with one, or
more, elements, called fields, of the
same or different data types.
The language FORTRAN did NOT
have records which is why it
was NOT good for business.
COBOL (Common Business
Oriented Language) introduced
the record data structure.
A Note About Classes
A class is a record
that can also store methods.
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.
First Array Definition Again
An array is a data structure with one, or
more, elements of the same type.
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.
Array Example
[16]
Ingrid
[17]
Darlene
[18]
Gene
[19]
Sean
[20]
Stephanie
[11]
Holly
[12]
Blake
[13]
Michelle
[14]
Remy
[15]
Haley
[06]
Diana
[07]
Jessica
[08]
David
[09]
Anthony
[10]
Alec
[01]
Isolde
[02]
John
[03]
Greg
[04]
Maria
[05]
Heidi
// Java1101.java This program declares 10 different int variables.
// Each variable is assigned a value and each variable value is displayed.
Java1101
// This approach is very inefficient for a large number of variables.
public class Java1101
100
101 102 103 104 105
{
public static void main(String args[])
{
System.out.println("Java1101\n");
int number0 = 100; int number1 = 101;
int number2 = 102; int number3 = 103;
int number4 = 104; int number5 = 105;
int number6 = 106; int number7 = 107;
int number8 = 108; int number9 = 109;
System.out.print(number0 + " ");
System.out.print(number1 + " ");
System.out.print(number2 + " ");
System.out.print(number3 + " ");
System.out.print(number4 + " ");
System.out.print(number5 + " ");
System.out.print(number6 + " ");
System.out.print(number7 + " ");
System.out.print(number8 + " ");
System.out.print(number9 + " ");
System.out.println();
}
}
106 107 108 109
// Java1102.java This program declares an array of 10 int elements.
// Each array element value is individually assigned and displayed.
// There does not appear any real benefit from the from program example.
Java1102
public class Java1102
{
public static void main(String args[])
{
System.out.println("Java1102\n");
int list[];
// declares the array object identifier
list = new int[10];
// allocates memory for 10 array elements
100 101 102 103 104 105 106 107 108 109
list[0] = 100;
list[2] = 102;
list[4] = 104;
list[6] = 106;
list[8] = 108;
list[1] = 101;
list[3] = 103;
list[5] = 105;
list[7] = 107;
list[9] = 109;
System.out.print(list[0] + " ");
System.out.print(list[1] + " ");
System.out.print(list[2] + " ");
System.out.print(list[3] + " ");
System.out.print(list[4] + " ");
System.out.print(list[5] + " ");
System.out.print(list[6] + " ");
System.out.print(list[7] + " ");
System.out.print(list[8] + " ");
System.out.print(list[9] + " ");
System.out.println();
}
}
Index
Value
0
100
1
101
2
102
3
103
4
104
5
105
6
106
7
107
8
108
9
109
Array Index Note
Java arrays indicate individual elements
with an index inside two brackets,
following the array identifier, like list[3]
The array index is always an integer and
starts at 0.
In an array of N elements, the largest
index is N-1.
// Java1103.java
//
The previous program with separate statements for each array member
Java1103
// assignment and display is now replaced with two loops. The loop counter,
// index, is used to specify each array element in an efficient manner.
100 101 102 103 104 105 106 107 108 109
public class Java1103
{
public static void main(String args[])
{
System.out.println("Java1103\n");
int list[];
list = new int[10];
for (int index = 0; index <=9; index++)
list[index] = index + 100;
for (int index = 0; index <=9; index++)
System.out.print(list[index] + " ");
System.out.println();
}
}
Index
Value
0
100
1
101
2
102
3
103
4
104
5
105
6
106
7
107
8
108
9
109
Defining Static Arrays
int list[];
list = new int[10];
// declares the array list identifier
// allocates memory for 10 integers
char names[];
names = new char[25];
// declares the names array identifier
// allocates memory for 25 characters
double grades[];
// declares the grades array identifier
grades = new double[50]; // allocates memory for 50 doubles
Defining Static Arrays
Preferred Method
int list[] = new int[10];
char names[] = new char[25];
double grades[] = new double[50];
This is similar to what you
learned in Chapters 3 & 6.
Chapter This will work:
This is preferred:
3
int x;
x = 5;
int x = 5;
6
Bank tom;
tom = new Bank();
Bank tom = new Bank();
11
int list[ ];
list = new int [10];
int list[] = new int[10];
// Java1104.java
// This program has the same <list> array and the same <list> values
// as the previous program. This time the array declaration and
// allocation is accomplished with a single statement.
public class Java1104
{
public static void main(String args[])
{
System.out.println("Java1104\n");
int list[ ] = new int[10];
for (int index = 0; index <=9; index++)
list[index] = index + 100;
for (int index = 0; index <=9; index++)
System.out.print(list[index] + " ");
}
}
// Java1105.java
// This program demonstrates how to initialize array elements.
// The <new> operator is not necessary in this case.
public class Java1105
{
This is
called
initializer
list.
public
static
void an
main(String
args[])
{Note that the size of the array
does
not need to be specified.
System.out.println("Java1105\n");
int list[ ] = {100,101,102,103,104,105,106,107};
for (int k = 0; k <= 7; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
}
}
// Java1105.java
Java1105
// This program demonstrates how to initialize array elements.
// The <new> operator is not necessary in this
case. = 100
list[0]
list[1]
list[2]
public class Java1105
list[3]
{
list[4]
public static void main(String args[]) list[5]
{
list[6]
System.out.println("Java1105\n"); list[7]
=
=
=
=
=
=
=
101
102
103
104
105
106
107
int list[ ] = {100,101,102,103,104,105,106,107};
for (int k = 0; k <= 7; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
0
1
2
3
4
5
6
7
}
}
100 101 102 103 104 105 106 107
1 2 3 4 5 6 7 8 9 10 11 11 13 14 15 16 17 18 19 20 21 22 23 24 25
//0 Java1106.java
// This
program
a string
A
B C
D E demonstrates
F G H I aJcharacter
K L Marray
N Oand
PQ
R Sarray.
T U VWX Y Z
// Both arrays use an initializer list.
public class Java1106
Java1106
{
public static void main(String args[])
ABCDEFGHIJKLMNOPQRSTUVWXYZ
{
System.out.println("Java1106\n"); John
Greg
char list1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M',
Maria
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
Heidi
Diana
for (int k = 0; k < 26; k++)
David
System.out.print(list1[k]);
System.out.println("\n");
String list2[] = {"John","Greg","Maria","Heidi","Diana","David"};
for (int k = 0; k < 6; k++)
System.out.println(list2[k]);
System.out.println();
0
1
2
3
4
5
}
}
list2
John
Greg
Maria Heidi
Diana David
// Java1106.java
// This program demonstrates a character array and a string array.
// Both arrays use an initializer list.
Try This!
Add one or more names to this list.
public class Java1106
Will
they show up in the output? Why?
{
public static void main(String args[])
{
System.out.println("Java1106\n");
char list1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for (int k = 0; k < 26; k++)
System.out.print(list1[k]);
System.out.println("\n");
String list2[] = {"John","Greg","Maria","Heidi","Diana","David"};
for (int k = 0; k < 6; k++)
System.out.println(list2[k]);
System.out.println();
}
}
// Java1106.java
// This program demonstrates a character array and a string array.
// Both arrays use an initializer list.
Now Try This!
Remove several names from this list.
public class Java1106
Does
the program still work? Why?
{
public static void main(String args[])
{
System.out.println("Java1106\n");
char list1[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
for (int k = 0; k < 26; k++)
System.out.print(list1[k]);
System.out.println("\n");
String list2[] = {"John","Greg","Maria","Heidi","Diana","David"};
for (int k = 0; k < 6; k++)
System.out.println(list2[k]);
System.out.println();
}
}
// Java1107.java
Java1107
// This program introduces the <length>
to determine the number of elements in the
// array. NOTE: <length> is a data field, not a method. There are no (parentheses).
// Remove the comments from lineThere
19 to observe
happens when
the length
are what
4 array
elements.
// field is altered.
names[0] = Joe
names[1] = Tom
names[2] = Sue
names[3] = Meg
public class Java1107
{
public static void main(String args[])
{
System.out.println("Java1107\n");
String names[] = {"Joe","Tom","Sue","Meg"};
System.out.println("There are " + names.length + " array elements.");
for(int k = 0; k < names.length; k++)
System.out.println("names[" + k + "] = " + names[k]);
//
names.length = 10;
System.out.println();
}
}
0
1
2
3
Joe
Tom
Sue
Meg
// Java1107.java
// This program introduces the <length> to determine the number of elements in the
// array. NOTE: <length> is a data field, not a method. There are no (parentheses).
//
Remove
the comments
from line
19 to observe
Add
one
or more
names
to what
thishappens
list. when the length
// field is altered.
Try This!
Will they show up in the output? Why?
public
class
Java1107
Why
is
this different from program Java1106?
{
public static void main(String args[])
{
System.out.println("Java1107\n");
String names[ ] = {"Joe","Tom","Sue","Meg"};
System.out.println("There are " + names.length + " array elements.");
for(int k = 0; k < names.length; k++)
System.out.println("names[" + k + "] = " + names[k]);
//
names.length = 10;
System.out.println();
}
}
// Java1107.java
// This program introduces the <length> to determine the number of elements in the
// array. NOTE: <length> is a data field, not a method. There are no (parentheses).
//
Remove the comments
fromnames
line 19 to observe
what
happens
when the length
Remove
several
from
this
list.
// field is altered.
Now Try This!
Does the program still work? Why?
public
class
Java1107
Why
is
this different from program Java1106?
{
public static void main(String args[])
{
System.out.println("Java1107\n");
String names[ ] = {"Joe","Tom","Sue","Meg"};
System.out.println("There are " + names.length + " array elements.");
for(int k = 0; k < names.length; k++)
System.out.println("names[" + k + "] = " + names[k]);
//
names.length = 10;
System.out.println();
}
}
// Java1107.java
// This program introduces the <length> to determine the number of elements in the
// array. NOTE: <length> is a data field, not a method. There are no (parentheses).
//
Remove the comments
from line 19 to symbol
observe what happens
length
Remove
the comment
from when
thethelast
// field is altered.
Try This Also!
program statement in this program.
public
class
Java1107
Will
the
program still compile?
{
public static void main(String args[])
{
System.out.println("Java1107\n");
String names[ ] = {"Joe","Tom","Sue","Meg"};
System.out.println("There are " + names.length + " array elements.");
for(int k = 0; k < names.length; k++)
System.out.println("names[" + k + "] = " + names[k]);
//
names.length = 10;
System.out.println();
}
}
// Java1107.java
// This program introduces the <length> to determine the number of elements in the
// array. NOTE: <length> is a data field, not a method. There are no (parentheses).
// Remove the comments from line 19 to observe what happens when the length
// field is altered.
NO!
public class Java1107
{
public static void main(String args[])
The
length field is a
{
System.out.println("Java1107\n");
final
or constant attribute
String names[ ] = {"Joe","Tom","Sue","Meg"};
just like the PI in Math.PI
System.out.println("There are " + names.length + " array elements.");
for(int k = 0; k < names.length; k++)
System.out.println("names[" + k + "] = " + names[k]);
names.length = 10;
System.out.println();
}
}
Java1108
// Java1108.java
// This program fills an integer array with a random set
of numbers.
list[0]
= 244
list[1]
= 594
// The random numbers will be generated in the [100..999]
range.
list[2] = 902
// Note that a "fixed loop" is used for the data entry into
the array
list[3]
= 385
list[4] = 949
// as well as the display of the array elements.
list[5] = 792
list[6] = 636
list[7] = 894
list[8] = 126
list[9] = 103
list[10] = 938
list[11] = 592
list[12] = 584
list[13] = 112
list[14] = 766
list[15] = 791
list[16] = 447
list[17] = 298
list[18] = 596
list[19] = 243
public class Java1108
{
public static void main(String args[])
{
System.out.println("Java1108\n");
int list[] = new int[20];
for (int k = 0; k < 20; k++)
list[k] = Expo.random(100,999);
for (int k = 0; k < 20; k++)
System.out.println("list[" + k + "] = " + list[k]);
System.out.println();
0 }1
2
3
4
5
6
7
8
9 10 11 12 13 14 15 16 17 18 19
} 594 902 385 949 792 636 894 126 103 938 592 584 112 766 791 447 298 596 243
244
// Java1109.java
// This program will display 15 random sentences.
// With 7 different ranks, 7 different people, 7 different actions and 7 different locations,
// there are more than 2400 different sentences possible.
System.out.println("Java1109\n");
String rank[] = {"Private","Corporal","Sargent","Lieutenant","Captain","Major","General"};
String person[] = {"Smith", "Gonzales", "Brown", "Jackson", "Powers", "Jones", "Nguyen"};
String action[] = {"drive the tank", "drive the jeep", "take the troops", "bring all supplies",
"escort the visitor", "prepare to relocate", "bring the Admiral"};
String location[] = {"over the next hill", "to the top of the mountain", "outside the barracks",
"30 miles into the desert", "to the middle of the forest",
"to my present location", "to anywhere but here"};
for (int j = 1; j <= 15; j++)
{
int randomRank
=
int randomPerson =
int randomAction =
int randomLocation =
Expo.random(0,rank.length-1);
Expo.random(0,person.length-1);
Expo.random(0,action.length-1);
Expo.random(0,location.length-1);
String sentence = rank[randomRank] + " " + person[randomPerson] + ", " +
action[randomAction] + " " + location[randomLocation] + ".";
System.out.println("\n" + sentence);
}
Java1109
Sargent Brown, bring all supplies outside the barracks.
Major Brown, drive the tank to anywhere but here.
General Smith, prepare to relocate to the middle of the forest.
Corporal Jones, bring the Admiral over the next hill.
General Jackson, take the troops outside the barracks.
Captain Brown, escort the visitor to my present location.
Corporal Powers, drive the tank to my present location.
Private Smith, drive the jeep to my present location.
Captain Jackson, escort the visitor to anywhere but here.
Corporal Smith, bring the Admiral over the next hill.
Corporal Smith, drive the jeep to anywhere but here.
Corporal Gonzales, prepare to relocate outside the barracks.
Major Gonzales, escort the visitor to the top of the mountain.
Major Smith, escort the visitor to the top of the mountain.
Lieutenant Gonzales, escort the visitor over the next hill.
// Java1110.java
//
This program introduces the static <Arrays> class.
Java1110
// In this program the <toString> method is used to display the array elements.
[11,
22, 33, 44, 55,
66, to77,
88,
99]class
import java.util.Arrays;
// necessary
use the
<Arrays>
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]
public
[A, class
B, Java1110
C, D, E, F, G, H, I]
{[AA, BB, CC, DD, EE, FF, GG, HH, II]
public static void main (String args[])
{
System.out.println("Java1110\n");
int list1[] = {11,22,33,44,55,66,77,88,99};
double list2[] = {1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9};
char list3[] = {'A','B','C','D','E','F','G','H','I'};
String list4[] = {"AA","BB","CC","DD","EE","FF","GG","HH","II"};
System.out.println(Arrays.toString(list1));
System.out.println(Arrays.toString(list2));
System.out.println(Arrays.toString(list3));
System.out.println(Arrays.toString(list4));
System.out.println("\n\n");
}
}
Class Arrays
Method toString
Method toString displays the elements of
an array object separated by commas and
bounded by [square brackets].
int list[] = {11,22,33,44,55,66,77,88,99};
System.out.println(Arrays.toString(list));
[11, 22, 33, 44, 55, 66, 77, 88, 99]
// Java1111.java
Java1111
// This program demonstrates the <fill> method of the <Arrays> class.
// The <fill> method assigns the same value to every array element.
[123,
123, 123, 123, 123,
123,to123,
123,
import java.util.Arrays;
// necessary
use the123,
<Arrays>
class 123]
[1.23,
1.23, 1.23, 1.23, 1.23, 1.23, 1.23, 1.23, 1.23, 1.23]
public class Java1111
[Q, Q, Q, Q, Q, Q, Q, Q, Q, Q]
{
[USA, USA, USA, USA, USA, USA, USA, USA, USA, USA]
public static void main (String args[])
{
System.out.println("Java1111\n");
int list1[] = new int[10];
double list2[] = new double[10];
char list3[] = new char[10];
String list4[] = new String[10];
Arrays.fill(list1,123);
Arrays.fill(list2,1.23);
Arrays.fill(list3,'Q');
Arrays.fill(list4,"USA");
System.out.println(Arrays.toString(list1));
System.out.println(Arrays.toString(list2));
System.out.println(Arrays.toString(list3));
System.out.println(Arrays.toString(list4));
System.out.println("\n\n");
}
}
Class Arrays
Method fill
Method fill assigns the same
value to every array element.
int list1[] = new int[10];
double list2[] = new double[10];
char list3[] = new char[10];
String list4[] = new String[10];
Arrays.fill(list1,113);
Arrays.fill(list2,1.23);
Arrays.fill(list3,'Q');
Arrays.fill(list4,"USA");
// Java1112.java
Java1112
// This program demonstrates the <sort> method of the <Arrays> class.
// Method <sort> arranges array elements in ascending order.
[11,
22, 33, 44, 55, 66,
77, 88,
import java.util.Arrays;
// necessary
to use99]
the <Arrays> class
[1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9]
public
class C,
Java1112
[A,
B,
D, E, F, G, H, I]
{
[AA,public
BB,static
CC,
GG, HH, II]
voidDD,
main EE,
(StringFF,
args[])
[ELEFANT,
FOX, GORILLA, HARE, aardvark, bobcat, cougar, dog]
{
System.out.println("Java1112\n");
int list1[] = {11,99,22,88,33,77,44,66,55};
double list2[] = {9.9,8.8,7.7,6.6,5.5,4.4,3.3,2.2,1.1};
char list3[] = {'A','I','B','H','C','G','D','F','E'};
String list4[] = {"AA","II","BB","HH","CC","GG","DD","FF","EE"};
String list5[] =
{"aardvark","bobcat","cougar","dog","ELEFANT","FOX","GORILLA","HARE"};
Arrays.sort(list1);
Arrays.sort(list2);
Arrays.sort(list3);
Arrays.sort(list4);
Arrays.sort(list5);
System.out.println(Arrays.toString(list1));
System.out.println(Arrays.toString(list2));
System.out.println(Arrays.toString(list3));
System.out.println(Arrays.toString(list4));
System.out.println(Arrays.toString(list5));
System.out.println("\n\n");
}
}
Capital Letters have numeric
code values from 65-90.
Lowercase letters have numeric
code values from 97-122.
If lowercase letters are sorted
together with capital letters, the
capitals will come first because
of the smaller code values.
Class Arrays
Method sort
Method sort arranges the array elements in ascending
order.
String and character array elements
are sorted in ascending order of the
numerical code values.
Incorrect processing may occur if
the string array contains strings with
both upper-case and lower-case letters.
// Java1113.java
Java1113
// This program demonstrates the <binarySearch>
method of the <Arrays> class.
// Method <binarySearch> returns the index of a search element if it exists,
[11, 99, 22, 88, 33, 77, 44, 66, 55]
// and returns a negative index value otherwise.
[11, 22, 33, 44, 55, 66, 77, 88, 99]
import java.util.Arrays;
// necessary to use the <Arrays> class
public class Java1113
Index of 33 is 2
Index of 11 is 0
{
Index of 99 is 8
public static void main (String args[])
Index of 10 is -1
{
System.out.println("Java1113\n");
int list[] = {11,99,22,88,33,77,44,66,55};
System.out.println(Arrays.toString(list));
Arrays.sort(list);
System.out.println(Arrays.toString(list));
System.out.println();
System.out.println("Index of 33 is " + Arrays.binarySearch(list,33));
System.out.println("Index of 11 is " + Arrays.binarySearch(list,11));
System.out.println("Index of 99 is " + Arrays.binarySearch(list,99));
System.out.println("Index of 10 is " + Arrays.binarySearch(list,10));
System.out.println("\n\n");
}
}
// Java1114.java
Java1114
// This program demonstrates that
an array must be sorted before
// the <binarySearch> method is called.
[11,returned
99, 22, if
88,
33,
44, sorted!!!
66, 55]
// Erroneous indexes are
the
list77,
is not
import java.util.Arrays;
// necessary
use4the <Arrays> class
Index
of 33 tois
Index of 11 is 0
Index of 99 is -10
Index of 10 is -1
public class Java1114
{
public static void main (String args[])
{
System.out.println("Java1114\n");
int list[] = {11,99,22,88,33,77,44,66,55};
System.out.println(Arrays.toString(list));
System.out.println();
System.out.println("Index of 33 is " + Arrays.binarySearch(list,33));
System.out.println("Index of 11 is " + Arrays.binarySearch(list,11));
System.out.println("Index of 99 is " + Arrays.binarySearch(list,99));
System.out.println("Index of 10 is " + Arrays.binarySearch(list,10));
System.out.println("\n\n");
}
}
Class Arrays
Method binarySearch
Method binarySearch searches the elements of an array
object for a specified value.
The index of the array element is returned, if the element is
found and a negative index is returned otherwise.
Array elements must be sorted; otherwise the
binarySearch method returns incorrect information.
int list[] = {11,99,22,88,33,77,44,66,55};
System.out.println("Index of 33 is " + Arrays.binarySearch(list,33));