Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Day #1. Getting Java for your personal computer Go to http://www.oracle.com/technetwork/java/javase/downloads/jdk-netbeans-jsp142931.html. Accept the license agreement and then download the software. Run the setup program. It should install two things on your computer: 1. The Java Development Kit (JDK). This is Java. 2. The NetBeans Integrated Development Environment (IDE). This is your editor and compiler. Creating a New Java Application 1. Create a folder called Java on your M drive for storing all of your programs this semester. 2. Start NetBeans. 3. Choose File | New Project. Under Categories, select Java. Under Projects, select Java Application and click Next. 4. Under Project Name, enter the name of your app. Make sure the Project Location is set to your M drive (\\Host3\Students). If NetBeans has problems with the M drive, you can always save to your desktop or a USB drive. Document1 1 of 7 4/30/2017 5. Ensure that the Create Main Class checkbox is checked. 6. Click Finish. Your project is displayed in the Project window and Main.java opens in the Source Editor. Changing some defaults All options are found by clicking on Tools | Options. Changing some defaults All options are found by clicking on Tools | Options. Indentation Click on the Editor button at the top. Click on the Formatting tab. In the Language drop-down, click on Java. In the Category drop-down, click on Tabs and indents. Set Number of spaces per indent and Tab Size to 3. (You may have to turn off the Use All Languages Settings check box.) Braces Click on the Editor button at the top. Click on the Formatting tab. In the Language drop-down, click on Java. In the Category drop-down, click on Braces. Under Braces Placement, select New Line for all three options. Under Braces Generation, select Generate for all four options. Document1 2 of 7 4/30/2017 Change colors Click on the Fonts and Colors button at the top. Click on the Syntax tab. In the Language drop-down, click on Java. In the Category drop-down, click on Braces. Under Category, click on Comment. Under Foreground, click on the dropdown and select green (or Inherited was the color on mine). Turn on autocomplete for variables: Click on the Editor button at the top. Click on the Code completion tab. In the Language drop-down, click on Java. Turn on Auto Popup on Typing Any Java Identifier Part. Other useful things to know If you have more than one project in your Projects list (left window), right-click and choose Set as main project. NetBeans always runs your main project. The keyboard shortcut to run the main project is F6. The keyboard shortcut to "pretty-print" your source code is alt-shift-F. Document1 3 of 7 4/30/2017 Java Java is an object-oriented programming language. Almost everything in Java is an object. The only data elements in Java that are NOT objects are the basic data types (called primitives in Java). Java’s primitives are: integers, floating-point numbers, characters, and Booleans. Everything else (including Strings and arrays) is an object. Java is a relatively young programming language; it has been around since 1995. The best way to learn how to write programs in a language is to read programs written in the language. 2.2 Writing a Simple Program Sample program (not same as textbook) public class Sample001 { public static void main (String args[]) { int i = 100; double x = 1.5; boolean flag = true; char middleInitial = 'X'; String firstName = "George"; String lastName = "Washington"; System.out.println (" Integer System.out.println (" Double System.out.println (" Flag System.out.println ("Full name " " + middleInitial + ". " is: " + i ); is: " + x ); is: " + flag); is: " + firstName + + lastName); } } This program demonstrates: Basic structure of a Java program. Every program we write will be a class with a public static void main procedure. Every statement ends with a semicolon. The beginning of a method (procedure/function) body is marked with a left curly bracket ({) and the end is marked with a right curly bracket (}). Basic types we will be using in this class: int (also byte, short, and long) double (also float) boolean (can be assigned true or false). char We won't be using it very much. Note that single quotes are used to enclose a char constant. Also note that characters in Java are represented using Unicode (2 bytes per character). String Note that "String" is capitalized and that it is NOT blue and bold like the other data types! This is because it is not a "primitive" (built-in) data type; it is a class/object. String constants are enclosed in double Document1 4 of 7 4/30/2017 quotation marks. Java is case-sensitive!!!!! "String" is not the same as "string"! Variable declaration and initialization. Find out what happens when you don't initialize a variable. It used to be that numbers are initialized to 0, Boolean to false, char to null character, and string to empty string. Now, you get compile errors. Output to the screen To output to the screen, use System.out.print or System.out.println. The argument (only one argument is allowed) to the print/println statements must be a string. You can concatenate strings using the "+" sign. Numbers are automatically converted to strings when used in a print or println statement ("a toString method is not necessary). 2.3 Reading from the Console (later) 2.4 Identifiers Naming Rules Identifiers are used to name variables, constants, procedures and functions (called methods in Java), classes, etc. All identifiers must follow the following rules: Must begin with a letter or an underscore (use a letter) Must be composed of letters, digits, underscores, and dollar signs (don't use dollar signs – they are reserved for system use). Cannot be a keyword, a boolean literal, or the reserved word null. Are case-sensitive! It must be unique within its scope. A variable may have the same name as a variable whose declaration appears in a different scope. In some situations, a variable may share the same name as another variable if it is declared within a nested block of code. (We will cover this in the next section, Scope.) Naming Conventions Variable names and method names: begin with a lowercase letter. If a variable name consists of more than one word, the words are joined together, and each word after the first begins with an uppercase letter, like this: isVisible. Constant names: all upper-case, with underscores used to separate multi-word identifiers. Class names: begin with an uppercase letter. The underscore character (_) is acceptable anywhere in a name, but by convention is used only to separate words in constants (because constants are all caps by convention and thus cannot be case-delimited). 2.5 Variables Declaring a variable: int i; Declaring multiple variables: double x, y; Document1 5 of 7 4/30/2017 Declaring and initializing variables: double x = 10, y = 20; Variables in Java may be either primitive or reference. A primitive variable actually holds its data in the memory location that the variable refers to. Primitive data types are: Integer types: byte (1), short (2), int (4), long (8 bytes) Floating point types: float (4 bytes), double (8 bytes) Character: char (implemented as 16-bit Unicode) Boolean: boolean (can only take on the values true and false) Keyword Description Size/Format (integers) byte Byte-length integer 8-bit two's complement short Short integer 16-bit two's complement int Integer 32-bit two's complement long Long integer 64-bit two's complement (real numbers) float Single-precision floating point 32-bit IEEE 754 double Double-precision floating point 64-bit IEEE 754 (other types) char A single character 16-bit Unicode character boolean A boolean value (true or false) true or false In this class, we will always use int when we need an integer type (unless we need a value larger than 2 billion) and double when we need a floating-point type. A reference variable holds the address of the data that it refers to. All objects are reference variables. The String type is an object in Java, and therefore all strings are reference variables. Document1 6 of 7 4/30/2017 Variable Initialization Local variables and member variables can be initialized with an assignment statement when they're declared. The data type of the variable must match the data type of the value assigned to it. Here are some examples, with the initialization code set in red: public static void main(String[] args) { // integers byte largestByte = Byte.MAX_VALUE; short largestShort = Short.MAX_VALUE; int largestInteger = Integer.MAX_VALUE; long largestLong = Long.MAX_VALUE; // real numbers float largestFloat = Float.MAX_VALUE; double largestDouble = Double.MAX_VALUE; // other primitive types char aChar = 'S'; boolean aBoolean = true; System.out.println("Largest byte: " + largestByte); System.out.println("Largest short: " + largestShort); System.out.println("Largest integer: " + largestInteger); System.out.println("Largest long: " + largestLong); System.out.println("Largest float: " + largestFloat); System.out.println("Largest double: " + largestDouble); System.out.println("Character: " + aChar); System.out.println("Boolean: " + aBoolean); } Parameters and exception-handler parameters cannot be initialized in this way. The value for a parameter is set by the caller. Document1 7 of 7 4/30/2017