├── .gitignore ├── recursion ├── how-to-think-recursively ├── Program.java └── IO.java ├── objects ├── Trainer.java ├── Pokemon.java ├── Student.java ├── Program.java └── IO.java ├── README.md └── arrays ├── TwoDim.java ├── Program.java └── IO.java /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | -------------------------------------------------------------------------------- /recursion/how-to-think-recursively: -------------------------------------------------------------------------------- 1 | When thinking about a problem recursively, think about three things: 2 | 3 | 1. What is/are my base case(s)? 4 | 2. How do I break down this problem into smaller problem(s)? 5 | 3. How do I take the answer of the smaller problem(s) and get the 6 | answer to the problem? 7 | -------------------------------------------------------------------------------- /objects/Trainer.java: -------------------------------------------------------------------------------- 1 | public class Trainer{ 2 | 3 | String name; 4 | int badges; 5 | Pokemon[] pokemons; 6 | 7 | public Trainer(String name) { 8 | 9 | this.name = name; 10 | this.badges = 0; 11 | this.pokemons = new Pokemon[6]; 12 | 13 | } 14 | 15 | public boolean capture(Pokemon p) { 16 | 17 | return false; 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This repository contains example code from Fall 2012 CS111 recitation. 2 | 3 | You can download a zip file of this code by clicking on [this 4 | link](https://github.com/vaibhav2614/recitation/archive/master.zip). 5 | 6 | You can alternatively also [set up 7 | git](https://help.github.com/articles/set-up-git) if you'd like, and this will 8 | make for easier updates of future code 9 | -------------------------------------------------------------------------------- /objects/Pokemon.java: -------------------------------------------------------------------------------- 1 | public class Pokemon { 2 | 3 | String name; 4 | int lv; 5 | double hp; 6 | 7 | /* Constructor for creating a Pokemon Object */ 8 | // Parameters: 9 | // Name of the Pokemon 10 | // Level default to 1 11 | // HP depends on the Pokemon's level 12 | public Pokemon(String name) { 13 | 14 | this.name = name; 15 | this.lv = 1; 16 | this.hp = this.lv * 10 + this.lv * 5; 17 | 18 | } 19 | 20 | public boolean attack(Pokemon enemy) { 21 | 22 | return false; 23 | 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /recursion/Program.java: -------------------------------------------------------------------------------- 1 | public class Program 2 | { 3 | public static void main(String[] args) 4 | { 5 | int x = IO.readInt(); 6 | System.out.println(fibonacci(x)); 7 | } 8 | 9 | public static int factorialIter(int x) 10 | { 11 | int product = 1; 12 | for(int i=1; i<=x; i++) 13 | { 14 | product = product * i; 15 | } 16 | return product; 17 | } 18 | 19 | public static int factorialRec(int x) 20 | { 21 | if (x == 1) 22 | return 1; 23 | return x * factorialRec(x - 1); 24 | } 25 | 26 | public static int fibonacci(int n) 27 | { 28 | if(n == 1 || n == 2) 29 | return 1; 30 | return fibonacci(n -1) + fibonacci(n-2); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /arrays/TwoDim.java: -------------------------------------------------------------------------------- 1 | public class TwoDim 2 | { 3 | public static void main(String[] args) 4 | { 5 | int[][] matrix = new int[5][6];//create an array with 5 rows and 6 cols 6 | matrix[0][0] = 5; 7 | 8 | int var = matrix[0][0]; 9 | int[] var2 = matrix[0]; 10 | 11 | matrix[1][2] //row 1 col 2. 0 indexed 12 | //which one do I do? 13 | matrix[2][1]; 14 | 15 | //iterates over rows 16 | //matrix.length tells me about the number of rows. 17 | for(int i=0; i Type of Object */ 2 | public class Student 3 | { 4 | //Fields / Properties / instance variables 5 | public String name; 6 | public int id; 7 | //non static. Belong to the instance. 8 | public String location; 9 | 10 | public int[] grades; 11 | 12 | //methods to a class 13 | public double gpa() 14 | { 15 | System.out.println("Computing gpa for "+name); 16 | int sum = 0; 17 | for(int i=0; i {2,3,4,5,6} 48 | 49 | for(int i=0; i