Chapter 1

36
Chapter 1 CSIS-120: Java Intro

description

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) - PowerPoint PPT Presentation

Transcript of Chapter 1

Page 1: Chapter 1

Chapter 1

CSIS-120: Java Intro

Page 2: Chapter 1

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

Page 3: Chapter 1

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.

Page 4: Chapter 1

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.

Page 5: Chapter 1
Page 6: Chapter 1

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

Page 7: Chapter 1

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.

Page 8: Chapter 1

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

Page 9: Chapter 1

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

Page 10: Chapter 1

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.

Page 11: Chapter 1

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)

Page 12: Chapter 1
Page 13: Chapter 1
Page 14: Chapter 1

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.

Page 15: Chapter 1

Let’s start with a most basic program

Traditionally, the ‘hello world’ program is the first one taught in most classes/texts.

Page 16: Chapter 1

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

Page 17: Chapter 1

• 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

Page 18: Chapter 1

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

Page 19: Chapter 1

• 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

Page 20: Chapter 1

• 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

Page 21: Chapter 1

• 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

Page 22: Chapter 1

• 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

Page 23: Chapter 1

Syntax 1.1 Method Call

Page 24: Chapter 1

• String: a sequence of characters enclosed in double quotation marks:

"Hello, World!"

The Structure of a Simple Program: Strings

Page 25: Chapter 1

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

Page 26: Chapter 1

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

Page 27: Chapter 1

• 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

Page 28: Chapter 1

• 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

Page 29: Chapter 1

HelloPrinter in a Console Window  

Page 30: Chapter 1

  HelloPrinter in an IDE

Page 31: Chapter 1

From Source Code to Running Program

 

Page 32: Chapter 1

What do you expect to see when you load a class file into your text editor?

Self Check 1.14

Page 33: Chapter 1

• 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

Page 34: Chapter 1

• 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

Page 35: Chapter 1

• 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

Page 36: Chapter 1

Program Development Process