1.04k likes | 1.35k Views
Dr. Jeff Gray gray@cs.ua.edu http://gray.cs.ua.edu July 22, 2013 9:00am-3:30pm These slides available at: http://www.cs.ua.edu/~gray/outreach/camps/week1/. Week 1: Java Bootcamp. Camp Introduction. Liability forms Photos throughout the week Lunch each day – 11:00am-12:00pm
E N D
Dr. Jeff Gray gray@cs.ua.edu http://gray.cs.ua.edu July 22, 2013 9:00am-3:30pm These slides available at: http://www.cs.ua.edu/~gray/outreach/camps/week1/ Week 1: Java Bootcamp
Camp Introduction • Liability forms • Photos throughout the week • Lunch each day – 11:00am-12:00pm • Today: Steak day at the Freshens Food Court • Tomorrow: Pizza lunch (Student Services center) • Tue-Thu: Lunch in the Freshens Food Court (Ferguson Center) • Fri: Catered lunch • Restrooms • USB Stick • Assumption – general computer knowledge • Using Windows Explorer to: • Copy files • Create new folders/directories • Navigate directories
Special Events • Tuesday night from 6:15pm-8:15pm • Social event – swimming at Outdoor Rec Pool • Mandatory for all dormers to attend • Commuters also welcome • Wednesday afternoon • Special snack! • Thursday robotics lab visit • Dr. Monica Anderson • Thursday night • Possible social event – wiffle ball on the quad • Friday talk by Mrs. Dill
Camp rules • Camp Rules • No cell phone usage in class except for emergencies • No headphones (except for project need) • No surfing the web or playing games during lectures • No food or drink are allowed in the lab • Pay attention during class exercises; do not jump ahead and let us know if you fall behind • Because of the large size of the camp, please keep discussions to a minimum to assist others in hearing instructions • Quite in the hallways – classes in session
Disclaimer • This is a “bootcamp” – it is designed to provide very quick immersion into a specific computer language; based on several methods for teaching Java since 1998 • We try to cover 16-weeks worth of material in 1 week • Obviously, we will not go into depth in many topics and this week primarily serves as an overview of Java • You will have a general view of Java at the end of the week, but not at a proficient level of expectation to take the AP computer science course • This is really a “teaser” to introduce you to computer science and prepare you with enough skills to be successful in the future camp weeks • We make not claim that you will be a Java expert at the end of the week, but hopefully you will learn some new things
Free Java Resources • We do not provide you with a textbook for the camp, but there are several free resources available on the internet • Please do not print these on our printers (30 students times hundreds of pages = lots of dead trees!), but rather view online • http://www.mindview.net/Books/TIJ/ • http://mcis.western.edu/freejavabook/ • http://math.hws.edu/javanotes/ • http://www.bluepelicanjava.com/
Motivation for Studying CS • See • http://gray.cs.ua.edu/outreach/why-cs-talk/why-cs-talk.ppt
Analog vs. Digital • There are two basic ways to store and manage data: • Analog • continuous, in direct proportion to the data represented • music on a record album - a needle rides on ridges in the grooves that are directly proportional to the voltages sent to the speaker • Digital • the information is broken down into pieces, and each piece is represented separately • music on a compact disc - the disc stores numbers representing specific voltage levels sampled at specific times
Digital Information • Computers store all information digitally: • numbers • text • graphics and images • video • audio • program instructions • In some way, all information is digitized - broken down into pieces and represented as numbers
72 105 44 32 72 101 97 116 104 101 114 46 Representing Text Digitally • For example, every character is stored as a number, including spaces, digits, and punctuation • Corresponding upper and lower case letters are separate characters H i , H e a t h e r .
Binary Numbers • Once information is digitized, it is represented and stored in memory using the binary number system • A single binary digit (0 or 1) is called a bit • Devices that store and move information are cheaper and more reliable if they have to represent only two states • A single bit can represent two possible states, like a light bulb that is either on (1) or off (0) • Permutations of bits are used to store values
1 bit 2 bits 3 bits 4 bits 0 1 00 01 10 11 000 001 010 011 100 101 110 111 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Bit Permutations Each additional bit doubles the number of possible permutations
1 bit ? 2 bits ? 3 bits ? 4 bits ? 5 bits ? How many items can be represented by Bit Permutations • Each permutation can represent a particular item • There are 2N permutations of N bits • Therefore, N bits are needed to represent 2N unique items 21 = 2 items 22 = 4 items 23 = 8 items 24 = 16 items 25 = 32 items
Converting Binary to Decimal • Consider what the decimal number “1,234” means: • 1234 = 1000 + 200 + 30 + 4 • = 1*1000 + 2*100 + 3*10 + 4 * 1 • = 1*103 + 2*102 + 3*101 + 4*100 • In general, na…n1n0 represented in base “b” is: • na…n1n0 = na*ba + … + n1*b1 + n0*b0 • Apply this to generalization to binary “01101100” (b=2) • 01101100 = 0*27 + 1*26 + 1*25 + 0*24 + 1*23 + 1*22 + 0*21 + 0*20 • = 0*128 + 1*64 + 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 • = 0 + 64 + 32 + 0 + 8 + 4 + 0 + 0 • = 64 + 32 + 8 + 4 • = 108
9278 9279 9280 9281 9282 9283 9284 9285 9286 Main memory is divided into many memory locations (or cells) Each memory cell has a numeric address, which uniquely identifies it Memory
Each memory cell stores a set number of bits (usually 8 bits, or one byte) Large values are stored in consecutive memory locations Storing Information 9278 9279 9280 9281 9282 9283 9284 9285 9286 10011010
Storage Capacity • Every memory device has a storage capacity, indicating the number of bytes it can hold • Capacities are expressed in various units: Unit Symbol Number of Bytes KB 210 = 1024 kilobyte megabyte gigabyte terabyte MB 220 (over 1 million) GB 230 (over 1 billion) TB 240 (over 1 trillion)
Java • A programming language specifies the words and symbols that we can use to write a program • A programming language employs a set of rules that dictate how the words and symbols can be put together to form valid program statements • The Java programming language was created by Sun Microsystems, Inc. • It was introduced in 1995 and it's popularity has grown quickly since
Java Program Structure • In the Java programming language: • A program is made up of one or more classes • A class contains one or more methods • A method contains program statements • Similar to how a book is broken into chapters, paragraphs, sentences, and words. This serves as an organizational structuring mechanism. • These terms will be explored in detail throughout the week • A Java application always contains a method called main
Java Program Structure // comments about the class public class MyProgram { } class header class body Comments can be placed almost anywhere
Java Program Structure // comments about the class public class MyProgram { } // comments about the method public static void main (String[] args) { } method header method body
Identifiers • Identifiers are the words a programmer uses in a program • An identifier can be made up of letters, digits, the underscore character ( _ ), and the dollar sign • Identifiers cannot begin with a digit • Java is case sensitive - Total, total, and TOTAL are different identifiers • By convention, programmers use different case styles for different types of identifiers, such as • title case for class names - Lincoln • upper case for constants - MAXIMUM
Identifiers • Sometimes we choose identifiers ourselves when writing a program (such as Lincoln) • Sometimes we are using another programmer's code, so we use the identifiers that he or she chose (such as println) • Often we use special identifiers called reserved words that already have a predefined meaning in the language; a reserved represents the core of the Java language • A reserved word cannot be used in any other way
Reserved Words • The Java reserved words: abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
White Space • Spaces, blank lines, and tabs are called white space • White space is used to separate words and symbols in a program • Extra white space is ignored • A valid Java program can be formatted many ways • Programs should be formatted to enhance readability, using consistent indentation
Program Development • The mechanics of developing a program include several activities • writing the program in a specific programming language (such as Java) • translating the program into a form that the computer can execute • investigating and fixing various types of errors that can occur • Software tools can be used to help with all parts of this process • Integrated Development Environments (IDEs) like Eclipse combine all of these activites (Tuesday)
Language Levels • There are four programming language levels: • machine language • assembly language • high-level language • fourth-generation language • Each type of CPU has its own specific machine language • The other levels were created to make it easier for a human to read and write programs
Programming Languages • Each type of CPU executes only a particular machine language • A program must be translated into machine language before it can be executed • A compiler is a software tool which translates source code into a specific target language • Often, that target language is the machine language for a particular CPU type • The Java approach is somewhat different
Java Translation • The Java compiler translates Java source code into a special representation called bytecode • Java bytecode is not the machine language for any traditional CPU • Another software tool, called an interpreter, translates bytecode into machine language and executes it • Therefore, the Java compiler is not tied to any particular machine • Java is considered to be architecture-neutral
Java Translation Java source code Java bytecode Java compiler Bytecode interpreter Bytecode compiler Machine code
Development Environments • There are many programs that support the development of Java software, including: • Sun Java Development Kit (JDK) • Sun NetBeans • IBM Eclipse • Borland JBuilder • MetroWerks CodeWarrior • BlueJ • jGRASP • Though the details of these environments differ, the basic compilation and execution process is essentially the same
Syntax and Semantics • The syntax rules of a language define how we can put together symbols, reserved words, and identifiers to make a valid program • The semantics of a program statement define what that statement means (its purpose or role in a program) • A program that is syntactically correct is not necessarily logically (semantically) correct • A program will always do what we tell it to do, not what we meant to tell it to do
Errors • A program can have three types of errors • The compiler will find syntax errors and other basic problems (compile-time errors) • If compile-time errors exist, an executable version of the program is not created • A problem can occur during program execution, such as trying to divide by zero, which causes a program to terminate abnormally (run-time errors) • A program may run, but produce incorrect results, perhaps using an incorrect formula (logical errors)
errors errors Basic Program Development Edit and save program Compile program Execute program and evaluate results
Applets • A Java application is a stand-alone program with a main method (like the ones we've seen so far) • A Java applet is a program that is intended to be transported over the Web and executed using a web browser • An applet also can be executed using the appletviewer tool of the Java Software Development Kit • An applet doesn't have a main method • Instead, there are several special methods that serve specific purposes • We will only concentrate on applications this week
data type variable name Variables • A variable is a name for a location in memory • A variable must be declared by specifying the variable's name and the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration
Variable Initialization • A variable can be given an initial value in the declaration int sum = 0; int base = 32, max = 149; • When a variable is referenced in a program, its current value is used
Assignment • An assignment statement changes the value of a variable • The assignment operator is the = sign total = 55; • The expression on the right is evaluated and the result is stored in the variable on the left • The value that was in total is overwritten • This is not a mathematical EQUATION • You can only assign a value to a variable that is consistent with the variable's declared type
Constants • A constant is an identifier that is similar to a variable except that it holds the same value during its entire existence • As the name implies, it is constant, not variable • The compiler will issue an error if you try to change the value of a constant • In Java, we use the final modifier to declare a constant final int MIN_HEIGHT = 69;
Constants • Constants are useful for three important reasons • First, they give meaning to otherwise unclear literal values • For example, MAX_LOAD means more than the literal 250 • Second, they facilitate program maintenance • If a constant is used in multiple places, its value need only be updated in one place • Third, they formally establish that a value should not change, avoiding inadvertent errors by other programmers
Primitive Data • There are eight primitive data types in Java • Four of them represent integers: • byte, short, int, long • Two of them represent floating point numbers: • float, double • One of them represents characters: • char • And one of them represents boolean values: • Boolean • Question: What does Boolean refer to? • Question: Why would we need so many different types?
Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value -128 -32,768 -2,147,483,648 < -9 x 1018 +/- 3.4 x 1038 with 7 significant digits +/- 1.7 x 10308 with 15 significant digits Max Value 127 32,767 2,147,483,647 > 9 x 1018 Numeric Primitive Data • The difference between the various numeric primitive types is their size, and therefore the values they can store:
Characters • A char variable stores a single character • Character literals are delimited by single quotes: 'a' 'X' '7' '$' ',' '\n' • Example declarations: char topGrade = 'A'; char terminator = ';', separator = ' '; • Note the distinction between a primitive character variable, which holds only one character, and a String object, which can hold multiple characters
Character Sets • A character set is an ordered list of characters, with each character corresponding to a unique number • A char variable in Java can store any character from the Unicode character set • The Unicode character set uses sixteen bits per character, allowing for 65,536 unique characters • It is an international character set, containing symbols and characters from many world languages • See http://www.unicode.org/charts/
uppercase letters lowercase letters punctuation digits special symbols control characters A, B, C, … a, b, c, … period, semi-colon, … 0, 1, 2, … &, |, \, … carriage return, tab, ... Characters • The ASCII character set is older and smaller than Unicode, but is still quite popular • The ASCII characters are a subset of the Unicode character set, including: • See http://www.lookuptables.com/
Boolean • A boolean value represents a true or false condition • The reserved words true and false are the only valid values for a boolean type boolean done = false; • A boolean variable can also be used to represent any two states, such as a light bulb being on or off
Addition Subtraction Multiplication Division Remainder + - * / % Expressions • An expression is a combination of one or more operators and operands • Arithmetic expressions compute numeric results and make use of the arithmetic operators: • If either or both operands used by an arithmetic operator are floating point, then the result is a floating point
Division and Remainder • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3equals 4 8 / 12equals 0 • The remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3equals 2 8 % 12equals 8
Operator Precedence • Operators can be combined into complex expressions result = total + count / max - offset; • Operators have a well-defined precedence which determines the order in which they are evaluated • Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation • Arithmetic operators with the same precedence are evaluated from left to right, but parentheses can be used to force the evaluation order