Post on 31-Dec-2015
description
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 1
Chapter 1
How to get started with Java and NetBeans
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 2
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.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 3
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.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 4
Java timeline
Year Release/Event
1996 Java Development Kit 1.0 (JDK 1.0).
1997 Java Development Kit 1.1 (JDK 1.1).
1998 Java 2 Platform with version 1.2 of the Software Development Kit (SDK 1.2).
1999 Java 2 Platform, Standard Edition (J2SE).
Java 2 Platform, Enterprise Edition (J2EE).
2000 J2SE with version 1.3 of the SDK.
2002 J2SE with version 1.4 of the SDK.
2004 J2SE 5.0 with version 1.5 of the JDK.
2006 Java SE 6 with version 1.6 of the JDK.
2010 Oracle buys Sun.
2011 Java SE 7 with version 1.7 of the JDK.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 5
Operating systems supported by Java Windows (XP, Vista, 7)
Linux
Solaris
Macintosh OS X
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 6
Java compared to C++ and C#
Feature Description
Syntax Java syntax is similar to C++ and C# syntax.
Platforms 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.
Speed C++ and C# run faster than Java, but Java is getting faster with each new version.
Memory Both Java and C# handle most memory operations automatically, while C++ programmers must write code that manages memory.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 7
A GUI application
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 8
A console application
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 9
An applet
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 10
A servlet
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 11
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: ");
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 12
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");
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 13
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; } }
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 14
How Java compiles and interprets code
source code(*.java files)
bytecodes(*.class files)
Java IDEOr
Text editor
Java compiler
Java interpreter
Java virtual machine (JVM)
Operating system
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 15
Popular Java IDEs NetBeans
Eclipse
IntelliJ IDEA
JCreator LE
BlueJ
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 16
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.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 17
NetBeans with a Java project open
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 18
The dialog box for opening a project
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 19
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 that’s 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 that’s 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, you’ll 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.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 20
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 don’t 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.
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 21
The Output window used for input and output
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 22
NetBeans with two open projects
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 23
The first dialog box for creating a new project
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 24
The second dialog box for creating a new project
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 25
The Sources category of the Project Properties dialog box
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 26
The Libraries category of the Project Properties dialog box
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 27
The starting source code for a project
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 28
The code editor with a code completion list
Murach’s Java Programming, C1 © 2011, Mike Murach & Associates, Inc. Slide 29
The code editor with an error displayed