primesfactory lab laugh if you get the pun. don’t forget: you can copy- paste this slide into...

18
PrimesFactory Lab Laugh if you get the pun

Upload: ferdinand-stone

Post on 16-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

PrimesFactory LabLaugh if you get the pun

Page 2: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Poll: Which of these is not a prime number?

Page 3: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Prime NumbersA prime number (or a prime) is a natural

number greater than 1 that has no positive divisors other than 1 and itself.

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89…

If a number p is prime, then the only cases where p % t == 0 are when t==1 or t==p◦e.g. If the number 23 is prime, then the only

cases where 23 % t == 0 are when t==1 or t==23

Page 4: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Poll: Which one of these is not a factor of 45...

Page 5: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

FactorsIn mathematics, a divisor of an

integer n, also called a factor of n, is an integer which divides n without leaving a remainder.

Factors of 24◦1, 2, 3, 4, 6, 8,12, 24

For an integer n, the factors t are numbers that satisfy the rule n % t == 0

Page 6: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Poll: Which of these is not a prime factor of ...

Page 7: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Prime FactorsThe prime factors of a positive

integer are the prime numbers that divide that integer exactly.

Factors of 24◦1, 2, 3, 4, 6, 8,12, 24

Prime factors of 24 are 2 and 3

Page 8: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

PrimesFactoryThree important methods

1. Calculate the prime factorization of any number

2. Print a list of all prime numbers3. State whether or not a given

number is a prime number

Page 9: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

PrimesFactoryThree important methods

1. getPrimeFactors(int num) //returns an ArrayList with all of the prime

factors of num

2. listPrimesUpTo(int num) //returns a comma separated list of all

prime numbers less than num

3. isPrime(int num) //return true if num is a prime number

Page 10: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Arrays vs. ArrayList

Arrays ArrayList

String[] arr = new String[10];…//insert Strings into array…for(int i=0; i<arr.length; i++){ System.out.println(arr[i]);}

ArrayList<String> arrList = new ArrayList<String>();…//insert Strings into ArrayList…for(int i=0; i<arr.size(); i++){ System.out.println

(arrList.get(i));}

Page 11: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Arrays vs. ArrayList

Arrays ArrayList

String[] arr = new String[10];…//insert Strings into array…for(String x : arr){ System.out.println(x);}

ArrayList<String> arrList = new ArrayList<String>();…//insert Strings into ArrayList…for(String x : arrList){ System.out.println(x);}

Page 12: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Arrays vs. ArrayList

Arrays ArrayList

Fixed length, set when it is created

Must keep track of last slot if array is not full

Must write code to shift elements if you want to insert or delete

Shrinks and grows as needed

Last slot is always arrList.size()-1

Insert with justarrList.add(object)Delete with just arrList.remove(objectIndex) or arrList.remove(object)

Page 13: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

ArrayList methodsadd(Object elem)remove(int index)remove(Object elem)contains(Object elem)isEmpty()indexOf(Object elem)size()get(int index)

Page 14: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

GenericsArrayList class is generic, which

means it has a type parameter◦Class header public class

ArrayList<E> <E> is placeholder for any non-primitive

type

◦ArrayList<String> stores Strings◦ArrayList<Integer> stores ints

List is restricted to particular data type

Built-in safety

Page 15: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

The Jokes Get Pun-nierWhat kind of music do Santa’s

elves listen to the most?What kind of musicians are

Integers and Doubles?Wrap music/Wrappers :P

Page 16: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Auto-Boxing and -UnboxingArrayList must contain objects

◦NO PRIMITIVES Objects usually start with upper case

letter String, Egg, Pokemon, Dog, etc.

Primitives usually start with lower case letter int, double, boolean, etc.

◦Numbers must be boxed—placed in wrapper classes like Integer or Double—before insertion into an ArrayList

Page 17: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Auto-Boxing and -UnboxingAuto-boxing

◦Automatic wrapping of primitive types in their wrapper classes

◦Use intValue() or doubleValue() to retrieve the numerical value

Auto-unboxing◦Automatic conversion of a wrapper

class to its corresponding primitive type

Page 18: PrimesFactory Lab Laugh if you get the pun. Don’t forget: You can copy- paste this slide into other presentations, and move or resize the poll. Poll:

Auto-Boxing and –Unboxing ExampleArrayList<Integer> arrList = new

ArrayList<Integer>();

arrList.add(4); //auto-boxing//4 is an int, wrapped

in an//Integer before

insertionint n = list.get(0);//auto-unboxing

//Integer is retrieved

//and converted to int