Recitation 4 Static Array & Arraylist: 1.00/1.001 Introduction To Computers and Engineering Problem Solving
Recitation 4 Static Array & Arraylist: 1.00/1.001 Introduction To Computers and Engineering Problem Solving
001
Introduction to Computers and Engineering Problem Solving
Recitation 4
Static
Array & ArrayList
Spring 2012
1
Quiz 1
• March 9 (Friday)
• 3:05pm-4:25pm
• Review session:
– Wed. March 7, 7pm - 9pm
• LA office hour: Thu 7:00-9:00pm
• Open notes/book, no electronic devices
2
Today’s Recitation
• Keywords: Static
3
Static
• Static members:
– not associated with any particular instance of the class—
one copy shared by all instances
– accessible to both static and non-static methods
• Static Methods:
– may only access static members, not instance members
– should be called using Classname.methodName()
4
Static Members
public class Number {
...
}
5
Static Members
public class Number {
...
}
6
Static Methods
public class Number {
...
}
7
When to Use Static Methods
• When no access to any instance field is required. Usually one
of two scenarios:
– The method takes in all the information it needs as
arguments:
Math.pow(double base, double exp)
– Or, the method needs access to only static variables.
– Usually you can think of these methods as taking in some
information and performing a service for you
– Typically, they do not alter the state of the class, as they do
not have access to instance variables
8
Exercise 1: Static Members
• Write a class Ticket that
– Keeps track of how many tickets there are
– Assigns a unique ticket number to each ticket, starting with
100, 101, etc.
– Has a method to return the number of tickets
– Has a method to return the ticket number
• Write a class TicketTest that creates some
Ticket objects and then prints out how many
were created.
9
Keywords Summary
public / private
Control access to data members and methods. Public data members can be
accessed outside the class, and public methods can be invoked outside the class.
Private data members and methods are not visible outside the class
static
Each instance (object) of a class has its own copy of each non-static data member,
but there is only one copy of each static data members, shared by all instances
(objects) of the class.
A static method can only use the static data members of the class and its input
parameters. Static methods are invoked on the class name, not on any object
name.
void
Apart from constructors, every method has a declared return type. If a method does
not return anything, its return type must be void.
final
The value of a final data member cannot be modified after it has been initialized.
10
Keywords Summary
public class UGrad{ What is the function of each keyword in this class?
12
Array and ArrayList
• Setting and accessing data is different:
int[] array = new int[3];
for(int i = 0; i < array.length ; i++) {
array[i] = i;
}
13
Array or ArrayList?
• Which would you use for the following
problems: an Array or ArrayList?
– Write a method that returns all the primes
between 2 and a specified number
– Write a method that returns a specified number of
random numbers
14
ArrayList Exercise
• Write a method findPrimes(int n) that
returns an ArrayList<Integer> of all the
primes between 2 and n
15
ArrayList Exercise
public static ArrayList<Integer> findPrimes(int n) {
//Declare the ArrayList
for (int i=2; i<=n; i++)
//Put i in the ArrayList
int i = 0;
while //condition to make sure i is a valid index
int j = i + 1;
while //condition to make sure j is a valid index
if //(element j)%(element i)==0
//remove the proper number from the list
else
j++;
}
i++;
}
//return your ArrayList
}
16
Array Exercise
• Write a method makeRandom(int n) that
returns an array of n random numbers
17
Array Exercise
private static Random r = new Random();
public static int[] makeRandom(int n) {
//Create your array, then assign random
//numbers using r.nextInt();
}
18
Self assessment, lectures 1-11
Skill Below Expected Above
Data types Types misused. Ints and reals used Int, long, double,
Static not used distinguishably. boolean, and static
Static used used purposefully
correctly
Variables Numbers and Variables used for Variables easy to
variables used most quantities read. Naming is
without distinction consistent and
accurate
Expressions Complex Complex Complex
expressions not expressions expressions
defined correctly; organized by structured for
simple expressions parentheses and increased clarify
ok use of variables
Loop constructs Successfully used Clear and Appropriate choice
understandable of for, while, do-
19
while
Self assessment, lectures 1-11
Skill Below Expected Above
Methods Methods not clearly Methods defined Methods organized,
defined, or use but overall named clearly,
poor arguments or structure not perform clear
return values. always clear. Static behaviors. Static
Wrong use of static used correctly. used appropriately
Method arguments Few or no Appropriate Arguments versus
arguments used arguments used data members
clearly designed
Variable scope Local variable scope Most variables have All variables
inconsistent, often appropriate scope defined just before
too large use and go out of
scope after use
20
Self assessment, lectures 1-11
Skill Below Expected Above
Comments Some critical Comments explain Comments make
comments missing, basic code code self-
some not clear explanatory
Indentation No indentation Some indentation All code properly
used used but not indented
consistent
21
Homework 4: Scrabble
Dictionary
Z E T R L C H
twoLetterWords =
What are all the two and three “AA” “AB” “AD” …
letter words that can be made
with your hand of 7 letters? threeLetterWords =
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.