[工学]AnIntroductiontoJavaApplication.ppt_第1页
[工学]AnIntroductiontoJavaApplication.ppt_第2页
[工学]AnIntroductiontoJavaApplication.ppt_第3页
[工学]AnIntroductiontoJavaApplication.ppt_第4页
[工学]AnIntroductiontoJavaApplication.ppt_第5页
已阅读5页,还剩86页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

An Introduction to Java Application,Yaoxiaoling,Topic,Our First java Program Output statements Variable Input statements Arithmetic operators Memory Decision-making statements Relational and equality operators,Our first java Program,Text out “Welcome to java programming”,Talking,What was wrong with your program? What will you do if we want to edit and run our java program? What is function of your program?,Edit java program in notepad,public class Message /* * 这是一个 main 方法。 */ public static void main(String args) /* 输出此消息 */ System.out.println(“欢迎来到 Java 世界!“); ,演示编辑和保存Java程序,编译和运行,Display how to compile and run java program,Before you begin,We need Java development Environment J2SE Development kit /javase/downloads/index.jsp Editor notepad editplus UltraEdit IDE Jcreator Eclipse NetBean,Let us install JDK,/javase/downloads/index.jsp,JDK Environment,Java directories bin : executable files such as java.exe javac.exe lib:java library classes files. include:local files. demo:display programs. jre: java running environment,After we installed jdk.,Setting environmental variables Path variable Classpath variable,Setting the Path Variable,Setting the classpath Variable,classpath,C:Program FilesJavajdk1.6.0_13libdt.jar; C:Program FilesJavajdk1.6.0_13libtools.jar;,What the function of our program?,Output a literal word Welcome to java progaramming.,What we learn from this program,Java program structure Output statement,java program structure,source file, class , method ,statement,Put a class in a source file Put methods in a class Put statements in a method,Our first java program,What goes in a source file? What goes in a class files? What goes in a method files?,public class Welcome1 Class,public static void main(String args) method,System.out.println(“Welcome to java programming”); statements,Reading time,comment Comment is used to enhance your program readability Questions: How many kinds of comment methods does Java provided? Tell us difference among these comments? Class class is a logical unit of java program. Questions: How should we declare a class? How should we name a legal identifier? What is a good classname? How should name a Java file?,Method What is feature of main method? What is function of this method? Output statement What is System.out? What is println? How should we compile and execute a java program? How should we enhance our program readability?,2.2 First Program in Java: Printing a Line of Text (Cont.),Comments start with: / Comments ignored during program execution Document and describe code Provides code readability Traditional comments: /* . */ /* This is a traditional comment. It can be split over many lines */ Another line of comments Note: line numbers not part of program, added for reference,2.2 First Program in Java: Printing a Line of Text (Cont.),Blank line Makes program more readable Blank lines, spaces, and tabs are white-space characters Ignored by compiler Begins class declaration for class Welcome1 Every Java program has at least one user-defined class Keyword: words reserved for use by Java class keyword followed by class name Naming classes: capitalize every word SampleClassName,2.2 First Program in Java: Printing a Line of Text (Cont.),Java identifier Series of characters consisting of letters, digits, underscores ( _ ) and dollar signs ( $ ) Does not begin with a digit, has no spaces Examples: Welcome1, $value, _value, button7 7button is invalid Java is case sensitive (capitalization matters) a1 and A1 are different In chapters 2 to 7, use public class Certain details not important now Mimic certain features, discussions later,2.2 First Program in Java: Printing a Line of Text (Cont.),Saving files File name must be class name with .java extension Welcome1.java Left brace Begins body of every class Right brace ends declarations (line 13),2.2 First Program in Java: Printing a Line of Text (Cont.),Part of every Java application Applications begin executing at main Parentheses indicate main is a method (Ch. 3 and 6) Java applications contain one or more methods Exactly one method must be called main Methods can perform tasks and return information void means main returns no information For now, mimic mains first line Left brace begins body of method declaration Ended by right brace (line 11),2.2 First Program in Java: Printing a Line of Text (Cont.),Instructs computer to perform an action Prints string of characters String - series characters inside double quotes White-spaces in strings are not ignored by compiler System.out Standard output object Print to command window (i.e., MS-DOS prompt) Method System.out.println Displays line of text This line known as a statement Statements must end with semicolon ;,2.2 First Program in Java: Printing a Line of Text (Cont.),Ends method declaration Ends class declaration Can add comments to keep track of ending braces,2.2 First Program in Java: Printing a Line of Text (Cont.),Compiling a program Open a command prompt window, go to directory where program is stored Type javac Welcome1.java If no syntax errors, Welcome1.class created Has bytecodes that represent application Bytecodes passed to JVM,Fig. 2.2 | Executing Welcome1 in a Microsoft Windows XP Command Prompt window.,You type this command to execute the application,The program outputs Welcome to Java Programming!,Modify our First Java Program,public class Welcome /Class,public static void main(String args) / method,System.out.println(“Hello”); System.out.pritnln(“World”); /statements,What is the output of Welcome.java,The output is,Hello World,New Question,We want to output this string in same line with two statements,Hello World,Another output statement,System.out.print(text);,System.out.print();,Now a new output statement System.out.print(text); Read Page 43,System.out.print(“Welcome to ”); System.out.println(“Java Programming”);,The diffrence between print and println,System.out.print(“Welcome to ”); System.out.println(“Java Programming”);,System.out.println(“Welcome to ”); System.out.println(“Java Programming”);,Welcome to Java Programming,Welcome to Java Programming,The difference between printf and println,System.out.print() keeps the cursor on the same line System.out.println() Output at the beginning of the next line,Task,Write a program to output the following text with a single statement.,Welcome to Java Programming,Our statement,System.out.println(“Welcome to java Programming”); System.out.println(“Welcome to Java Programming);,Escape Characters,Reading Page 45 : Welcome3.java,System.out.println(“WelcomentonJavanprogramming”);,Welcome To Java Programming,The Result is:,Escape Sequence,n Backslash ():escape character Indicates special characters be output n:escape sequence New line character,Fig. 2.5 | Some common escape sequences.,2.4 Displaying Text with printf,System.out.printf New feature of J2SE 5.0 Displays formatted data Format string Fixed text Format specifier placeholder for a value Format specifier %s placeholder for a string,Summary,Three output statements System.out.println(); System.out.print (); System.out.printf();,Your job,Output the following text ,with different print statements. Hello World! I am Coming!,Now you are a programmer,A Simple CAI program Enter two numbers ,we want to get the sum of these,then display the sum To solve this task ,we should Enter two number Get the sum of these number Output the sum,New Things-1,Input a number Import java.util.Scanner; Scanner input = new Scanner(System.in); number1 = input.nextInt();,New Things-2,Variables Variables Declaration Datatype variableName; Using the variables Example int number1; number1 = input.nextInt(); number1=4;,Value 4,Memory,Datatype int float double char,New Things-3,Arithmetic calculations Arithmetic operators : + - * / % Example sum = number1 + number2; assignment operator = assign result to variable sum Calculates sum of number1 and number2 (right hand side),You job,Finish this program Page 68 2.15,2.5 Adding integers,With this program,we will learn: Input statement Memory concept Variable Declaration Arithmetic operator,Outline,Addition.java (1 of 2) import declaration Scanner nextInt,Outline,Addition.java (2 of 2) 4. Addition 5. printf,Read an integer from the user and assign it to number2.,Calculate the sum of the variables number1 and number2, assign result to sum.,Display the sum using formatted output.,What we learn from this program?,How should we input by keyboard?,Import java.util.Scanner;,import Scanner from java package: java.util,Question,1.What is the function of the imports statement? 2.Where we locate this statement? 3. For Line 11,What is System.in and what is a Scanner? 4. How to create a Scanner object? 5.For Line13-15, why declare the same type of variables in different lines?,Question,1.How to identify a variable name? 2.Which package are the class System in and why not import this class? 3.How to obtain a number from keyboard? 4.What is the function of System.out.println statement?,2.5 Another Java Application: Adding Integers (Cont.),import declarations Used by compiler to identify and locate classes used in Java programs Tells compiler to load class Scanner from java.util package Begins public class Addition Recall that file name must be Addition.java,2.5 Another Java Application: Adding Integers (Cont.),Variable Declaration Statement Variables Location in memory that stores a value Declare with name and type before use Input is of type Scanner Enables a program to read data for use Variable name: any valid identifier Declarations end with semicolons ; Initialize variable in its declaration Equal sign Standard input object System.in,2.5 Another Java Application: Adding Integers (Cont.),Declare variable number1, number2 and sum of type int int holds integer values (whole numbers): i.e., 0, -4, 97 Types float and double can hold decimal numbers Type char can hold a single character: i.e., x, $, n, 7 int, float, double and char are primitive types Can add comments to describe purpose of variables Can declare multiple variables of the same type in one declaration Use comma-separated list,2.5 Another Java Application: Adding Integers (Cont.),Message called a prompt - directs user to perform an action Package java.lang Result of call to nextInt given to number1 using assignment operator = Assignment statement = binary operator - takes two operands Expression on right evaluated and assigned to variable on left Read as: number1 gets the value of input.nextInt(),2.5 Another Java Application: Adding Integers (Cont.),Similar to previous statement Prompts the user to input the second integer Similar to previous statement Assign variable number2 to second integer input Assignment statement Calculates sum of number1 and number2 (right hand side) Uses assignment operator = to assign result to variable sum Read as: sum gets the value of number1 + number2 number1 and number2 are operands,2.5 Another Java Application: Adding Integers (Cont.),Use System.out.printf to display results Format specifier %d Placeholder for an int value Calculations can also be performed inside printf Parentheses around the expression number1 + number2 are not required,What we learn from this program?,How should we input by keyboard?,Import java.util.Scanner;,Scanner input = new Scanner(System.in);,Create an object of scanner to accept the input from user,What we learn from this program?,How should we input by keyboard?,Import java.util.Scanner;,Scanner input = new Scanner(System.in); int number =,input.nextInt();,Use method nextInt of class Scanner to accept the a integer from keyboard,variable Declaration,Int number1; Int number2;,data type,Variable name,2.6 Memory Concepts,Variables Every variable has a name, a type, a size and a value Name corresponds to location in memory When new value is placed into a variable, replaces (and destroys) previous value Reading variables from memory does not change them,Fig. 2.8 | Memory location showing the name and value of variable number1.,Fig. 2.9 | Memory locations after storing values for number1 and number2.,Fig. 2.10 | Memory locations after calculating and storing the sum of number1 and number2.,2.7 Arithmetic,Arithmetic calculations used in most programs Usage * for multiplication / for division % for remainder +, - Integer division truncates remainder 7 / 5 evaluates to 1 Remainder operator % returns the remainder 7 % 5 evaluates to 2,Fig. 2.11 | Arithmetic operators.,2.7 Arithmetic (Cont.),Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition) Use parenthesis when needed Example: Find the average of three variables a, b and c Do not use: a + b + c / 3 Use: ( a + b + c ) / 3,Fig. 2.12 | Precedence of arithmetic operators.,Fig. 2.13 | Order in which a second-degree polynomial is evaluated.,You are a programmer,Enter two numbers, then compare these numbers, and give the result of comparison. Example1 two number : 300 23 Output 30023 300!=23 300=23 Example2 two number : 300 300 Output 300=300 300=23 300=23,To solve task3,3 steps Enter two numbers Compare the relation between two numbers = = = ! = Output the relation of two numbers,New things-1,Compare two numbers Equality and relational operators = 7 : false 5=7 : true 5= 7 : false,New thing-2:if,If statement compare two number if true output the relation else output nothing If(condition) statement; If a condition is true, then the body of the if statement executed,Outline,Comparison.java (1 of 2) 1. Class Comparison 1.1 main 1.2 Declarations 1.3 Input data (nextInt) 1.4 Compare two inputs using if statements,Outline,Comparison.java (2 of 2) Program output,System.out.print(“Enter number:”); number1= input.nextInt();,Scanner input = new Scanner(System.in); int number1,number2;,System.out.print(“Enter number:”); number2= input.nextInt();,Enter number 777,Enter number 777,777=777,System.out.printf(“%d=%d”,number1,number2);,777!=777,System.out.printf(“%d!=%d”,number1,number2);,777=777,True,false,True,false,True,777!=777,false,777=777,777777,System.out.printf(“%d%d”,number1,number2);,777777,System.out.printf(“%d%d”,number1,number2);,777777,True,false,True,false,777777,false,false,777=777,System.out.printf(“%d=%d”,number1,number2);,777=777,System.out.printf(“%d=%d”,number1,number2);,777=777,True,false,True,false,True,777=777,777=777,True,777=777,End,A simple if,If (condition) statement; the result of Condition will be true or false If (true) statement will be executed If(false) statement will skip. dont forget the left and right (),Your job,Finish this program Page 68 2.16 Page 69 2.26,Page 68 2.16,Key statements if(number1number2) System.out.printf(“%d is larger”,number1); if(number1number2) System.out.printf (“%d is larger”,number2); if(number1=number2) System.out.printf(“These number are equals”);,Page 69 2.26,Key statements int remainder = num

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论