530 likes | 708 Views
INTRO TO COMPUTERS & JAVA. CSC 110 Introduction to Computer Science for Majors Summer 2009. Computer system: Hardware plus software Hardware: The physical components Examples? Software: Programs (i.e. the instructions that tell the hardware what to do) Examples?. Computer Basics.
E N D
INTRO TO COMPUTERS & JAVA CSC 110 Introduction to Computer Science for Majors Summer 2009 Introduction to Computers and Java
Computer system: Hardware plus software • Hardware: The physical components • Examples? • Software: Programs (i.e. the instructions that tell the hardware what to do) • Examples? Computer Basics Hardware examples>>
Computers • Monitors • Keyboards • PDA’s • Servers • Cell phones (telephones) • SmartCards HARDWARE More hardware examples>>
Floppy and hard drives • Printers • Video cams • Backup devices • Scanners • Modems HARDWARE Hardware configuration>>
Keyboard • Monitor • Chassis (Tower) • cpu (processor) • memory (main and auxiliary) • disk drives • i/o connectors • cards (i.e. network) TYPICAL HARDWARE CONFIGURATION CPU>>
CPU (Central Processing Unit) • The computer’s microprocessor chip • The “brain” consists of several million transistors • Pentium is one of better known chips • Carries out “simple” instructions, and can add and subtract • The design is simple Memory>>
Main or random access memory (RAM) • used when computer is running a program • i.e. 256 meg of RAM • temporarily stores program and working data while processing • Auxiliary (secondary) • floppy & hard disk drives, CDs, tape, USB keychains • permanent storage for programs and data • used to swap back and forth between RAM Two Kinds of Memory Bits & bytes>>
Bits • 0’s or 1’s, on or off, true or false • simply a state • Bytes • 8 bits • main memory holds one byte, at a numbered address Memory Base 2 numbering system>>
Bits and Bytes • Byte 01001101 = 77 • base 2 numbering system • all one’s = 255 • adding zero = 256 Main memory organization>>
Byte - Addressable • Main memory is a list of numbered locations that contain one byte of data in each location • A0000 to FFFFF is typical “look” • Number of bytes per data item may vary Main Memory Organization Auxilliary memory organization>>
Auxiliary Memory Organization Aux memory organization cont'd>>
Files • bytes grouped in larger units when stored on a floppy • has a name w/file extension and can store data • a java program is stored as a file (i.e. .java or .class) • Directories (i.e. Folders) • called both • files are often grouped in a folder Auxiliary Memory Organization h: drive>>
Create folders • CSC110 • CSC110/Minilabs • CSC110/Programs h: drive SOFTWARE>>
A Program such as Java, is a set of instructions for a computer to follow: Program (mortgage calculator) SOFTWARE - A program that runs (or executes) Computer Input (principle and interest data) Output (monthly payment) Types>>
Operating System • DOS, MS- Windows(3.x, 95, 98, NT, XP), Mac OS, UNIX • Existing applications • word processors • web browsers • compilers and assemblers • virus protection • User-created applications • Java applet for a web site Many Types of Programs Java programs>>
Java Programs • Applets • Small, executable program embedded in a web page • Little application • Sent over Internet, run on client, needs HTML • Applications • Installed and run on a computer like any program • Larger (i.e. Student Information System) • One line of code determines application/applet A taste of Java>>
What’s in a name? • Sun Microsystems and Project “Green” 1991 • Once developed, called “Oak,” a name already used • Java! Marketing scheme, or an accident at a café? • Technology puns include JavaBeans, Café, Mocha (a Java “decompiler” tool) A Taste of Java! Java history>>
History • Intermediate Language for appliances (compilers are too expensive) in 1991 • 1994 first “written-in-Java” web-browser called HotJava (Note: Mosaic was first browser) • 1995 concept of an interactive web, Java allowed small programs (applets) to be embedded in the page A Taste of Java! Java features>>
JavaFeatures • Familiar, simple, object-oriented • Basic syntax modeled after C • Omits pointers and memory management • Object interactivity rather than procedural processing • Robust • Simplicity reduces errors • Runs without crashing • Provides exception handling for unexpected events cont'd>>
Java Features • Secure • Implements measures for network transmission • JVM unbreakable • Platform independence • Architecture neutral, portable cont'd>>
Java Features • High performance • Balancing act between platform independence and performance • Multitasking (threads) allows users to input data with graphics executing in the background • Communication delays are generally accepted on a web page Compiling a Java program>>
public class FirstProgram { public static void main(String[] args) { System.out.println("Hello out there."); System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no."); char answerLetter; answerLetter = SavitchIn.readLineNonwhiteChar(); if (answerLetter == 'y') System.out.println("Nice weather we are having."); System.out.println("Good-bye."); System.out.println("Press enter key to end..."); String junk; junk = SavitchIn.readLine(); } } A Sample Java Program Explanation of code>>
Code to begin the program (to be explained later): public class FirstProgram { public static void main(String[ ] args) { } } • Java applications all have similar code at the beginning • The name of the class differs from one program to another. • Other information about the class might also be included on the first line. Save as FirstProgram.java in ClassTonight folder!!! Note: Type in “exactly” as we explain the code. Lab & Reinforcement of Code ...Create Program in TextPad cont'd>>
Code to display a text string: System.out.println("Hello out there."); System.out.println("Want to talk some more?"); System.out.println("Answer y for yes or n for no."); • Note the “dot” operator • System.out is an object • println is a method that it carries out • double-quoted text inside the parentheses is an argument to the method • general syntax: Object_Name.Method_Name(Arguments) Explanation of Code ... cont'd>>
Code to create a variable named answerLetter to contain a single character of data: char answerLetter; • This variable is used to store the user’s response. … Explanation of Code ... cont'd>>
Read a character typed in from the keyboard and store it in the variable answerLetter: answerLetter = SavitchIn.readLineNonwhiteChar(); • SavitchIn is a class used for obtaining input from the keyboard • readLineNonwhiteChar() is a method that reads a single, non-blank character from the keyboard and discards any remaining characters on the line. • the equal sign is not the same as in math; it means “assign the value on the right to the variable on the left;” in this case, store the value read from the keyboard into the variable answerLetter Be sure to place copy of SavitchIn.class in folder … Explanation of Code ... cont'd>>
Question: If “=“ means “assign the value of the expression on the right to the variable on the left,” how do we indicate “equals”? Answer: use a double equals (“==“) Example: check to see if the character entered is ‘y’: if (answerLetter == 'y') • the value inside the parentheses will be True if the letter ‘y’ was typed in, otherwise it will be False (if any other letter was typed in) … Explanation of Code ... cont'd>>
Code to display the line “Nice weather we are having.” if the user entered the character ‘y’: if (answerLetter == 'y') System.out.println("Nice weather we are having."); • Note that the line will not be printed if any letter other than ‘y’ is entered. • Unconditionally display the line “Good-bye.”: System.out.println("Good-bye."); • only the previous System.out.println is conditionally printed, depending on the value entered; the next instruction is executed regardless of the value entered. … Explanation of Code ... cont'd>>
Code to prevent the display from scrolling off the screen before you can read it: System.out.println("Press enter key to end program."); String junk; junk = SavitchIn.readLine(); • junk is a variable that can contain a string of characters. • readLine() is a method to read in an entire line of text. • The program halts until a character is entered. • Any character entered will make the program continue. • The character entered is assigned to the variable junk, but is ignored (it is not used). • There are no more lines of code, so the program terminates. … Explanation of Code Syntax rules>>
Identifier - the name of something (e.g. a variable, object, or method) used in a Java program. Identifiers: • cannot use reserved words (e.g. “if,” “for”, etc.) (see App. 1) • must contain only letters, digits, and the underscore character, _. • cannot have a digit for the first character. • $ is allowed but has special meaning, so do not use it. • have no official length limit (there is always a finite limit, but it is very large and big enough for reasonable names) . • are case sensitive! • junk, JUNK, and Junk are three valid and different identifiers, so be sure to be careful in your typing! • Note that no spaces or dots are allowed. Syntax Rules for Identifiers Good programming practice>>
Always use meaningful names, e.g. finalExamScore, instead of something like x, or even just score. • Use only letters and digits. • Capitalize interior words in multi-word names, e.g. answerLetter. • Names of classes start with an uppercase letter. • every program in Java is a class as well as a program. • Names of variables, objects, and methods start with a lowercase letter. Good Programming Practice:Identifier Naming Conventions Compiling a program>>
Assuming the java compiler is already set up and all the files are in the same folder (subdirectory): • Each class used in a program should be in a separate file • The name of the file should be the same as the class except with “.java” added to it • First compile each class definition used in the program • e.g. SavitchInin the sample program for Sun Microsystems’ JDK (Java Development Kit), typejavac SavitchIn.java • a byte-code file is created with the name SavitchIn.class • Next compile the program file: • javac <file>.java (which creates <file>.class) Compiling a Java Program Running a Java program>>
Only the class with public static void main(String[] args)can be run • the critical word to look for is main • For Sun Microsystems’ JDK (Java Development Kit), typejava <file> • <file> is the same name used in the original source file <file>.java • use just <file>; do not use <file>.java or <file>.class • Note that you compile in a separate step andinvoke the Java interpreter and linker when you run the program. Running a Java Program at a Command Prompt (& TextPad) OOP>>
Objects • Trees, cars, buildings, people, jobs, traffic lights • Nouns • Can perform actions (i.e. traffic light turns green, its behavior) • Can effect other objects (i.e. cars accelerate, another behavior) • Has data (variables) that determines its state, or its attributes • Methods • The actions an object can take that determines its behavior • Verbs (light turns green, car accelerates) • Classes • Type of objects • Objects of the same kind (type) belong to a class Object-Oriented Programming (OOP) OOP examples>>
Scenario • Student enters and requests assistance regarding a math problem • Receptionist summons math tutor with expertise in subject matter • Tutor provides solution to student’s request • Objects • Student, receptionist, tutor, math solution • Perform actions, effects other objects, has data (information) • Methods • Student requests assistance, receptionist summons, tutor provides • Classes • Type of student (or….type of person, i.e. inheritance!),type of tutor, type of solution Use Case Example – Math Solutions State and behavior>>
State (determined with attributes) • Student is a MAT 120 (intermediate algebra) requestor • Math tutor’s state is algebra expertise (attribute) • Determined with associated data • Behavior • Algebra student makes request (actions) • Math tutor provides solution (actions) • Implemented with methods State and Behavior OOP definition>>
A programming methodology that consists of objects that interact with each other by means of actions. • Combines the actions (methods), and the data into objects (encapsulation). • Everything an object knows is captured in its variables (data) • Everything it can do is expressed in its methods OOP Definition OOP design>>
Encapsulation • Hides the details of what’s inside the object • Sometimes called information hiding • Many details about the student, but only the type is necessary for the receptionist object to interact • Polymorphism • Greek term meaning “many forms” • Human communication is “polymorphic” (i.e. ask same question to several people and you will get several answers) • One method instruction (message) causes different actions depending upon object • Inheritance • Organizes classes (objects) with common properties in groups so that they are only defined once OOP Design Principles Algorithm>>
Algorithms A strategy or plan for carrying out an action A set of instructions Complete and precise Can be a diagram, such as a flow chart Usually expressed in English and programming language as pseudocode Example: Print student profile Set time (t) to nearest quarter hour i.e. time = 15:30; OOP summary>>
A different style of programming • May look “procedural” because of algorithmic programming on top and bottom layers • OOP objects and classes are used for the middle layers • Focus is on data and interaction between objects, not on algorithm steps Summary - OOP Reusable components>>
Design objects for reuse • Person object • General • Do not design for one project • Specify exactly how this object interacts with other objects (encapsulate) • Through inheritance, can be a nurse, doctor, staff Reusable Components Testing and debugging>>
Bug • A mistake in the program • Debugging eliminates bugs in the program • Syntax errors • Grammatical mistake (i.e. no semicolon) • Run-time errors • Occurs during execution • Logic errors • Wrong output with no error message • Hardest to troubleshoot Testing and Debugging cont'd>>
Test program • Reliable data to measure output • Can detect errors when changes are made to code Testing and Debugging Programming languages and compilers>>
Programming Languages & Compilers Machine language>>
High-Level Language (HLL) • natural language • words, numbers, and math symbols • not directly understood by hardware • “portable” source code (hardware independent) • Java, C, C++, COBOL, FORTRAN, BASIC, Lisp, Ada, etc. • Machine Language(lowest level) • least natural language for humans, most natural language for hardware • 0’s and 1’s • directly understood by hardware • not portable (hardware dependent) The highs and lows of programming languages ... Assembly language>>
a more or less human readable version of machine language • words, abbreviations, letters and numbers replace 0s and 1s • easily translated from human readable to machine executable code • like machine code, not portable (hardware dependent) Assembly Language(middle level) Source to machine code>>
“Compiling a program” = translating from a high-level language source code to machine (object, or executable) code. • “Compiler” = a program that translates HLL source code to machine (object, or executable) code. • “Assembly” = translating from assemble language source code to machine (object, or executable) code. • “Assembler” = a program that translates assembly source code to machine (object, or executable) code. • Compilers need to know the specific target hardware, i.e. what type computer CPU the program will run on. Getting from Sourceto Machine Code Interpreters>>
Compilers and Assemblers • translation is a separate user step • translation is “off-line,” i.e. not at run time • Interpreters - another way to translate source to object code • interpretation (from source to object code) is not a separate user step • translation is “on-line,” i.e. at run time Compiler, Assembler, or Interpreter Object Code Source Code Compilers vs. Assemblers vs. Interpreters Java program translation>>
Both Compilation and Interpretation • Intermediate Code:“Byte Code” • portable low-level code • similar to assembly code,but hardware independent • invisible to Java programmer • Interpreter translates from generic byte code to hardware-specific machine code JVM Java Program Data for Java Program Java Compiler JavaVirtual Machine Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Java Program Translation Computer Execution of Machine-Language Instructions Java linker>> Output of Java Program
Previously Compiled Helper Programs Java Program Data for Java Program Java Compiler JavaVirtual Machine Byte-Code Program Byte-Code Interpreter Machine-Language Instructions Java Linker Computer Execution of Machine-Language Instructions Linker Advantages>> Output of Java Program