This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Recursion { | |
public static class ExampleOne | |
{ | |
public static void main (String args[]) | |
{ | |
count(0); | |
} | |
public static void count (int index) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestDigitalWallet { | |
public static void main(String[] args) { | |
DigitalWallet w1 = new DigitalWallet(1); | |
w1.deposit(10000); | |
w1.withdraw(5000); | |
DigitalWallet.setMaxWithdrawalPct(.75); | |
System.out.println(w1.canWithdraw(4000)); // false | |
DigitalWallet w2 = new DigitalWallet(2, 20000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DigitalWallet { | |
private static double maxWithdrawalPct = .5; | |
private long accountId; | |
private long balance = 0; | |
private TransactionHistory transactions = new TransactionHistory(); | |
public DigitalWallet(long accountId) { | |
this.accountId = accountId; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Constructors { | |
// Constructors initializes a class for use | |
// They must have the same name as the name of the class | |
public static class Cube { | |
// You can provide initial values for your instance variables like so | |
// (You don't necessarily need to do this in a constructor) | |
private int length = 1; | |
private int width = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Scoping { // class scope begins | |
// Because 'a' is declared just inside the *class curly braces* it is in 'class scope' | |
// therefore its visible everywhere inside the class. This is the 'widest' scope in this file. | |
private int a = 0; | |
// Class variables have class scope! | |
private static String classVariable = "I am a class variable!"; | |
public void methodOne() { // methodOne scope begins |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Question 1 | |
public static boolean isVowell(char letter) { | |
switch (letter) { | |
case 'a': | |
case 'e': | |
case 'i': | |
case 'o': | |
case 'u': | |
return true; | |
case 'y': |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
public class Methods { | |
public static void main(String[] args) { | |
// Methods are just functions that belong to a particular class | |
// Methods are sub-routines. Reusable code. | |
// Method have parameters, are called with arguments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FizzBuzz { | |
public static void main(String[] args) { | |
for(int i = 0 ; i < 100 ; ++i) { | |
boolean fizz = i % 3 == 0; | |
boolean buzz = i % 5 == 0; | |
if (fizz && buzz) { | |
System.out.println("FizzBuzz"); | |
} else if (fizz) { | |
System.out.println("Fizz"); | |
} else if (buzz) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class ForEach { | |
public static void main(String[] args) { | |
// Let's say we have a plain old array, like this one. | |
char[] array = "This is a character array.".toCharArray(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ListBasedQueue<T> implements Queue<T> | |
{ | |
private Node<T> first; // beginning of queue | |
private Node<T> last; // For efficiency, maintain a reference to the least-recently added Node on the queue. | |
public ListBasedQueue() { | |
first = null; | |
last = null; | |
} |