Presentation is loading. Please wait.

Presentation is loading. Please wait.

Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program.

Similar presentations


Presentation on theme: "Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program."— Presentation transcript:

1

2 Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program Section 4 - Three Types of Java Output Programs Section 5 - Printing to a Console Window Section 6 - Binary Representation of Information 1 Go

3 Chapter 1 - Section 1 What is Java? 2

4 1.1 What is Java? Java is an Object-Oriented programming language that allows you to write the code that will produce several different kinds of programs: 1) Stand-alone programs that will run on any computer if the application file is copied to the computer. 2) Applet programs that can be embedded in web pages, like games or other kinds of web applications that can be run on any computer using a browser. 3) Console programs that give output in a text window which may give the results of calculations or printed information. 3

5 Chapter 1 - Section 2 The Four Steps for Creating & Running a Java Program 4

6 1.2 Four Steps of Running a Java Program Step 1. Write the source code in a.java file Step 2. Compile the source code into byte code. A.class file is created. Step 3. The JVM interprets the byte code into machine language 1s and 0s. Step 4. The computer executes the machinge language of 1s and 0s and displays the program. 5

7 1.2 Source Code is Written in a.java File Step1. When a programmer writes Java code, it is stored in a source code file that has a.java extension like Rocket.java. The source code file is what the programmer compiles and runs to see the output of a program. Source code can be typed in any simple text editor, but we will use Eclipse, which is both a text editor and a compiler. Eclipse is a nice because it will point out syntax errors when java code is typed incorrectly. A program will not run until all syntax errors are removed. 6

8 1.2 Source Code is Compiled to Byte Code Step 2. An IDE (Integrated Development Environment) like Eclipse, is a piece of software that has a built in compiler that translates the Java source code into Java byte code. When source code is compiled into byte code, the byte code is stored in a file that has the same name as the source code file but with a.class extension. It is not a text file and you cannot open it! For example, the source code file Rocket.java will be compiled into a byte code file named Rocket.class. The file is created during the compiling process, however, Eclipse sometimes translates.java files into.class files behind the scenes before you ever compile them. You can see these files in your package folders if you open your workspace folder on your hard drive. 7

9 1.2 Byte Code is Interpreted to Machine Code Step 3. Before a program can be displayed, the JVM (Java Virtual Machine) must interpret the byte code into machine language 1s and 0s. The JVM is software that acts like hardware. That is why it is called a virtual machine. It’s this software that allows Java to run on a computer with any operating system. The Java Runtime Environment in your system software contains the JVM. Java bytecodes are translated on the fly to 1s and 0s so they can be executed. The main advantage of Java is that there are JVMs for every kind of computer and this makes the code portable from one platform to another. A JVM was automatically installed when your computer’s system software was installed and updates to the JVM may be contained in any system software updates you install now or in the future. 8

10 1.2 Machine Language Code is Executed Step 4. Finally, the machine language code (1s and 0s) are executed. This is the code that the computer understands at the lowest level, This allows the program to appear in its runnable form, either in a console window, an applet window or a GUI (Graphical User Interface) window. Again the overall process is …. 9

11 Chapter 1 - Section 3 The Framework of a Simple Java Program 10

