Chapter 1
How to get started with Java and NetBeans
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 1
Objectives
Applied Given a NetBeans project that contains the source code for a Java application, use NetBeans to open the project, view and compile the source code, and run the application. Given the source code for a Java application, use NetBeans to create a project; enter edit, and compile the source code; and run the application.
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 2
Objectives (cont.)
Knowledge Describe how Java compares with C++ and C# based on these features: syntax, platform independence, speed, and memory management. Name and describe the three types of programs that you can create with Java. Describe how Java compiles and interprets code. Explain how the use of bytecodes lets Java achieve platform independence. Describe the benefits of using a Java IDE like NetBeans or Eclipse. Describe how NetBeans detects and displays syntax errors.
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 3
Java timeline
Year 1996 1997 1998 1999 2000 2002 2004 2006 2010 2011 Release/Event Java Development Kit 1.0 (JDK 1.0). Java Development Kit 1.1 (JDK 1.1). Java 2 Platform with version 1.2 of the Software Development Kit (SDK 1.2). Java 2 Platform, Standard Edition (J2SE). Java 2 Platform, Enterprise Edition (J2EE). J2SE with version 1.3 of the SDK. J2SE with version 1.4 of the SDK. J2SE 5.0 with version 1.5 of the JDK. Java SE 6 with version 1.6 of the JDK. Oracle buys Sun. Java SE 7 with version 1.7 of the JDK.
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 4
Operating systems supported by Java
Windows (XP, Vista, 7) Linux Solaris Macintosh OS X
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 5
Java compared to C++ and C#
Feature Syntax Platforms Description Java syntax is similar to C++ and C# syntax. Compiled Java code can be run on any platform that has a Java interpreter. Similarly, compiled C# code (MSIL) can be run on any system that has the appropriate interpreter. Currently, only Windows has an interpreter for MSIL. C++ code must be compiled once for each type of system that it is going to be run on. C++ and C# run faster than Java, but Java is getting faster with each new version. Both Java and C# handle most memory operations automatically, while C++ programmers must write code that manages memory.
Speed Memory
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 6
A GUI application
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 7
A console application
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 8
An applet
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 9
A servlet
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 10
The code for the Future Value application
import java.util.Scanner; import java.text.NumberFormat; public class FutureValueApp { public static void main(String[] args) { System.out.println( "\nWelcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.print( "Enter monthly investment: "); double monthlyInvestment = sc.nextDouble(); System.out.print( "Enter yearly interest rate: ");
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 11
The code for the Future Value application (cont.)
double interestRate = sc.nextDouble(); System.out.print( "Enter number of years: "); int years = sc.nextInt(); // calculate the future value double monthlyInterestRate = interestRate/12/100; int months = years * 12; double futureValue = calculateFutureValue( monthlyInvestment, monthlyInterestRate, months); // format and display the result NumberFormat currency = NumberFormat.getCurrencyInstance(); System.out.println( "Future value: " + currency.format(futureValue) + "\n");
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 12
The code for the Future Value application (cont.)
// see if the user wants to continue System.out.print("Continue? (y/n): "); choice = sc.next(); System.out.println(); } } private static double calculateFutureValue( double monthlyInvestment, double monthlyInterestRate, int months) { double futureValue = 0; for (int i = 1; i <= months; i++) futureValue = (futureValue + monthlyInvestment) * (1 + monthlyInterestRate); return futureValue; } }
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 13
How Java compiles and interprets code
Java IDE Or Text editor source code (*.java files)
Java compiler
bytecodes (*.class files)
Java virtual machine (JVM) Java interpreter
Operating system
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 14
Popular Java IDEs
NetBeans Eclipse IntelliJ IDEA JCreator LE BlueJ
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 15
Features provided by most IDEs
A code editor with code completion and error detection. Automatic compilation of classes when you run the application. A debugger that lets you set breakpoints, step through code, and view the values of active variables. A GUI builder that lets you create graphical user interfaces by dragging controls onto a form, setting properties, and writing code that handles the events that are triggered when a user interacts with the form.
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 16
NetBeans with a Java project open
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 17
The dialog box for opening a project
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 18
How to open, close, and delete a project
To open a project, click the Open Project button in the toolbar or select the FileOpen Project command. Then, use the Open Project dialog box thats displayed to locate and select the project and click the Open Project button. You can also open a project by using the FileOpen Recent Project command and then selecting the project from the list thats displayed. To close a project, right-click on the project in the Projects window and select the Close command, or select the project and then use the FileClose Project command. To delete a project, right-click on the project in the Projects window and select the Delete command. When you do, youll have the option of deleting just the files that NetBeans uses to manage the project or deleting all the folders and files for the project.
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 19
How to compile and run a project
To run a project, press F6, use the RunRun Project command, or click the Run Project button in the toolbar. When you run a project, NetBeans automatically compiles it. As a result, you usually dont need to compile a project separately. To compile a project without running it, you can right-click on the project in the Projects window and select the Build command. To delete all compiled files for a project and compile them again, you can right-click on the project and select the Clean and Build command. This removes files that are no longer needed and compiles the entire project.
Mac OS X note
To enable right-clicking with Mac OS X, you can edit the system preferences for the mouse.
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 20
The Output window used for input and output
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 21
NetBeans with two open projects
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 22
The first dialog box for creating a new project
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 23
The second dialog box for creating a new project
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 24
The Sources category of the Project Properties dialog box
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 25
The Libraries category of the Project Properties dialog box
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 26
The starting source code for a project
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 27
The code editor with a code completion list
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 28
The code editor with an error displayed
Murachs Java Programming, C1
2011, Mike Murach & Associates, Inc.
Slide 29