Presentation is loading. Please wait.

Presentation is loading. Please wait.

What is Computer Science?

Similar presentations


Presentation on theme: "What is Computer Science?"— Presentation transcript:

1 What is Computer Science?
The process of finding digital solutions to real world problems Examples ACORNS: support tribal efforts to revitalize and restore culture and language Global Positioning: computer based computations that map places on the earth Weather Models: Predict climate change Robotics: Create devices that can assist in every day life Data Mining: Extract information from huge amounts of data Web services: Create portals for interacting with web-sites Security: Protecting computer systems from attack Cognition: Create programs to help stroke victims

2 This class: teach foundational skills
Computer programming consists of those skills that provide a foundation to getting a computer to do anything that we can imagine A program is those instructions needed to accomplish some useful task This class introduces you to the skills needed to design, implement, and make programs work Computer Scientists Lifelong learning those aspects of real world problems so solutions can be digitized Communication skills to be able to relate to people in other disciplines without technical background

3 Computers and memory Computer memory is a huge table of numbers
This table holds program instructions and data Each spot in the table is called a location and is numbered A possible snapshot of memory is on the left Location Contents Instruction Data Note: Computers also have locations within the processing unit (the ‘brain’) called registers. These are very fast, but not actually part of memory.

4 Computer instructions
Computer instructions tell the computer what to do Each instruction is simply a numeric value that the computer processing unit (CPU) understands Computers execute instructions one at a time in order A program is a group of instructions that performs some useful task An application is a group of programs that work together to mimic some real world task A small sample of possible machine instructions: Load the contents of a memory location into a CPU register Add the contents of two registers together Store a register back into a memory location Input from the keyboard into memory Output from memory to the disk

5 How computers work The CPU loads the next instruction from the program counter (PC) into the CPU Execute the instruction Increment the PC Go back to step 1 Note: A branch is an instruction that tells the CPU to change the PC to another location that is far away.

6 How do we write programs?
Machine Language: We need to memorize all of the numeric numbers for every possible instruction. Not fun!! Assembly Language: We need to know all of the computer’s instructions, but using names instead of numbers. An assembler program converts the names we use to machine language so it can run. Still hard, and only runs on a single type of computer (platform) ! High level: We write instructions that are more English like. A compiler program converts to machine language so the program can run. Scripting Language: We write instructions into a text file. A program interprets the instruction as it processes the text file. Markup Language: Scatter commands throughout a text file Declarative Special Purpose: Instructions are an easy-to-use syntax. A program interprets these instructions and executes them. Natural Language: We speak to the computer in our native languages (like in Star Trek).

7 Compilers and Interpreters
Compiler (Example: Translate a book to French once) Translate the program’s instructions machine code before it executes Advantage: executes fast Disadvantage: Must recompile for each computer platform, hard to debug Interpreter( Example: A translator interprets as a speech is given) Interprets the instructions as the program runs Advantage: easier to create programs and debug Disadvantage: slow Hybrid Compile instruction partially, and interpret or compile the rest of the way (just-in-time technology) as the program runs Advantage: Don’t have to recompile for each computer platform, just-in-time technology achieves speeds almost as fast as a compiled language Disadvantage: Still somewhat slower than compiled languages

8 Programming languages
High Level: C, C++, Fortran, Cobol Scripting Languages: php, JavaScript, asp, perl, awk Markup Languages: HTML, XML, postscript Declarative: SQL Hybrid: Java, Visual Basic Natural Language: None yet Note: The syntax (grammatical rules) is similar for C, C++, php, JavaScript, and Perl. The kinds of instructions of many of the others is vey similar. Note: Byte code defines the format of a partially Java compiled program which a Java Virtual Machine (JVM) understands. Note: Why Java? Answer: Java is at this time the most used programming language in the world, followed closely by C and C++. Visual Basic is 4th.

9 More on memory Computer memory is measured in bytes (ex: 2gigabytes [2gb]) A bit is an electronic circuit ______/ ______ (off = 0) ______________ (on = 1) A byte is eight bits strung together Questions: How many unique numbers can a byte hold? If half of the numbers are negative, what is the possible range? 128 64 32 16 8 4 2 1

10 Encodings Hexadecimal groups four bits together
Sixteen possible values: 0, 1, …, 9, A, B, C, D, E, F Used to represent colors, two digits for red, blue and green (Ex: FF0000 for Red). Letters The letters of the keyboard are each given a numeric value. Capital A: 65 (decimal) 41 (hexadecimal) Capital B: 66 (decimal) 42 (hexadecimal) Space: 32 (decimal) 20 (hexadecimal) The letter 1: 49 (decimal) 31 (hexadecimal)

11 ASCII Number to letter mapping
Note: Unicode II, is a two byte code extension of ASCII to represent all of the glyphs of the world’s languages. Java uses Unicode II

12 Definitions Memorize now, understand later
Class: A blueprint for making objects Object: A collection of properties and methods Method: A block of instructions called by name Variable: A place in memory that can hold a value Property: A variable that is in an object Instance Variable: Synonym for property Identifier: The name we give to a variable Literal: A built-in constant (like 15.3 or -1). Call a method: Cause the method’s instructions to execute Instantiation: The creating of an object from a class Static: A method or variable that goes with a class

13 More Definitions Expression: A series of operands and operators
Argument: An expression passed to a method Parameter: An identifier in a method referring to an argument Type: A category of data String: A sequence of letters Integer: int, byte, long Fractions: float, double Boolean: true or false value Declaring: creating a variable so it can be used Array: An indexed table of variables

