0% found this document useful (0 votes)
59 views23 pages

Recitation 4 Static Array & Arraylist: 1.00/1.001 Introduction To Computers and Engineering Problem Solving

This document provides an overview of an assignment to implement a Scrabble word scoring program. It discusses providing a dictionary of valid two and three letter words, as well as scoring letters and words according to Scrabble rules. The document outlines having methods to score individual letters, score entire words by summing letter scores, and checking if a played word is valid using the dictionaries. It recommends testing the program by having it score sample words to verify it works as intended.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views23 pages

Recitation 4 Static Array & Arraylist: 1.00/1.001 Introduction To Computers and Engineering Problem Solving

This document provides an overview of an assignment to implement a Scrabble word scoring program. It discusses providing a dictionary of valid two and three letter words, as well as scoring letters and words according to Scrabble rules. The document outlines having methods to score individual letters, score entire words by summing letter scores, and checking if a played word is valid using the dictionaries. It recommends testing the program by having it score sample words to verify it works as intended.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1.00/1.

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

• Array and ArrayList

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 {

private int num;

...
}

Number object 1 Number object 2 Number object 3


num = ... num = ... num = ...

5
Static Members
public class Number {

private static int num;

...
}

Number object 1 Number object 2 Number object 3


num = ... num = ... num = ...

6
Static Methods
public class Number {

private static int num;


private int num2;

public int sum() {


return num + num2; Is this ok?
}

...
}

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?

private double gpa;


public static final int MAX_GPA = 5;

public double getGPA() {


return gpa;
}

public void printGPA() {


System.out.println("GPA: " + gpa + " / " + MAX_GPA);
}

public static int getMaxGpa() {


return MAX_GPA;
}
}
11
Array vs. ArrayList
• Size fixed at creation • Size varies as data is
• Accessed with z[] added/removed
• Object with no methods • Accessed with
z.get()
• One public data
member: z.length • Object with no data
• Slightly faster members
• Has lots of methods:
e.g., z.add(),
z.size()
• More flexible

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;
}

What does array contain?


ArrayList<Integer> arrayList = new ArrayList<Integer>();
for(int i = array.length-1; i > -1 ; i--) {
arrayList.add(array[i]);
}

What does arrayList contain?

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

• Start by putting in all the numbers, then remove the


ones that are multiples of each other in the ArrayList

• Some code is provided

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

• Some code is provided

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 =

0 “AAH” “AAL” “AAS” …


“ET” “ETH” “ER” … 1 “BAA” “BAD” “BAG” …
2 “CAB” “CAD” “CAM” …
3 “DAB” “DAD” “DAG” …
ArrayList array of
of Strings Strings 4 “EAR” “EAT” “EAU” …
… …
22
MIT OpenCourseWare
http://ocw.mit.edu

1.00 / 1.001 / 1.002 Introduction to Computers and Engineering Problem Solving


Spring 2012

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy