Unit 6-10 Collegeboard topics
Documentation of Units 6-10
- Scores
- Links To HWs and Notebooks for each lesson
- General Notes
- Sorting Algorithms
- Sorting Algorithms
- Recursion
- Hashmap
- Inheritance
General Notes
- Building on from previous units... Primitives are predefined, lowercase, "Primitives", which cannot call methods, are a value, and can be different sizes based on what primitive it is. Non primitives are defined by the coder, are uppercase, "Reference types", which can call methods, can be null, and are all the same size
OOP or object oriented programming is a type of programming language. Classes are a template or blueprint from which objects are created. Objects are instances of a class. Methods are a set of code that perform a specific task. Classes in java can have data members, methods, constructors, nested classes, and interfaces.
If/else statements allow you to create a code segment that has checks. If/ElseIf/Else statements can be used to decide different program outputs depending on what the user inputted. Switch statements are also an alternative method to if statements.
Iteration is worth 25% of the test so its pretty important. While loops, for loops, and recursion loops are types of loops. Another example is nested iteration, which can put a loop inside another loop.
Classes are a blueprint for instantiating objects. An object is an instance of a class, a class defines an abstract type, methods are the behaviors you get objects to perform. Constructors create the object, are a special method for object instantiation, default constructor has no arguments. Accessor methods are getters and setters, which allow you to get values of variables and return a copy, or edit a variable.
Common errors when making arrays include bound errors, unfilled and uninitialized arrays, when you allocate, or assign a single array variable, but not the whole array. You can use for loops to iterate through an array, by using array.length.
void sort(int[] array) {
boolean isInOrder = false;
while (isInOrder == false) {
isInOrder = true;
for (int i = 0; i < array.length - 1; i++) {
if (array[i + 1] < array[i]) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
isInOrder = false;
}
}
}
}
void sort(int[] array) {
Random rand = new Random();
boolean isInOrder = false;
while (isInOrder == false) {
isInOrder = true;
for (int i = 0; i < array.length - 1; i++) {
if (array[i + 1] < array[i]) {
int rand1 = rand.nextInt(array.length);
int rand2 = rand.nextInt(array.length);
int temp = array[rand1];
array[rand1] = array[rand2];
array[rand2] = temp;
isInOrder = false;
}
}
}
}
public static int linearSearch(String[] words, String value) {
//using a for loop to find the value
// in the array. Return the location in the array
// where the value was found.
// If the value is not found in the array, return -1.
for (int i = 0; i < words.length; i++) {
if (words[i] == value) {
return i;
}
}
return -1;
}
public static int binarySearch(int[] array, int start, int end, int value) {
// if end is greater than or equal to start, then
// do code below
if (end >= start) {
// integer called mid and set it equal
// to the half way point between start and end
int mid = (start + end) / 2;
// 4. if the array element at mid is equal to value
// then return mid
if (mid < array.length && array[mid] == value) {
return mid;
}
// if the array element at mid is greater than value
// then return the value returned from a call to the
// binarySearch method. Pass in start and mid - 1
// for the end variable.
if (mid < array.length && array[mid] > value) {
return binarySearch(array, start, mid - 1, value);
}
// return the value returned from a call to the binarySearch
// method. Use mid + 1 as the start, and pass in end.
return binarySearch(array, mid + 1, end, value);
}
// else return -1 because the value was not found
return -1;
}
public static int recursiveMultiplication(int number, int times) {
// if times is 1 return number
if (times == 1) {
return number;
} else if (times == 0) {
return 0;
} else {
// else return number + recursionMultiplication(number, times-1)
return number + recursiveMultiplication(number, times - 1);
}
}
public static int recursiveDivision(int number, int numberToDivideBy) {
if (numberToDivideBy> number) {
return 0;
} else {
return 1 + recursiveDivision(number-numberToDivideBy, numberToDivideBy);
}
}
public static int recursivePower(int number, int power) {
if (power == 0) {
return 1;
} else {
return number * recursivePower(number, power - 1);
}
}
// 1. Create a HashMap called roots with Integers for the keys and Doubles for
// the values.
HashMap<Integer, Double> roots = new HashMap<Integer, Double>();
// 2. Using a for-loop, add 500 entries to your HashMap. The key entry will be
// the
// current iteration of the loop (i). The value entry will be the square root of
// i.
for (int i = 0; i < 37; i++) {
roots.put(i, Math.sqrt(i));
}
// 3. Iterate through all the entries in your HashMap displaying the keys with
// their respective
// square roots (values). Use the following format.
for (int i = 0; i < roots.size(); i++) {
System.out.println("The square root of "+i+"is"+roots.get(i));
}
// The square Root of 0 is 0.0
// The square Root of 1 is 1.0
// The square Root of 2 is 1.4142135623730951
// The square Root of 3 is 1.7320508075688772
public class WorldCup {
private char group;
public WorldCup(char group) {
this.group = group;
}
public char getGroup() {
return group;
}
public class Brazil extends WorldCup {
private String name;
private String continent;
private String jerseyColor;
public Brazil(char group, String name, String continent, String jerseyColor) {
super(group);
this.name = name;
this.continent = continent;
this.jerseyColor = jerseyColor;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
+ "}";
}
}
public class Argentina extends WorldCup {
private String name;
private String continent;
private String jerseyColor;
public Argentina(char group, String name, String continent, String jerseyColor) {
super(group);
this.name = name;
this.continent = continent;
this.jerseyColor = jerseyColor;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
+ "}";
}
}
public class Croatia extends WorldCup {
private String name;
private String continent;
private String jerseyColor;
public Croatia(char group, String name, String continent, String jerseyColor) {
super(group);
this.name = name;
this.continent = continent;
this.jerseyColor = jerseyColor;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
+ "}";
}
}
public class Morocco extends WorldCup {
private String name;
private String continent;
private String jerseyColor;
public Morocco(char group, String name, String continent, String jerseyColor) {
super(group);
this.name = name;
this.continent = continent;
this.jerseyColor = jerseyColor;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
+ "}";
}
}
public class France extends WorldCup {
private String name;
private String continent;
private String jerseyColor;
public France(char group, String name, String continent, String jerseyColor) {
super(group);
this.name = name;
this.continent = continent;
this.jerseyColor = jerseyColor;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name + ": {" + "group: " + group + " continent: " + continent + ", jersey color: " + jerseyColor
+ "}";
}
}
public static void main(String[] args) {
WorldCup Qatar = new WorldCup('A');
Brazil brazilTeam = Qatar.new Brazil('F', "Brazil", "South America", "Yellow");
Argentina argentinaTeam = Qatar.new Argentina('C', "Argentina", "South America", "Blue");
Croatia croatiaTeam = Qatar.new Croatia('F', "Croatia", "Europe", "White");
Morocco moroccoTeam = Qatar.new Morocco('F', "Morocco", "Africa", "Red");
France franceTeam = Qatar.new France('D', "France", "Europe", "Blue");
System.out.println(brazilTeam);
System.out.println(argentinaTeam);
System.out.println(croatiaTeam);
System.out.println(moroccoTeam);
System.out.println(franceTeam);
}
}
WorldCup.main(null);