Lecture 1: Computing Basics
Welcome to CS11111
Let’s take care of a few administrative/social tasks before we dive into the material :-)
[5 min] Introduce yourself to at least two people sitting near you. If you feel comfortable, share whether or not this is your first coding class (and/or share if this is your first time in this building and what you think about the lecture hall)
[3 min] We will use Ed as our main communication hub. Please join Ed (via invite emailed to you) if you haven’t already.
[5 min] Please fill out the Day 1 survey on Ed now. This will be your first participation credit.
[1 min] Write an (anonymous, if you like) post to Ed under the thread “Day 1 comment”: in a sentence or two, why are you a CS major (or why are you taking this course)?
Our first Java Program
One of the simplest programs we can write in Java, or any programming language, is “Hello World”. We are basically asking the program to print out a greeting; this is a common way of verifying that we installed our program correctly and we’re able to see its output. This is typically the first step in debugging: make sure you can run your code and see the result you expected.
Here is what this program looks like in Java:
public class Hello {
public static void main(String[] args) {
// generate some simple output
System.out.println("Hello, World!");
}
}
If you run this program (in a terminal), its output will be Hello, World!
because this is the string (in green) we specified the program to print.
Let’s try this now in a web tool that allows us to run Java code online.
Why can’t we just use English to tell the computer to do something?
Well, perhaps you already can (hello ChatGPT…).
Just ask it How many letter Rs are in the word strawberry?
and then try telling it that it’s wrong. Enjoy.
I wouldn’t use ChatGPT for anything I don’t already know the answer to.
English is a rich but ambiguous language. This means that a simple statement can take on multiple meanings, and only one is right in certain contexts. For example, if we told someone pick up the bat that flew across the field
, and there was both a sick bat (mammal) and a baseball bat (wood) at the edge of a baseball field, which one did we mean? Well, that depends if we are animal control, or a baseball player…but the dangerous worst case scenario is someone picks up the sick bat without gloves and now has rabies.
Even something like stop the car at the stop sign
is too complex to tell a car – this request must be broken down into multiple steps that include pressing the brake (with what pressure? for how long?) and defining where to actually stop the car (before the white line? a foot into the intersection?) which requires further mathematical calculations. And is a rolling stop okay, or do you have to actually come to a stop (as the law requires)? Tesla Motors found out the hard way.
Solution: because natural (human) languages are ambiguous and often too high-level to communicate a new problem to a computer, we need to write code in a high level programming language, like Java. It still reads like English (once you get good at it), but what each sentence means is completely unambiguous (otherwise it wouldn’t compile).
But, how does a computer work?
While we can sort of buy how the Hello World program prints out that string, for a more complete picture let’s visualize how the hardware of a computer can actually take a Java program, run it, and send the output to the screen.
Take notes! This will be on your first coding quiz. Make sure, by the end of the lecture, you understand at least the following topics:
- What are the main hardware components of a computer
- What a file system is, and how to navigate through its folders
- How a file is stored in memory
- At a very high level, explain how the Central Processing Unit (CPU) is used by the computer to execute a computer program that is in memory.
High level vs low level programming languages
As we just saw, a Java program needs to be translated, eventually, into machine-level instructions (of 0s and 1s) in order to be executed on the CPU. This is because on a computer, 0s and 1s can be represented through electrical currents (either on or off), and gates can be used to control how the current flows.
Of course, it would, in theory, be possible to just write our program in binary. But this is obviously bad – Hello World takes thousands of machine-level instructions to run. No human wants to write in this language. Instead, it’s much better to write in a very human readable (high-) level language, like Java, and use compilers and/or interpreters to translate that into binary, as we just saw.
Some languages, like python, are directly translated into machine-level instructions via an interpreter. For example, when you type python hello.py
in your terminal, it would just run the file directly.
In Java, there are actually two steps to getting code to run, which you have/will see in lab this week. In the first step, we use a compiler, called javac
, to translate the Java code into an intermediary, lower-level representation called bytecode. When you do something like javac Hello.java
you’ll notice this generates a bytecode file called Hello.class
. This is a portable intermediary format that you can look at (though it won’t make much sense unless you look up what the bytes are meant to represent) that is computer architecture agnostic. When you feed this bytecode into the interpreter using the java Hello
terminal command, it will convert it into the 0s and 1s that your specific CPU understands. Then, your computer can finally run the program and print out your greeting :-)
Running code in the terminal
The terminal is just another program you can run on your computer, albeit, a special one. It allows you to directly interface with your file system (like Windows Explorer and the Finder on your Mac) as well as run programs from the command line (like javac
and java
).
You’ll get a lot of practice running code in the terminal this semester in your classes. However, during lecture we will typically use the Java Visualizer website, as it’s simpler and has some elegant debugging properties.
Next class
We’ll discuss the basic parts of a program using the Hello World example.
Prep for next time
Make sure you’ve submitted your class participation activity to BB.