xXAASXx / FASTPAGES2

Apache License 2.0
0 stars 0 forks source link

Units 6-10 Review Vocabulary and More! #11

Open xXAASXx opened 1 year ago

xXAASXx commented 1 year ago

Scores of All Homework Assignments Units 6-10!

Unit 6: 0.9/1

Unit 7: 0.8/1

Unit 8: 0.9/1

Unit 9: 0.7/1

Unit 10: 0.8/1

Unit 6

How to declare an array -Examples:

for(int i = 0; i < values.length; i++) { //adding values of an array through a for loop total += values[i]; }

for(int element : values) { //adding values of an array through an enhanced for loop total += element; }

# [Unit 7](https://github.com/xXAASXx/FASTPAGES2/issues/12)
<img width="830" alt="Screen Shot 2022-12-15 at 9 31 23 AM" src="https://user-images.githubusercontent.com/89277945/207928469-13ae6200-e85e-4c74-bdcc-62e1db67ff2a.png">

- ArrayLists are like arrays, but can have vsize changed
- many methods which can be used with arrayLists
- size();
- Returns the number of elements in the list
- add(obj);
- Adds element at the end
- add(index, object);
- Adds element at specific index
- remove(index);
- Removes element from specific index
- set(index, object);
- Replaces element at index with new object
- get(index);
- Returns element at index
- can traverse arrayList with for-loop/enhanced for-loop

// Unit 7 code example import java.util.ArrayList; //never forget to import

public class methodsArrayList { public static void main (String[] args) { ArrayList dogs = new ArrayList(Arrays.asList("Sparky", "Duke", "Noodle")); //remeber the <(Variable Type)> ArrayList dogs2 = new ArrayList(Arrays.asList("Sparky", "Duke", "Noodle")); System.out.println("There are " + dogs.size() + " in the ArrayList"); System.out.println("There are " + dogs2.size() + " in the ArrayList");

    //objects you add must be of the same data type
    dogs.add("Peanut"); //using arrayList functions
    System.out.println("There are now " + dogs.size() + " dogs in the ArrayList"); 

    String myDog = dogs.get(2);
    System.out.println("My dog is named " + myDog);
}

}

//Note: you don't need to declare again after new ArrayList

methodsArrayList.main(null);

# [Unit 8](https://github.com/xXAASXx/FASTPAGES2/issues/13) 

// Unit 8 code example public class Test {

public static void main(String[] args) {

    String[][] arr = { // two indexes, to help store data in a 2D model
        { "Atlanta", "Baltimore", "Chicago" },
        { "Australia", "Boston", "Cincinnati" },
        { "Austin", "Beaumont", "Columbus" }
    };

    String longest = arr[0][0]; //defining the longest array at the moment

    for(int row = 0; row < arr.length; row++) { //incrementing through each part to find largest string
        for(int column = 0; column < arr[row].length; column++) {
            if (arr[row][column].length() > longest.length()) {
                longest = arr[row][column];
            }
        }
     }

    System.out.println(longest); 

    // Use nested for loops to find the longest or shortest string!
    System.out.println("Use nested for loops to find the longest or shortest string!");

}

} Test.main(null);


- 2D Arrays are a lot like arrays, but have an extra index which makes them 2D instead of 1D

# [Unit 9](https://github.com/xXAASXx/FASTPAGES2/issues/14)

- Inheritance is the order in which different attributes, methods, or strings are called
- depends on whentheer class is extended or not, if class contains methods, parameters, strings, etc
- constructor: Where the attributes of a class are defined
- Overriding: allows a subclass or child class to provide a specific implementation of a method that has already been provided by a super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type (or sub-type) as a method in its super-class, then the method in the subclass will override the method in the super-class.
- Polymorphism: running mutliple things at once with the same name, can be done with different amounts of parameters in method
- toString method defines and JSONify's data protected is an access modifier so that the attribute isn't affected by outside modifiers.
- to use constructor of superclass in subclass, need to use super keyword, allowing use of constructors that were made in the superclass

```Example code...
// Demonstration of equals method
// Outputs boolean value of true or false
// If one object equals another
public class Student
{
   private String name;

   public Student(String name)
   {
      this.name = name;
   }

   public static void main(String[] args)
   {
      Student student1 = new Student("Bob");
      Student student2 = new Student("Jeff");
      Student student3 = student1;
      Student student4 = new Student("A");
      Student student5 = student4;
      System.out.println(student1.equals(student2));
      System.out.println(student2.equals(student3));
      System.out.println(student1.equals(student3));
      System.out.println(student3.equals(student4));
      System.out.println(student3.equals(student4));
      System.out.println(student5.equals(student4));

   }
}
Student.main(null);

Unit 10

Screen Shot 2022-12-15 at 9 37 42 AM
// Unit 10 recursion example
public int fact(int n) // a random number inputed, factorial of which wil be calculated
{
    if (n == 1) // base case 
        return 1; //once n is found to be the same value as 1, the code stops
    else    
        return n*fact(n-1); //if values not equal, then the function keeps calling the else part   
}