AP Exam Other Topics

advertisement
• Static (Class) variables
• Integer.MIN_VALUE and MAX_VALUE
• Two-Dimensional arrays
• List interface
Static is a reserved word used to designate
something that exists as part of a class,
but not part of a specific object.
Static variables and methods exist even
if no object of that class has been
instantiated.
Static means one!
All Objects will share the same static
variables and methods.
Examples:
Integer.parseInt()
Math.random()
Math.PI
Color.RED
class Monster
{
private String myName;
private static int count = 0;
Each Monster has its
own unique myName
All Monsters share
count (which is static)
public Monster( String name )
{
myName = name;
count++; // Increments count each time a Monster is constructed
}
public static int getCount()
{
return count; // returns the count of monsters constructed
}
}
In client class:
Monster fred = new Monster(“Fred”);
Monster jan = new Monster(“Jan”);
System.out.println(jan.getCount());
System.out.println(Monster.getCount());
OUTPUT
2
2
This is saved on my website
NOTE: Integer.MIN_VALUE and
Integer.MAX_VALUE could be on the AP
Exam
data type
memory usage
min .. max
byte
8 bits
-128 to 127
short
16 bits
-32768 to 32767
int
32 bits
-2 billion to 2 billion
long
64 bits
-big to +big
float
32 bits
-big to +big
double
64 bits
-big to +big
char
16 bit unsigned
0 - 65535
reference
32 bits
n/a
Memory consists of bits and bytes.
8 bits = 1001 0010 = 1 byte
16 bits = 0101 1001 0100 1001 = 2 bytes
The more bits you have the
more you can store.
1 byte = 8 bits
System.out.println(Byte.MIN_VALUE);
System.out.println(Byte.MAX_VALUE);
System.out.println(Short.MIN_VALUE);
System.out.println(Short.MAX_VALUE);
OUTPUT
FYI: NOT on AP Exam
-128
127
-32768
32767
System.out.println(Integer.MIN_VALUE);
System.out.println(Integer.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
System.out.println(Long.MAX_VALUE);
This could be on the
AP Exam: Know that
the valid values of
an integer are: from
-2 billion to 2 billion
OUTPUT
-2147483648
2147483647
-9223372036854775808
9223372036854775807
This could be on
int num = Integer.MAX_VALUE;
the AP Exam
System.out.println(num);
num=num+1;
System.out.println(num);
num=num-1;
System.out.println(num);
OUTPUT
Why does adding 1 to
MAX_VALUE give you the
MIN_VALUE?
2147483647
-2147483648
2147483647
Overflow errors occur at run-time when a variable that is
too large/too small is increased/decreased.
This is saved on my website
Two-Dimensional Arrays
– A table of numbers, for instance, can be implemented as a twodimensional array. The figure shows a two-dimensional array
with four rows and five columns.
col 0
col 1
col 2
col 3
col 4
row 0
20
10
45
72
90
row 1
30
25
60
32
45
row 2
10
12
71
11
62
row 3
15
8
53
84
14
– Suppose we call the array table; then to indicate an element in
table, we specify its row and column position, remembering
that indexes start at 0:
int x = table[2][3];
row
column
// Sets x to 11, the value
// in (row 2, column 3)
Java Concepts 7.6 (Two-Dimensional Arrays)
Two-Dimensional Arrays
Declare and Instantiate
–
Declaring and instantiating twodimensional arrays is accomplished by
extending the processes used for onedimensional arrays:
int[][] table = new int [4][5];
//
//
//
//
//
The variable named table references a
two-dimensional array of integers.
Instantiate table as an array of size 4,
each of whose elements will reference an
array of 5 integers.
Java Concepts 7.6 (Two-Dimensional Arrays)
Two-Dimensional Arrays
–
–
–
Initializer lists can be used with two-dimensional
arrays. This requires a list of lists.
The number of inner lists determines the number of
rows, and the size of each inner list determines the
size of the corresponding row.
The rows do not have to be the same size, but they
are in this example:
int[][] table = {{ 0, 1, 2, 3, 4},
{10,11,12,13,14},
{20,21,22,23,24},
{30,31,32,33,34}};
//
//
//
//
row
row
row
row
0
1
2
3
Java Concepts 7.6 (Two-Dimensional Arrays)
Two-Dimensional Arrays
– Remember, all the elements of a two-dimensional array
must be of the same type, whether they are integers,
doubles, string, or whatever.
– To determine the number of rows in the 2-dimensional
array, you can use the length variable (just like a 1dimensional array).
– If we refer to the table 2-dim array from the previous
slide, what would the following print to the console?
System.out.print(table.length);
//prints 4
Java Concepts 7.6 (Two-Dimensional Arrays)
Two-Dimensional Arrays
The following code uses a two-dimensional array. Each row
represents a classroom and each column represents the students in
that classroom:
//The following code will print every student in each class
String[][] students = {{"Bill", "Brenda", "Bob", "Bonnie"},
{"Sally", "Sam", "Sandra", "Steve"}};
for (int row = 0; row < students.length; row++)
{
System.out.println("");
System.out.print("The following students are in class " + row + ": " );
for (int col = 0; col < students[0].length; col++)
{
System.out.print( students[row][col] + ", " );
}
}
Java Concepts 7.6 (2-Dimensional Arrays)
• List interface indicates the behavior of
a collection of objects. It allows
duplicate objects and one or more
elements to be null.
• ArrayList implements List and
redefines the methods shown on next
slide
List Interface
Useful methods from List interface
Method name
How is it used?
add(obj)
Adds an objects to the collection
clear()
Removes all objects from the collection
contains(obj)
Returns true if object is in the collection
get(i)
Returns the element at the index location
size()
Returns the number of elements in the
collection
remove(i)
Removes the element at the specified index
location
Download