Presentation is loading. Please wait.

Presentation is loading. Please wait.

©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()

Similar presentations


Presentation on theme: "©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()"— Presentation transcript:

1 ©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println() Methods 1.4Programming Style 1.5Creating a Dialog Box

2 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Programming Computer hardware by itself doesn't do anything Software in the form of a program tells the computer what to do –Low-level programs are written in the processor's language of 0s and 1s –High-level programming languages are easier for people to understand

3 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Programs process data A typical program does three things 1.Get some input data 2.Transform the input data to produce a result 3.Output the result

4 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Two Distinct Java Environments

5 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sample java Programs DisplayHelloWorld.java –A console application HelloDisplay.java –Graphical (windowed) applications HelloApplet.java –An Applet is a program that is run by a web browser

6 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Algorithms Before you can tell the computer what to do, you have to figure out how to do what needs to be done. An algorithm is a step-by-step procedure describing what needs to be done. Two ways to describe an algorithm –Pseudocode - use words to describe the process –Flowcharts

7 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Algorithms for Summing the Numbers from 1 through 100

8 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Flowchart Symbols

9 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Flowchart for Calculating the Average of Three Numbers

10 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Simple Paint-by-Number Figure

11 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Coding an Algorithm Once you have a correct algorithm, writing the program is easy.

12 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object-Oriented Programming Java is an object-oriented language In object-oriented programs we use classes and objects to help organize our program Objects have –Data –Behavior A class defines what the data and behavior should be for a particular kind of object

13 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using UML Diagrams Pictures can be easier to understand than large amounts of text UML is a diagram language for describing object-oriented programs There are many different kinds of diagrams –We'll use only a few of them

14 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Rectangle Class Suppose we have a class to represent a rectangle. –The properties of a rectangle are the length and width –From these we can determine the area or the perimeter –We should be able to change the properties –We might want to dispplay the properties

15 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Set of Rectangle Objects Once the Rectangle class is defined, you can create rectangle objects Each Rectangle has its own value of length and width

16 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sending a Message to a Rectangle Object We can send a message to a Rectangle object In this case, the object replies to our message by sending back the area

17 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Traditional Translation Process From code to executable Program code is written in a high-level language A processor understands binary code (0s and 1s) A program needs to be translated from high-level code to the low- level code that the computer understands

18 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Compiling and Executing a Java Program Java works a little differently 1.Convert high-level code to byte-code 2.Use another program to execute the byte-code Under Linux 1.javac converts source code into byte code Result has extension.class 2.java executes the byte-code file

19 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Constructing a Java Program Every Java program consists of one or more classes One class must be a main class –The main class must contain a method called main

20 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Simple Java Program

21 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Class Header public class Hello public - modifier specifying visibility of the class class - keyword that is part of the Java syntax for creating a class Hello - an identifier used to name the class

22 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Method Header public static void main( String[] args) public - modifier specifying visibility of the method static - modifier specifying class method void - keyword specifying no return value main - an identifier used to name the method (String[] args) - parameter list

23 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 A Well-Designed Program Is Built Using Modules

24 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Modules We use classes to construct modules in Java Within a class, methods can be used to modularize functionality –If you have some operation that is done in several different places, make a method

25 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Keywords and Identifiers Keywords are words that have special meaning in the java language –Keywords are reserved; they can't be used as names –Keywords need to appear in the correct context Identifiers are used for naming things –Objects, classes, methods all need names –Rules for naming First character must be letter or underscore (_) Remaining characters can be letters, digits or underscore

26 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Syntax Syntax is the rules that specify how the words in your program can be arranged. Programming language syntax is simpler that natural language syntax. –Because it has to be translated by a program - programs are less flexible than people

27 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Output to the Console Most of our programs will be console applications –Output appears on the console The System class has an object called out that you use to send output to the console –out is a PrintStream object –PrintStream has two methods that send text to the console print( text) println( text)

28 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 print and println Text output with print starts at the current cursor position and continues until the text ends Text output with println start starts at the current cursor position and continues until the text ends and then appends a newline –Next output will appear at the beginning of the next line.

29 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Escape sequences What if you need to make a single String extend over more than one line? –Text enclosed in double quotes needs to fit on a single line in your program. Typing return breaks it onto two lines. The backslash (\) character is used for representing characters that you can't just type. –\n is the newline character –\" allows you to put double quotes in a double-quoted string –\\ allows you to put a backslash in your string

30 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Programming Style The java compiler doesn't care how many lines you use for your program statements The people who have to read your code will find it easier if you format it nicely. Look at the examples in the book for one common formatting style Sun has a recommended style for Java programs

31 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Comments We often need to put text into a program to help explain the code –We call this kind of text comments –We want the compiler to ignore comments Java has three types of comment 1.// starts a one-line comment 2./* … */ encloses multiline comments 3./** … */ encloses javadoc comments

32 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Dialog Boxes The JOptionPane class has several class methods for creating common dialog boxes. showMessageDialog is used for providing information to the user of a GUI program Creating GUI programs entirely out of dialog boxes is rather tedious for the user.

33 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 showMessageDialog() Dialog Boxes WARNING_MESSAGEQUESTION_MESSAGE PLAIN_MESSAGE ERROR_MESSAGEINFORMATION_MESSAGE

34 Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Dialog Produced by MultiLineDialog.java


Download ppt "©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()"

Similar presentations


Ads by Google