Download Java Source Files

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
– Capital B: 66
– Space: 32
– The letter 1: 49
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?
.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
Integrated Development Environment (IDE)
• Text book uses 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
Clcick 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
• History
–
–
–
–
Early computers consisted of mechanical levers and gears
A moth got stuck in one of these machines and it malfunctioned
They removed the moth (debugged), 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
– 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
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?