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

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