01
May
11

Helping beginners get started.

In an attempt to make helping beginners with questions about how to start programming, I have created this post. It will discuss some of my favorite books, with a quick explanation about the book. Also, I will include some highly recommended books which I have not gotten around to reading just yet. This post will contain not just java specific books, but c++, and other languages as well, including one non language related book(although it does use java for examples). The post will cover the tutorials which are widely recommended and thought to be a great source of information.

To start, The Oracle Tutorials, these are probably one of the best places to learn java. Everything in the language is covered, including the most advanced topics. The people writing these tutorials are extremely experienced with the language, which makes sense, since the company they work for were involved in the languages design and are responsible for the huge class library which makes java so nice. Each topic in the tutorials are covered with fairly good detail, and include a few runnable examples. Typing the code, compiling it, and running it can be very helpful for a beginner. It will get you familiar with the syntax and will get you learning to debug almost automatically. Let’s face it, as a beginner you will make lots of small mistakes and you will be pulling your hair out trying to fix it. I tend to use these tutorials and I am actually coding something that I am not entirely familiar with. The examples and explanations can be quite helpful. For an absolute beginner, the tutorials may seem like an alien language, however; if you stick with it, you will begin to understand them. The Java API is also another good page to have bookmarked. To help you can generally get away with the following google statement to find any class “Java 6 ClassName”.

I am going to start with java books and move onto c++, and then books on other languages.

My absolute favorite book is Head First Java, the book is thorough, funny, and an easy read. It covers most topics very well, and some of the more advanced topics briefly. This is one of my favorite books to recommend. It taught me many things about java, many of it I still remember well. It is a great book for beginners and could be one of the best books to start your learning process.

Thinking in Java– This book is something that I don’t easily recommend, but looking back at all I learned from it, I can’t not recommend it. At times it seems like Bruce is a very good programmer who doesn’t take the time to break down the topics to beginners. Instead it feels like you are expected to know the content already. If you are reading this book with another it can be helpful. A beginner book can help you learn the basics, while this book teaches you the details. The explanations are very thorough, and offers lots of insight. You will learn lots of good things, as well as read some incredible chapters. One thing I like to note having learned, is the reason why using this to compare two ints is bad

class X{
  int x;
  public int compareTo(Object o)
    X item = (X)o;
    return x - item.x;
  }
}

The main reason this is bad has to do with underflow/overflow producing incorrect results in some examples. If one class has Integer.MAX_VALUE, and the other has -10, it will underflow to Integer.MAX_VALUE – 10, which is not what you wanted. This is one of the good things the book teaches, it also teaches a good chapter on exceptions, strings, collections, and a few others. The HashCode is another thing that is explained very well in this book. It is definitely worth reading.

Java Software Solutions– This is the book I am currently reading, it seems to do a good job explaining the topics. My favorite thing about this book is the incredible amount of exercises the book contains. For a beginner, it is invaluable to have plenty of challenging exercises. This book does a fairly good job, I may update this with more when I finish the book, but for now, I definitely recommend it.

C++ Without Fear– This book is very good for a beginner, it explains the most basic concepts of programming; if you are unfamiliar with loops and other very basic concepts this book is helpful. It also teaches some more complicated c++ topics, when finished you should have a better understanding of programming, and a decent c++ understanding. I read this book before any java books and it helped me a lot in the transition.

Accelerated C++– This book expects you to already know some programming, the more you understand going into this book, the easier it will be to learn. The book covers the STL a lot and it’s quite an interesting read. The authors are very knowledgeable. While I am not done with this book, I am about 2/3 of the way through it and have learned a lot of good c++ topics. Before this book I knew nothings about c++ iterators, with this book I have learned what they are, why they are important, and how to use them. This is a great book, definitely consider it as well.

Practical C++ Programming — Just finished this book the other day. It really covers pointers, and other complicated topics. The book gives you a lot of explanations of the c++ language, when you finish this you will have a fairly good understanding of c++, however; with a language like c++ you will still have plenty to learn.

C++ Primer– I have not read all of this book, I am recommending it because of the reviews I have read, it seems to be a very well written, thorough book. The quick preliminary scan definitely leads me to believe that the book will be a good book for learning.

How to Design Programs– Great book, this is the first book I ever read about programming. It uses a dialect of lisp called Dr.Scheme and does a great job explaining how to program. The language uses an incredible amount of recursion and when you finish the book you will really understand recursion. It offers lots of very challenging exercises, which are very rewarding when you solve them.

