Download Introduction to Java Applications

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

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

Document related concepts
no text concepts found
Transcript
1
2
Introduction
to Java Applications
© 1992-2007 Pearson Education, Inc. All rights reserved.
2
2.2
First Program in Java: Printing a Line of Text
2.3
Modifying Our First Java Program
2.4
Displaying Text with printf
2.5
Another Java Application: Scanner
2.7
Arithmetic
2.8
Decision Making: Equality and Relational
Operators
© 1992-2007 Pearson Education, Inc. All rights reserved.
3
2.2 First Program in Java: Printing a Line
of Text
• Download JDK6
– http://java.sun.com/
– set path=C:\Program Files\Java\jdk1.6.0_16\bin;%PATH%
• Application
– javac Welcome1.java
– java Welcome1
// file name is class name
// invoke main() of the specified class
• System.out.println()
– System: a class
– out: standard output object (a member of System class)
– println(): a method
© 1992-2007 Pearson Education, Inc. All rights reserved.
1
4
Fig 2.1
public class Welcome1
Welcome1.java
{
// main method begins execution of Java application
public static void main( String args[] )
{
System.out.println( "Welcome to Java Programming!" );
}
}
(JRE)
> Welcome to Java Programming!
© 1992-2007 Pearson Education, Inc. All rights reserved.
5
(source: http://java.sun.com/docs/books/tutorial/getStarted/intro/definition.html)
© 1992-2007 Pearson Education, Inc. All rights reserved.
6
Good Programming Practice 2.3
© 1992-2007 Pearson Education, Inc. All rights reserved.
2
7
Common Programming Error 2.2
© 1992-2007 Pearson Education, Inc. All rights reserved.
8
2.3 Modifying Our First Java Program
// Fig. 2.3: Welcome2.java
© 1992-2007 Pearson Education, Inc. All rights reserved.
9
// Fig. 2.4: Welcome3.java
© 1992-2007 Pearson Education, Inc. All rights reserved.
3
10
2.4 Displaying Text with printf
•System.out.printf
– Feature added in Java SE 5.0
– Displays formatted data
© 1992-2007 Pearson Education, Inc. All rights reserved.
11
// Fig. 2.6: Welcome4.java
© 1992-2007 Pearson Education, Inc. All rights reserved.
12
2.5 Another Java Application: Adding
Integers
• Upcoming program
– Use Scanner to read two integers from user
– Use packages
© 1992-2007 Pearson Education, Inc. All rights reserved.
4
13
– Primitive type
– Class
// app.java
class book
{
int page_num;
float price;
// class declaration
// class definition
// data member, field, or instance variable
void display()
// member function, or method
{
System.out.println(page_num + " " + price);
}
}
© 1992-2007 Pearson Education, Inc. All rights reserved.
14
class app
{
public static void main(String[] args)
{
book b1;
// object reference
b1 = new book();
b1.page_num = 200;
b1.price = 299.0f;
b1.display();
}
}
// instantiation
Object instance
b1
page_num
price
> javac app.java // generate app.class & book.class
> java app
> 200 299.0
void display();
© 1992-2007 Pearson Education, Inc. All rights reserved.
15
Fig 2.7
Fig 2.10
© 1992-2007 Pearson Education, Inc. All rights reserved.
5
16
3
import java.util.Scanner;
// program uses class Scanner
– import declarations
• Used by compiler to identify and locate classes used in Java
programs
• All import declarations must appear before the first class
declaration in the file.
• Forgetting to include an import declaration for a class used in
your program typically results in a compilation error
containing a message such as “cannot resolve symbol.”
© 1992-2007 Pearson Education, Inc. All rights reserved.
17
Good Programming Practice 2.12
© 1992-2007 Pearson Education, Inc. All rights reserved.
18
2.7 Arithmetic
• Arithmetic calculations used in most programs
– Usage
•
•
•
•
* for multiplication
/ for division
% for remainder (e.g., 7 % 5 evaluates to 2)
+, -
– 6.0: double precision in default (vs. 6.0f)
• float a = 6.0; // ??
– Integer division truncates remainder
7 / 5 evaluates to 1 (vs. 7.0/5)
© 1992-2007 Pearson Education, Inc. All rights reserved.
6
19
2.8 Decision Making: Equality and
Relational Operators
Fig. 2.14 | Equality and relational operators.
© 1992-2007 Pearson Education, Inc. All rights reserved.
20
Fig 2.15
© 1992-2007 Pearson Education, Inc. All rights reserved.
21
Exercise
1.EX 2.25
• 輸入攝氏溫度,將之轉換成華氏溫度
© 1992-2007 Pearson Education, Inc. All rights reserved.
7