1 / 36

Chapter 1

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)

marcin
Download Presentation

Chapter 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 1 CSIS-120: Java Intro

  2. 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

  3. 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.

  4. 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.

  5. 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

  6. 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.

  7. 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

  8. 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

  9. 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.

  10. 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)

  11. 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.

  12. Let’s start with a most basic program • Traditionally, the ‘hello world’ program is the first one taught in most classes/texts.

  13. HelloPrinter.java 1 publicclass HelloPrinter 2 { 3 publicstaticvoid 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!

  14. The Structure of a Simple Program: Class Declaration • 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

  15. 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

  16. The Structure of a Simple Program: main Method • Every Java application contains a class with a main method • When the application starts, the instructions in the main method are executed • publicstaticvoidmain(String[] args) • { • . . . • } • declares a main method

  17. The Structure of a Simple Program: Comments • 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

  18. The Structure of a Simple Program: Statements • 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

  19. The Structure of a Simple Program: Method Call • System.out.println("Hello, World!"); • isa methodcall • 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!")

  20. Syntax1.1 Method Call

  21. The Structure of a Simple Program: Strings • String: a sequence of characters enclosed in double quotation marks: • "Hello, World!"

  22. SelfCheck1.10 • How would you modify the HelloPrinterprogram to print the words "Hello," and "World!" on two lines? • Answer: • System.out.println("Hello,"); System.out.println("World!");

  23. SelfCheck1.12 What does the following set of statements print? System.out.print("My lucky number is"); System.out.println(3 + 4 + 5);

  24. Editing a Java Program • 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

  25. Compiling and Running 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

  26. HelloPrinter in a Console Window

  27. HelloPrinter in an IDE

  28. FromSourceCodetoRunningProgram

  29. Self Check 1.14 What do you expect to see when you load a class file into your text editor?

  30. Errors • 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

  31. Algorithms • 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

  32. Pseudocode • 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

  33. Program Development Process

More Related