Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
into a single application Eclipse, NetBeans, etc. makes coding efficient and organized
files
Compiler Program
Build - included in the Eclipse IDE
What is a Computer?
A computer consists of a CPU, memory, hard disk, monitor, printer, and communication devices.
Bus
Memory
CPU
CPU
central processing unit (CPU) retrieves instructions from memory and executes them the CPU speed is measured in megahertz (MHz) 1 megahertz = 1 million pulses per second
Bus
Memory
CPU
Memory
stores data and program instructions for CPU to execute ordered sequence of bytes (8 bits binary base unit)
Bus
Memory
CPU
and processes binary numbers than decimal numbers more efficient: space & cost
Number Systems
binary octal decimal hexdecimal 0, 1 0, 1, 2, 3, 4, 5, 6, 7 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Number Systems
Computers use binary numbers internally because storage devices like memory and disk are made to store 0s and 1s. A number or a text inside a computer is stored as a sequence of 0s and 1s. Each 0 and 1 is called a bit (short for binary digit) Binary numbers are not intuitive, since we use decimal numbers in our daily life. When you write a number like 20 in a program, it is assumed to be a decimal number. Internally, computer software is used to convert decimal numbers into binary numbers, and vice versa.
10
Number Systems
The digits in the decimal number system are 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. A decimal number is represented using a sequence of one or more of these digits. The value that each digit in the sequence represents depends on its position. A position in a sequence has a value that is an integral power of 10. e.g., the digits 7, 4, 2, and 3 in decimal number 7423 represent 7000, 400, 20, and 3, respectively: = 7 10 3 + 4 10 2 + 2 101 + 3 100
7 4 2 3 103 102 101 100
We say that 10 is the base or radix of the decimal number system. The base of the binary number system is 2 since the binary number system has two digits The base of the hex number system is 16 since the hex number system has sixteen digits.
11
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
Number Systems
Binary numbers tend to be very long and cumbersome: For example: 101010101010 Hexadecimal numbers are often used to abbreviate binary numbers: For example: AAA The hexadecimal number system has 16 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F. The letters A, B, C, D, E, and F correspond to the decimal numbers 10, 11, 12, 13, 14, and 15.
12
bn 2 n + bn 1 2 n1 + bn 2 2 n2 + ... + b 2 2 2 + b1 21 + b 0 2 0
1 21 + 0
1 2 3 + 0 2 2 + 1 2 + 0
1 2 7 + 0 2 6 + 1 25 + 0 2 4 + 1 23 + 0 2 2 + 1 2 + 1
d = bn 2 n + bn 1 2 n1 + bn 2 2 n2 + ... + b 2 2 2 + b1 21 + b 0 2 0
These numbers can be found by successively dividing d by 2 until the quotient is 0. The remainders are bn, bn 1, bn 2,..., b 2, b1, b 0 For example, the decimal number 123 is 1111011 in binary. The conversion is conducted as follows:
0 2 1 0 1 b6 2 1 3 2 1 b5 2 3 7 6 1 b4 2 7 15 14 1 b3 2 15 30 30 0 b2 2 30 61 60 1 b1 2 61 123 122 1 b0 Remainder Quotient
14
Windows Calculator
The Windows Calculator is a useful tool for performing number conversions. To run it, choose Programs, Accessories, and Calculator from the Start button.
15
7 161 + 15
= 127 in decimal
16
These numbers can be found by successively dividing d by 16 until the quotient is 0. The remainders are h 0, h1, h 2,..., hn 2, hn 1, hn For example, the decimal number 123 is 7B in hexadecimal. The conversion is conducted as follows:
17
0 16 7 0 7 h1 16
7 123 112 11 h0
Quotient
Remainder
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
To convert a hexadecimal number to a binary number, simply convert each digit in the hexadecimal number into a four-digit binary number. To convert a binary number to a hexadecimal, convert every four binary digits from right to left in the binary number into a hexadecimal number. For example,
1110001101
3
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
19
a memory addresses
20
21
Programming Languages
Machine Language Assembly Language High-Level Language
A program called assembler is used to convert assembly language programs into machine code For example, to add two numbers, you might write an instruction in assembly code like this: ADDF3 R1, R2, R3
Assembly Source File ADDF3 R1, R2, R3 Machine Code File 1101101010011010
Assembler
23
Programming Languages
Machine Language Assembly Language High-Level Language
assembly: Far easier to use than binary BUT: not very user friendly, very low-level operations, programming is time consuming High Level programming Languages (HLL): more user friendly, easy to use more flexible platform independent
24
It is typically integrated with an assembly together they can make an executable or binary program
Source File
Compiler
Machine-language File
Linker
Executable File
Library Code
26
Operating Systems
The operating system (OS) is a program that manages and controls a computers activities Windows XP, Vista or 7 MacOS Android
User
Application Programs
Operating System
Hardware
27
Why Java?
Java is somewhat different Java has a principle, write once, run anywhere What does that mean?
Platform independence for compiled Java code
How?
The Java Virtual Machine
memory for Java programs Why? Each platform is different (Mac/PC/Linux/etc.) Java can be used to develop Web applications. Java Applets Java Web Applications Java can also be used to develop applications for handheld devices such as Palm and cell phones 29
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
30
JDK Versions
JDK 1.02 (1995) JDK 1.1 (1996) JDK 1.2 (1998) JDK 1.3 (2000) JDK 1.4 (2002) JDK 1.5 (2004) a. k. a. JDK 5 or Java 5 JDK 1.6 (2006) a. k. a. JDK 6 or Java 6 JDK 1.7 (2011) a. k. a. JDK 7 or Java 7
31
JDK Editions
Java Standard Edition (J2SE) J2SE can be used to develop client-side standalone applications or applets. Java Enterprise Edition (J2EE) J2EE can be used to develop server-side applications such as Java servlets and Java ServerPages. Java Micro Edition (J2ME). J2ME can be used to develop applications for mobile devices such as cell phones.
33
public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Source Code
Byte code (generated by the compiler for JVM to read and interpret, not for you to understand)
Method Welcome() 0 aload_0 Method void main(java.lang.String[]) 0 getstatic #2 3 ldc #3 <String "Welcome to Java!"> 5 invokevirtual #4 8 return
Bytecode
Result
34
(c) Pearson Education, Inc. & P.Fodor (CS Stonyruntime errors or incorrect result If Brook)
35
36
37
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
39
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
40
//This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
Comments
Three types of comments in Java.
Line comment: A line comment is preceded by two slashes (//) in a line. Paragraph comment: A paragraph comment is enclosed between /* and */ in one or multiple lines.
javadoc comment: javadoc comments begin with /** and end with */. They are used for documenting classes, data, and methods. They can be extracted into an HTML file using JDK's javadoc command.
43
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
Reserved Words
Reserved words or keywords are words that have a specific meaning to the compiler Cannot be used for other purposes in the program
Example: class
the word after class is the name for the class
44
45
Modifiers
Java uses certain reserved words called modifiers that specify the properties of the data, methods, and classes and how they can be used
Examples: public, static, private,
final, abstract, protected A public datum, method, or class can be accessed by other programs A private datum or method cannot be accessed by other programs
46
Statements
A statement represents an action or a sequence of actions
System.out.println("Welcome to Java!");
47
Blocks
A pair of braces in a program forms a block that groups components of a program.
public class Test { public static void main(String[] args) { System.out.println("Welcome to Java!"); } }
48
Your Identifiers (Names) Why name them? they are your data and commands youll need to reference them elsewhere in your program int myVariable = 5; // Declaration ... // Using the variable myVariable = myVariable + 1;
49
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
my4Variable
@#$myClass
50
toString Classes start with upper case letters: Person Variables and Class identifiers should generally be nouns Method identifiers should be verbs Use Camel notation: myVariable, MyClass Although it is legal, do not begin with _ (underscore). Use descriptive names: LinkedList, compareTo area = PI * radius * radius;
51
Variables
In a program, they store data Primitives store single pieces of data (ex: char)
char letter = 'A'; Objects store multiple pieces of data (ex: String) String text = "ABCDEFG"; All Java variables must have a declared type A variables type determines:
what kind of value the variable can hold how much memory to reserve for that variable
52
9223372036854775807)
Variables
Must be declared before being assigned values
public void methodWithGoodDeclaration() { double salary; salary = 20000.0; System.out.println("Salary is " + salary); } public void methodWithBadDeclaration() { salary = 20000.0; double salary; System.out.println("Salary is " + salary); }
54
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
Variables
Must be initialized before being referenced
public void methodWithGoodReference() { double salary; salary = 20000.0; double raise = salary * 0.05; // 5% raise System.out.println("Raise is " + raise); } public void methodWithBadReference() { double salary; // Salary has no value. double raise = salary * 0.05; System.out.println("Raise is " + raise); } (c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
55
Variables
A variable gets a value in an assignment statement (discussed
56
Variables
Should only be declared once
public void methodWithGoodDeclaration() { double salary = 20000.0; System.out.println("Salary is " + salary); salary = 60000.0; System.out.println("Salary is " + salary); } public void methodWithBadDeclaration() { double salary = 50000.0; System.out.println("Salary is " + salary); double salary = 60000.0; // Second declaration System.out.println("Salary is " + salary);
57
Variables
Variables can be declared and initialized at once
char yesChar = 'y'; String word = "Hello!"; double avg = 0.0, stdDev = 0.0; char initial3 = 'T'; boolean completed = false;
58
Variables
Variables can only be used inside the block { } or
} // x is in scope here. public void methodWithBadScope() { double y = 100.0; if (y > 0.0) { double x = 5.0; } System.out.println("x " + x); // x is not in scope 59 }
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
Variables
The Assignment Statement
variable = expression;
What does it do?
Solves/evaluates expression first Assigns resulting value to the variable
60
Variables
The Assignment Statement
variable = expression;
What does it do?
Solves/evaluates expression first Assigns resulting value to the variable
25
61
Variables
Assignment Compatibility: The variable and expression should be the same type if not, you may get a compiler error. Examples: int sumGrades, gradeX, gradeY; gradeX = 1; sumGrades = 1473; sumGrades = 1472 + 1; sumGrades = 1472 + gradeX; sumGrades = true; // ILLEGAL
62
Variables
What about mixing numeric types? Are these assignment statements ok?
int x = 5; long y = x; double z = y; What about these? double a = 6.5; long b = a; int c = b; byte <short <int <long <float <double
63
Variables
Type Casting as a type override temporarily change a data type to another type (type_name), example: (int) no type casting to/from boolean Examples:
64
Arithmetic Operators
+ * / % Addition Subtraction Multiplication Division Modulo/Remainder (integer operands only)
int x = 5; int y = 10; int z = 2; int num1 = (x + y) * z; System.out.println(num1);
65
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
Arithmetic Operators
+ * / % Addition Subtraction Multiplication Division Modulo/Remainder (integer operands only)
int x = 5; int y = 10; int z = 2; int num1 = (x + y) * z; System.out.println(num1);
66
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
30
Arithmetic Operators
Multiplication (*) has higher precedence over addition (+).
//12 //4
67
Arithmetic Operators
Multiplication (*) has higher precedence over addition (+).
25
//12 //4
68
Arithmetic Operators
Division Operator - evaluate full expression first
69
double average = 100.0/8.0; average = 100.0/8; average = 100/8; int sumGrades = 100/8; sumGrades = 100.0/8.0; sumGrades = (int)100.0/8.0; sumGrades = (int)(100.0/8.0); int fifty_percent = 50/100; double fiftyPercent = 50/100; fiftyPercent = 50.0/100.0;
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
//12.5 //12.5 //12.0 //12 //ERROR //ERROR //12 //0 //0.0 //0.5
Arithmetic Operators
The modulo/remainder % operator
Produces division remainders
70
Arithmetic Operators
The modulo/remainder % operator
Produces division remainders
71
Arithmetic Operators
++ -+= -= *= /= Increment by one Decrement by one Increment by specified amount Decrement by specified amount Multiply by specified amount Divide by specified amount
72
? ? ?
Arithmetic Operators
++ -+= -= *= /= Increment by one Decrement by one Increment by specified amount Decrement by specified amount Multiply by specified amount Divide by specified amount
73
6 ? ?
Arithmetic Operators
++ -+= -= *= /= Increment by one Decrement by one Increment by specified amount Decrement by specified amount Multiply by specified amount Divide by specified amount
74
6 16 ?
Arithmetic Operators
++ -+= -= *= /= Increment by one Decrement by one Increment by specified amount Decrement by specified amount Multiply by specified amount Divide by specified amount
75
6 16 26
int newNum = 10 * i; i = i + 1;
Same effect as
i = i + 1; int newNum = 10 * i;
76
Scientific Notation
Floating-point literals can also be specified in scientific
notation:
lowercase or uppercase
Examples
77
Classes
A class is a template or blueprint for objects A program is defined by using one or more classes
public class ClassName { public static void main(String[] args) { // ClassName PROGRAMS POINT OF ENTRY // THIS PROGRAMS INSTRUCTIONS // START HERE } }
78
Methods
A method is a collection of statements that performs a sequence of operations It is used by invoking a statement with arguments
System.out.println("Welcome to Java!");
79
main Method
The main method provides the control of program flow. ClassName is executable because it has a main method we can compile and then run it Not all classes require main methods only those classes that initiate program execution require a main method public class ClassName { public static void main(String[] args) {...} }
80
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)
Homework 1
Homework 1 won't be not graded Learn submission of homework
82
Homework: Implement public static void main(String[] args) { ChangeMaker int change, rem, qs, ds, ns, ps; System.out.print("Input change amount (1-99): "); Eclipse
Scanner input = new Scanner(System.in); change = input.nextInt(); qs = change / 25; rem = change % 25; ds = rem / 10; rem = rem % 10; ns = rem / 5; rem = rem % 5; ps = rem; System.out.print(qs + " quarters," + ds + " dimes,"); System.out.println(ns + " nickels and" + ps + " pennies");
}
84 }
(c) Pearson Education, Inc. & P.Fodor (CS Stony Brook)