Содержание
- 2. Motivations In the preceding lesson, you learned how to create, compile, and run a Java program.
- 3. Introducing Programming with an Example Computing the Area of a Circle
- 4. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[]
- 5. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[]
- 6. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[]
- 7. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[]
- 8. Trace a Program Execution public class ComputeArea { /** Main method */ public static void main(String[]
- 9. 1. Create a Scanner object Scanner input = new Scanner(System.in); 2. Use the methods next(), nextByte(),
- 10. Identifiers An identifier is a sequence of characters that consist of letters, digits, underscores (_), and
- 11. Variables // Compute the first area radius = 1.0; area = radius * radius * 3.14159;
- 12. Declaring Variables int x; // Declare x to be an // integer variable; double radius; //
- 13. Assignment Statements x = 1; // Assign 1 to x; radius = 1.0; // Assign 1.0
- 14. Declaring and Initializing in One Step int x = 1; double d = 1.4;
- 15. Constants final datatype CONSTANTNAME = VALUE; final double PI = 3.14159; final int SIZE = 3;
- 16. Numerical Data Types
- 17. Numeric Operators Name Meaning Example Result + Addition 34 + 1 35 - Subtraction 34.0 –
- 18. Integer Division +, -, *, /, and % 5 / 2 yields an integer 2. 5.0
- 19. Remainder Operator Remainder is very useful in programming. For example, an even number % 2 is
- 20. NOTE Calculations involving floating-point numbers are approximated because these numbers are not stored with complete accuracy.
- 21. Number Literals A literal is a constant value that appears directly in the program. For example,
- 22. Integer Literals An integer literal can be assigned to an integer variable as long as it
- 23. Floating-Point Literals Floating-point literals are written with a decimal point. By default, a floating-point literal is
- 24. Scientific Notation Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as
- 25. Arithmetic Expressions is translated to (3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)
- 26. How to Evaluate an Expression Though Java has its own way to evaluate an expression behind
- 27. Problem: Converting Temperatures Write a program that converts a Fahrenheit degree to Celsius using the formula:
- 28. Problem: Displaying Current Time Write a program that displays current time in GMT in the format
- 29. Shortcut Assignment Operators Operator Example Equivalent += i += 8 i = i + 8 -=
- 30. Increment and Decrement Operators
- 31. Increment and Decrement Operators, cont.
- 32. Increment and Decrement Operators, cont. Using increment and decrement operators makes expressions short, but it also
- 33. Assignment Expressions and Assignment Statements Prior to Java 2, all the expressions can be used as
- 34. Numeric Type Conversion Consider the following statements: byte i = 100; long k = i *
- 35. Conversion Rules When performing a binary operation involving two operands of different types, Java automatically converts
- 36. Type Casting Implicit casting double d = 3; (type widening) Explicit casting int i = (int)3.0;
- 37. Problem: Computing Loan Payments This program lets the user enter the interest rate, number of years,
- 38. Character Data Type char letter = 'A'; (ASCII) char numChar = '4'; (ASCII) char letter =
- 39. Unicode Format Java characters use Unicode, a 16-bit encoding scheme established by the Unicode Consortium to
- 40. Problem: Displaying Unicodes Write a program that displays two Chinese characters and three Greek letters.
- 41. Escape Sequences for Special Characters Description Escape Sequence Unicode Backspace \b \u0008 Tab \t \u0009 Linefeed
- 42. Appendix B: ASCII Character Set ASCII Character Set is a subset of the Unicode from \u0000
- 43. ASCII Character Set, cont. ASCII Character Set is a subset of the Unicode from \u0000 to
- 44. Casting between char and Numeric Types // Same as int i = (int)'a'; int i =
- 45. The String Type The char type only represents one character. To represent a string of characters,
- 46. String Concatenation // Three strings are concatenated String message = "Welcome " + "to " +
- 47. Programming Style and Documentation Appropriate Comments Naming Conventions Proper Indentation and Spacing Lines Block Styles
- 48. Appropriate Comments Include a summary at the beginning of the program to explain what the program
- 49. Naming Conventions Choose meaningful and descriptive names. Variables and method names: Use lowercase. If the name
- 50. Naming Conventions, cont. Class names: Capitalize the first letter of each word in the name. For
- 51. Proper Indentation and Spacing Indentation Indent two spaces. Spacing Use blank line to separate segments of
- 52. Block Styles Use end-of-line style for braces.
- 53. Programming Errors Syntax Errors Detected by the compiler Runtime Errors Causes the program to abort Logic
- 54. Syntax Errors public class ShowSyntaxErrors { public static void main(String[] args) { i = 30; System.out.println(i
- 55. Runtime Errors public class ShowRuntimeErrors { public static void main(String[] args){ int i = 1 /
- 56. Logic Errors public class ShowLogicErrors { // Determine if a number is between 1 and 100
- 57. Debugging Logic errors are called bugs. The process of finding and correcting errors is called debugging.
- 58. Debugger Debugger is a program that facilitates debugging. You can use a debugger to: Execute a
- 59. JOptionPane Input Two ways of obtaining input. Using the Scanner class (console input) Using JOptionPane input
- 60. Getting Input from Input Dialog Boxes String input = JOptionPane.showInputDialog ("Enter an input");
- 61. Getting Input from Input Dialog Boxes String string = JOptionPane.showInputDialog(null, “Prompting Message”, “Dialog Title”, JOptionPane.QUESTION_MESSAGE);
- 62. Two Ways to Invoke the Method There are several ways to use the showInputDialog method. For
- 63. Converting Strings to Integers The input returned from the input dialog box is a string. If
- 64. Converting Strings to Doubles To convert a string into a double value, you can use the
- 66. Скачать презентацию