CISC 1115
Introduction to Programming Using Java
Lecture 2
Basic Concepts / Basic Java


Reading from the Text

Chapter 2

Appendix A

What You're Going to Learn in this Class

Software Development: Some Basic Terminology and Concepts

Problem Statements

Let's look at a variation on the Hello World program:

P02.1 Welcomer

Write a program that prompts the user for their first name and displays a welcoming message to the screen.

Developing an Algorithm

Implementing the Algorithm: Programming Languages

Source Files and Editing

Compilation

Execution and Interpretation

Testing

In production programming environments, all the above is a small part of the software development process; the major portion — by far — is the testing and subsequent debugging (finding and repairing errors) phase.

Debugging

Once an error has been detected, the programmer must determine the exact nature of the error, what part of the system is causing the error, and how to fix it There are standard techniques and tools to aid the programmer in this task:

Integrated Development Environments (IDE)

The above steps: editing, compiling, executing, testing, debugging, were all traditionally handled using separate tools. Eventually, as the sophistication and complexity of software evolved, these tools were combined into a single software development tool known as an integrated development environment or IDE. Examples of IDE's are: The first three are industry-level development environments, Dr Java is aimed at students beginning in Java, and Code::Blocks is somewhere in between (and mainly for C++).

Basic Imperative Java

Lecture 1 was a whirlwind tour of the basic elements of computation: imperatives, conditionals, and iteration. In the process, a number of specific language elements were introduced. We present, elaborate, and expand on the imperative aspects in the rest of this lecture.

Basic Programming Elements

Programming involves sequences of the following three operations: Anything that can be computed can be represented as a sequence of the above three

Organizational Elements

As the program sequences get larger, they require organizational techniques for them to be comprehensible and maintainable by human programmers.a The two basic organizational elements are:

Methods

Methods are sequences of code typically comprising a subtask. They are the 'atomic' building block of program organization in Java.

Classes

Classes consist of methods and data (typically variables) that are logically related. They are the basic unit of organization in Java.

Basic Program Structure & Boilerplate

Java programs are composed of classes working in unison; each class consisting of (as mentioned above) methods and possibly data/variables.

Indentation

Indenting the various levels of our code provides a logical structure for the human reader; without it, program code would quickly become impossible to comprehend.
public class ClassName {
	public static void main(String [] args) {
		…	
		…
		if …
			…
		else
			…
		…
		…
		for … {
			…
			if …
				…
			else
				…
		}
		…
	}
}

Imperative Java

The imperative (actions) portion of Java consists of statements instructing the machine:

Literals

Variables

Types

Arithmetic Expressions

Notes

The String + (concatenation) Operator

Expressions

An expression is a sequence of symbols and values that are evaluated into a result value:

Integer Division

If both operands are integer, the result if an integer as well, and the division performed is integer division, i.e., any fractional portion is truncated (discarded).

P02.2 — Integer Division

P02.2 Integer Division
Write an application that illustrates the truncation caused by integer division.
1. public class IntegerDivision {
2.	public static void main(String [] args) {
3.		System.out.println("30/3: " + 30/3);
4.		System.out.println("10/3 + 10/3 + 10/3: " + (10/3 + 10/ 3 + 10/ 3));
5.		System.out.println("1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3: " +
6.			(1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3));
7.	}
8. }
Output
30/3: 10
10/3 + 10/3 + 10/3: 9
1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3 + 1/3: 0
Notes

Floating point

So named because the decimal point can 'float' among the digits; i.e., it is not in a fixed position

P02.2 — Rounding Errors

Floating point values are approximations (unlike integers which are exact values); this imprecision can lead to unexpected results.

P02.3 Rounding Errors
Write an application that illustrates floating point rounding errors
 1. public class RoundingErrors {
 2.     public static void main(String [] args) {
 3.             System.out.println("1/10.0: " + (1/10.0));
 4.             System.out.println();
 5.             System.out.println("10 * 0.1: " + (10 * 0.1));
 6.             System.out.println("0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1: " + (0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1));
 7.             double d1 = 10 * 0.1;
 8.             double d2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
 9.             if (d1 == d2)
10.                     System.out.println("equal");
11.             else
12.                     System.out.println("not equal");
13. 
14.             System.out.println();
15. 
16.             System.out.print("Added: ");
17.             double total = 0;
18.             System.out.print(total + " ");
19.             for (int i = 1; i <= 15; i = i + 1) {
20.                     total = total + 0.1;
21.                     System.out.print(total + " ");
22.             }
23.             System.out.println();
24. 
25.             System.out.print("Multiplied: ");
26.             total = 0;
27.             System.out.print(total + " ");
28.             for (int i = 1; i <= 15; i = i + 1) {
29.                     System.out.print(i * 0.1 + " ");
30.             }
31.             System.out.println();
32.             System.out.println();
33. 
34.             System.out.println("12 * 0.1: " + (12 * 0.1));
35.             System.out.println("0.1 + ... + 0.1 (12): " + (0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1));
36.             d1 = 12 * 0.1;
37.             d2 = 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1;
38.             if (d1 == d2)
39.                     System.out.println("equal");
40.             else
41.                     System.out.println("not equal");
42.             System.out.println();
43. 
44.             System.out.println("4 * 0.1: " + (4 * 0.1));
45.             System.out.println("0.1 + 0.1 + 0.1 + 0.1: " + (0.1 + 0.1 + 0.1 + 0.1));
46.             d1 = 4 * 0.1;
47.             d2 = 0.1 + 0.1 + 0.1 + 0.1;
48.             if (d1 == d2)
49.                     System.out.println("equal");
50.             else
51.                     System.out.println("not equal");
52. 
53.             System.out.println();
54. 
55.     }
56. }
Notes
This section of Notes address coding issues; the actual semantics of the output are dealt with below
Output
 1. 1/10.0: 0.1
 2. 
 3. 10 * 0.1: 1.0
 4. 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1: 0.9999999999999999
 5. not equal
 6. 
 7. Added: 0.0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6 0.7 0.7999999999999999 0.8999999999999999 0.9999999999999999 
 	1.0999999999999999 1.2 1.3 1.4000000000000001 1.5000000000000002 
 8. Multiplied: 0.0 0.1 0.2 0.30000000000000004 0.4 0.5 0.6000000000000001 0.7000000000000001 0.8 0.9 1.0 
 	1.1 1.2000000000000002 1.3 1.4000000000000001 1.5 
 9. 
10. 12 * 0.1: 1.2000000000000002
11. 0.1 + ... + 0.1 (12): 1.2
12. not equal
13. 
14. 4 * 0.1: 0.4
15. 0.1 + 0.1 + 0.1 + 0.1: 0.4
16. equal
Notes
Note that the above is not an error, it is merely the result of the semantics of integer division.

Declarations

Getting Values into Variables: Initialization & Assignment

Initialization

Assignment

Statements

Method Calls

Screen (Console) Output

Keyboard (Console) Input

Put together, the following is the basic pattern for obtaining data from the keyboard
import java.util.Scanner;		// at top of source file … before class header
…
public class … {
	public static void main(String [] args) {
		…
		Scanner scanner = new Scanner(System.in);
		…
		… = scanner.next();	// or nextInt
		…
	}
}

A Comment About Comments

Adding Some Additional Features to our Grading Program

What we're going to spend our time on:

The Essentials

Files used in Lecture 2

Lab for Lecture 2