Created
July 11, 2015 22:55
-
-
Save anoadragon453/cdfb05730a3367ac190b to your computer and use it in GitHub Desktop.
This file contains 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.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.*; | |
public class main { | |
public static void main(String[] args) { | |
// Printing out to console | |
System.out.print("Hey!"); // Print on the same line | |
System.out.println("Hi!"); // Print on the next line | |
// Variables | |
Scanner scannnnnner = new Scanner(System.in); // Creating objects | |
int age = 4; // built-in (int, double, String, Boolean) | |
String myName = "Bob"; | |
String hisName = "stupid"; | |
int hisAge = 678; | |
boolean amIHungry = true; | |
// Incremental shortcuts | |
int i = 0; | |
i++; // add one to i | |
i--; // Subtract by one | |
i+=10; // add whatever number (works with other operands (*, / , -) | |
System.out.println(i); | |
// If/else (logic) | |
if (age == hisAge) { // If primitives are equal | |
System.out.println("They're equal!"); | |
} else if (age > (2 * hisAge)) { // greater than (also > < >= <=) | |
System.out.println("I'm older! I think..."); | |
} else if (myName.equals(hisName)){ // comparing Strings | |
System.out.println("He is me and I am him."); | |
} else if (amIHungry){ // boolean | |
System.out.println("Get me a sammich."); | |
} else if (amIHungry || hisAge > age) { // or | |
System.out.println("I have conflicting emotions."); | |
} else if(amIHungry && hisAge > age) { // and | |
System.out.println("I have conflicting emotions."); | |
} else if (amIHungry ^ hisAge > age) { // exclusive or (only one can be true) aka XOR | |
System.out.println("I have conflicting emotions."); | |
} else { | |
System.out.println("i r ded."); | |
} | |
// Scanner (reading in from console/textfile) | |
// Make sure to input java.util.Scanner | |
Scanner scan = new Scanner(System.in); // Create Scanner instance | |
System.out.println("Greeting adventurer! What is your name sir/madam?"); | |
String userName = scan.nextLine(); // Read in next line | |
System.out.println("Hello " + userName + "!"); // Do something with captured input (String) | |
System.out.println("How old are you?"); | |
int userAge = scan.nextInt(); | |
System.out.println("I see you are " + Integer.toString(userAge) + " years old!"); // Convert captured int to string so we can concatenate it... | |
// text file input | |
// Make sure to import java.io.File and java.io.FileNotFoundException | |
File textDocument = new File("textdoc.txt"); | |
try { | |
Scanner scanText = new Scanner(textDocument); | |
// Print out every line of file | |
while(scan.hasNextLine()) { | |
System.out.println(scan.nextLine()); | |
} | |
} catch(FileNotFoundException e) { // import java.io.FileNotFoundException | |
System.out.println("Cannot find text doc."); | |
} | |
// For/while loops | |
// For | |
for(int j = 0; j < 100; j++){ // Loop 100 times | |
System.out.println(j); // Print out j/do something with j | |
} | |
// While | |
int j = 0; | |
while(j < 100) { // Loop until condition is false | |
System.out.println(j); // Print j | |
j++; // Increment j by one | |
} | |
// do/while (run code at least once) | |
do { | |
} while (false); | |
// Arrays/ArrayList | |
// Arrays (static size) | |
// Declare with: Type[] name = new Type[size]; | |
String[] stringArray = new String[5]; // String array with 5 slots | |
String[] stringArrayContainingStrings = {"hey", "there", "compadre"}; // Creating array with variables | |
// Array stuff | |
stringArray[0] = "newString"; // Set to new value at specified index | |
// ArrayLists | |
// Declare with: ArrayList<Type> name = new ArrayList<Type>(); | |
List<String> stringArrayList = new ArrayList<String>(); // Create empty ArrayList | |
// ArrayList methods | |
stringArrayList.add("myNameHere"); // Add object to ArrayList | |
stringArrayList.get(0); // Get the object in the first slot | |
stringArrayList.set(0, "newString"); // Set object at index to new value | |
stringArrayList.remove(0); // Remove object at specified index | |
// Compare arrays | |
// if(arrayOne.equals(arrayTwo)) | |
// Iterating through arrays | |
for (int k = 0;k < stringArrayList.size(); k++) { // Get size of Array with array.length | |
System.out.println(stringArrayList.get(k)); // Print out every value of stringArrayList | |
// Set two ArrayLists equal to each other | |
// stringArrayList.set(k, otherArrayList.get(k)); | |
// For arrays... | |
// ArrayOne[k] = ArrayTwo[k]; | |
} | |
} | |
// Methods and Objects | |
// Methods | |
// Declare: (public/private) (static/[don't put static]) (returnType/void[for not returning anything]) methodName(arguments[ObjectType name]) { | |
// Calling: methodName(arguments); | |
// Method overloading | |
// You can have two methods with the same name, as long as they have different arguments | |
// ex. foo(a) and foo(a,b) | |
// Objects | |
// Declare constructor: public className(arguments) { <code> } // Notice className is returnType and name (and this is always static) | |
// Comments | |
// <-- comment out one line | |
/* comment | |
out | |
multiple | |
lines | |
*/ | |
/* Where to go from here? | |
* Math class (from java.util.Math) | |
* Do your own projects! (abstracts, interfaces, libraries/packages, GUIs) | |
* Reading in from different files (JSON, CSV, etc.) | |
* | |
* Resources: | |
* http://www.projecteuler.com | |
* http://www.reddit.com/r/dailyprogrammer | |
* https://github.com | |
* Java Documentation (https://docs.oracle.com/javase/7/docs/api/) | |
* https://stackoverflow.com (Q/A programming website) | |
* | |
* Pick up more languages (ex. python, javascript, c++, ruby) | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment