Download Programming Fundamentals: Introduction to Java and more Exams History in PDF only on Docsity!
Programming Fundamentals
Hyesung Park, Na’el Abu-halaweh, Sonal S. Dekhane, Wei Jin, Robert Lutz, Richard W. Price, Tacksoo Im
- Getting Started Table of Contents
- 1.1. Learning Outcomes
- 1.2. Key Terms
- 1.3. Resources
- 1.3.1. Text
- 1.3.2. Video/Tutorial
- 1.4. Overview.........................................................................................................................
- 1.5. Basic Computer Architecture
- 1.6. Programming Basics
- 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?
- 1.7. Binary Number System (Resources)........................................................................
- 1.7.1. Complements
- 1.7.2. Decimal Complements
- 1.7.3. Binary Complements
- 1.8. Algorithms
- 1.8.1. Algorithms are Everywhere!..............................................................................
- 1.8.2. What are Algorithms?
- 1.8.3. Algorithm Examples
- 1.9. Code Conventions for the Java Programming Language...................................
- 1.10. What is Java
- 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
- 1.13. Features of Java Programs.......................................................................................
- 1.13.1. Basic Form of the first program "MyFirstJava"
- 1.13.2. JavaTutor Example
- 1.13.3. Use of print vs. println MyPrintln
- 1.13.4. JavaTutor Example
- 1.13.5. Use of println with ThreeMessages
- 1.13.6. JavaTutor Example
- 1.13.7. Simple Computation
- 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........................................
- 1.14.1. Video: JDK 12 install in Windows
- 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:
- 1.15.2. Write a program named Fibonacci.java that displays the result of......
- 1 + 1 + 2 + 3 + 5 + 8 + 13 +
- 1.15.3. Determine the nines complements of the decimal numbers:.................
- 1.15.4. Determine the one’s and two’s complements of the binary numbers:
- 1.15.5. Convert each binary number to its decimal equivalent:
- 1.15.6. Convert each decimal number to its binary equivalent:
- 1.15.7. Write a program named Formula.java that displays the result of
- 5.6 × 5.6 − 4 × 6.2 × 5.12 × 7.8 − 3 × 5.6
- table: 1.15.8. Write a program named Molecular.java that displays the following
- 1.15.9. SpeedLight.java
- 1.15.10. Chocolate.java
- 1.15.11. Stamps.java
- 1.15.12. Cycle.java
- 1.15.13. FindX.java
- 1.15.14. MaleStudent.java
- 1.15.15. Circle.java
- 1.16. Do You Have Any Questions about Chapter 1?
- Datatype, Variables, and Expressions
- 2.1. Learning Outcomes
- 2.2. Key Terms
- 2.3. Resources
- 2.4. Overview.........................................................................................................................
- 2.5. Variable
- 2.6. Data Type
- 2.7. Declaration Statement: Declare Data Type for a Variable
- 2.8. Assignment Statement: Change the Value of a Variable....................................
- 2.9. Expressions
- 2.10. Output Statement
- 2.10.1. Formatting outputs using printf
- 2.11. Comments and Order of Execution
- 2.12. Declare Constants and Get User Input with Scanner
- 2.12.1. Create a Scanner Object
- 2.12.2. Invoke Methods on the Scanner Object to Retrieve Information
- 2.13. Augmented Assignment Operators
- 2.14. Exercises
- 2.15. Do You Have Any Questions about Chapter 2?
- Conditions..........................................................................................................................
- 3.1. Learning Outcomes
- 3.2. Key Terms
- 3.3. Resources
- 3.4. Overview.........................................................................................................................
- type: 3.5. Relational Operators, Simple Boolean Expressions and the boolean Data
- 3.6. Logical Operators and Compound Boolean Expressions...................................
- 3.7. Operator Precedence
- 3.8. if Statement
- 3.8.1. if Statement
- 3.8.2. if-else Statement
- 3.8.3. Multi-branch (Cascaded) if Statement
- 3.8.4. Nested if Statement...............................................................................................
- 3.9. switch Statement
- 3.10. Summary
- 3.11. Exercises/Problem Solving:
- 3.12. Do You Have Any Questions about Chapter 3?
- Chapter 4: Loops
- 4.1. Learning Outcomes
- 4.2. Key Terms
- 4.3. Resources
- 4.3.1. Text
- 4.3.2. Videos
- 4.4. Introduction
- 4.5. For Loops
- 4.6. While Loops
- 4.7. Common Loop Algorithms
- 4.7.1. Java Tutor Example
- 4.7.2. Java Tutor Example
- 4.7.3. Java Tutor Example
- 4.7.4. Sequence generating loops
- 4.8. Do-while Loops
- 4.9. break and continue
- 4.10. Nested Loops
- 4.11. Exercises
- 4.11.1. Exercise
- 4.11.2. Exercise
- 4.11.3. Exercise
- 4.11.4. Exercise
- 4.11.5. Exercise
- 4.11.6. Exercise
- 4.11.7. Exercise
- 4.11.8. Exercise
- 4.11.9. Exercise
- 4.11.10. Exercise
- 4.12. Do You Have Any Questions about Chapter 4?
- Methods
- 5.1. Learning Objectives
- 5.2. Resources
- 5.3. Key Terms
- 5.3.1. Think Java Vocabulary Chapter
- 5.3.2. Think Java Vocabulary Chapter
- 5.4. Overview.........................................................................................................................
- 5.5. Method Basics................................................................................................................
- 5.6. Example Method
- 5.7. Java Provided Methods
- 5.8. Create Your Own Methods
- 5.8.1. Parts of a Method
- 5.8.2. Method Header
- 5.8.3. Parameter Types
- 5.8.4. Primitive Parameters
- 5.8.5. Reference Parameters
- 5.8.6. Method Signature
- 5.8.7. Parameters
- 5.8.8. Method Body
- 5.9. Types of Methods..........................................................................................................
- 5.9.1. Class methods
- 5.9.2. JavaTutor Example
- 5.9.3. Instance methods
- 5.9.4. JavaTutor Example
- 5.10. Overloaded Methods
- 5.10.1. JavaTutor Example
- 5.11. Calling Methods
- 5.12. Member variables
- 5.12.1. Declaring member variables
- 5.13. Exercises
- 5.14. Do You Have Any Questions about Chapter 5?
- Arrays and ArrayLists
- 6.1. How Does This Topic Relate to Object Oriented Programming?
- 6.2. Learning Objectives
- 6.3. Key Terms
- 6.4. Resources
- 6.4.1. Text
- 6.4.2. Video / Tutorial
- 6.5. Overview.........................................................................................................................
- 6.6. Arrays
- 6.6.1. Creating Arrays
- 6.6.2. JavaTutor Example
- 6.6.3. Indexing Arrays
- 6.6.4. JavaTutor Example
- 6.6.5. Array Properties
- 6.6.6. JavaTutor Example
- 6.6.7. Arrays and For Loops...........................................................................................
- 6.6.8. JavaTutor Example
- 6.6.9. JavaTutor Example
- 6.6.10. Arrays Example
- 6.6.11. JavaTutor Example
- 6.7. ArrayLists
- 6.7.1. Creating ArrayLists
- 6.7.2. JavaTutor Example
- 6.7.3. Indexing ArrayLists
- 6.7.4. JavaTutor Example
- 6.7.5. ArrayList Maintenance - Adding and Removing Elements
- 6.7.6. JavaTutor Example
- 6.7.7. ArrayList Methods
- 6.7.8. JavaTutor Example
- 6.7.9. ArrayLists and For Loops....................................................................................
- 6.7.10. JavaTutor Example
- 6.7.11. Dice Rolling Example, Revisited......................................................................
- 6.7.12. JavaTutor Example
- 6.7.13. ArrayList to Array Conversion, and Vice-Versa
- 6.7.14. JavaTutor Example
- 6.8. Exercises
- 6.8.1. Exercise
- 6.8.2. Exercise
- 6.8.3. Exercise
- 6.8.4. Exercise
- 6.8.5. Exercise
- 6.8.6. Exercise
- 6.8.7. Exercise
- 6.8.8. Exercise
- 6.9. Do You Have Any Questions about Chapter 6?
- Object Oriented Programming
- 7.1. Learning Objectives
- 7.2. Resources
- 7.3. Key Terms
- 7.4. Overview.........................................................................................................................
- 7.5. Classes and Objects
- 7.6. Defining a class
- 7.7. Constructor
- 7.8. Getters and Setters
- 7.9. Static vs. Instance (non-static) methods
- 7.10. Implementing the equals() and toString() method
- 7.11. Using two or more classes together
- 7.12. Exercises
- 7.12.1. Create the Student Class
- 7.12.2. Create the equals() and toString() method for the Student Class
- 7.12.3. Create the School Class
- Do You Have Any Questions about Chapter 7?
- 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:
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 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
Number 1 Binary number 1111 0000 1111 2 One’s complement 0000 1111 0000 3 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
- You are adding 1 to 0000 1111 0000 to make it Two's complement.
More example resources
Two’s Complement Ones' complement
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
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. The White Paper Buzzwords
- 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
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
JavaTutor Example (click “Vizualize Execution”)
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
1.13.4. JavaTutor Example
Java Tutor Example (click “Vizualize Execution”)
1.13.5. Use of println with ThreeMessages
1.13.6. JavaTutor Example
Java Tutor Example (click “Vizualize Execution”)
1.13.7. Simple Computation
1.13.8. JavaTutor Example
Java Tutor Example (click “Vizualize Execution”)
1.13.9. Escape Sequences
- How to use escape sequences
- From Java Tutorials
1.13.10. Programming Errors in General (not only for Java)
- Syntax errors
- Runtime errors
- Logic errors