Chapter I - IT108 Introduction to Program Design George...

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: IT108 Introduction to Program Design George Mason University How Data is Stored? Data are encoded as a series of bits Digital devices have two stable states A byte is the minimum storage unit. Memory address Memory content . . . . . . 2000 01001010 Encoding for character ‘J’ 2001 01100001 Encoding for character ‘a’ 2002 01110110 Encoding for character ‘v’ 2003 01100001 Encoding for character ‘a’ 2004 00000011 Encoding for number 3 Storage Devices Memory is volatile • Secondary Storage is used • Memory Disk, CD, and Tape Storage Devices Input Devices Keyboard, Mouse Output Devices Monitor, Printer CPU Modem, and NIC Communication Devices Programs Computer programs, known as software, are instructions to the computer. Programs are written using programming languages. Programming Languages Machine Language Assembly Language High-Level Language Machine language binary code to add two numbers, you might write the an instruction in binary like this: 1101101010011010 Programming Languages Machine Language Assembly Language High-Level Language An assembler is used to convert assembly language programs into machine code For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3 Assembly Source File … ADDF3 R1, R2, R3 … Machine Code File Assembler … 1101101010011010 … Programming Languages Machine Language Assembly Language High-Level Language The high-level languages are English-like a high-level language statement that computes the area of a circle with radius 5: area = 5 * 5 * 3.1415; Popular High-Level Languages Java (We use it in the book) COBOL (COmmon Business Oriented Language) FORTRAN (FORmula TRANslation) BASIC (Beginner All-purpose Symbolic Instructional Code) Pascal (named for Blaise Pascal) Ada (named for Ada Lovelace) C (whose developer designed B first) Visual Basic (Basic-like visual language developed by Microsoft) Delphi (Pascal-like visual language developed by Borland) C++ (an object-oriented language, based on C) Compiling Source Code a compiler is used to translate the source program into a machine language program called an object program. The object program is often then linked with other supporting library code before the object can be executed on the machine. Source File Compiler Object File Linker Excutable File Compiling Source Code You can port a source program to any machine with appropriate compilers. The source program must be recompiled, however, because the object program can only run on a specific machine. Java was designed to run object programs on any platform. Compiled into bytecode bytecode can then run on any computer with a Java Virtual Machine (interpreted) Java Bytecode Java Virtual Machine Any Computer Java, Web, and Beyond Java can be used to develop Web applications. Java Applets Java Servlets and JavaServer Pages Java can also be used to develop applications for hand-held devices such as Palm and cell phones Creating and Editing Using NotePad To use NotePad, type notepad Welcome.java from the DOS prompt. Creating, Compiling, and Running Programs Create/Modify Source Code Source code (developed by the programmer) package chapter1; public class Welcome { public static void main(String args) { System.out.println("Welcome to Java!"); } } Byte code (generated by the compiler for JVM to read and interpret, not for you to understand) … Method Welcome() 0 aload_0 … Method void main(java.lang.String) 0 getstatic #2 … 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 … 8 return Saved on the disk Source Code Compile Source Code i.e., javac Welcome.java If compilation errors stored on the disk Bytecode Run Byteode i.e., java Welcome Result If runtime errors or incorrect result Compiling and Executing Java Programs from the Command Prompt • • • • javac Welcome.java (compiles, produces a Welcome.class file) java Welcome (to run) The filename (Welcome.java) must match the name of the class (Welcome) even as to case To run, one can’t use the extension .class JRE, JDK, SDK, IDE • • • • The Java Runtime Environment (JRE) is necessary to run a Java Program The JDK (also known as SDK) is used to compile a Java program An Integrated Development Environment (IDE) simplifies program development and/or compilation/execution At least some IDEs (e.g. TextPad), require the JDK/SDK and JRE to be installed Location of JRE and JDK/SDK • • java.exe is in C:\Program Files\Java\jre6\bin javac.exe is in C:\Program Files\Java\jdk1.6.0_14\bin – there is also a java.exe there Anatomy of a Java Program • • • • • • • • • Comments Package Reserved words Modifiers Statements Blocks Classes Methods The main method Comments Following (//) in a line • Enclosed between /* and */ in one or multiple lines • Compiler ignores comment • Reserved Words Reserved words or keywords are words that have a specific meaning to the compiler and cannot be used for other purposes in the program. Statements • A statement represents an action or a sequence of actions • System.out.println("Welcome to Java!") ; Blocks A pair of braces in a program forms a block that groups components of a program. public class Test { public static void main(String args) { System.out.println("Welcome to Java!"); } } Class block Method block Classes class is the essential Java construct • template or blueprint for objects; objects will be instances of the class • Methods collection of statements that performs a sequence of operations • can be used even without fully understanding the details of how it works • can call the same method with a different arguments • main Method control of program flow • interpreter executes the application by invoking the main method • public static void main(String args) { // Statements; } The showMessageDialog Method JOptionPane.showMessageDialog(null, "Welcome to Java!", "Example 1.2 Output", JOptionPane.INFORMATION_MESSAGE); Writing a Java Program • Design algorithm • • • • • • • • This is done on paper Use flowchart (or pseudo-code) You will need to turn this in along with your lab solutions Write the Java code Create source code file in the IDE (e.g Eclipse, TextPad) Compile Run Test Flowcharting Terminal Start/Stop Process Calculation Assignment Decision (yes/no) Input/Output Connector Example Create an application to print out your name and today’s date Algorithm Design Start Output name Output date Stop Write the Code public class IntroDemo { public static void main (String args) { System.out.println("\t Ima Programmer"); System.out.print("\t George Mason University"); System.out.println(" -- IT 108"); } } Enter the code into Eclipse Save/Run your program Result Java Application Using GUI Output JOptionPane: provides dialog boxes for GUI output Implementation • Import class package containing JOptionPane – • Use showMessageDialog( ) – – • Example: import javax.swing.JOptionPane; Like println ( ), but with two arguments Pass null and new literal string Must use exit ( ) method to return control to OS Code for GUI Output import javax.swing.JOptionPane; public class GUI { public static void main (String args) { JOptionPane.showMessageDialog(null, "Welcome to IT 108"); } } ...
View Full Document

{[ snackBarMessage ]}

Page1 / 36

Chapter I - IT108 Introduction to Program Design George...

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

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