SICP– I have not read this book yet, I purchased it recently, however; I have heard it is a great book, I mainly include this book here for the recommendations I have seen on it. I have read a large part of the first chapter and like what I have read. Definitely worth reading.

These are some of the books I would like to suggest right now, I will be including more information as I continue reading books, and I will also include some more advanced books soon as well. I hope this post helps you get started on your programming journey.

07
Mar
11

It’s been a while.

I haven’t been around to post here lately, I decided I should start again to continue getting practice writing. I have been reading a lot, I am almost done reading thinking in java, and I have finished Head First Java, which I found to be a great book. The book I am currently reading is Head First Design Patterns, and I am loving it, Head first books do a great job from this beginners point of view. So far I have covered some interesting patterns which seem to offer a lot of power. These patterns I have learned so far are strategy, observer, factory, and singleton. The general idea seems to be to abstract the design and make the coupling loose. This way you can change classes without effecting other code. The strategy pattern is basically heavy use of composition, which is preferred over inheritance. With composition you give the class an instance variable reference of another class. This other class can be used to add more functionality. You can make the reference take an interface as the type and then through polymorphism there is so much you can add on to the code. This pattern is hard to explain in a short blog post, if you want more in depth discussions of it I would absolutely suggest you read head first design patterns. The last three patterns I discuss will be discussed similarly, with a short explanation which doesn’t go into much detail but gives the basics. Maybe in future posts I will expand upon these patterns, as well as add new ones I have learned. The next patter I want to speak about is the decorator pattern, which is largely used in the Java I/O class. You decorate the input streams with other things(bufferedstreams, and others, which all add some functionality) How it works is actually quite interesting. You create an abstract super class and then concrete subclasses which are the item to decorate. From there you can create an interface which inherits from the abstract class as well. Finally you make concrete decorators. Through inheritance, each decorator is of the same type as the abstract super class, and takes an another instance of the abstract type(or through polymorphism, anything that inherits from the super class)  This allows you to “chain” together multiple decorators and produce very powerful capabilities. One downfall of decorators is that it can get complex since you can end up having many classes(think of the I/O library).

 

The next patter that I would love to discuss is the factory pattern, this pattern decouples item generation and makes the factory and all the possible class dependent on only one class. The factory method defines an abstract factory method which creates an instance of the class for you, you subclass the abstract class and implement the factory method in your subclass and it can handle generating that type of classes. Another factory class is the abstract factory class which is used to create many items.

 

I will go into more detail on factories, and cover the singleton pattern in my next post, for now though, my laptop battery is low, and I need to plug it in. I have learned a lot since my last post and I intend to do my best to update this blog with the new knowledge.

14
Jan
11

Post 2, some basics

I feel last week I was a bit complex/unclear, today I attempt to solve this. I am going to go over some basic program syntax and logic. I recently received a few books, among them, linux/unix shell programming, practical c++, and sams teach yourself java in 21 days. My original intent was to purchase the “Head first Java” book, but it wasn’t available at the particular bookstore I went to. The reviews I read for sams teach yourself java weren’t too great, but I find the book(at least so far) to be pretty good. The first thing I like to do is scan through the book, see what kind of content it has, how it’s written, and the exercises provided. I truly enjoy having good exercises to help me in learning a language. One of my gripes about thinking in java was actually the exercises. This week, like stated previously, I am going over some fairly basic programming concepts. I am currently trying to read approximately 1 chapter a day of linux/unix shell programming, sams, and thinking in java.

Some of the starting concepts I will be covering include compiling, and using if else clauses, and loops. I will also cover declaring variables to be used, this will actually be the first thing I discuss, it is quite important to declare variables to store data.


<access><type><name>

some examples are: int anItem; boolean aTruth;

These are simply a few, when creating a name, the general form is the first letter of the first word is lower case, and all the rest of the words start with a capital letter, as you can see above. The type is used to tell the compiler what kind of information to hold:

  1. byte: holds 8 bits, and can represent from -128 to 127
  2. short: holds 16 bits, represents a lower value of -32,768 to 32,767
  3. int: holds 32 bits, represents a lower value of -2,147,483,648 to 2,147,483,647
  4. long: holds 64 bits, represents a lower value of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  5. float
  6. double
  7. boolean:holds a true and false value
  8. char: which holds a 16 bit unicode item

to find out more about these types, check out http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html.

