Chapter 1 CSIS-120: Java Intro
Jan 09, 2016
Chapter 1
CSIS-120: Java Intro
What is Programming?
A: It is what makes computer so useful.
The flexibility of a computer is amazing Write a term paper (Word processing program) Balance checkbook (Accounting program) Play a game (Games are programs) Post on Facebook (Facebook is program)
Programming Computer Programs
What is a Computer Scientist?
Somebody who can write programs to solve lots of different problems.
You don’t need to program to use programs
But, often times you need to program to solve an array of different problems.
Computer Hardware
Hasn’t changed as much as you would think.
See Diagram on page 6
Programming has improved just as much as the hardware.
Evolution of programming
Early 60’s Specify instructions in binary
01010101101010101
Late 60’s to 70’s Machine language
Instruction codes and value in hexidecimal 2F 4A0 56 C13
Evolution of programming
70’s to Today Assembly language (more general, more human readable –
get translated into machine language) SET r1, 10 ; r1 will take the place of i
SET r2, 1 ; r2 will hold the value to subtract each timeLOOP1TOP:
SUB r1, r1, r2 ; subtract one from r1 CMP r1, r0 JMP NEQ, LOOP1TOP ;
Late 70’s to Today High level language
Designed for humans to “easily” read/write C++, Java, etc.
Evolution of programming
90’s to Today Graphical languages
makes it even “easier” to program Alice, Labview, etc.
But in the end it all get compiled to binary High Level Language
Assembly Code Machine Instructions Binary
Compilation
High-level languages are compiled into machine language.
A compiler itself is a program that simply translates from high-level lanaguage to low-level.
Most compilers create machine language for a specific type of operating system/computer (called a platform) PC Platform Mac Platform Unix Platform
Compilation & Java
A Java compiler creates “machine language” (Java Byte Code) that can be run on any type of computer.
The computer just needs a Java Virtual Machine (JVM)
The JVM is just a program that can execute Java Byte code for a specific computer.
In theory, your compiled program can run on any computer.
Traditional Compilation (C++)
The compiler eventually creates machine language for a specific operating system/computer platform (Windows, Mac, Linux, etc.)
If you wish to run you program on a PC and a Mac, you need two compilers and need to compile two different running programs.
In contrast, a Java program just needs to be compiled once. But, the PC and Mac will both need installed JVMs (Java
Virtual Machines)
Java is still very popular
Platform independence: Compiled programs can run on any device with a JVM installed.
Java Enterprise Edition EE Java works well with the Internet & WWW Rather than run programs on your computer, Java allows
you to easily create programs that can run on a web server. Java Server Pages (JSP) can “call” Java programs.
Thus, complex programs can be run from web-enabled devices (iPhone, Droid Phone, etc.) without actually “executing” the program locally programs execute on the server.
Let’s start with a most basic program
Traditionally, the ‘hello world’ program is the first one taught in most classes/texts.
1 public class HelloPrinter 2 { 3 public static void main(String[] args) 4 { 5 // Display a greeting in the console window 6 7 System.out.println("Hello, World!"); 8 } 9 }
Program Run: Hello, World!
HelloPrinter.java
• Classes are the fundamental building blocks of Java programs:
public class HelloPrinter
starts a new class
• A class defines a “reusable” programming object.
• In a computer game, the classes might include things like
public class Zombie
public class RocketLauncher
The Structure of a Simple Program: Class Declaration
Each class goes in a .java file
• Every source file can contain at most one public class
• The name of the public class must match the name of the file containing the class:
• Class HelloPrinter must be contained in a file named HelloPrinter.java
• Every Java application contains a class with a main method
• When the application starts, the instructions in the main method are executed
• public static void main(String[] args) { . . . }
declares a main method
The Structure of a Simple Program: main Method
• The first line inside the main method is a comment:
// Display a greeting in the console window
• Compiler ignores any text enclosed between // and end of the line
• Use comments to help human readers understand your program
The Structure of a Simple Program: Comments
• The body of the main method contains statements inside the curly brackets ({})
• Each statement ends in a semicolon (;)
• Statements are executed one by one
• Our method has a single statement:
System.out.println("Hello, World!");
which prints a line of text:
Hello, World
The Structure of a Simple Program: Statements
• System.out.println("Hello, World!");
is a method call
• A method call requires:
• The object that you want to use (in this case, System.out)
• The name of the method you want to use (in this case, println)
• Parameters enclosed in parentheses (()) containing any other information the method needs (in this case, "Hello, World!")
The Structure of a Simple Program: Method Call
Syntax 1.1 Method Call
• String: a sequence of characters enclosed in double quotation marks:
"Hello, World!"
The Structure of a Simple Program: Strings
How would you modify the HelloPrinter program to print the words "Hello," and "World!" on two lines?
Answer:
System.out.println("Hello,"); System.out.println("World!");
Self Check 1.10
What does the following set of statements print?
System.out.print("My lucky number is"); System.out.println(3 + 4 + 5);
Self Check 1.12
• Use an editor to enter and modify the program text
• Java is case-sensitive
• Be careful to distinguish between upper- and lowercase letters
• Lay out your programs so that they are easy to read
Editing a Java Program
• The Java compiler translates source code into class files that contain instructions for the Java virtual machine
• A class file has extension .class
• The compiler does not produce a class file if it has found errors in your program
• The Java virtual machine loads instructions from the program's class file, starts the program, and loads the necessary library files as they are required
Compiling and Running a Java Program
HelloPrinter in a Console Window
HelloPrinter in an IDE
From Source Code to Running Program
What do you expect to see when you load a class file into your text editor?
Self Check 1.14
• Compile-time error: A violation of the programming language rules that is detected by the compiler• Example:
System.ou.println("Hello, World!);
• Syntax error
• Run-time error: Causes the program to take an action that the programmer did not intend
• Examples:
System.out.println("Hello, Word!"); System.out.println(1/0);
• Logic error
Errors
• Algorithm: A sequence of steps that is:
• unambiguous
• executable
• terminating
• Algorithm for deciding which car to buy, based on total costs:
For each car, compute the total cost as follows: annual fuel consumed = annual miles driven / fuel efficiency annual fuel cost = price per gallon x annual fuel consumed operating cost = 10 x annual fuel cost total cost = purchase price + operating cost If total cost1 < total cost2 Choose car1 Else Choose car2
Algorithms
• Pseudocode: An informal description of an algorithm:
• Describe how a value is set or changed:
total cost = purchase price + operating cost
• Describe decisions and repetitions:
For each car operating cost = 10 x annual fuel cost total cost = purchase price + operating cost
Use indentation to indicate which statements should be selected or repeated
• Indicate results:
Choose car1
Pseudocode
Program Development Process