12 1.3 Framework of a Simple Java Program The Framework of a simple console or standalone program: 1. package declaration for the file 2. import statements that bring in any additional code from other classes 3. class definition line that declares the class 4. main method that starts the program running package ch01; public class Rocket { public static void main(String args[ ]) { // code for program is contained here } // ending curly brace for main method } // ending curly brace for the RobotMan class There are no import statements for this program. All Java console and JFrame (standalone) programs have this framework with a main method. The framework for applets is different. More on that later. 11

13 1.3 Classes in Java Most of the time, every.java source code file contains 1 class. It is possible to have more than one class in a.java file, but this is usually only done in advanced programming projects where the code is more complicated and there is a reason for doing it. So don’t worry about that for now. So assume that every.java file contains only one class. However, a Java program can contain more than one.java file, but to start all of our programs will contain only one.java file. A class is a module of code that can stand alone by itself and if made “public” other files can “see it” and “access it”. Eclipse is a nice IDE for organizing Java files so that classes are accessible to each other if they are in the same package folder or if an appropriate import statement that tells the path to a class is used. Eclipse manages this behind the scenes automatically and lets you know if something is “unorganized” so you can organize it. 12

14 1.3 Declaring Classes in a File A class is declared with a line like: public class Rocket and has a set of curly braces that go with it { and }. In Java, a class like Rocket must be stored in a file named Rocket.java. If they don’t match, then Eclipse or any other IDE will flag it with an error and it must be corrected before the file is used in a program. Java programs can be designed so that they contain just one class. In the next section, we will look at some examples. 13

15 Chapter 1 - Section 4 Three Types of Java Output Programs 14

16 1.4 Three Types of Java Output Programs There are basically three types of Java output programs that we have mentioned before that a programmer can create: 1. A console text output program. 2. A standalone graphics or GUI program that can be displayed in a JFrame window. 3. An applet graphics or GUI program that can be displayed in a JApplet window or in a browser. 15

17 1.4 Console Text Output Programs 16 Some Java programs, like Rocket or DistanceConverter, are displayed only in a console window, because they show only text output. You will learn how to do a lot more than draw things with text characters in a console window. Some programs may make mathematical calculations and display them in a GUI text field. Console programs can be made into stand- alone applications if necessary.

18 1.4 Stand-Alone Programs A stand-alone program is an application file that you can double click on to start the program and run it. You have done this with numerous applications that are on your computer. For example, First Class, Camino, OpenOffice, FireFox, DreamWeaver and others are all stand-alone applications. So you can use Java to create stand-alone applications of these types. It just takes a little know how and some experience. 17

19 1.4 Graphics & GUI Programs Besides console text programs, you can have applet or standalone programs that contain graphics or GUI components: 1. GUI components are elements in a program like text fields, buttons, labels, text areas, or menus. 2. Graphics are lines, and curves, and pretty much anything that can be drawn. So you can have quite a few different combinations of the above two: 1. An applet program with GUI components. 2. An applet program with graphics. 3. A standalone program with GUI components. 4. A standalone program with graphics. 5. An applet or standalone program that has both GUI components and graphics. 18

20 1.4 Graphics Output Programs Applet or Stand-alone Graphics program may contain lines, arcs, ovals, rounded-rectangles, etc. GUI or graphics programs pretty much look the same whether they are in an applet or standalone program. 19

21 1.4 Applet GUI Output Programs Java applets are Java programs that run in a Web browser. A JVM is incorporated into every browser, so the program can be interpreted, executed and displayed inside the browser. Any game you have played online is more than likely a Java applet program. Both applet and standalone programs can have GUI components as seen here. Because Java programs run inside a Virtual Machine, it is possible to limit their capabilities. What this means is that some other programmer can’t inject a virus into the code you have written for an applet program. Therefore, everybody doesn’t have to worry about a Java applet infecting their computers with a virus that will erase files on their hard drive or steal sensitive information. A GUI program with fields, buttons, labels, and a text area. 20

22 1.4 Applets - Byte Code - Browsers As we mentioned, applets are small Java programs that are embedded in web pages. When you load a web page that has a game in it, the game is an applet program. More specifically, the applet code is compressed into a jar file that contains the byte code for all the.java files that are needed for the game. A jar file is like a zip file, where a number of files are compressed into one file. An HTML web page can contain a special kind of tag called an Applet tag. That tag tells the web page that the byte code is stored in a jar file with a specific name. You’ll be shown that code at some point. Browser software, like FireFox, Chrome, Camino, or Safari will then use its built-in JVM interpreter to translate the byte code into machine language 1s and 0s so the program can be executed. 21

23 Chapter 1 - Section 5 Printing to a Console Window print and println Statements 22

24 1.5 Java Is Hot Source Code package ch01; public class JavaIsHot { public static void main(String args[ ]) { System.out.println( " d "); System.out.println( " o l "); System.out.println( " l r "); System.out.println( " l o "); System.out.println( " e w "); System.out.println( " H "); System.out.println( " xxxxxxxxxxxxxxxxx "); System.out.println( " x x x "); System.out.println( " x Java x x "); System.out.println( " x xxxx "); System.out.println( " x is Hot! x "); System.out.println( " x x "); System.out.println( " xxxxxxxxxxxxxx "); } // end of main method } // end of JavaIsHot class 23 This program uses System.out.println statements to print text to the console output window.

25 1.5 print and println Statements Output to a console text window in Java is accomplished through the use of print and println (pronounced print-line) statements. print and println are two operations (or as we say in Java) … methods that can be called. These operations output text information to the console window in Eclipse. We call methods when we want to accomplish some operation or task. 24

26 1.5 print and println Statements Here are lines of code that call the two methods print and println … either one can be used: 1. System.out.print(“Hello World”); 2. System.out.println(“Hello World”); Think of methods as operations, but we always refer to them as methods in Java. Some other languages refer to them as functions or procedures. When we say we are “calling a method”, what we mean is that we are executing an operation! 25

27 1.5 print Statements You may be wondering what the difference between the two methods print and println are. print will display its information on one line but not start a new line after it displays it. So the next print or println statement will begin printing on that same line. Here is an example: System.out.print(“Hello World! ”); System.out.print(“How are you doing? ”); System.out.print(“I am doing fine.”); (Note the extra spaces at the end of the first two print statements or the sentences would jam up together in output) The output in the console window is all on one line as seen below: Hello World! How are you doing? I am doing fine. 26

28 1.5 println Statements println will display its information on one line and then start a new line and get ready to begin displaying whatever information is next from any other print or println statements. Consider these three lines of code: System.out. print(“Hello World! ”); System.out. println(“How are you doing?”); System.out. println(“I am doing fine.”); The output is on two lines in the console window: Hello World! How are you doing? I am doing fine. 27

29 1.5 println Statements If the lines were changed to three println statements, then the output of each statement would end up on a separate line. System.out. println(“Hello World! ”); System.out. println(“How are you doing?”); System.out. println(“I am doing fine.”); The output is on two lines in the console window: Hello World! How are you doing? I am doing fine. 28

30 1.5 System.out and Syntax Errors So when we use the line of code: System.out.print(“Hello World”); We are calling the print method. Printing to a console window requires that we place System.out. prior to the word print. Please notice the periods “.” (method selector operators) that separate the words System, out, and print. Also, notice that the first S of System is capitalized (upper case). The reason is System is the name of a class. Also, notice that after the word print there are parentheses that include in double quotes the literal words that we want to display to the screen and the entire line of code ends in a semicolon. All of this is required and must be typed exactly or you will get a syntax error. Programs with syntax errors won’t run! 29

31 1.5 Semicolons End Most Java Statements A statement is a “sentence” in a program. A semicolon ( ; ) marks the end of most but not all Java statements. See the blue arrows below. package ch01; public class JavaIsHot { public static void main(String args[ ]) { System.out.println( " d "); System.out.println( " o l "); Some Java lines do not end in a semicolon. See the red arrows above. 30

32 Chapter 1 - Section 6 The Binary Representation of Information 31

33 1.6 Bits and Bytes Bit (binary digit): Smallest unit of information processed by a computer. A bit represents a single memory cell in RAM memory. A bit can hold either a single 0 or 1. Byte: 8 adjacent bits or memory cells located in RAM memory. Capacity of computer memory and storage devices usually expressed in bytes. For example: 2 Gigabytes of RAM 2 Terabyte hard drive 32

34 1.6 Byte Levels Some commonly used quantities of information storage from smallest to largest. Unit of BytesNumber of BytesRepresentative Storage Kilobyte1 thousand bytesA small file Megabyte1 million bytesLarge files, CDs Gigabyte1 billion bytesDVDs, RAM, and hard disks Terabyte1 trillion bytesFile Servers Petabyte1 quatrillion bytesLarge Server Arrays Exabyte1 quintillion bytes ??? 33

35 1.6 Binary Representation of Information Computer memory stores patterns of electronic signals as 1s and 0s. There are two states for an electronic signal: 1. On - represented by a 1 2. Off - represented by a 0 Computer scientists use the following number bases: binary (base 2) octal (base 8) hexadecimal (base 16) 34

36 1.6 Base-10 Representation of Numbers 35 39473268 8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit When we think of the integer … 39,473,268 we may not stop and think that the 8 represents the ones digit, which can be identified as the 10 0 place, and the 6 represents the tens digit, which identifies the 10 1 place, and the 2 represents the hundreds digit, which identifies the 10 2 place, and so on. The only thing that is different about base-2 numbers is we can only have 0 and 1 as integers in each bit and each digit represents a power of 2 NOT a power of 10. 10 7 10 6 10 5 10 4 10 3 10 2 10 1 10 0

37 1.6 8 Bits Make a Byte A computer stores all information as a series of 1s and 0s using the base – 2 number system. That means that all letters, words, and sentences and all integers and floating-point numbers must be converted to 1s and 0s so that they can be stored or manipulated. In most computer systems, 8 bits make a byte. Remember … a bit is a single “cell” of memory that can hold either a “0” or a “1”. You may know that the base-2 number system (binary) has only 1s and 0s in it. So a computer works with a binary number system. Here is how we could diagrammatically represent one byte of computer memory: 36 10101010 8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit The next slide shows why we order the bits from right to left instead of left to right.

38 1.6 Base-2 Numbers (Binary) 37 11011001 8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit Each bit represents a power of 2 that allows us to properly view a binary number. The above base-2 number 11011001 2 is equal to the base-10 number 217. Note the power of 2 that represents each bit. We can calculate the equivalent base-10 number by using scientific notation … 217 = 1 x 2 7 + 1 x 2 6 + 0 x 2 5 + 1 x 2 4 + 1 x 2 3 + 0 x 2 2 + 0 x 2 1 + 1 x 2 0 Some computers may use a 4 byte “word”. This is the same as saying a group of 4 bytes. Of course every byte is 8 bits, so a 4 byte word represents 32 bits. An 8 byte word, represents 64 bits. Most computers are now 64 bit processors. 2 7 2 6 2 5 2 4 2 3 2 2 2 1 2 0

39 1.6 Converting From Base 2 to Base 10 The base-2 number 10011 2 can easily be converted to 19 10 base-10 using scientific notation. 10011 2 = (1 * 2 4 ) + (0 * 2 3 ) + (0 * 2 2 ) + (1 * 2 1 ) + (1 * 2 0 ) = 16 + 0 + 0 + 2 + 1 = 19 And the number 111010 2 is equal to 58 10 since … 111010 2 = (1 * 2 5 ) + (1 * 2 4 ) + (1 * 2 3 ) + (0 * 2 2 ) + (1 * 2 1 ) + (0 * 2 0 ) = 32 + 16 + 8 + 0 + 2 + 0 = 58 38

40 1.6 Converting From Base 10 to Base 2 We can successively divide any base 10 number by two and record the remainders to determine the equivalent base 2 number. 19 10 is equal to 10011 2 58 10 is equal to 111010 2 19 divided by 2 gives 9 with a remainder of 1. Notice carefully where the 9 and 1 are recorded. You continue to divide by 2 until you get 0. This tells you to stop. 39

41 1.6 Counting in Binary 40 Base 10 NumberEquivalent Base 2 Number 00000 10000 0001 20000 0010 30000 0011 40000 0100 50000 0101 60000 0110 70000 0111 80000 1000 90000 1001 100000 1010 110000 1011 120000 1100 130000 1101 140000 1110 150000 1111 160001 0000

42 1.6 Base 16 Hexadecimal Numbers Base 16 numbers are called hexadecimal numbers. Here is how you count in base 16: 0 1 2 3 4 5 6 7 8 9 A B C D E F So …. A represents 10 B represents 11 C represents 12 D represents 13 E represents 14 F represents 15 41

43 1.6 Base-16 Hexadecimal Numbers 0000146F 8th bit 7th bit 6th bit 5th bit 4th bit 3rd bit 2nd bit 1st bit Each bit represents a power of 16 that allows us to properly view a hexadecimal number. The above base-16 number 146F 16 is equal to the base-10 number 5231 10. Note the power of 16 that represents each bit. We can calculate the equivalent base-10 number by using scientific notation … 5231 = 0 x 16 7 + 0 x 16 6 + 0 x 16 5 + 0 x 16 4 + 1 x 16 3 + 4 x 16 2 + 6 x 16 1 + 15 x 16 0 16 7 16 6 16 5 16 4 16 3 16 2 16 1 16 0 42

44 1.6 Converting From Base 16 to Base 10 The base 16 number 146F 16 can easily be converted to 5231 10 base-10 also using scientific notation. 146F 16 = (1 * 16 3 ) + (4 * 16 2 ) + (6 * 16 1 ) + (15 * 16 0 ) = 4,096 + 1024 + 96 + 15 = 5231 Similarly, the base 16 number 3B9C 16 can be converted to 15,260 10 base-10 using the same process. 3B9C 16 = (3 * 16 3 ) + (11 * 16 2 ) + (9 * 16 1 ) + (12 * 16 0 ) = 12288 + 2816 + 144 + 12 = 15260 43

45 1.6 Converting From Base 10 to Base 16 We can successively divide any base 10 number by 16 and record the remainders to determine the equivalent hexadecimal number. 5231 divided by 16 gives 326 with a remainder of.9375 and multiplying it by 16 gives 15. The hexadecimal digit for 15 is F. Note when using a calculator, the decimal portion of the number is multiplied by 16 to find the whole number remainder. Similarly, 15,260 10 is equal to 3B9C 16. 44

46 1.6 The ASCII and Unicode Encoding Originally in the 1960s, computers in the United States, used the ASCII encoding scheme (American Standard Code for Information Interchange), where each number or character on the keyboard is represented as 1 byte, a pattern of 8 bits (Eight 1s or 0s). This allows us to represent up to 256 characters, since 2 8 = 256. However, the United States and all countries now use the expanded Unicode system, because Java is an international language, where – 2 bytes (16 bits) are used to represent every character – This allows us to represent 65,536 different characters since 65,536 = 2 16 and all the characters from different languages in the world can be represented! 45

47 1.6 The ASCII and Unicode Encoding Scheme We still refer to the ASCII code table, because it is a subset of Unicode. The letter “A” is represented by the number 65. Small “a” is represented by the number 97. The Hex column represents the equivalent hexadecimal number. 46

48 1.6 Character Digit Representations 47 Keyboard CharacterASCII NumberBinary Representation 0480011 0000 1490011 0001 2500011 0010 3510011 4520011 0100 5530011 0101 6540011 0110 7550011 0111 8560011 1000 9570011 1001 As you have seen from the ASCII Code Chart, numbers and alphabetical characters are represented by base 10 numbers that have an equivalent hexadecimal and binary number. The digits 0 - 9 not only represent numbers but they also represent characters as in the password “Bearcat2014”, so the digits aren’t converted to binary, but the associated ASCII number is converted to binary.

49 1.6 Upper Case Letter Representations 48 Keyboard CharacterASCII NumberBinary Representation A650100 0001 B660100 0010 C670100 0011 D680100 E690100 0101 F700100 0110 G710100 0111 H720100 1000 I730100 1001 J740100 1010 K750100 1011 L760100 1100 M770100 1101

50 1.6 Upper Case Letter Representations 49 Keyboard CharacterASCII NumberBinary Representation N780100 1110 O790100 1111 P800101 0000 Q810101 0001 R820101 0010 S830101 0011 T840101 0100 U850101 V860101 0110 W870101 0111 X880101 1000 Y890101 1001 Z900101 1010

51 1.6 Lower Case Letter Representations 50 Keyboard CharacterASCII NumberBinary Representation a970110 0001 b980110 0010 c990110 0011 d1000110 0100 e1010110 0101 f1020110 g1030110 0111 h1040110 1000 i1050110 1001 j1060110 1010 k1070110 1011 l1080110 1100 m1090110 1101 Note the only difference in the binary representation of ‘A’ and ‘a’ is the third digit from the left is changed from a 0 to a 1.

52 1.6 Lower Case Letter Representations 51 Keyboard CharacterASCII NumberBinary Representation n1100110 1110 o1110110 1111 p1120111 0000 q1130111 0001 r1140111 0010 s1150111 0011 t1160111 0100 u1170111 0101 v1180111 0110 w1190111 x1200111 1000 y1210111 1001 z1220111 1010

53 1.6 Representation of Non-Text Data Besides numbers and letters that are text …. sounds, images, and video can be represented by binary numbers. This is why sounds are able to be heard, images displayed, and videos watched on a computer. They are all converted from analog to digital and then back to analog when used. 52


Download ppt "Go Chapter 1 - Intro to Java Section 1 - What is Java? Section 2 - Creating and Running a Java Program Section 3 - Framework of a Simple Java Program."

Similar presentations


Ads by Google