Recursion
Arrays
Classes and Objects
Linked Lists
Misc.
100

How would you extract the last digit in an integer?

Example: 123456 and you want just the 6

n % 10

100

What are the TWO ways to create an array?

String [] arr = new String [ 4 ];

Use this way when you know how large you want the array to be and plan to fill the values LATER

String [] arr = { "Hello", "World", "Cassie", "Logan"};

Use this way when you know the values but keep in mind that you can still overwrite the values later. 

100

Which of these methods count as a Constructor?

1. public Animal( ) {

}

2. public Animal( x ){

        this.x = x

}

3. public Animal( String name, int age ){

        this.name = name;

        this.age = age;

}

4. public Animal( Person owner, String name, int age){

       this.owner = owner;

       this.name = name;

       this.age = age;

}

5. public Animal( "Dog" ){

        this.classification = "Dog";

}

1, 3, and 4.

2 is not correct because you need to pass a data type in the parameter.

5 is not correct because you cannot hardcode the String into a parameter. 


100

How are linked lists and arrays different and when should you use one or the other?

Linked listed can change. They are flexible! You can add or delete nodes.

Arrays are fixed length! You cannot increase or decrease the size of an array

Arrays are faster when you are looking for something because you can index ( arr[0] ) but you need to visit every node if you search something in a linked list

100

All programming languages use the same syntax?

False

Java is different from C and Python and Haskell...


200

Is this an example of recursion?

public static int maybe( int x, int y ){

     if( x == 0 ){

           return 2 * y;

     }

     return 5 * x + y;

}

No! Identify recursion by seeing if there is any method calls within the method that call itself

ex: return maybe( 2+x, 2*y) 

200

Consider the following array:

String [] arr = { "Hello", "World", "Cassie", "Logan"};

What is the value at arr[arr.length-1]?

Also, why can't we do arr[arr.length]?

"Logan" 

We can't do arr[arr.length] because it would go out of bounds of the array. Index is different from the length of an array. 

200

This method is the example of a what method?

public String _____( ){

       return this.name;

}

this is an example of a getter method! 

Getters have 

    > return type in the method signature

    > return something

    > HAVE NO PARAMETERS

200

What is the value of next for the tail node?

The tail node will have the value NULL. it points to nothing.

200

Known as the Father of the Computer

Who is Charles Babbage.

300

How would you compute the sum of an integer array using recursion?

public static int sum( int [] nums, int idx ){

      if( idx == nums.length-1 ){

           return nums[idx];

       }

       else{

              return nums[idx] + sum( nums, idx+1) 

300

Consider the following array:

String [] arr = { "Hello", "World", "Cassie", "Logan"};

What is the value at arr[arr.length-1].length()?

5

Because you access the element at that position in the array and because it is a String, you are able to do .length()

.length != .length()

300

this method is an example of a what method?

public void _____( String name ){

       this.name = name;

}

this is an example of a setter method.

Setters have:

      > void in the method signature

      > return NOTHING

      > have a parameter and use it to SET the attribute or change something.

300

What are the attributes of a Node?

data and Node next. You can have whatever data but you will always have a next attribute of type Node

300

What are the primitive data types we have been using? (Beginning of the semester)

int

double

char

float

400

How would you print values in an array in reverse order using recursion?

reverse( nums, nums.length-1 )

public static void reverse( int [] nums, int idx ){

      if ( idx == 0 ){

           System.out.println( nums[idx] );

           return;

      }

       else{

            System.out.print( nums[ idx ] );

            reverse( nums, idx-1)



400

Arrays can have multiple data types?

No! 

When you declare an array such as:

Animal[] myPets = new Animal[4];

You can ONLY store Animal objects in the array. 

You CANNOT store Strings or ints or whatever

400

Considering the following CONSTRUCTORS, create three instances using different constructors.

public Animal(){


}

public Animal(String name){

        this.name= name;

}


 public Animal(Person owner){

        this.owner = owner;

  }

 public Animal(String name, int age, String classification, String breed){

        this.name = name;

        this.classification = classification;

        this.breed = breed;

        this.age = age;

}


public Animal(Person owner, String name, String classification, String breed, int age, int weight){

        this.owner = owner;

        this.name = name;

        this.classification = classification;

        this.breed = breed;

        this.age = age;

        this.weight = weight;

}

Animal dog = new Animal();


Animal cat = new Animal("Garfield");


Person cassie = new Person("Cassie");

Animal myDog = new Animal(cassie);


Animal stray = new Animal("Smelly", 3, "Cat", "Unknown");

400
What are the attributes of a Linked List?


Node head and Node tail

400

How do I initialize a Scanner that reads a file?

import java.util.Scanner;

import java.util.File;

public static void main( String [] args ){

        File file1 = new File( "example.txt " );

        Scanner scnr = new Scanner( file1 );

        while( scar.hasNextLine() ){
               System.out.print( scnr.nextLine() );

         }

}

500

Determine the problem with this code:

try mystery(8)

public static int mystery( int x ){

    if( x % 2 != 0 && x > 1 ){

          return 1;

    }

   return 2 + mystery( x/2 );

}

This code gives infinite recursion!

it keeps dividing by 2 but the base case checks if it is not even AND greater than 1 (not including 1).


500

Say you have an array:

int [] nums = {1,2,3,4,5}

and you want to create an array with the reverse of nums:

int [] reverse = {5,4,3,2,1}

How would you do that?

int [] reverse = new int [nums.length];

for( int I = nums.length-1; I >= 0; I--){

     for( int j = 0; j < nums.length; j++){
          reverse[j] = nums[I];

      }

}

500

Assume I have an Animals class with attributes: owner, name, weight, age, etc. and assume that I have setters and getters already. 

In the main method, can I do:

Animal myDog = new Animal("Hades", "Doberman", 3)

System.out.println(myDog.name)

No! that is the purpose of getters because attributes are only accessible within in class. They are private unless you use a public method to grab the name. 

500

Trace the following: 

LinkedList list = new LinkedList();

list.add(10)

list.add(20)

list.add(30)

list.remove(20)

Draw on the board :)

500
in a class file, is it necessary to use 


this.name = nameIn;

No! it is just to be clear so that you don't get confused but you can do 

name = nameIn because they are different variable names.