Download Week 1 Power Point Slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
This class: teach foundational skills
• 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
Computers and memory
Location
Contents
…
1000223
Instruction
1000224
Instruction
…
1001353
Instruction
1002345
Data
…
1002499
Data
…
• 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
Note: Computers also have locations within the processing unit (the ‘brain’)
called registers. These are very fast, but not actually part of memory.
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
How computers work
1. The CPU loads the next instruction from the
program counter (PC) into the CPU
2. Execute the instruction
3. Increment the PC
4. 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.
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).
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-intime technology achieves speeds almost as fast as a compiled language
– Disadvantage: Still somewhat slower than compiled languages
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.
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
128
64
32
16
8
4
2
1
1
1
1
0
1
0
0
1
0
0
1
1
1
1
0
0
0
1
0
0
1
1
0
0
0
1
0
1
0
1
0
1
• Questions:
– How many unique numbers can a byte hold?
– If half of the numbers are negative, what is the possible range?
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)
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
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
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
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.
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?
A Sample Java Program
Questions
1. How would you print: Java
public class SampleProgram
is not a drink?
{
2.What strings are in this
public static void main(String[] args)
program?
{
System.out.println("Hello, this is a program"); 3.What strings are used in the
program? Are they literals?
System.out.println("Welcome to Java");
4.How would we perform
multiplication?
System.out.println("Let's do a calculation");
5.What is concatenation?
Where is it used in this
int answer;
// Declare an integer
program?
answer = 2 + 2; // Perform a calculation
6.This class has one method.
System.out.println("2 plus 2 is " + answer);
What is its name?
}
7.What are the Java reserved
}
words that are used in this
program?
8.Is a string an object? How
about an integer?
.java, .class, and .jar
• 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
JVM
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
Integrated Development Environment (IDE)
• Command line instructions (The hard way)
1.
2.
3.
4.
•
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)
1.
2.
3.
4.
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
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
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
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
Reference Types
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
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
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
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?