Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab: Running Java Programs CS46A Lab Running Java Programs Copyright � Cay S. Horstmann 2009 This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License. Instructions You work in pairs One of you is the “scribe”, the other the “driver” who runs the tools Next lab, you switch roles Scribe: Start writing a report with the title “Lab 1 scribe's name (Scribe) / driver's name (Driver)”, for example Lab 1 Fred Foo (Scribe) / Becky Bar (Driver) When a step contains a question, put the answer in the report. Otherwise, just put “Step n: done” You won't finish the lab in one day Stuck? Discuss with your buddy, and if you are both stuck, ask for help!!! Part A Install the Java Development Kit Windows: Go to http://java.sun.com/javase/downloads. Curse the Sun marketing department as you see far too many options that change frequently. You do not want any bundles. You do not want the JRE. Look for the Java SE Development Kit (JDK) without anything bundled. You will download an installer file. Run the installer after downloading and accept all defaults. Ubuntu: Install the sun-java6-jdk package, for example by running sudo apt-get install sun-java6-jdk Mac: Java is already installed. Hooray! Download and install BlueJ from http://bluej.org/download/download.html. Windows: Run the downloaded installer Ubuntu: Run java -jar bluej-252.jar in a console window Mac: Double-click on the downloaded file and install Start BlueJ. Windows/Mac: Double-click on the BlueJ icon Ubuntu: Using the Places menu, navigate to the folder in which you installed BlueJ and double-click on bluej Make a new project: Project -> New Project, then type hello into the dialog. Click on New Class. Give a class name of HelloPrinter. Double-click on the HelloPrinter rectangle. Erase all code in the editor window and paste in this code: public class HelloPrinter { public static void main(String[] args) { // Display a greeting in the console window System.out.println("Hello, World!"); } } Click on Compile. You should get a status message “Class compiled - no syntax errors” Click on Close. Right-click on the HelloPrinter class. Select void main(String[] args) from the menu. Click Ok on the next dialog. You should get this display: Congratulations: You have just executed your first Java program. Now change the program so that it prints Hello, your name!. What change did you make? When the lab instructions ask you a question about what you did, the scribe needs to enter the answer into the lab report. For example, “We double-clicked on the HelloPrinter class and changed the code so that it looks like this: (paste in code). Then we repeated steps 7 and 8.” It is unfortunately easy to make simple spelling mistakes that confuse the compiler. Replace System.out.println with System.ouch.println. Click Compile. What happens? Remember to put the answer into your lab report. Fix this mistake and now make another mistake of your choice that triggers a compiler error. What mistake did you make, and what error message did you get? Remember to put the answer ... Replace the program with the following: public class HelloPrinter { public static void main(String[] args) { // Display a greeting in the console window System.out.println("+---------------+"); System.out.println("| Hello, World! |"); System.out.println("+---------------+); } } Click Compile. What error do you get? How do you fix it? What does the program print? Remember to ... Replace the program with the following: public class HelloPrinter { public static void main(String[] args) { // Display a greeting in the console window System.out.println(+---------------+); System.out.println(| Hello, World! |); System.out.println(+---------------+); } } Click Compile. What error do you get? How do you fix it? Now change the program so that it displays a face, similar to, but hopefully prettier than ///// | o o | (| ^ |) | [_] | ----- What is the code of your program? Part B. Ok, enough about System.out.println. Now we want to experience a different kind of Java programs. You will install the Alice tools from Carnegie Mellon. This semester, the tools are still a bit experimental, so the setup isn't as smooth as we'd like it to be. Ask if you need help! First, download Alice from http://kenai.com/projects/alice/downloads. Pick the latest version (currently 3.0.0.64), not one of the ones labeled OLD. Pick the multi-purpose zip. Unzip somewhere. The download takes a very long time, so you should do this at home, not in the lab. Also download the Alice Netbeans Plugin (currently version 3.0.0.64.1). Download NetBeans 6.8 from http://www.netbeans.org/downloads/index.html. Pick the first option (Java SE). That's also a pretty long download that you should do at home. Then install it. Start NetBeans (not Alice!). Select Tools -> Plugins -> Downloaded -> Add Plugins. Navigate to the plugin file that you downloaded in step 1 (Alice3ProjectWizard3.0.0.64.1.nbm). Click Ok. Click Install. Download this file. In NetBeans (not Alice!), select File -> New Project -> Java Project from Existing Alice Project -> Next -> Browse, then browse for the file you just downloaded. Click Finish. Select Run -> Run Main Project from the menu. What happens? If you get a message about the location of the Alice galleries, click on "Select Gallery location" and locate the gallery folder inside the unzipped Alice download. Click on the triangle next to Source packages, then the triangle next to . Then double-click on MyScene.java and scroll down to the end, until you can see the run method. (Ignore all the complicated code above—in future versions of Alice, this is going to be in a separate place where students won't see it.) Make a change so that the beach chair says something different. What change did you make? Add the following line under the say command: chair.move(MoveDirection.LEFT, 1); Run the program. What happens? Part C You and your buddy will demo to the lab assistant (a) the face (b) the Alice chair. Each of you demoes one. Start the app (BlueJ or NetBeans) and do what it takes to run the sample program. If you have additional time, help your neighbor. Since you can get Alice+NetBeans to work, help those who can't. If you have additional time, start on the homework assignment. Copy/paste any relevant starter code (from similar lab work), and get your code to compile without errors. Do not work on the actual solution--that is individual work, not groupwork.