Introduction to Computers and Programming LanguagesCS 180Sunil PrabhakarDepartment of Computer Science Purdue University12Week 1 ObjectivesThis week we will REVIEW:Computer systems and JavaSimple Java programsJava data typesConditional statements: if and switch23Computer Architecture (simplified)CPU“brains”KeyboardMonitorMemoryStorageDevicesMousePrinterNetworkDevicesMore in CS250: ComputerArchitecture.34SoftwareEverything is in binary -- 0s and 1sTwo types of informationInstructions(programs) -- executed by the CPUData -- manipulated by CPUThese are stored in memoryThe software provides a means to access and control the hardwareThis is done through a very important piece of software called the Operating SystemThe OS is always running. More in CS35445Machine LanguageA computer only runs programs that are specified in its own machine language (ML)Also called binary or executable code.The ML is specific to the CPU, e.g. Pentium, 386, PowerPC G3, G4, …A program written for one CPU will not run on another CPU -- i.e. it is not portable.56Assembly languageMachine language codes are not easy to rememberAssembly language uses mnemonics and symbols to ease programming, e.g. JMP L2 A special program called an assembler must be used to convert the assembly code to machine codeThe assembly code is also hardware-specific.Eases programming but still requires one to think in terms of low-level steps taken by the CPU.Humans think at a higher level.67High-Level LanguagesAllow programmers to work with constructs that are closer to human language.E.g. Java, C, C++, Basic, Fortran, COBOL, Lisp, …Need a special purpose program to convert the high-level program to machine language.This program is called a compiler.Can write programs in many different HLLs for the same CPU.Need a compiler for each language and CPU (OS).Efficient conversion is still an issue. More in CS352 Compilers78High-Level Languages (cont.)Since the language is not specific to the hardware, HLL programs are more portableSome hardware, OS issues limit portabilityAll we need is the program and a compiler for that language on the given hardware platformE.g. a C compiler for Mac OSXThus we can write a program once in a HLL and compile it to run on various platforms, e.g. Netscape89AlgorithmsHumans tend to think of programs at a higher level than HLL -- more in terms of algorithms.An algorithm is a well-defined, finite set of steps that solves a given problemE.g. the rules for multiplying two numbers910HLL ParadigmsProceduralA program is composed of packets of code called procedures, and variables. A procedure is at full liberty to operate on data that it can see. E.g. C, Pascal, COBOL, FortranObject-OrientedPrograms are composed of Objects, each of a specific class with well defined methods. Data and programs are tightly coupled -- better design. E.g. Java, C++, Objective-C, C#FunctionalPrograms are composed of functions. E.g. LispMore in CS456 Programming Languages.1011Object-Oriented ProgrammingThe OOP paradigm uses the notion of objects and classes as basic building blocksOther important components of OOP areEncapsulationInheritancePolymorphism Dynamic binding 1112JavaJava is based upon C++ (which in turn is based on C).Unlike C++ which is really a hybrid language, Java is purely Object-Oriented. This results in significant advantages.Most HLL programs are compiled to run on a single platform.Java programs can run on multiple platforms after compilation -- i.e. its compiled format is platform-independent.This design choice comes from its history.1213History of JavaJava was developed by J. Gosling at Sun Microsystems in 1991 for programming home appliances (variety of hardware platforms).With the advent of the WWW (1994), Java’s potential for making web pages more interesting and useful was recognized. Java began to be added to web pages (as applets) that could run on any computer (where the browser was running).Since then it has been more widely accepted and used as a general-purpose programming language, partly due toits platform-independence, and it is a truly OO language (unlike C++)1314Platform-IndependenceNotion of a “Java Virtual Machine” (JVM)Java programs are compiled to run on a virtual machine (just a specification of a machine). This code is called Byte CodeEach physical machine that runs a Java program (byte code) must “pretend” to be a JVM. This is achieved by running a program on the machine that implements the JVM and interprets the byte code to the appropriate machine code.This interpreting is done at run-time which can cause a slow down!1415Regular Programming LanguagesFortran programC-OSXCompilerC-LinuxCompilerFor-OSXCompilerFOR-LinuxCompiler010101100101110100100111C program1010011011111101001001111516JavaJava ProgramJavaCompilerOSXInterpreter010101100101110100100111LinuxInterpreterJava ByteCode16Simple Input & Output171718Standard OutputUsing the print method of the System.out class is a simple way to write to the console window from which the program was run.System.out.print(“How are you?”);This output will appear at the console window.> How are you?1819JOptionPaneUsing showMessageDialog of the JOptionPane class is a simple way to bring up a window with a message.JOptionPane.showMessageDialog(null, “How are you?”);This dialog will appear at the center of the screen.1920Displaying Multiple Lines of TextWe can display multiple lines of text by separating lines with a new line marker \n.JOptionPane.showMessageDialog(null,“one\ntwo\nthree”);2021JOptionPane for InputUsing showInputDialog of the JOptionPane class is another way to input a string.String name;name = JOptionPane.showInputDialog (null, “Your full name:”);This dialog will appear at the center of the screen ready to accept an input.2122Standard Input and ScannerThe System class has a special object that accepts input from the keyboard: System.inIt reads only one byte at a time. We often need to read multiple bytes at a time.The Scanner class provides the necessary methods.A scanner object is created that “wraps” the System.in object.Calls to the method next() return one “word” at a time from the standard inputWords are separated by whitespaces.2223Standard Input and Scannerimport
View Full Document