Java Lab

457 views

Published on

Java Lab
http://leonguyen.com

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
457
On SlideShare
0
From Embeds
0
Number of Embeds
87
Actions
Shares
0
Downloads
28
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Java Lab

  1. 1. Java Lableonguyen.com
  2. 2. 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
  3. 3. Githubhttps://github.com/leonguyen/JavaLab
  4. 4. Lab 01An introduction to Java
  5. 5. Outline- Download and install JDK- Write a Helloworld program- Install and use Eclipse to write a Java program- Debugging Programs in Eclipse
  6. 6. Exercise 1Download and install JDK
  7. 7. JDK & JRE
  8. 8. Task 1 - Download JDK- Goto Java download site http://www.oracle.com/technetwork/java/javase/downloads/index.html
  9. 9. Task 1 - Download JDK (cont)- Select Java Platform (JDK), choose your operation platform (egWindow X86) and download it
  10. 10. Task 2 - Install JDK- Run the downloaded installer, which installs both the JDK (JavaDevelopment Kit) and JRE (Java Runtime). By default the JDK andJRE will be installed into directories C:Program FilesJava
  11. 11. Task 3 - Config PATH environment variables- Windows Operating System searches the current directory and thedirectories listed in the PATH environment variable for executableprograms invoked from the CMD shell. It helps programmer cancompile Java code in CMD shell.- Click the "Start" button > "Control Panel" > "System" > (Vista/7 only)"Advanced system settings"
  12. 12. Task 3 - Config PATH .. (cont)
  13. 13. Task 4 - Verify the JDK Installation- Launch a CMD shell > type java –version to check that JDK isproperly installed and display its version, and javac to check Path workproperly too.
  14. 14. Exercise 2Write a Helloworld program
  15. 15. Task 1 - Open your editor- For example: Notepad++
  16. 16. Task 2 - Write your code- Type this code bellow to editor window
  17. 17. 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.
  18. 18. Task 3 - Compile and run on command-line- Launch a CMD shell > type javac to compile the source code andjava to run the program using the JDK runtime
  19. 19. Exercise 3Install and use Eclipse to write aJava program
  20. 20. Task 1 - Download Eclipse- Download Eclipse from http://www.eclipse.org/downloads. Forbeginners, choose the minimal Eclipse IDE for Java Developers.- Unzip the downloaded file into a directory of your choice.
  21. 21. Task 2 - Launch Eclipse- Open eclipse.exe in the Eclipse installed directory- Choose an appropriate directory for your workspace
  22. 22. Task 3 - Create a new Java Project- Choose "File" menu > "New" > "Java project"
  23. 23. Task 4 - Write a Java program- In the "Package Explorer" (left panel) > Right-click on "JavaLab" (oruse the "File" menu) > New > Class
  24. 24. Task 4 - Write a Java program (cont)- Enter the following codes
  25. 25. 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" > "JavaApplication".
  26. 26. Exercise 4Debugging Programs in Eclipse
  27. 27. Task 1 - Create project and write a simpleprogram- Create a new class named Debug and enter the following code.
  28. 28. Task 2 - Set an Initial Breakpoint- Set a breakpoint at main() method by double-clicking on the left-margin of the line containing main().
  29. 29. 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.
  30. 30. Lab 02Java Language 01
  31. 31. Outline- Relational & Logical Operators- Types- Scanner object- If/else- Switch case- Command-Line Arguments
  32. 32. 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
  33. 33. Exercise 1Relational & Logical Operators
  34. 34. Task 1 - Create a RelationalLogicalOpTestclass- Enter the following code.
  35. 35. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  36. 36. Exercise 2Types
  37. 37. Task 1 - Create a TypesMinMax class- Enter the following code.
  38. 38. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  39. 39. Exercise 3Scanner object
  40. 40. Task 1 - Create a ScannerTest class- Enter the following code.
  41. 41. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  42. 42. Exercise 4If/else
  43. 43. Task 1 - Create a Mark class- Enter the following code.
  44. 44. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  45. 45. Exercise 5Switch case
  46. 46. Task 1 - Create a Month class- Enter the following code.
  47. 47. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  48. 48. Exercise 6Command-Line Arguments
  49. 49. Task 1 - Create a Arithemetic class- Enter the following code.
  50. 50. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  51. 51. Lab 02 (cont)Java Language 02
  52. 52. Exercise 1OddEvenSum
  53. 53. Task 1 - Create a OddEvenSum class- Enter the following code.
  54. 54. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  55. 55. Exercise 2Compute PI
  56. 56. Task 1 - Create a ComputePI class- Enter the following code.
  57. 57. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  58. 58. Exercise 3Do While statement
  59. 59. Task 1 - Create a DoWhile class- Enter the following code.
  60. 60. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  61. 61. Exercise 4Array
  62. 62. Task 1 - Create a Arraytest class- Enter the following code.
  63. 63. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  64. 64. Exercise 5Loops and conditional statement
  65. 65. Task 1 - Create a PrimeList class- Enter the following code.
  66. 66. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  67. 67. Exercise 6Nested loop
  68. 68. Task 1 - Create a PrintPattern class- Enter the following code.
  69. 69. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  70. 70. Exercise 7Break label
  71. 71. Task 1 - Create a JavaGoto class- Enter the following code.
  72. 72. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  73. 73. Exercise 8Continue label
  74. 74. Task 1 - Create a ContinueLabel class- Enter the following code.
  75. 75. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  76. 76. Exercise 9Array Sort
  77. 77. Task 1 - Create a ArrayBubleSort class- Enter the following code.
  78. 78. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  79. 79. Exercise 10Reverse String
  80. 80. Task 1 - Create a ReverString class- Enter the following code.
  81. 81. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  82. 82. Exercise 11Array of String
  83. 83. Task 1 - Create a DayofWeek class- Enter the following code.
  84. 84. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  85. 85. Lab 03Class
  86. 86. Outline- Booking Room- Project assignment
  87. 87. What is OOP
  88. 88. Classes and Objects
  89. 89. Lab 03 (cont)Class
  90. 90. Exercise 1Booking Room
  91. 91. Class diagram
  92. 92. Task 1 - Create Date class- Enter the following code.
  93. 93. Task 2 - Create Time class- Enter the following code.
  94. 94. Task 3 - Create BookingRoomclass- Enter the following code.
  95. 95. Task 3 - Create BookingRoomclass (cont)- Enter the following code.
  96. 96. Task 3 - Create BookingRoomclass (cont)- Enter the following code.
  97. 97. Task 3 - Create BookingRoomclass (cont)- Enter the following code.
  98. 98. Task 4 - Write a Program class- Enter the following code.
  99. 99. Task 5 - Execute your program- Enter the following code.
  100. 100. Exercise 2Project assignment
  101. 101. Class diagram
  102. 102. Task 1 - Create Teacher class- Enter the following code.
  103. 103. Task 1 - Create Teacher class (cont)- Enter the following code.
  104. 104. Task 2 - Create Student class- Enter the following code.
  105. 105. Task 2 - Create Student class (cont)- Enter the following code.
  106. 106. Task 3 - Write a Program class- Enter the following code.
  107. 107. Task 4 - Execute your program- Enter the following code.
  108. 108. Lab 04Exception
  109. 109. Outline- Exception- Try-cath-finally- Creating Your Own Exception Classes- Assertion
  110. 110. Exercise 1Exception
  111. 111. Diagram- MobilePhone class to store mobile phone numbers and send amessages to one of of numbers store in the array.- PhoneTest class to demonstrate exception handling.
  112. 112. Task 1 - Create MobilePhone class- Enter the following code.
  113. 113. Task 1 - Create MobilePhone class (cont)- Enter the following code.
  114. 114. Task 2 - Create PhoneTest program- Enter the following code.
  115. 115. Task 3 - Execute your program- Enter the following code.
  116. 116. Exercise 2Try-cath-finally
  117. 117. Task 1 - Create TryCatchFinally program- Enter the following code.
  118. 118. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  119. 119. Exercise 3Creating Your Own ExceptionClasses
  120. 120. Task 1 - Create MyException class- Enter the following code.
  121. 121. Task 2 - Create MyExceptionTest program- Enter the following code.
  122. 122. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  123. 123. Exercise 4Assertion
  124. 124. Task 1 - Create AssertionTest program- Enter the following code.
  125. 125. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  126. 126. Exercise 5Account in a Bank
  127. 127. Diagram
  128. 128. Diagram (cont)The management of the A Bank is looking at automation as a means tosave time and effort required in their work. In order to achieve this, themanagement has planned to computerize the following transactions:• Creating a new account• Withdrawing money from an account• Depositing money in an accountThe CEO of the company and a team of experts have chosen yourcompany to provide a solution for the same. Consider yourself to be apart of the team that implements the solution for designing theapplication.
  129. 129. Diagram (cont)Create an application using exceptions and assertions to implement thetransactions. The application should consist of the following classes.1. Account.java2. Account Test.java3. InsufficientFundException.java4. NegativeAmountException.javaEach class has a specific purpose and functionality. The descriptions ofeach class are as follows.
  130. 130. Task 1 - Create Account class(The Account class represents an actual bank account. It stores thefollowing details of a bank account)• customerName• accountNumber• accountbalance• void displayAccountDetails() : This method displays the details ofthe account
  131. 131. 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 fromthe account. The method then searches in the array of accounts for the accountnumber. Use assertions for checking whether the account number and theamount to be withdrawn are positive. Also use an assertion to check if the arrayof accounts contains a minimum of one account record. The method alsothrows the user-defined exception InsufficientFund***ception in case theamount to be withdrawn exceeds• void deposit() :This method is used to deposit money in an account. Theaccount number and the amount to be deposited in the account is acceptedfrom the user. Use an assertion to check whether the account number ispositive. The method searches for the account number and deposits theamount in the account if it exists. The displayAccountDetails() method is calledif the operation succeeds. Use appropriate try catch blocks to handle all thepossible exceptions that can be thrown due to the user inputs. A user-definedexception is thrown if the account number does not exist.
  132. 132. Task 2 - Create AccountTest class(The AccountTest class is a java main class used to test the Accountclass. It creates an instance of the Account class and displays thefollowing menu of options to the user)· Create a new account· Withdraw Cash· Deposit cash· ExitThe user can select any of the options and a corresponding method isinvoked on the instance of the Bank class. Use an assertion to checkfor the control-flow invariant in case the user types an invalid option.The application exits when the Exit option is selected.
  133. 133. Task 3 - Create InsufficientFundExceptionclass- This is a user-defined exception class derived from the base classException. This exception is thrown when the user tries to withdrawmore money than the current account balance.
  134. 134. Task 4 - Create NegativeAmountExceptionclass- This is a user-defined exception class derived from the base classException. This exception is thrown when the user tries to withdraw ordeposit a negative amount.
  135. 135. Lab 05Thread
  136. 136. Outline- Use the Thread class- Implement the Runable Interface- Using join() to wait for threads to finish- Thread priority- Synchronization- Dining Philosopher
  137. 137. Exercise 1Use the Thread class
  138. 138. Task 1 - Create a RunThread class- Enter the following code.
  139. 139. Task 2 - Main program- Enter the following code.
  140. 140. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  141. 141. Exercise 2Implement the Runable Interface
  142. 142. Task 1 - Create a RunThread1 class- Enter the following code.
  143. 143. Task 2 - Main program- Enter the following code.
  144. 144. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  145. 145. Exercise 3Using join() to wait for threads tofinish
  146. 146. Task 1 - Create a MyThread class- Enter the following code.
  147. 147. Task 2 - Main program- Enter the following code.
  148. 148. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  149. 149. Exercise 4Thread priority
  150. 150. Task 1 - Create Clicker class- Enter the following code.
  151. 151. Task 2 - Main program- Enter the following code.
  152. 152. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  153. 153. Exercise 5Synchronization
  154. 154. Unsynchronized and synchronized
  155. 155. Task 1 - Create CountPrimesThread class- Enter the following code.
  156. 156. Task 2 - Main program- Enter the following code.
  157. 157. Task 2 - Main program (cont)- Enter the following code.
  158. 158. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  159. 159. Exercise 6Dining Philosopher
  160. 160. Concept
  161. 161. Task 1 - Create ChopStick class- Enter the following code.
  162. 162. Task 2 - Create Philosopher class- Enter the following code.
  163. 163. Task 2 - Create Philosopher class (cont)- Enter the following code.
  164. 164. Task 2 - Main program- Enter the following code.
  165. 165. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  166. 166. Lab 06Input / Output
  167. 167. Stream- Most fundamental I/O in Java is based on streams- Reading information into a program- Writing information from a program
  168. 168. java.io package
  169. 169. 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
  170. 170. Exercise 1Read a file - BufferedReader
  171. 171. Task 1 - Create ReadFileBuffered program- The simplest and most common-used method - BufferedReader.
  172. 172. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  173. 173. Exercise 2Write a file - BufferedWriter
  174. 174. Task 1 - Create WriteFileBuffered program- BufferedWriter is a character streams class to handle the characterdata. Unlike bytes stream (convert data into bytes), you can just writethe strings, arrays or characters data directly to file.
  175. 175. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  176. 176. Exercise 3Write a file - FileOutputStream
  177. 177. Task 1 - Create WriteFileStream program- FileOutputStream is a bytes stream class that’s used to handle rawbinary data. To write the data to file, you have to convert the data intobytes and save it to file.
  178. 178. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  179. 179. Exercise 4List the contents of a directory(Recursively)
  180. 180. Task 1 - Create ListDirectoryRecusiveprogram- Enter the following code.
  181. 181. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  182. 182. Exercise 5Copying a file without Buffering
  183. 183. Task 1 - Create FileCopyNoBuffer class- Enter the following code.
  184. 184. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  185. 185. Exercise 6Copying a file with a Programmer-ManagedBuffer
  186. 186. Task 1 - Create FileCopyUserBuffer class- Enter the following code.
  187. 187. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  188. 188. Exercise 7Copying a file with Buffered Streams
  189. 189. Task 1 - Create FileCopyBuffered class- Enter the following code.
  190. 190. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  191. 191. Exercise 8Best Copier
  192. 192. Task 1 - Create FileCopyUserBufferBestclass- Enter the following code.
  193. 193. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  194. 194. Exercise 9InputStreamReader and OutputStreamWriter
  195. 195. Task 1 - Create UnicodeFileIO program- Enter the following code.
  196. 196. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  197. 197. Exercise 10BufferedReader and BufferedWriter
  198. 198. Task 1 - Create UnicodeFileIOBufferedprogram- Enter the following code.
  199. 199. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  200. 200. Exercise 11PipedReader and PipedWriter
  201. 201. Task 1 - Create pReader class- Enter the following code.
  202. 202. Task 2 - Create pWriter class- Enter the following code.
  203. 203. Task 3 - Main program- Enter the following code.
  204. 204. Task 4 - Execute your program- Choose "Run As" > "Java Application".
  205. 205. Exercise 12Serializable - ObjectOutputStream
  206. 206. Task 1 - Create SerializedObject class- Enter the following code.
  207. 207. Task 2 - Create SerializedObject class- Enter the following code.
  208. 208. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  209. 209. Lab 07Generic
  210. 210. Outline- Generic class and Wildcard- Student List- ArrayList- Employee Management- LinkedList- Set- Map
  211. 211. Exercise 1Generic class and Wildcard
  212. 212. Task 1 - Create Box class- Enter the following code.
  213. 213. Task 2 - Create the program- Enter the following code.
  214. 214. Task 3 - Execute your program- Choose "Run As" > "Java Application".
  215. 215. Task 4 - Create A, B, C class- Enter the following code.
  216. 216. Task 5 - Create BoxB, BoxC class- Enter the following code.
  217. 217. Task 6 - Create Test BoxB, BoxC program- Choose "Run As" > "Java Application".
  218. 218. Task 7 - Execute your program- Choose "Run As" > "Java Application".
  219. 219. Exercise 2Student List
  220. 220. Student List
  221. 221. Task 1 - Create Student class- Enter the following code.
  222. 222. Task 2 - Create StudentList class- Enter the following code.
  223. 223. Task 3 - Create the program- Enter the following code.
  224. 224. Task 4 - Execute your program- Choose "Run As" > "Java Application".
  225. 225. Exercise 3ArrayList
  226. 226. Task 1 - Create ExArrayList program- Enter the following code.
  227. 227. Task 2 - Execute your program- Choose "Run As" > "Java Application".
  228. 228. Exercise 4Employee Management
  229. 229. Task 1 - Create Employee class- Enter the following code.
  230. 230. Task 2 - Create HREmpoyee class- Enter the following code.
  231. 231. Task 3 - Create the program- Enter the following code.
  232. 232. Task 4 - Execute your program- Choose "Run As" > "Java Application".
  233. 233. Task 5 - Create EmpoyeeDetail class- Enter the following code.
  234. 234. Task 6 - Edit the program- Enter the following code.
  235. 235. Task 7 - Execute your program- Choose "Run As" > "Java Application".
  236. 236. Exercise 5LinkedList
  237. 237. Task 1 - Create ExLinkedList program- Enter the following code.
  238. 238. Task 1 - Create ExLinkedList program (cont)- Enter the following code.
  239. 239. Task 1 - Create ExLinkedList program (cont)- Enter the following code.
  240. 240. Task 2 - Execute your program- Enter the following code.
  241. 241. Exercise 6Set
  242. 242. Task 1 - Create ListDirectoryRecusive class- Enter the following code.
  243. 243. Task 2 - Execute your program- Enter the following code.
  244. 244. Exercise 7Map
  245. 245. Task 1 - Create ExMap program- Enter the following code.
  246. 246. Task 1 - Create ExMap program (cont)- Enter the following code.
  247. 247. Task 2 - Execute your program- Enter the following code.

×