14 Syntax The grammar of a language
Statement: A single program language instruction (sentence) Semi-colon: Java statements MUST end with semi-colons Braces: ({}) enclose a block of instructions Comments (Ignored by Java) /* … */ multi-line // the rest of a line Square braces ([]): used to declare and access arrays Parenthesis: enclose expressions, arguments, or parameters Quotes: enclose the characters of a string literal (like “abc”) Case sensitive: abc is different than Abc (be careful!!) Reserved words (always lower case): Part of the Java language. Examples include: static, void, true, false, and class.

15 First Java program There are lots of important concepts here to discuss public class HelloWorld { public static void main(String args[]) System.out.println(“Hello World”); } } // End of Hello World class Note: Every Java program must have a main method Questions: Which are the reserved words?

16 A Sample Java Program Questions
How would you print: Java is not a drink? What strings are in this program? What strings are used in the program? Are they literals? How would we perform multiplication? What is concatenation? Where is it used in this program? This class has one method. What is its name? What are the Java reserved words that are used in this program? Is a string an object? How about an integer? public class SampleProgram { public static void main(String[] args) { System.out.println("Hello, this is a program"); System.out.println("Welcome to Java"); System.out.println("Let's do a calculation"); int answer; // Declare an integer answer = 2 + 2; // Perform a calculation System.out.println("2 plus 2 is " + answer); } }

17 .java, .class, and .jar Jar files Java Source Files
Pure text file containing Java program statements Notepad++ or Notepad are programs for entering pure text Word can be used, but you must save as plain text We will use an integrated development environment Java source files should be saved with a .java extension Java byte code files Compile java source file into byte code Java byte code files have a .class extension Execute java byte code using an Java Virtual Machine (JVM) Jar files Compression that lumps lots of java byte code files together

18 JVM The JVM is an application written for a given computer platform
Java Virtual Machine The JVM is an application written for a given computer platform If the JVM exists and is installed, all Java programs will run (“write once, run everywhere”) A JVM has a class loader that loads a class when it first is accessed, converting it to machine code. Subsequent references then execute at machine speed (“Just in time compilation (JIT)”) As a result, Java applications excecute about 70% of the speed of faster languages, such as C

19 Integrated Development Environment (IDE)
Command line instructions (The hard way) Install Java, use Notepad++ to create a .java source file Click on start, run, then type cmd To compile: javac HelloWorld.java (create a .class bytecode file) To execute: java HelloWorld (execute the .class bytecode) We will use JGrasp (An easier way) Install Java, and then JGrasp Click on File, New, Java, and type the Java Program, save it with a .java extension Click on green plus to compile and create a .class bytecode file Click on the red stick figure to execute Other Free IDE’s: Java, Netbeans, Eclipse

20 Debugging Debugging is the process of getting a program to work
Thomas Edison letter, 1878: It has been just so in all of my inventions. The first step is an intuition, and comes with a burst, then difficulties arise — this thing gives out and [it is] then that "Bugs" — as such little faults and difficulties are called — show themselves Computer History Admiral Grace Hopper, Mark II Computer at Harvord A moth got stuck in one of the relays causing a malfunction They “debugged” the moth and everything was fine That moth is in the Smithsonian to this day Why don’t programs work? Syntax: The Java compiler will detect; fix and recompile Runtime: An error occurs during runtime causing a program crash Management: Changes not saved, or saved in the wrong place Logic: The instructions were not what you intended Hint: Make sure you understand before making random changes

21 Operators Standard mathematical operators (+, -, /, *)
(A symbol indicating how to manipulate one or more operands) Standard mathematical operators (+, -, /, *) Additional Java operator (%) Modula, or easier to remember remainder Example: 5%2 is 1 because 1 is the remainder when dividing 5 by 2 Additional Java operators (+=, -=, *=, /=) Modify the previous value by the right side Modify by one (++, --) System.out.println(++x ); // add one and then print System.out.println(x++); // print and then add one

22 Primitive Types In Java, every variable must have a type
Primitive types (Java reserved words) char, byte, short, int, long float, double Widening store a narrow range variable into a wider range variable Example: byte x = 3; int y; y = x; Narrowing (Causes a compile error) Store a wider range variable into a narow range variable Example: int y; float f = 3; y = f; Force narrowing by type casting Treat the wider range variable as narrow range Example: int y; float f = 3; y = (int)f; Note: Java reserved words are always lower case

23 Reference Types Difference from primitive variables Example: String
A variable declared using a class type Difference from primitive variables Reference variables can have embedded methods and properties Reference variables contain the address (pointer to) the data and not the data itself Example: String String str = "abc"; System.out.println(str.charAt(2)); // charAt method String str2 = str + str; // Concatenation of strings

24 Conventions for Naming Identifiers
Symbolic constants all caps using underscores between words Example: int MAX_YEARS = 10; Variables Camel case, but with first word starting with a lower case letter Example: int salesTotal; Class names Camel case, first word starting with an upper case letter Example: public class FourierTransform Remember: Java is case sensitive, so be consistent in how things are named

25 Commenting and Indenting
Top of program: Author, date, purpose, general description, list of recent changes/by who/data Above each method Description of purpose Describe how it is called Near a particularly complex group of instructions Description of what is being done Indenting: 3 or 4 spaces the lines between an opening brace and a closing brace Note: About 80% of work is in maintaining a program, 20% originally writing it

26 Review questions What are some of the definitions that we discussed?
What is debugging? What is bytecode? What is the difference between compiling and interpreting? What is a high level language? What are .java, .class, and .jar files? What is an IDE? State some examples. How are the following symbols used in Java: ; {} [] () /* */ // “ What does case sensitive mean? Describe bit, byte, binary and hexadecimal? What is computer memory?


Download ppt "What is Computer Science?"

Similar presentations


Ads by Google