ENGR 131 Lecture Notes

ENGR 131 Lecture Notes - Printing Thursday, August 31, 2006...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
The Programming Process A Java program is a plain text file, called a source code , written in an editor . [ Java virtual machine executes or runs the byte code] The compiler translates the program into an "executable" form called the byte code and discovers bugs. Ex: BlueJ, Dr. Java, Eclipse Alternatively, a development environment can be used instead of a simple editor. Compilation bugs are syntax errors and the like that cause a program to malfunction. Execution bugs are errors that undermine the function of the program. Java: "Hello, World!" // Hello, World! program in Java public class HelloWorld { public static void main( String args[] ) { System.out.println( "Hello, World!" ); } } // begins a comment line. public , class , static , and void are keywords , always lowercase. HelloWorld in line 2 is a class name that, by convention, is one word. main is a method . Parenthesis-enclosed items are parameters . To compile this program, use the command "javac HelloWorld.java" "javac" is the compiler name . This creates a class file called "HelloWorld.class". To run the class file, use the command "java HelloWorld". "java" is the Java virtual machine . println will print a string and break the line, whereas print will print the string without a break. Escape Characters \n for return \t for tab \\ for backslash Formatted Text System.out.printf("", variable); %d for an integer %f for a float or double ( %.2f can be used to tell Java to round displayed decimals to the second place beyond the decimal) %s for a string %c for a character Printing Thursday, August 31, 2006 8:30 AM Lecture Notes Page 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Data Types Identifier : a name, consisting of letters, digits, _, and $ (name must begin with a letter). Variable : a chunk of memory that contains a value . Name : identifier that refers to the variable. Data type : specifies the size of the data chunk needed for storage and specifies how bits are to be interpreted. In Java, all variables are typed ahead of time so that the compiler can allocate the appropriate amount of memory space. Types of data: Integer data ( int - 4B , byte - 1B, short - 2B , long - 8B ) : 3, -17, 0, 43 Floating Point data ( float - 4B , double - 8B ) : 3.1415, 2.1 10 11 , -9.0 Character data ( char - 2B ) : '3', 'A', '%' String data ( String ) - sequences of characters in double quotes Byte, B = 8 bits = 8 binary digits Declaring A Variable int a; or int b=7; or int c=1, d, e=3; Variables can only be declared once. To redefine, simply enter b=6 without the int . Scanner // Import the Scanner library to allow for user input. import java.util.Scanner; // Define a scanner named input. public static Scanner input = new Scanner (System.in); Tuesday, September 05, 2006 8:30 AM Lecture Notes Page 2
Background image of page 2
Math Operations + Addition (integers, doubles, and strings) Subtraction * Multiplication / Division % Remainder a/b = m a%b = r a = b*m + r 5/3 // == 1 (double) 5/3 // == 1.66667 5./3. // == 1.66667 (double) (5/3) // == 1.0 If only integers are used in a calculation, the answer will be given as an integer too,
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 13

ENGR 131 Lecture Notes - Printing Thursday, August 31, 2006...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online