Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
GETTING STARTED: YOUR FIRST JAVA APPLICATION 3 CREATING YOUR FIRST JAVA APPLICATION (USING WINDOWS) GETTING STARTED: YOUR FIRST JAVA APPLICATION Checklist: The most recent version of Java SE Development Kit (JDK) – can be downloaded from http://java.sun.com/javase/downloads/index.jsp A text editor – such as Notepad or WordPad Your first application, HelloWorld will simply display the greeting “Hello world!” using the command prompt. The steps are: 1. Create a source file To create your first Java source file (or called as source code): i. Start any text editor then create a new document, if necessary. ii. Type the class header public class HelloWorld. Press Enter once then type {. iii. Press Enter again and then type }. We will add main()method between these curly braces. iv. As shown in Figure 3.1 below, add the main()method between the curly braces by typing public static void main (String[] args). Then type a set of curly braces for main(). public class HelloWorld { public static void main(String[] args) { } } Figure 3.1 The main()method for the HelloWorld class 15 16 Java Programming Workbook v. Next, add the statement System.out.println(“Hello world!”) within the main() method that can produce the output, Hello World!. public class HelloWorld { public static void main(String[] args) { System.out.println("Hello world!"); // Display the string. } } Figure 3.2 Complete HelloWorld class vi. Save the application as HelloWorld.java. Important: Make sure the file extension is .java by appending the .java to the file name when saving the file. Now, you may exit the text editor. 2. Compile the source file After saving the application, you must compile the source code into a .class file (or called as bytecode). To compile your source code from the command line: i. Start the command prompt. ii. Change the default drive prompt to the drive and the directory where your application is stored. iii. Type javac followed by the name of the file that contains the source code. Then press Enter. For example, to compile the HelloWorld.java, you type javac HelloWorld.java then press Enter. Figure 3.3 Changing the directory of the application FIRST JAVA APPLICATION USING ECLIPSE IDE Important: The name must match exactly, including the use of uppercase and lowercase characters. The compiler (javac) and launcher (java) tools are case-sensitive. Now that you have a .class file, you can run the application. 3. Run the application When the compile is successful, in order to run the source code from the command line: i. Type java followed by the class name of the source code at the command line and press Enter. For example, to run the HelloWorld.java, you type java HelloWorld. ii. The output should appear on the next line as shown in Figure 3.4 below. Figure 3.4 Output of the HelloWorld application Congratulations, your program works! FIRST JAVA APPLICATION USING ECLIPSE IDE Checklist: The most recent version of Java SE Development Kit (JDK) Eclipse IDE The steps to display the greeting “Hello world!” using the Eclipse IDE are: 17 18 Java Programming Workbook 1. Create a Java project Before you can do anything else in Eclipse, such as creating a Java program, you need to create a Java project. To create a new Java project, follow these steps: i. Start Eclipse IDE and make sure it is in Java perspective. If you are not already in the Java perspective, in the main menu select Window, point to Open Perspective and then choose Java. ii. Click File in the main menu and then point to New, and choose Java Project. iii. Enter helloworld for the project name as shown in Figure 3.5, then click Finish. iv. The helloworld project will be shown as a folder on the Package Explorer. 2. Create a Java class Once you have created a project for it to live in, you can create your first Java program. Although doing so is not necessary, it's a good practice to organize your Java classes into packages. Here, we'll use a package named helloworld. It is a good convention to use the top package the same name as the project. To create a new Java class, follow these steps: i. Right-click on the helloworld project and point to New, and then select Class to bring up the New Java Project window. ii. Leave the Source Folder field as it is which is helloworld/src by default. Figure 3.5 Creating new Java project named HelloWorld FIRST JAVA APPLICATION USING ECLIPSE IDE iii. iv. In the Package field enter helloworld and enter HelloWorld in the Name field. Check the box for public static void main(String[] arg) in the ‘Which Method Stubs Would You Like to Create?’ section as shown in Figure 3.6. Then, click Finish. Figure 3.6 Creating new Java class named HelloWorld v. vi. The Java editor will automatically open showing your new class. The code that is automatically generated includes a method stub for main(). Now that you have your HelloWorld class, in the main() method, add the following statement: System.out.println("Hello world!"); vii. Then save your changes. The class will automatically compile upon saving. 3. Run the application When the compile is successful, in order to run your application in Eclipse: i. Right-click on HelloWorld.java in the Package Explorer, point to Run As and select Java Application. ii. The Console view should appear at the bottom and display the "Hello, world!" output as shown in Figure 3.7. 19 20 Java Programming Workbook Figure 3.7 Output of the HelloWorld application Congratulations! You have successfully created a Hello World application. RUN JAVA APPLICATION OUTSIDE ECLIPSE IDE In previous example, we run the application in the Eclipse IDE. To run a Java application outside the Eclipse, we need to export the program as a Java ARchive (or JAR) file. To create a JAR file for the helloworld project: 1. Right-click on the helloworld project and select Export. 2. In the Export window, select Java and then select JAR file. Click Next. 3. In the JAR Export window as shown in Figure 3.8, select helloworld project as the resource to be export. Select an export destination and a name for the JAR file. For example, choose your Desktop as the destination and helloworld.jar as the JAR file name. 4. Click Finish. A JAR file will be created in your selected destination. To run the JAR file: 1. Start the command prompt. 2. Change the default drive prompt to the drive and the directory where your JAR file is stored. 3. Type the following statement and press Enter. java -classpath helloworld.jar helloworld.HelloWorld 4. The output should appear on the next line as shown in Figure 3.9. CREATING A DIALOG BOX Figure 3.8 JAR Export window Figure 3.9 Output when running the helloworld.jar CREATING A DIALOG BOX To create a Java application that produces output in a dialog box: 1. Open the Eclipse IDE, if necessary. 2. To create a new class, right-click on the helloworld package, point to New and then click Class. 3. Enter HelloDialog in the Name field then click Finish. 4. The Java editor will automatically open showing your new class. The code that is automatically generated includes a class header and an opening and closing brace for the class named HelloDialog. 5. Position the insertion point at the end of the first line that contains the package declaration then press Enter to insert a new line. 6. Type the import statement that allows you to use the JOptionPane class: import javax.swing.JOptionPane; 21 22 Java Programming Workbook 7. Position the insertion point after the opening curly brace, press Enter, and then type the following: public static void main(String[] args) { JOptionPane.showMessageDialog(null, “Hello world!”); } 8. Save the application. The class will automatically compile upon saving. 9. Run the application. The output should appear as shown in Figure 3.10. Click OK to close the dialog box. Figure 3.10 Output of the HelloDialog application ADDING COMMENTS TO A CLASS You will add comments to your HelloWorld.java application and save it as a new class named HelloWorld2. To add comments to your application and save it as a new class: 1. Open the Eclipse IDE, if necessary. 2. Double-click the HelloWorld.java in the Package Explorer to open the file in the Java Editor. 3. Position the insertion point at the top of the file that contains the package declaration then press Enter to insert a new line. 4. Press the Up arrow to go to that line, and then type the following comments at the top of the file. Press Enter after typing each line. Insert your name and today’s date where indicated. ADDING COMMENTS TO A CLASS // Filename HelloWorld2.java // Written by <your name> // Written on <today’s date> 5. Change the HelloWorld class name to HelloWorld2, press Enter and then type the following block comment: /* This class demonstrates the use of the print() and println() method to print the message Hello, world! and I am a new student.I'm ready for Java programming. */ 6. The HelloWorld2 class name will be underlined with a problem highlight line. Ignore this problem. 7. Add the following statements below the statement that prints “Hello, world!” as shown in Figure 3.11: System.out.print(“I am a new student.”); System.out.println(“I’m ready for Java programming!”); 8. Click File on the main menu and click Save As. 9. In the Save As window, change the File name to HelloWorld2.java. Click OK. You will see a new class named HelloWorld2.java created in the Package Explorer. The problem highlight name is also being fixed. 10. Run the application. The output should appear as shown in Figure 3.12. Figure 3.11 The HelloWorld2 class 23 24 Java Programming Workbook Figure 3.12 Output of the HelloWorld2 application Note: As you work through this book, add comments as the first three lines of every exercise that you do. The comments should contain the number of question and the class name, purpose of the source code, your name and the date created. Do it yourself 3.1 Write, compile and test a class that prints your name on the screen. Save the class as Name.java. 3.2 Write, compile and test a class that prints your full name, matric number and your address in three separate lines on the screen. Save the class as Student.java. 3.3 Write, compile and test a class that produces a series of three dialog boxes so that each displays one line of the output from Exercise 3.2 in turn. Save the class as StudentDialog.java. 3.4 Identify and fix the errors in the following code: Public class Welcome { public void Main(string[] args) { System.out.println(‘Welcome to Java’) } Do it yourself 3.5 Identify the output of the following code: // Ex 3.5: Hello.java // Printing a line of text with multiple statements public class Hello { public static void main(String[] args) //begin execution of program { System.out.print(“Welcome to ”); System.out.println(“Java Programming!”); }// end method main }// end class Hello 3.6 Write a Java program that will prints multiple lines of text as follows: Welcome to Java Programming! CASE PROJECT Making Money Bank intends to provide a new automated teller machine (ATM) for the customers to perform basic financial transactions. Each user has only one account at the bank. ATM users should be able to view their account balance, withdraw cash and deposit funds. The company has asked you to write a Java application for the ATM. Write, compile and test a Java class that displays “Making Money Bank ATM Service” and “Welcome!” in an attractive layout on the console screen. Be certain to use appropriate comments in your class. Save the class as MakingMoneyATM.java. Example of the layout you can create: ******************************************************************** * * * Making Money Bank ATM Service * * * ******************************************************************** Welcome! 25 26 Java Programming Workbook NOTES