CSC1030 HANDS-ON INTRODUCTION to JAVA Lab Exercise #6
Total Page:16
File Type:pdf, Size:1020Kb
CSC1030 HANDS-ON INTRODUCTION TO JAVA Lab Exercise #6
Download the lab package Lab6Kit.zip from the course web: http://www.cse.cuhk.edu.hk/~ pffung/ csc1030 /
Problem 1 (Debugging) Fix the bugs in the accompanied NetBeans package called debugging, the class is named DebugExercise.
Part A: Debug a static method named "checkTriangle" that performs the following tasks: a) Ask the user for three numbers that are supposed to represent the length of each side of a triangle. b) Tell the user if such a triangle exists. c) In case the any of the input is invalid, the method returns quietly immediately.
For examples, if the numbers are 3, 4, and 5, your program should indicate that such a triangle exists. If the numbers are 1, 1, and 10, then your program should indicate that such a triangle does not exist.
To test the method "checkTriangle", invoke it from the method "main".
Part B: Debug a static method named "findDistance" that performs the following tasks: a) Ask the user for two pairs of numbers (x1, y1) and (x2, y2) that represent the coordinate of two points. [Note: You need to read the input as 4 separate numbers.] b) Calculate and output the distance between the points. c) In case the any of the input is invalid, the method returns quietly immediately.
To test the method "findDistance", invoke it from the method "main".
Part C: Debug and Modify the method "main" so that it will repeatedly ask the user for 3 choices and perform the corresponding task accordingly. The choices are: Choice 1: Invoke "checkTriangle" Choice 2: Invoke "findDistance" Choice 3: Quit the program.
Your program should ONLY terminate when the user has selected choice #3. In case of any invalid input, prompt the user and ask again. Your program should NOT stop due to any Exception.
1 Problem 2 (Inheritance)
1. Study the code Timer.java in the lab package, which inherits an abstract class AbstractCounter. An abstract class is often used as the root class of relevant subclasses, to provide a common base.
2. Finish a class Clock that is derived from Timer by introducing a new field hours (0 - 23.) Override the methods of Timer to manipulate, access, and print out hours, minutes, and seconds.
Submit Problem 1 and Problem 2 solutions together in a ZIP package Lab6.zip of the whole Netbeans folder to CUForum.
Problem 3 (Applet Tic-Tac-Toe 打井)
Applet is a very interesting application of Java, together with the graphics drawing capabilities of Java.
1. Compile the FirstApplet.java using either Netbeans. 2. Run the Applet by double-clicking the file FirstApplet.html file under folder Lab6Kit\build\classes.
What is an Applet/ JApplet?
Applet mean Application-Let, i.e. a small web application. The JApplet is an applet class under Swing, which is an extended GUI class to make inclusion of GUI elements easier. Typical GUI elements such as buttons menu, and scrolling text windows are available.
Event Listening
To make the GUI element responsive to user input, we need to tell the source of the event e.g. JButton where to send the event. Objects that can receive events are called listeners. Here we try another example.
1. Build the Project to Compile ButtonApplet.java and Response.java. 2. Run the Applet by double-clicking the file ButtonApplet.html file under folder Lab6Kit\build\classes. 3. Note how the button press event is being processed by the Response class.
Better Practice
We see that the demos Scribble and ButtonApplet are ad-hoc. The better practice is to create a JPanel Form under Netbeans, which encapsulates all the GUI components and application logic. Then we create our own “wrapper” class to handle the JFrame and/ or JApplet stuff in order to embed the JPanel into our application. In this way, we can create a dual-executable application. See Michael’s demo.
Your Work
Create an Applet Game Tic-Tac-Toe. It’s a turn-based 2-player game with the minimum requirement of a 3x3 game board with 9 buttons. End-game and Reset-game should be implemented. It should be a dual- executable Java GUI Application and Applet.
Submit Problem 3 work in a ZIP package TicTacToe.zip of the whole Netbeans folder to CUForum.
2 Marking Scheme and Notes:
1. Comment/remark, indentation, style are under assessment in every programming assignments unless specified otherwise.
2. Do your submission before 18:00 p.m. of the due date. No late submission would be accepted.
3. If you submit multiple times, ONLY the content and time-stamp of the latest one would be counted. You may retrieve your assignment from the web and re-submit any time before the due line. Remember to check your own submission before logout CUForum.
4. Plagiarism is strictly monitored and punished. You should read and understand the University Policy on Academic Honesty: http://www.cuhk.edu.hk/policy/academichonesty.
3