Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java Lab

Java Lab

LeoNguyen.com

July 11, 2013
Tweet

More Decks by LeoNguyen.com

Other Decks in Programming

Transcript

  1. Outline - Lab 01: An introduction to Java - Lab

    02: Java Language - Lab 03: Class and Relationship - Lab 04: Exception and debugging - Lab 05: Thread - Lab 06: Input/Output - Lab 07: Generic programming and Collections
  2. Outline - Download and install JDK - Write a Helloworld

    program - Install and use Eclipse to write a Java program - Debugging Programs in Eclipse
  3. Task 1 - Download JDK - Goto Java download site

    http://www.oracle. com/technetwork/java/javase/downloads/index.html
  4. Task 1 - Download JDK (cont) - Select Java Platform

    (JDK), choose your operation platform (eg Window X86) and download it
  5. Task 2 - Install JDK - Run the downloaded installer,

    which installs both the JDK (Java Development Kit) and JRE (Java Runtime). By default the JDK and JRE will be installed into directories C:\Program Files\Java
  6. Task 3 - Config PATH environment variables - Windows Operating

    System searches the current directory and the directories listed in the PATH environment variable for executable programs invoked from the CMD shell. It helps programmer can compile Java code in CMD shell. - Click the "Start" button > "Control Panel" > "System" > (Vista/7 only) "Advanced system settings"
  7. Task 4 - Verify the JDK Installation - Launch a

    CMD shell > type java –version to check that JDK is properly installed and display its version, and javac to check Path work properly too.
  8. Task 2 - Write your code (cont) - Save the

    code in a file with the name Helloworld.java - Note: File name must same as class name.
  9. Task 3 - Compile and run on command-line - Launch

    a CMD shell > type javac to compile the source code and java to run the program using the JDK runtime
  10. Task 1 - Download Eclipse - Download Eclipse from http://www.eclipse.org/downloads.

    For beginners, choose the minimal Eclipse IDE for Java Developers. - Unzip the downloaded file into a directory of your choice.
  11. Task 2 - Launch Eclipse - Open eclipse.exe in the

    Eclipse installed directory - Choose an appropriate directory for your workspace
  12. Task 3 - Create a new Java Project - Choose

    "File" menu > "New" > "Java project"
  13. Task 4 - Write a Java program - In the

    "Package Explorer" (left panel) > Right-click on "JavaLab" (or use the "File" menu) > New > Class
  14. Task 5 - Compile and Execute - To run the

    program, right-click anywhere on the source file "HelloWorld.java" (or from the "Run" menu) > Choose "Run As" > "Java Application".
  15. Task 1 - Create project and write a simple program

    - Create a new class named Debug and enter the following code.
  16. Task 2 - Set an Initial Breakpoint - Set a

    breakpoint at main() method by double-clicking on the left- margin of the line containing main().
  17. Task 3 - Debug - Right click anywhere on the

    source code (or from the "Run" menu) > "Debug As" > "Java Application" > choose "Yes" to switch into "Debug" perspective Step-Over and Watch the Variables and Outputs.
  18. Outline - Relational & Logical Operators - Types - Scanner

    object - If/else - Switch case - Command-Line Arguments
  19. Outline (cont) - OddEvenSum - Compute PI - Do While

    statement - Array - Loops and conditional statement - Nested loop - Break label - Continue label - Array Sort - Reverse String - Array of String
  20. Diagram - MobilePhone class to store mobile phone numbers and

    send a messages to one of of numbers store in the array. - PhoneTest class to demonstrate exception handling.
  21. Diagram (cont) The management of the A Bank is looking

    at automation as a means to save time and effort required in their work. In order to achieve this, the management has planned to computerize the following transactions: • Creating a new account • Withdrawing money from an account • Depositing money in an account The CEO of the company and a team of experts have chosen your company to provide a solution for the same. Consider yourself to be a part of the team that implements the solution for designing the application.
  22. Diagram (cont) Create an application using exceptions and assertions to

    implement the transactions. The application should consist of the following classes. 1. Account.java 2. Account Test.java 3. InsufficientFundException.java 4. NegativeAmountException.java Each class has a specific purpose and functionality. The descriptions of each class are as follows.
  23. Task 1 - Create Account class (The Account class represents

    an actual bank account. It stores the following details of a bank account) • customerName • accountNumber • accountbalance • void displayAccountDetails() : This method displays the details of the account
  24. Task 1 - Create Account class (cont) • void withdraw()

    : This method is used to withdraw money from an account. This method accepts the account number and the amount to be withdrawn from the account. The method then searches in the array of accounts for the account number. Use assertions for checking whether the account number and the amount to be withdrawn are positive. Also use an assertion to check if the array of accounts contains a minimum of one account record. The method also throws the user-defined exception InsufficientFund***ception in case the amount to be withdrawn exceeds • void deposit() :This method is used to deposit money in an account. The account number and the amount to be deposited in the account is accepted from the user. Use an assertion to check whether the account number is positive. The method searches for the account number and deposits the amount in the account if it exists. The displayAccountDetails() method is called if the operation succeeds. Use appropriate try catch blocks to handle all the possible exceptions that can be thrown due to the user inputs. A user-defined exception is thrown if the account number does not exist.
  25. Task 2 - Create AccountTest class (The AccountTest class is

    a java main class used to test the Account class. It creates an instance of the Account class and displays the following menu of options to the user) · Create a new account · Withdraw Cash · Deposit cash · Exit The user can select any of the options and a corresponding method is invoked on the instance of the Bank class. Use an assertion to check for the control-flow invariant in case the user types an invalid option. The application exits when the Exit option is selected.
  26. Task 3 - Create InsufficientFundException class - This is a

    user-defined exception class derived from the base class Exception. This exception is thrown when the user tries to withdraw more money than the current account balance.
  27. Task 4 - Create NegativeAmountException class - This is a

    user-defined exception class derived from the base class Exception. This exception is thrown when the user tries to withdraw or deposit a negative amount.
  28. Outline - Use the Thread class - Implement the Runable

    Interface - Using join() to wait for threads to finish - Thread priority - Synchronization - Dining Philosopher
  29. Stream - Most fundamental I/O in Java is based on

    streams - Reading information into a program - Writing information from a program
  30. Outline - Read a file - BufferedReader - Write a

    file - BufferedWriter - Write a file - FileOutputStream - List the contents of a directory (Recursively) - Copying a file without Buffering - Copying a file with a Programmer-Managed Buffer - Copying a file with Buffered Streams - Best Copier - InputStreamReader and OutputStreamWriter - BufferedReader and BufferedWriter - PipedReader and PipedWriter - Serializable - ObjectOutputStream
  31. Task 1 - Create ReadFileBuffered program - The simplest and

    most common-used method - BufferedReader.
  32. Task 1 - Create WriteFileBuffered program - BufferedWriter is a

    character streams class to handle the character data. Unlike bytes stream (convert data into bytes), you can just write the strings, arrays or characters data directly to file.
  33. Task 1 - Create WriteFileStream program - FileOutputStream is a

    bytes stream class that’s used to handle raw binary data. To write the data to file, you have to convert the data into bytes and save it to file.
  34. Outline - Generic class and Wildcard - Student List -

    ArrayList - Employee Management - LinkedList - Set - Map
  35. Task 6 - Create Test BoxB, BoxC program - Choose

    "Run As" > "Java Application".