Homework 1
Install the virtual machine onto your Bancroft machine. There are instructions and a link on the course "Resources" page. It does not need to be turned in, but you should make sure you can code and run our Hello World program below.
Are there any shortcuts for learning a second language?
Excellent question. There definitely are.
Learning your first language is the hardest. There are lots of new concepts to understand. For most people, it is the first time they have to work so strictly with logic and grammar.
The good news is that once you learn the basics of programming, switching to another language is fairly easy. Here is a mnemonic to make it even easier. Finding these 7 things will get you up and running in any new language fairly quickly:
- Comparitor
- Loop
- Output
- Variable
- Editor
- Run
- Sample
| Know the syntax for comparing any two variables or constants. e.g.: if(X>Y), if(X>0||Y!=0), if(X==Y), if(X===Y) | |
| Know one or more methods of looping over code. e.g.: for(int i; i<10; i++){}; while(1){}. | |
| Know how to create output. e.g. print "Hello"; printf("%s\n", "Hello"); System.out.println("x"); | |
| Know how to create a variable. e.g. int x = 0; float a,b,c; q=0 | |
| Find at least one editor you can use in your environment and get comfortable with it. Some editors will make your life easy by recognizing your language and highlighting keywords. Some editors are more complicated to learn than others. If you are going to focus on one language and OS exclusively then you should find a good IDE. For this class we are going to use basic text editors in Linux. Good options include gedit, nano, emacs, and vi. | |
| Know how to run a program from either the command-line or GUI. | |
| Most importantly - get a working sample of the new language. A small piece of working code will fill in the blanks on syntax, common includes, etc. Just Google for 'Java sample'. |
How does Java work?
There are a few aesthetic differences between Java and C++; they're not scary, and you'll get used to them soon enough. First, what does a Java program look like? As with C++, we'll have a simple text file with a special file extension; in C++, we used .cpp; in Java, we use .java. First, it should be called something that starts with a capital letter. This is our first program in a new language, so, as required by CS law, we'll call it HelloWorld.java. Our file follows:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Let's break this down line by line.
In C++, we didn't learn about classes or structs until the very end of the semester; in Java, there's no getting around them. EVERYTHING is part of a class. Our first line declares that class, HelloWorld. The class name MUST match the filename (HelloWorld.java). Otherwise, the compiler will yell at you.
The next line declares a function, namely, the main function. We know what void and main refer to, because they are the same language as C++. We will have plenty to say about public and static as the semester goes along.
You might guess that the arguments of the main function refer to an array of Strings, called args, and you would be right. No pointers to Strings, instead the argument type is just "an array of strings!" This array contains strings entered at runtime by the user; we'll play with this some.
The third line is Java's admittedly wordy way of saying "cout". There is a class called System, which contains an object variable called out, which has a function called println, which accepts a String. That function prints it to the screen.
Compiling/Running
We're not going to be working in an IDE like Visual Studio or Codeblocks. This semester is all Linux. You compile a .java file with the command javac, and you run it with the command java:
$ javac HelloWorld.java $ ls HelloWorld.java HelloWorld.class $ java HelloWorld Hello, World!
We'll let Systems teach you more about Linux.
Importing Libraries
Most Java programs need to import various libraries. This is similar to the C++ #include directive. Java libraries are generally stored as separate JAR (Java ARchive) files. Most helper classes are nested and stored hierarchically. The line below import the Scanner class, which we will need in the User Input section.
import java.util.Scanner;
User Input
So, we've seen printing out, but what about reading in from the user (like with cin)? For this, we need a class called Scanner. We'll explain these lines as best we can now, but they'll make more sense later.
Scanner in = new Scanner(System.in); int x = in.nextInt(); double d = in.nextDouble(); String s = in.next();
...and so on.
Functions
If you could make functions in C++, you can make functions in Java. If you couldn't make functions in C++... we've got work to do. Let's add a function to our program.
public static int square (int value) {
return value*value;
}
It doesn't matter if this comes before or after our main function, and we don't need any prototypes! For now, don't worry about the "public static." We'll learn about those words later. The int is the return type, and "square" is the name of the function, just like in C++.
Problems
- Modify the HelloWorld program to print the numbers from 0-9 and indicate whether they are odd or even. This program should demonstrate everything you need to fill in the CLOVERS acronym.
- Write an entire program to be saved as Class2.java, which takes an integer in from the user, and prints it back out again.
- Add a function which calculates the factorial of the input integer. Implement it iteratively and recursively.