All the information I am writing out is readily available in multiple places, this is simply me practicing describing the ideas and concepts.

When you program in java you are creating classes, all classes can contain variables, and/or methods, they can also all have a main method, when you create a program to run you must have one public class which is of the same name as the file, when you run the program it first loads the main function, and goes from there, calling what is necessary.

compiling and running programs can be a bit confusing at first, there are a lot of things to learn, and some I still struggle with:

javac filename.java
java FileName

You can also alternatively add -cp and specify a path for it to search for when using java. I have been told that this process is a good thing to become comfortable with, you can also set the CLASSPATH variable, in the system variables menu. One of my problems with compiling generally revolves around using package and imports. Since I am still getting comfortable with the proper way to compile and use packaged and imported items, I will not cover it.

Methods also must specify a few things, and they take the form of:

<access><type><name>

access can be package(which is default and occurs when you don’t name anything), private, public and protected.

  1. public: accessible to anything anywhere to use
  2. private: can only be used by the class, cannot be used by subclasses.
  3. protected can be used by it’s class and all subclasses.

Methods can also return a void type, which means it changes something as opposed to returning some type of item, it can also print some message when finished, a good example of a void return type would be this:

pseudocode

declare an int array: int[] anArray = new int[5];

fill the array with random items

call a sort method and use it on the array, it will change the positioning of the array

finally print the items in the array, which will now be in order

a program is made up of many statements which all must be terminated by a semicolon(;), if you forget semicolons you will receive a compile time error, generally these are fairly simple bugs to find. You declare a class with the class keyword followed by a name and then a statement block({ })


public class isPrime{

variable1, 2, 3;

method 1, 2, 3;

}

Basic use of classes is fairly simple and the difficulty comes when you start sub-classing and practicing polymorphism using abstract classes, and interfaces, all this I will explain, to the best of my ability in later blogs, for now I will leave by creating an example class to find prime numbers, commenting what each item is doing and explain it.


public class IsPrime{
 public static boolean isPrime(int a){
 /**declares a public method(accessible to be used by other classes,
 *static allows it to be called with out first creating a class item,
 *boolean means it returns a true or false
 */
 double x = Math.sqrt((double)a);//finds the square root of a for the loop, it would be very inefficient
 //to loop from 2 to the number everytime you want to determine if a number is prime
 boolean flag = true;//assume a number is prime, if it's determined to not be, change flag to false
 for(int i = 2; i <= x; i++){//basic for loop, sets and initializes i, tests a condition, then increments i
 if(a % i == 0){ //testes if a number is divisible by i, if it is, number is not prime
 flag = false;
 break; //if number is divisible its not prime, break the loop
 }//if it reaches x and flag isn't changed number is prime
 }
 return flag; //return whether the item is prime
 }
 public static void printPrimes(int n){//prints the prime numbers from 1 to n
 for(int i = 1; i <= n; i++){//loop starting at 1, condition is i <= n
 if(isPrime(i))//check if i is prime
 {
 System.out.print(i + ", ");//print i if it's prime
 }
 else
 {
 continue; //i is not prime, continue with the loops
 }
 }
 }
 public static void main(String[] args){//the main method
 int x = 55;
 if(isPrime(x))//if x is prime, print it
 {
 System.out.println(x + " is prime");
 }
 else
 {
 System.out.println(x + " is not prime");
 }
 //print all items from 1 to x
 printPrimes(x);
 }
}

output should look like this:

55 is not prime
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,

10
Jan
11

Thinking in Java, part 1

My blog is going to be on the topic of programming, and my experiences, as a new programmer, learning. I have some basic experience with c++ and drracket, but the programming world is large and I have lots to learn. The goal of this blog is for me to have somewhere to write about what I am learning and explain to the best of my ability what I have learned. In this way, I hope to understand the content more clearly. The book I am currently reading is titled “Thinking in java 4th edition” by Bruce Eckel, I will also be picking up “Head First Java” soon to have as a secondary source of reading.

While Thinking in java is a good book, at times it can be a bit confusing, one of my biggest complaints about the book is the examples and exercises, I feel the examples are not very useful, and the exercises are not challenging, I am in no way bashing the book. These are simply my opinions of the first 300 pages. This blog post will be longer than most since I will be recapping what I have learned so far, what I have struggled with, and what I have enjoyed. The remaining posts will probably be based on 100-150 pages, I will be updating this approximately once a week.

