1. Trang chủ
  2. » Công Nghệ Thông Tin

Chapter 2 Java Fundamentals

131 6 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 131
Dung lượng 1,01 MB
File đính kèm Chapter 2 - Java Fundamentals.rar (1 MB)

Nội dung

Weekly Report Chapter 2 Java Fundamentals 1 Contents 1 The Parts of a Java Program 2 The print and println Methods, and the Java API 3 Variables and Literals 4 Primitive Data Types 5 Arithmetic Operat.

Chapter Java Fundamentals Contents The Parts of a Java Program The print and println Methods, and the Java API Variables and Literals Primitive Data Types Arithmetic Operators Combined Assignment Operators Conversion between Primitive Types Creating Named Constraints with final Contents The String Class 10.Scope 11.Comments 12.Programming Style 13.Reading Keyboard Input 14.Dialog Box The Parts of a Java Program  Problem  Write a Java program to display a message (Programming is great fun!) on the screen The Parts of a Java Program (Cont’d)  Run eclipse  Create a new Java project  Enter the project name: MyFirstProject  Create a new Java class:  Enter the class name: Simple  This will create a source code file Simple.java  Enter the code The Parts of a Java Program (Cont’d) The Parts of a Java Program (Cont’d)  Run the program The Parts of a Java Program (Cont’d)  Line 1: //This is a simple Java program  // :    marks the beginning of a comment The compiler ignores everything from the double-slash to the end of the line You can type anything you want Comments help explain what’s going on The Parts of a Java Program (Cont’d)  Line 2:   A blank line Programmers often insert blank lines in programs to make them easier to read  Line 3: public class Simple  This is a class header, and it marks the beginning of a class definition The Parts of a Java Program (Cont’d)   A Java program must have at least one class definition public:    public is a Java keyword must be written in all lowercase letters public is an access specifier, and it controls where the class may be accessed from The public specifier means access to the class is unrestricted (the class is “open to the public”) 10 Reading a Character (Cont’d) String input; char answer; //To hold a line of input // To hold a single character Scanner keyboard; keyboard = new Scanner(System.in); System.out.println(“Are you having fun? (Y/N)”); input = keyboard.nextLine(); // Get a line of input answer = input.charAt(0); // Get the first character 117 Mixing Calls to nextLine with calls to Other Scanner Methods  When we call one of the Scanner class’s methods to read a primitive value, then call the nextLine method to read a string → The string is not read  Because the Scanner class’s methods to read a primitive value left a newline character in the keyboard buffer 118 Mixing Calls to nextLine with calls to Other Scanner Methods 119 Mixing Calls to nextLine with calls to Other Scanner Methods 120 Mixing Calls to nextLine with calls to Other Scanner Methods  To fix the problem  Insert the nextLine method to consume, or remove, the newline character that remains in the keyboard buffer 121 14 Dialog Boxes  A dialog box is a small graphical window that displays a message or requests input  The JOptionPane class allows you to quickly display a dialog box   Display dialog: To display a message Input dialog: To request input 122 Display Message Dialogs  The showMessageDialog method is used to display a message dialog JOptionPane.showMessageDialog(null, “Hello World”); 123 Display Input Dialog  The showInputDialog method is used to display an input dialog String name; name = JOptionPane.showInputDialog(“Enter your name”);  The name variable will reference the string value entered in the text field Text field 124 Converting String Input to Numbers  Because the showInputDialog method always returns the user’s input as a String, even if the user enters numeric data  We must convert the input string to a numeric value if the string represents numeric data 125 Converting String Input to Numbers (Cont’d)  Methods for converting strings to numbers  Byte.parseByte Convert a string to a byte byte num; Num = Byte.parseByte(str);   Double.parseDouble  Convert a string to a double 126 Converting String Input to Numbers (Cont’d)  Float.parseFloat   Integer.parseInteger   Convert a string to an int Long.parseLong   Convert a string to a float Convert a string to a long Short.parseShort  Convert a string to a short 127 PayrollDialog  Write the Payroll program using dialogs to get user’s input and display user’s information 128 PayrollDialog 129 PayrollDialog 130 131 ... of - 128 to + 127 short bytes Integers in the range of - 32, 768 to + 32, 767 int bytes Integers in the range of -2, 147,483,648 to +2, 147,483,647 long bytes Integers in the range of -9 ,22 3,3 72, 036,854,775,808... Parts of a Java Program (Cont’d)      Java is a case-sensitive language All Java program must be stored in a file with a name that ends with java Comments are ignored by the compiler A java file... Simple .java  Enter the code The Parts of a Java Program (Cont’d) The Parts of a Java Program (Cont’d)  Run the program The Parts of a Java Program (Cont’d)  Line 1: //This is a simple Java program

Ngày đăng: 27/09/2022, 22:10