Download presentation
Presentation is loading. Please wait.
1
Java basics
2
In class quiz What are the rules for an identifier in Java?
In what method does a program begin? What is the return type of that method? What parameter(s) does it require? What is the exponentiation operator in Java? How do we produce output in Java? What is operator precedence?
3
Programming Problem solving through the use of a computer system Maxim
You cannot make a computer do something if you do not know how to do it yourself
4
Software Program Sequence of instruction that tells a computer what to do Execution Performing the instruction sequence Programming language Language for writing instructions to a computer Major flavors Machine language or object code Assembly language High-level The instruction set for a machine is a set of binary codes that are unique to its CPU type. Consequently, different computers use different machine languages. The machine language understood by Intel’s Pentium processor is quite different from the machine language understood by IBM’s PowerPC® processor. A major problem with machine language programming is that it is very tedious and error prone to write directly in binary codes. Slightly less tedious is assembly language programming. An assembly language is a symbolic language for coding machine language instructions. Like machine language programmers, assembly language programmers must have a complete understanding of basic operations of the machine. Furthermore, because the corresponding machine operations are so primitive, for even very simple tasks, assembly language programs can be quite long and complicated. Program to which computer can respond directly. Each instruction is a binary code that corresponds to a native instruction
5
Symbolic language for coding machine language instructions
Software Program Sequence of instruction that tells a computer what to do Execution Performing the instruction sequence Programming language Language for writing instructions to a computer Major flavors Machine language or object code Assembly language High-level The instruction set for a machine is a set of binary codes that are unique to its CPU type. Consequently, different computers use different machine languages. The machine language understood by Intel’s Pentium processor is quite different from the machine language understood by IBM’s PowerPC® processor. A major problem with machine language programming is that it is very tedious and error prone to write directly in binary codes. Slightly less tedious is assembly language programming. An assembly language is a symbolic language for coding machine language instructions. Like machine language programmers, assembly language programmers must have a complete understanding of basic operations of the machine. Furthermore, because the corresponding machine operations are so primitive, for even very simple tasks, assembly language programs can be quite long and complicated. Symbolic language for coding machine language instructions
6
Software Program Sequence of instruction that tells a computer what to do Execution Performing the instruction sequence Programming language Language for writing instructions to a computer Major flavors Machine language or object code Assembly language High-level A distinguishing characteristic of a high-level programming language is that detailed knowledge of the machine being programmed is not required. Another characteristic is that a high-level programming language uses a vocabulary and structure that is close to the type of problem being solved. For example, the programming language FORTRAN, which is used to solve scientific and engineering problems, uses a notation that is mathematical. Indeed, the name FORTRAN is derived from the phrase formula translation. Because of the close coupling of a programming language to types of problems, there are literally hundreds of high-level programming languages. The commands in a high-level language program are not executed directly by a computer. A high-level language program has to be translated first. The conversion is accomplished by a specialized program called a translator. A translator accepts a program written in a one language and translates it to an equivalent program in another language. The input to the translator is the source program and the output of the translator is the target program. Most translators convert a high-level language program to a machine language program. For high-level languages, a translator normally is referred to as a compiler. Detailed knowledge of the machine is not required. Uses a vocabulary and structure closer to the problem being solved
7
Java is a high-level programming language
Software Program Sequence of instruction that tells a computer what to do Execution Performing the instruction sequence Programming language Language for writing instructions to a computer Major flavors Machine language or object code Assembly language High-level A distinguishing characteristic of a high-level programming language is that detailed knowledge of the machine being programmed is not required. Another characteristic is that a high-level programming language uses a vocabulary and structure that is close to the type of problem being solved. For example, the programming language FORTRAN, which is used to solve scientific and engineering problems, uses a notation that is mathematical. Indeed, the name FORTRAN is derived from the phrase formula translation. Because of the close coupling of a programming language to types of problems, there are literally hundreds of high-level programming languages. The commands in a high-level language program are not executed directly by a computer. A high-level language program has to be translated first. The conversion is accomplished by a specialized program called a translator. A translator accepts a program written in a one language and translates it to an equivalent program in another language. The input to the translator is the source program and the output of the translator is the target program. Most translators convert a high-level language program to a machine language program. For high-level languages, a translator normally is referred to as a compiler. Java is a high-level programming language
8
For program to be executed it must be translated
Software Program Sequence of instruction that tells a computer what to do Execution Performing the instruction sequence Programming language Language for writing instructions to a computer Major flavors Machine language or object code Assembly language High-level A distinguishing characteristic of a high-level programming language is that detailed knowledge of the machine being programmed is not required. Another characteristic is that a high-level programming language uses a vocabulary and structure that is close to the type of problem being solved. For example, the programming language FORTRAN, which is used to solve scientific and engineering problems, uses a notation that is mathematical. Indeed, the name FORTRAN is derived from the phrase formula translation. Because of the close coupling of a programming language to types of problems, there are literally hundreds of high-level programming languages. The commands in a high-level language program are not executed directly by a computer. A high-level language program has to be translated first. The conversion is accomplished by a specialized program called a translator. A translator accepts a program written in a one language and translates it to an equivalent program in another language. The input to the translator is the source program and the output of the translator is the target program. Most translators convert a high-level language program to a machine language program. For high-level languages, a translator normally is referred to as a compiler. For program to be executed it must be translated
9
Translation Translator
Accepts a program written in a source language and translates it to a program in a target language Compiler Standard name for a translator whose source language is a high-level language Interpreter A translator that both translates and executes a source program The commands in a high-level language program are not executed directly by a computer. A high-level language program has to be translated first. The conversion is accomplished by a specialized program called a translator. A translator accepts a program written in a one language and translates it to an equivalent program in another language. The input to the translator is the source program and the output of the translator is the target program. Most translators convert a high-level language program to a machine language program. For high-level languages, a translator normally is referred to as a compiler. A particular type of translator of interest to Java programmers is an interpreter. An interpreter is a translator that both translates and executes the source program.
10
Java translation Two-step process First step
Translation from Java to bytecodes Bytecodes are architecturally neutral object code Bytecodes are stored in a file with extension .class Second step An interpreter translates the bytecodes into machine instructions and executes them Interpreter is known a Java Virtual Machine or JVM At the beginning of this section, we mentioned that one of the key features of Java is that a Java program can run on a variety of different types of machines. This feature makes Java ideal for developing Internet applications where there are many different types of machines connected to the network. It is the Java interpreter that makes this happen. When we compile a Java program, the Java compiler does not produce a machine language program for a particular computer like the compilers for other programming languages such as C, C++, or FORTRAN do. Rather the Java compiler produces a program for an interpreter called the Java Virtual Machine (JVM). Essentially, the JVM is a program that mimics the operation of a real machine. The JVM reads the program produced by the Java compiler and executes the Java machine language instructions produced by the Java compiler. The Java machine language instructions are called Java bytecodes and can be viewed as architecturally neutral object code. The bytecodes are stored in a file with an extension of .class. For program DisplayForecast.java, a Java compiler produces a bytecode file named DisplayForecast.class.
11
Task Display the supposed forecast
I think there is a world market for maybe five computers. Thomas Watson, IBM, 1943.
12
Sample output
13
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson
// Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); }
14
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Three statements make up the action of method main() Method main() is part of class DisplayForecast
15
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } A method is a named piece of code that performs some action or implements a behavior
16
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } An application program is required to have a public static void method named main().
17
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } public, static, and void are keywords. They cannot be used as names public means the method is shareable
18
DisplayForecast.java Consider static and void later
// Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Consider static and void later
19
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Java allows a statement to be made up of multiple lines of text Semicolons delimit one statement from the next
20
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } A class defines an object form. An object can have methods and attributes Keyword class indicates a class definition follows
21
DisplayForecast.java The class has a name
// Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The class has a name
22
DisplayForecast.java // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Programs are read by people – make sure they are readable. Use whitespace, comments, and indentation to aid understanding
23
DisplayForecast.java Whitespace Whitespace separates program elements
// Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Whitespace Whitespace separates program elements Whitespace between program elements is ignored by Java
24
DisplayForecast.java Three comments
// Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Three comments // indicates rest of the line is a comment Comments are used to document authors, purpose, and program elements
25
Method main() is part of DisplayForecast
Indentation // Authors: J. P. Cohoon and J. W. Davidson // Purpose: display a quotation in a console window public class DisplayForecast { // method main(): application entry point public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Method main() is part of DisplayForecast Statements are part of method main() Indentation indicates subcomponents
26
Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Class System supplies objects that can print and read values System variable out references the standard printing object Known as the standard output stream Variable out provides access to printing methods print(): displays a value println(): displays a value and moves cursor to the next line
27
System.out
28
Selection
29
Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } Method print() and println() both take a string parameter The parameter specifies the value that is to be used in the invocation
30
Method main() The print() statement starts the program output
public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The print() statement starts the program output I think there is a world market for░
31
Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The first println() statement completes the first line of output I think there is a world market for maybe five computers ░
32
Method main() public static void main(String[] args) { System.out.print("I think there is a world market for"); System.out.println(" maybe five computers."); System.out.println(" Thomas Watson, IBM, 1943."); } The second println() statement starts and completes the second line of output I think there is a world market for maybe five computers Thomas Watson, IBM, 1943. ░
33
Experiments How do we do the following?
I will do good in my classes. You will do good in your classes. I'll do good in my classes. "You will do good in your classes," said Cohoon. It was a dark and stormy night; the rain fell in torrents--except at occasional intervals, when it was checked by a violent gust of wind which swept up the streets (for it is in London that our scene lies), rattling along the housetops, and fiercely agitating the scanty flame of the lamps that struggled against the darkness.
34
Computation Programmers frequently write small programs for computing useful things Example – body mass index (BMI) Measure of fitness Ratio of person’s weight to the square of the person’s height Weight in is kilograms, height is in meters Person of interest is 4.5 feet and weighs 75.5 pounds Metric conversions Kilograms per pound 0.454 Meters per foot
35
Common program elements
Type Set of values along with operators that can manipulate and create values from the set Primitive types support numeric, character, logical values double and float Values with decimals byte, short, int, long Integers char Characters (considered numeric) boolean Logical values Basic operators + addition - subtraction * multiplication / division
36
Common program elements
Constant Symbolic name for memory location whose value does not change KILOGRAMS_PER_POUND Variable Symbolic name for memory location whose value can change weightInPounds Good programmers prefer the systematic use of symbolic names over literal values—so-called “magic” numbers—because the names provide meaning and their use reduces the potential introduction of errant values through incorrect keyboarding. It is a Java programming convention that the identifier name of a constant be composed of uppercase letters with underscores separating the various words that make up a name. Thus, the use of KILOGRAMS_PER_POUND in BMI.java rather than kilogramsPerPound. It is a Java coding style convention that the identifier name of a variable be descriptive of the represented value and that it begin with a lowercase letter. If a variable name is composed of a single word, then the entire name is in lowercase. If a variable name is composed of multiple words, then the words are concatenated together and all letters are lowercase except for the letters that start successive words. Thus, the coding style calls for bodyTemperature rather than the nondescriptive bt or the underscore using body_temperature.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.