Tuesday, January 13, 2015

Head-First Java - Chapter 1 - What I Learned

  • The main thing I learned was how the syntax of Java differs from Python, like how:
    • All lines end in semicolons, like how most sentences end in periods or other forms of punctuation
    • Functions and loops are "boxed in" with curly braces, to differentiate the code within them that loops and the code outside of them that runs independently from it
    • White space generally doesn't matter, but can be used to organize the program and help aid in the readability of it
    • Object-oriented programming basically just means chopping up a big, procedural program into seperate classes and methods that would make sense in real life (e.g. class Animal, object Dog)
    • I used to get scared of Java because of the dreaded "public static void main (String[] args)" line, because I never had any clue what it meant. But now I understand that public makes the code, well public, void means there's no return value, and main is just the name of the current method, which is a group of code within a class
    • Most concepts from Scratch and Python, like how integers, strings, Booleans, expressions, loops, etc. work, are nearly the same in Java, but with subtle differences in syntax (e.g. String has to be capitalized when declaring a string variable in Java)

  • I made a separate post about the "99 Bottles of Beer on the Wall" problem, which can be found here.

  • I also made the "PhraseOMatic" program, according to how the chapter did it (code below). I was able to work my way through what was happening while I wrote it, and confirmed my beliefs on the following page, which explained the code:
    • The code creates three lists of words
    • Sets the length of each list, in terms of how many words there are in each one, to a variable
    • Generates a random number within the length of each list, creates a variable for each list with this number (the random function uses a value between 0 and 1, multiplies the length by it, and rounds the float to an integer, in essence creating a random number)
    • Creates a string that combines the three different random values for each list, pulling out a word from each list and concatenating them together
    • Finally, this concatenation is combined with a final string that puts the three words into a proper sentence, and prints that string
public class PhraseOMatic {
  public static void main (String[] args) {   

    String[] wordListOne = {"24/7", "multi-Tier", "30,000 foot", "B-to-B", "win-win", "front-end", "web-based", "pervasive", "smart", "six-sigma", "critical-path", "dynamic"};

    String[] wordListTwo = {"empowered", "sticky", "value-added", "oriented", "centric", "distributed", "clustered", "branded", "outside-the-box", "positioned", "networked", "focused", "leveraged", "aligned", "targeted", "shared", "cooperative", "accelerated"};

    String[] wordListThree = {"process", "tipping-point", "solution", "architecture", "core competency", "strategy", "mindshare","portal", "space", "vision", "paradigm", "mission"};

int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.legth;

int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);

String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];

System.out.println("What we need is a " + phrase); 

  }

No comments:

Post a Comment