I will try to keep this blog interesting, but at times it may be a bit dry, since I am ultimately writing to post my thoughts and explanations. As I said at the top, I am simply a beginner, I may not always be correct.

And so I begin, the world of programming is vast, and at times it feels overwhelming. I read more, and I realize how much I need to learn. I read books and understand the basics, how to manipulate mathematical problems, but I still need to learn about how to manipulate the computer. The concept of writing a virus seems so out of reach for me, I can’t even understand entirely how they are made at this point, there is so much to learn, I tend to have a hard time sticking with one thing. I begin reading one book, only to find 5 others I want to read as well, Java, Linux, Perl, Python, C++, C#, PHP, Javascript, and more, I eventually want to be fluent in all, but how? The books I have read so far are “How to Design Programs” and “C++ without fear”, and now, I begin Thinking in Java.

Finding this idea of programming has me wishing I discovered it years earlier, I feel old to be just picking it up now. In thinking with Java it starts as many do, with the idea of “Objects as everything”, one of my biggest hurdles is learning the compiler, I understand the basics, but the details tend to leave me confused, it looks like I am correct yet I receive error messages. I know I’m jumping around a bit, time to get back to objects, an object is a way to model real world behavior, you could for instance create a car as an object, moving it around on a race track or a road, which can also be constructed from objects. Objects to me are a kind of building block for Java.

The book quickly moves on to Operators, of which there are many. Many of them are the same as most languages, if you are familiar with C++ you will be familiar with Java operators, The simple +, -, *, /, %, =, == and more, which stand for, addition, subtraction, multiplication, division, modulus, assignment, and finally, equality. Some of these are so basic they do not need any explanation, so I will only explain modulus, assignment, and equality.

  1. Assignment: This operator allows you to “store” information in a variable to be used later.
  2. Modulus: This operator divides two items and returns the remainder 10 % 6 is 4.
  3. Equality: This tests two items to see if they are the same, 10 % 6 == 4, 4 == 4, it returns a Boolean value.

These are only a few of the many operators the language offers, some I left out are ||, &&, and !, which stand for or, and, and not. All three return a Boolean value. In C++ a Boolean value behaves different than in java, any nonzero number can be thought of as true, and a 0 is false, in java it is not like this, Boolean is either true, or false. There is much more to explain on operators, for now, I am going to move on as to not get too boring.

The next step is controlling execution of a program, this is the ultimate way to tell the computer what to do, a common thing to be learned is that the computer can be very stupid, it does EXACTLY what you tell it to, it’s up to you to tell it the correct thing to do with statements.

The big things learned in controlling execution are loops, and if else statements. There are 3 types of loops, I will explain them all in a little bit of detail, including syntax, I will also go over if else statements:

  1. Do…while: this has the following syntax:
     do statement while(condition)

    it will always perform the statement once and it will repeat as long as the condition is true. A condition can be represented in many ways, from simple comparisons such as x < y(x less than y) to more complex conditions(eg: determining if a number is prime, or a palindrome)

  2. next is for loops, they have the following syntax
    for(int i = 0(initialization); i < n(condition); i++(increment)){ statement }

    , the brackets represent a statement block, once it enters the block all statements inside are executed a for loop may seem strange, but it is quite simple. it first creates i and sets i to some value, in this case 0, then it checks if the condition is met, if i < n, if goes into the statement block and executes it, finally, it loops back and increments i(++ is increment), it continues this process until the condition is not met(returns false)

  3. while: while(condition){statement}its similar to do while, this one is not guaranteed to execute the statement however, if it returns true, it executes the statement, if it doesn’t return true, it moves on in the code.
  4. finally is, if else, these are pretty important for controlling execution, the syntax is as follows
     if(condition){statement} else{statement}

    the explanation is simple, if the condition is true, it executes the statement block, if it isn’t it moves to else and executes that block.

The ways to control execution can get quite complex and require commenting with // to keep information easy to understand

You could have something like this: for(int i = 0; i < n; i++){if(i == 10){if(n & i == 0){return true;}}else{return false;}} and this is actually not to involved, it may be a little unclear I am still getting used to the settings. I am not going to go over more chapters I have read just yet, maybe in a day or two, I feel I may have went on a bit long today. I hope I wasn’t too boring for anyone(if anyone does read this)who reads this blog. If I am, please feel free to tell me how to improve the blog and I will try my best.




Follow

Get every new post delivered to your Inbox.