“1.1. Learning Outcomes” in “Programming Fundamentals”
1. Getting Started
1.1. Learning Outcomes
Students will be able to
- Understand computer basics.
- Understand programming basics.
- Understand binary number system.
- Begin using the Java programming language.
- Display output on the console.
- Explain the differences between syntax errors, runtime errors, and logic errors.
1.2. Key Terms
Review the important terms.
1.3. Resources
1.3.1. Text
- Think Java Computer Programming by Allen Downey and Chris Mayfield.
- Core Java : Core Java Complete by Cay S. Horstmann
- Essentials of the Java Programming: Essentials by Oracle.com
- Memory Bits and Bites
- Bits and Bytes
- Convert a decimal number to binary numbers
- How the Binary Number System Works
- Binary Addition
- Binary Subtraction
- Method of Complements
1.3.2. Video/Tutorial
- Core Java 11: Fundamentals by Cay S. Horstmann
- An Introduction to Java link
- Understand the Fundamental Concepts of Object-Oriented Programming Why OOP?
- Early Computing
- What is Algorithm?
- Kahn Academy: How Computers Work
- YouTube Video on Fetch Decode Execute Cycle
1.4. Overview
You might have heard about computers many times. You might have questions about how computers perform so many tasks. You might have questions about programming. What is programming? Welcome! If you start to ask all these questions, then you are in the right place to start to learn about programming. We use programming to develop software. A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, like solving a system of equations or finding the roots of a polynomial. It can be a symbolic computation, like searching and replacing text in a document or compiling a program.
Learning a programming language is similar to learning a foreign language, such as French. The ultimate goal of learning a foreign language is to be able to compose sentences to communicate (the communication problem). Similarly, the ultimate goal of learning to program is to write proper statements that solve a particular problem, such as calculating the volume of a sphere or calculating the projectile of a rocket.
A programming language offers its set of different types of statements for programmers to use. Before you can write code using a type of statements, you need to learn its
- syntax
- semantics
For French, syntax is the grammar of the language and semantics is what a sentence means. For programming, when we specify how the code should look like and structural rules, we are talking about syntax. Programming languages, such as Java, have very strict syntax rules, much stricter than a natural language (e.g. French). If your code does not comply with the syntax rules, the compiler (the translator) will not understand your code and therefore cannot translate your code into machine code that can be executed on CPU.
In order to learn how to compose the right sentences in French to convey certain meanings, you will normally first do some reading comprehension exercises to learn how to interpret what sentences mean. Similarly, in order to write code to solve a problem, you first need to read code written by others and understand what that code does. What a piece of code does is similar to the meaning of a sentence in a foreign language. We call this semantics of the code. Only if you understand the semantics (the behavior) of code can you compose the right code to have the desired behaviors.
Through syntax and semantics learning, you will build your tool set of a programming language’s different types of statements. When the times come to write code to solve a problem, you need to pick up the right statements and use them in the right way and right order to achieve the desired behavior.
1.5. Basic Computer Architecture
The semantics of statement (an instruction written in high-level programming language, defined in the section above) is what it does on a computer. We need some very basic understanding and vocabulary in order to be able to state the behavior of a statement.
First, get some understanding of How Computer Works by watching the fun videos on Khan Academy: Khan Academy’s How Computer Works. Please watch the last two videos "CPU, Memory, Input and Output" and "Hardware and Software" (staring Bill Gates and other real technical people).
Second, watch this video on YouTube for a more detailed explanation about how CPU execute instructions in stored in memory: YouTube video (It’s less than 8 mins and please be patient and watch the whole video.)
We can see that memory holds instructions and data. Each memory location has a unique address. Instructions are fetched by CPU and executed by CPU. Besides the instructions, memory also holds data that need to be manipulated by the program.
1.6. Programming Basics
Computer programs, known as software, are instructions to the computer.
You tell a computer what to do through programs. Without programs, a computer is an empty machine. Computers do not understand human languages, so you need to use computer languages to communicate with them.
Programs are written using programming languages.
1.6.1. What is Computer Science?
1.6.2. What is a Computer?
1.6.3. Early Computing
1.6.4. What is Programming?
High-level languages:
Java, Python, C, C++, PHP, Ruby, and JavaScript.
Low-level languages:
It is a machine language and only a computer is capable of reading and interpreting the low-level languages of a collection of binary digits or bits.
How do low-level languages and high-level language work (Compilation of Java Programs)?
Before programs can run, programs in "high-level languages" have to be translated into a "low-level language", also called “machine language”. This translation takes some time, which is a small disadvantage of high-level languages.
But high-level languages have two major advantages:
It is much easier to program in a "high-level language". Programs take less time to write, they are shorter and easier to read, and they are more likely to be correct. "High-level languages" are portable, meaning they can run on different kinds of computers with few or no modifications. Low-level programs can only run on one kind of computer, and have to be rewritten to run on another.
Two kinds of programs translate "high-level languages" into "low-level languages: "interpreters and compilers". An interpreter reads a high-level program and executes it, meaning that it does what the program says. It processes the program a little at a time, alternately reading lines and performing computations.
(Read more from the Compiling Java Program)
1.7. Binary Number System (Resources)
- Memory Bits and Bytes
- Encoding Information
- Decimal to Binary
- Binary & data
- The Binary Number System
- Binary Addition
- Binary Subtraction
1.7.1. Complements
In mathematics and computing, there are two types of complements, the radix-minus-one complement and the radix complements. The complement by itself is the radix complement.
These complements are used to make the arithmetic operation in the digit system easier.
While we are familiar with decimal system, we will discuss the decimal complements first. In the decimal system, there are the nines complement and tens complement.
1.7.2. Decimal Complements
If A is a decimal number, the nines complement of A is obtained by subtracting A from (10^n – 1); and the tens complement of A is that we add 1 with the nines complement of A.
For example
Example 1 Example 2 Example 3 Decimal number 4206 9674 124134 Nines complement 5793 0325 875865 Tens complement 5794 0326 875866
We can illustrate the use of complements by taking 4206. The nine’s complement of 4206 will be
9999 − 4206 = 5793
Then, ten’s complement of 5793 is 5794 as below.
5793 + 1 = 5794
1.7.3. Binary Complements
The principles of complements in the decimal system can be translated into the binary digit system. If B is a binary number, the one’s complement of B is obtained by subtracting each digit of B from 1, and the two’s complement of B is obtained by adding 1.
For example
Binary number 1111 0000 1111
One's complement 0000 1111 0000
Two's complement 0000 1111 0001
We can illustrate the example of binary complements as below.
* Step 1: First, make the one's complement of 1111 0000 1111.
![]()
Simply you will change 0 to 1 and 1 to 0.
* Step 2: Two's complement of One's complement of 1111 0000 1111 which is now 0000 1111 0000. You are adding 1 to 0000 1111 0000 to make it Two's complement.
More example resources
1.8. Algorithms
1.8.1. Algorithms are Everywhere!
- Amazon.com
- Bank Systems
- Cash Registers
- Hospitals
- Internet Browsers
- Search Engines
1.8.2. What are Algorithms?
An algorithm specifies a series of steps that perform a particular computation or task. Algorithms were originally born as part of mathematics - the word "algorithm" comes from the writer Muhammad ibn Musa-al-Khwarizmi - but currently the word is associated with computer science.
More background information about Algorithms
In programming, each step should be clear and defined precisely to solve a particular problem most effectively.
What is Algorithm in brief? - Video Clip Resource
1.8.3. Algorithm Examples
Write an algorithm to add two numbers entered by user:
Step 1: Start
Step 2: Declare variables num1, num2 and sum.
Step 3: Read values num1 and num2.
Step 4: Add num1 and num2 and assign the result to sum. sum←num1+num2
Step 5: Display sum
Step 6: Stop
Write an algorithm to calculate and record the interest on a home mortgage for a client.
(Assuming that balance of the mortgage and the interest rate appear in the client’s record - a collection of related data items. e.g. data on a given client, and a file is a collection of similar records) - Flowchart is provided in Figure 1.
Step 1: Start
Step 2: Obtain name, balance, and rate from the client’s record.
Step 3: Compute: interest = balance * rate.
Step 4: Record client’s name and interest in the interest file.
More examples of Algorithm in Programming
There are two ways of presenting an algorithm. One way is by a flowchart and another way is to write it in pseudocode language. Figure 1 shows two flowcharts. First one is based on the mortgage interest algorithm and second flowchart reads two numbers, a and b. Then prints them in descending order, after assigning the larger number to big and the smaller number to small. Two arrows leaving the decision if a < b. If the answer is no, it is labeled "no" and the other labeled "yes".
1.9. Code Conventions for the Java Programming Language
Coding Standards
Code Conventions by Oracle
1.10. What is Java
Java is one of the most widely used computer programming languages. According to Hostmann (2016), Java is a well-designed programming language and is an efficient and secure environment with a huge library.
- Simple
- Object Oriented
- Distributed
- Robust
- Secure
- Architecture-Neutral
- Portable
- Interpreted
- High-Performance
- Multithreaded
- Dynamic
1.11. History of Java
1.11.1. History of Java by javapoint
1.11.2. A Short History of Java
1.12. Java Versions
Version | Release Date | Extended Support Until |
---|---|---|
JDK Beta | 1995 | |
JDK 1.0 | January 1996 | |
JDK 1.1 | February 1997 | |
J2SE 1.2 | December 1998 | |
J2SE 1.3 | May 2000 | |
J2SE 1.4 | February 2002 | February 2013 |
J2SE 5.0 | September 2004 | April 2015 |
Java SE 6 | December 2006 | December 2018 |
Java SE 7 | July 2011 | July 2022 |
Java SE 8 | March 2014 | March 2025 |
Java SE 9 | September 2017 | N/A |
Java SE 10 | March 2018 | N/A |
Java SE 11 | September 2018 | September 2026 |
Java SE 12 | March 2019 | N/A |
Java SE 13 | September 2019 | N/A |
Java SE 14 | March 2020 | N/A |
1.13. Features of Java Programs
1.13.1. Basic Form of the first program "MyFirstJava"
public class MyFirstJava {
public static void main(String [] args) {
System.out.println("My First Java!");
}
}
When MyFirstJava program runs it displays:
My First Java!
If you compare this output with the original code of MyFirstJava, you will find there is no double quotations. My First Java! is a String literal in the code and it is defined by using double quotation like System.out.println("My First Java!"). System.out.println is to display the String literal you want to print out on the console/screen (Read more)
"System.out.println displays results on the screen; the name println stands for “print line”. Confusingly, print can mean both “display on the screen” and “send to the printer”. In this book, we’ll try to say “display” when we mean output to the screen. Like most statements, the print statement ends with a semicolon (;).
Java is “case-sensitive”, which means that uppercase and lowercase are not the same. In this example, System has to begin with an uppercase letter; system and SYSTEM won’t work."
Following link shows how this MyFirstJava program works step by step by using the Java Visualizer program. MyFirstJava.java by Java Visualizer or use following embedded visualizer by scrolling down and to the right to expand the window.
Class and Methods in brief
A method is a named sequence of statements. This program defines one method named main:
public static void main(String[] args) The name and format of main is special: when the program runs, it starts at the first statement in main (All java programs begin here) and ends when it finishes the last statement. Later, we will see programs that define more than one method.
A class is a collection of methods. In this program defines a class named MyFirstJava. You can give a class any name you like, but it is conventional to start with a capital letter. The name of the class has to match the name of the file it is in, so this class has to be in a file named MyFirstJava.java.
Java uses curly braces ({ and }) to group things together. In MyFirstJava.java, the outermost braces contain the class definition, and the inner braces contain the method definition.
1.13.2. JavaTutor Example
System.out.println appends a newline. This newline starts from the beginning of the next line. If you want to make the String phrases appear in one line, then use print instead of println (see below example).
ClassName is written by a programmer and from the example above, MyFirstJava is the class name.
main is a special method which makes the program execute and println means display a message on the screen. The println statement terminates with a semi-colon(;).
1.13.3. Use of print vs. println MyPrintln
public class MyPrintln {
public static void main(String [] args) {
System.out.print("My First Java class");
System.out.println(" is fun!");
}
}
1.13.4. JavaTutor Example
1.13.5. Use of println with ThreeMessages
public class ThreeMessages {
public static void main(String [] args) {
System.out.println("First, create a java program.");
System.out.println("Second, compile a java program.");
System.out.println("Third, execute a java program.");
}
}
1.13.6. JavaTutor Example
1.13.7. Simple Computation
public class SimpleComputation {
public static void main(String[] args) {
System.out.println("10.5 * 5 / 4 - 5.2 / 2.0 = ");
System.out.println(10.5 * 5 / 4 - 5.2 / 2.0);
}
}
1.13.8. JavaTutor Example
1.13.9. Escape Sequences
1.13.10. Programming Errors in General (not only for Java)
1.14. Creating, Compiling, and Executing a Java Program
You must first set up the environment to compile and execute java programs. To be able to compile and run programs, you must install the JDK and configure it. Java SE 12.0.1 is the latest release for the Java SE Platform (as of June 09, 2019). Orale JDK
1.14.1. Video: JDK 12 install in Windows 10
1.14.2. Video: JDK 12 install on Mac OS X
1.14.3. Compiling and Running a Simple Program
1.15. Exercises
1.15.1. Write a program named Banana.java that displays as below:
A banana is an edible fruit.
If you wait the correct amount of time for it to ripen,
it will be sweet and delicious.
1.15.2. Write a program named Fibonacci.java that displays the result of
1 + 1 + 2 + 3 + 5 + 8 + 13 + 21
1.15.3. Determine the nines complements of the decimal numbers:
4535
507606
78534019
1.15.4. Determine the one’s and two’s complements of the binary numbers:
110011
10001100
10111011101
1.15.5. Convert each binary number to its decimal equivalent:
10
110110
111011001
101010101
1.15.6. Convert each decimal number to its binary equivalent:
2857
4503
46098
694
1.15.7. Write a program named Formula.java that displays the result of
1.15.8. Write a program named Molecular.java that displays the following table:
Atom | Weight (grams / molecule) |
---|---|
H | 1.00794 |
C | 12.0107 |
O | 15.9994 |
1.15.9. SpeedLight.java
The speed of sound is approximately 340 meter per second. Assume that you just saw a lightning flash and you heard the sound of thunder 5 seconds later. Write a program named SpeedLight.java that calculate the distance to a lightning strike based on the time elapsed between the flash and the sound of thunder.
1.15.10. Chocolate.java
Assume there are 9 bags of chocolate bars. Each bag has two chocolate bars. The bag is big enough to have three chocolate bars. If you want to take all the chocolates out of each bag and add three chocolate bars to each bag, how many bags will you need? Write a program to compute the number of bags you will need to add three chocolates instead of two chocolates.
1.15.11. Stamps.java
Susan and Jean just started collecting stamps as a hobby. Susan has 8 endangered animal collection stamps and Jean has 40 racing car collection stamps. How many more does Jean have than Susan? Write a program named Stamps.java that compute the difference between Jean’s and Susan’s collections of the stamps.
1.15.12. Cycle.java
In the Cycle shop, there are 10 bicycles and X numbers of tricycles. Assume that you count the number of wheels of the bicycles and there are 47 wheels of the bicycles and tricycles. How many of tricycle does this Cycle shop have? Write a program named Cycle.java and compute the total number tricycles at the shop.
1.15.13. FindX.java
Write a program named FindX.java to compute the number X based on the following formula:
5 + 19 + x + 47 = 194
1.15.14. MaleStudent.java
Assume that there are 389 students in a small middle school. There are 175 female students. Write a program named MaleStudent.java to compute how many students are male in this middle school.
1.15.15. Circle.java
Write a program named Circle.java that displays the area and perimeter of a Circle that has a radius of 9.5 using the following formula:
area = radius × radius × Math.Pi
perimeter = 2 × radius × Math.Pi
1.16. Do You Have Any Questions about Chapter 1?
We use cookies to analyze our traffic. Please decide if you are willing to accept cookies from our website. You can change this setting anytime in Privacy Settings.