Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
4003-243: Object Oriented Programming in Java - Lab 1 2011-3 Lab 1 - Introduction to CVS and Java Introduction You will write several programs in the Java programming language in this lab. You also will begin to learn some UNIX commands and how to use the version control software, CVS, to maintain the code you develop. Pre-Lab Work If you have used Eclipse before, you may skip the installation. If you have not built and run a Java program under Eclipse, you should do the section on "Using Eclipse for Java". Then you may proceed to configure CVS, learn how to use CVS version control with Eclipse and start the programming assignments. Installing Eclipse Before installing, watch the instructional video on setting up your environment. What follows are the details of the installation and initial configuration. Note that the video refers to the machine named holly.cs.rit.edu which no longer exists; instead you can use other department machines such as glados.cs.rit.edu. (This document will use the machine glados.cs.rit.edu.) If you are installing Eclipse on your own machine, download and install Eclipse Classic. After the installation, you will have a workspace directory on your machine. The workspace directory will hold all your Eclipse software development project files during development. Using Eclipse for Java If you have been using Eclipse for another language, such as Python, switch to the Java perspective. Select Window -> Open Perspective -> Java. Create a Hello project. Launch Eclipse and create a new Java project, File -> New -> Java Project and enter the following information: Project Name: Hello Create a HelloWorld.java program. In the Package Explorer pane right-click on the Hello folder and select New -> Class. Enter the following information and then click Finish. Name: HelloWorld public static void main(String[] args): select checkbox Generate comments: select checkbox Modify the HelloWorld.java program in the Eclipse editor. Run the program. In Package Explorer pane, right-click on Hello.java and select Run As -> Java Application. Verify the output in the Console window. Configuring CVS Part of this assignment is to begin your education in UNIX. You will begin to use the terminal command line because several software development operations may require you to know how to use the command line. You may find some of the following UNIX Documentation links useful: UNIX Reference Card CS Dept. Introduction to UNIX PDF Michael Stonebank's 2001 UNIX tutorial is useful and widely available. http://biocomp.health.unm.edu/biomed505/Unix/Unix2/ WinSCP, Secure copy file transfer program for MSWindows Search the internet for "xming" if you are a PC user. The xming facility adds XWindows capabilities to your PC. XWindows is the X11 windows server and toolkit developed over many years at MIT, and it allows you to run a GUI program remotely. Log in to a CS system. The ssh program is a secure, remote login shell program. For Unix/Mac users run a terminal program and in the shell type the following, where {username} is your CS username: $ ssh -Y {username}@glados.cs.rit.edu For Windows users, install and then run PuTTY. Under the Session category enter the following and click Open. Host Name: glados.cs.rit.edu Enter the command sequence shown below. In the illustration, the $ is the prompt and the hash mark begins a comment extending to the end of the line; do not enter these parts. The cvs and pico commands are the CVS program and the pico editing program respectively. $ cd # go to home dir. $ mkdir cvs # create cvs repository directory $ cvs -d ~/cvs init # initialize cvs repository $ mkdir -p .ssh # create .ssh dir. if non-existent $ pico .ssh/environment # edit environment file in .ssh dir. PATH=/opt/csw/bin:/usr/bin # add line to file. Ctrl-X + Y to save $ pwd # record full path for next step Go to Eclipse (on the lab machine, and later on your machine if there is time) and connect to your CVS repository. Select Window -> Open Perspective -> Other -> CVS Repository Exploring. Right-click in the CVS Repository pane and select New -> Repository Location. Enter the following information and click Finish and enter your CS password. Note: {path_from_pwd} is the string returned by the pwd command, and {username} and {password} are your CS credentials. Host: glados.cs.rit.edu Repository Path: {path_from_pwd}/cvs User: {username} Password: {password} Connection type: extssh select checkbox for Save password Committing A Project to CVS Change back to the Eclipse Java perspective by doing: Select Window -> Open Perspective -> Java. In the Package Explorer pane, right-click on the Hello folder and select Team -> Share Project. Select the repository you just created and click Next. Use the default project name and click Next. Select the checkbox for Launch commit wizard and click Finish. Enter a comment for the commit operation (i.e. Initial revision.) and click Finish. Examine the CVS tags in HelloWorld.java to see that they reflect the check-in. Add an additional print statement in HelloWorld.java and make a second commit of the file. On the file system, navigate to the workspace folder where your program file resides and verify that the file is present and correct. Checking Out a Project from CVS The advantage of using a source code repository is that you can work on code on different machines while keeping everything synchronized. Each time you complete a coding session, commit your changes to the repository. If you go to a different machine you will either check out the project (first time) or update it. To check out a project for the first time in Eclipse, select Window -> Open Perspective -> Other -> CVS Repository Exploring. In the CVS Repositories pane turn the arrow for the repository icon and the HEAD down. This will reveal all of your projects in the repository. Right-click on the project you want and select Check Out. ... OR ... To update a project within Eclipse that is already checked out from CVS, right-click on the project folder in the Package Explorer perspective and select Team -> Update. Account Registration for try (pre-project work) This course will be using the try program for project submissions. Before you can submit any work through try you will first need to register, so that try knows what section you are in (this makes sure that the work you submit is placed in the correct location for grading). To register, you will first need to log into one of the CS computers (glados, ...) using ssh for unix/macs, or PuTTY for PCs. Then, from the unix prompt, type: try grd-243 register /dev/null You will then be prompted to select a lecture section. After that the registration process will display the information that was recorded for you. Note that it will display a Lab section: ##, (where ## is a default value based on your lecture section number); don't worry about this as we generally won't be using try with the labs. For more information about the try program, you can find documentation at: Guide to the try submission tool Program 1: PizzaRun, a command line arguments program Java Documentation (javadocs) Java Command Line Arguments tutorial shows you what you need to process the args of the main method. Java Arrays tutorial introduces Java arrays (and args is an array). Double class is a wrapper class for the base type double and has methods for manipulating that kind of value. Integer class is a wrapper class for the base type int and has methods for manipulating that kind of value. PizzaRun documents the class that you must implement and has an example of program use. Description Develop your first program, PizzaRun.java, on the CS machines. You may use only the command line programs java, javac and a text editor (e.g. pico). (You may not use Eclipse.) The problem to solve: Each pizza has 8 slices and costs some amount of dollars. Several people each want varying numbers of pizza slices. They want to know how many pizzas to buy and how many extra slices are left over. To see an example execution of this program, please refer to the PizzaRun javadocs. Here are the steps to take to develop this program in a terminal window: Log in to a CS machine and open a terminal window. If working remotely, use ssh. Create a course directory to hold your programs; for example: $ mkdir cs243 # create cvs repository directory $ cd cs243 # change directory Use an editor in the terminal to write your program. Editor choices include pico, gedit, nedit, vi, emacs, among others. (If you are remotely logged into a CS system, you will not be able to use the graphical windowing editors gedit and nedit unless you have XWindows installed and used ssh -Y to connect.) Study the javadocs for Double and Integer to find the functions that convert String objects to different types. Compile your program with the command javac PizzaRun.java. This compiler will print errors if your code has syntax errors; in that case, you must edit to repair the defects. Run your program with the command java PizzaRun. Continue to edit, compile, and run your program until it works correctly. Ask your SLI to see your program run in the terminal window. Note: You must show the operation of your program to your SLI in order to receive full credit for this assignment. Adding the Program to CVS For this lab, you create a project in Eclipse, share that project's content as a module in CVS, and commit changes to CVS. The following steps set up Eclipse and CVS for the rest of the lab assignment. Go to Eclipse and create a new Java project (e.g. 243_Lab1) for all the programs in this lab. Import your program from the file system into the Eclipse project. Share the project to add it to your CVS repository. Add full comments to your program, including CVS tags, and commit your program to your CVS repository. Now run your program inside Eclipse. To enter command line arguments in Eclipse, right-click on Sum.java in the Package Explorer pane and select Run As -> Run configurations and create a run configuration for this program. The command line arguments can be entered in Program arguments under the Arguments tab. Program 2: Insertion Sort Animation courtesy of wikimedia.org Java Documentation ArrayList class is one choice for implementing your program's collections. Integer class serves to wrap base int values within generic collections. InsertionSort class has the javadocs for the class you must implement. Description The insertion sort algorithm is for sorting totally ordered values (e.g. numbers). Implement the algorithm as the Java program InsertionSort.java. Your implementation must conform to the following design: Read the integers from the command line. (Make sure to convert the strings to integers.) Use an ArrayList of Integers to store the collection of numbers. Write the insertionSort function that implements the sorting algorithm. See the javadocs for the function signature. Display the sorted list of numbers on standard output. To see an example execution of the program, please see the javadocs. Adding the Program to CVS Import your program source from the file system into the Eclipse project for this lab. Add the file to your CVS repository by doing this in Eclipse: right-click on Project icon and select Team -> Commit. A comment window will pop up; enter your version comments for the file and finish. Comment the code and check it in (commit it) to CVS. Program 3: Snowflake, a Java Turtle program S=100, N=1 S=100, N=2 S=100, N=3 S=100, N=4 Java Documentation StdDraw class, courtesy of Princeton CS Department, provides supporting drawing facilities. Turtle class, courtesy of Princeton CS Department, provides turtle drawing facilities. Snowflake has the javadocs for the class you must implement. Description The Snowflake program draws pictures using a Turtle program package developed at Princeton. Implement the Java program Snowflake.java. The following pseudocode draws a snowflake side and a whole snowflake: procedure snowflakeSide(S, N) if N is 1 forward(S) otherwise if N > 1 S3 = S/3 snowflakeSide(S3, N-1) left(60) snowflakeSide(S3, N-1) right(120) snowflakeSide(S3, N-1) left(60) snowflakeSide(S3, N-1) procedure snowflake(S, N) left(60) loop 3 times snowflakeSide(S, N) right(120) You have been given the source code for two Java programs/libraries, and your program will use these to draw. StdDraw.java is the low level graphics library that you should not need to call directly. Turtle.java is a Java class that has similar functionality to Python's turtle package. Study the main method to see an example of how the Turtle class can be used. When run, your program prompts for the line segment length, S, and the depth of recursion, N. To see an example execution of the program, please see the javadocs. The program then initializes the turtle and the world coordinates. Finally the program renders the snowflake image. Your design must adhere to the following: You must use the Turtle class to draw the image. You must implement a recursive function to draw the snowflake parts. Adding the Program to CVS Import both of the supplied files into your Eclipse project. Add these files to your CVS repository, and commit them. Implement your code in the file Snowflake.java, and add the file to CVS. Comment your code and commit the files to your CVS repository. Post Lab Writeup The Java programs below each have an error that prevents them either from compiling or running correctly. (hint: compile and run each of these on the command line to learn about errors and their messages.) Problem1.java Problem2.java Problem3.java Problem4.java Problem5.java Problem6.java Problem7.java Problem8.java Problem9.java Problem10.java Create a plain text file called postLabReport.txt to contain the answers to these problems. Your name and section number must be at the top of the file. For each problem program, write an entry that: identifies the problem number; states whether the problem occurs at compile or run time; and describes the problem in plain English (copying the error message is unacceptable). Add the postLabReport.txt file to your CVS repository and commit. Submission Zip all three .java program files and your postLabReport.txt Post Lab Writeup into the file lab1.zip. Submit the zip file to the MyCourses dropbox for this assignment. (See MyCourses for deadlines for on-time and late submissions. The syllabus describes the penalty for submitting to a late drop box. No submissions are accepted after the late deadline passes.) Your SLI will define the rules for lab attendance and participation, and how they will grade you. Grading The grade breakdown for this lab is as follows: Lab attendance and participation: 10% Try registration: 5% Program 1 - PizzaRun.java: 15% Program 2 - InsertionSort.java: 20% Program 3 - Snowflake.java: 20% Post Lab Report - postLabReport.txt: 20% Code Style and Documentation: 10%