Purdue CS 18000 - Introduction to Computers and Programming Languages

Unformatted text preview:

Introduction to Computers and Programming LanguagesCS 180Sunil PrabhakarDepartment of Computer Science Purdue University12Week 1 ObjectivesThis week we will REVIEW:Computer systems and JavaSimple Java programsJava data typesConditional statements: if and switch23Computer Architecture (simplified)CPU“brains”KeyboardMonitorMemoryStorageDevicesMousePrinterNetworkDevicesMore in CS250: ComputerArchitecture.34SoftwareEverything is in binary -- 0s and 1sTwo types of informationInstructions(programs) -- executed by the CPUData -- manipulated by CPUThese are stored in memoryThe software provides a means to access and control the hardwareThis is done through a very important piece of software called the Operating SystemThe OS is always running. More in CS35445Machine LanguageA 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 languageMachine language codes are not easy to rememberAssembly 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 codeThe 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 LanguagesAllow 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 portableSome hardware, OS issues limit portabilityAll we need is the program and a compiler for that language on the given hardware platformE.g. a C compiler for Mac OSXThus we can write a program once in a HLL and compile it to run on various platforms, e.g. Netscape89AlgorithmsHumans 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 problemE.g. the rules for multiplying two numbers910HLL ParadigmsProceduralA 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, FortranObject-OrientedPrograms 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#FunctionalPrograms are composed of functions. E.g. LispMore in CS456 Programming Languages.1011Object-Oriented ProgrammingThe OOP paradigm uses the notion of objects and classes as basic building blocksOther important components of OOP areEncapsulationInheritancePolymorphism Dynamic binding 1112JavaJava 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 JavaJava 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 toits platform-independence, and it is a truly OO language (unlike C++)1314Platform-IndependenceNotion 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 CodeEach 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 OutputUsing 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?1819JOptionPaneUsing 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 TextWe can display multiple lines of text by separating lines with a new line marker \n.JOptionPane.showMessageDialog(null,“one\ntwo\nthree”);2021JOptionPane for InputUsing 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 ScannerThe System class has a special object that accepts input from the keyboard: System.inIt 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 inputWords are separated by whitespaces.2223Standard Input and Scannerimport


View Full Document

Purdue CS 18000 - Introduction to Computers and Programming Languages

Download Introduction to Computers and Programming Languages
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Computers and Programming Languages and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Computers and Programming Languages 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?