Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 1/28  
 
 
CSCI 201 Lab 1 
Environment Setup 
"The journey of a thousand miles begins with one step." - Lao Tzu  
Introduction 
This lab document will go over the steps to install and set up Eclipse, which is a Java integrated 
development environment (IDE). Once set up, some important things will be covered, such as creating a 
new Java Project, exporting a Java Project, and importing an existing Java Project. 
I realize this lab seems really long, but the installation should not take that much time, and the length of 
this document is based on the screenshots and detailed tutorial we have provided. 
If you have programmed in Java and have used Eclipse before, please follow the guide anyway. There 
are some important steps that will ensure you are able to submit your homework assignments properly. 
Part 1 – Downloading Java and Eclipse 
To be able to program in Java, it is necessary to have a Java Development Kit (JDK). You can find the 
download links here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-
2133151.html 
Make sure you download JDK 8. If you already have a JDK, make sure it is the latest version. Do not 
download the JRE, download the JDK!  The JRE is the Java Runtime Environment, and it only contains 
the executables for running Java programs but not for compiling them. 
Note: If you have any previous version of Java installed, you must install JDK 1.8 on top of that 
version. Otherwise in future assignments your programs may not work! 
For this class, you will be programming in Eclipse. You can find the download links here: 
https://eclipse.org/downloads/ 
For Windows users, it doesn’t really matter whether you pick the 32-bit or 64-bit version of Eclipse. 
However, make sure that you match the JDK and Eclipse. 32-bit goes with X84 and 64-bit goes with X64. 
You can probably use 64-bit, unless you have a really old computer. To find out what hardware you 
have, you can go to your system properties in the Control Panel.  There should be some link that says 
something along the lines of “System” or “Properties.”  There you will see whether your hardware is 32-
bit or 64-bit. 
For Mac users, pick 64-bit since it’s the only flavor offered. 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 2/28  
 
 
Part 2 – Installing Java and Eclipse 
Windows: 
Regardless whether you picked 32-bit or 64-bit, the instructions are the same. Now that you have the 
files, go ahead and install them. The JDK should be very straightforward. 
Next-> Next-> Next-> Close. Perhaps change the install directory if you really want to for some reason, 
but I’d recommend leaving it as the default! 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 3/28  
 
 
Now let’s install Eclipse.  You should have a .zip file that needs to be extracted. 
 
This is as simple as clicking and dragging the contents into another folder. I recommend a place close to 
the root of your C: drive. 
 
Note: For some people, weird issues come up unless the folder is extracted into C:. If your desired 
directory doesn’t work out, try extracting the folder right into the root of your C: drive. 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 4/28  
 
 
 
You should now have an “eclipse” folder.  Make a shortcut to “eclipse.exe” and place it on your desktop 
if you wish. 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 5/28  
 
 
Run Eclipse! 
Pick a folder to be your ‘workspace’. This will be the directory that contains ALL of your projects and 
code. Make sure to remember where you put this, but as long as you don’t check that box that says, 
“Use this as the default and do not ask again,” you will be prompted for the workspace directory every 
time you run Eclipse. 
 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 6/28  
 
 
OSX: 
Now that you have the files, go ahead and install them. 
 
Simply double-click the icon to install the JDK. It’s fairly simple. 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 7/28  
 
 
Continue->Install->Close 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 8/28  
 
 
Now let’s install Eclipse. 
 
You should see this open in a folder. 
 
You can move the eclipse folder somewhere else if you’d like. Open the folder. 
 
Run the eclipse executable! 
Pick a folder to be your ‘workspace’. This will be the directory that contains ALL of your projects and 
code. Make sure to remember where you put this, but as long as you don’t check that box that says, 
“Use this as the default and do not ask again,” you will be prompted for the workspace directory every 
time you run Eclipse. 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 9/28  
 
 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 10/28  
 
 
Part 3 – Creating a project 
This tutorial will use a Windows computer, though everything is the same for OSX or Linux. Close out of 
the Welcome menu. 
 
You should see the following screen: 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 11/28  
 
 
Go to File->New…->Java Project. 
 
Make sure that 1.8 is selected under the execution environment. 
For the project name, use the following convention: username_CSCI201L_Assignment# 
For example, if your email is careymd@usc.edu, you would submit your Assignment1 for grading with 
the project name careymd_CSCI201L_Assignment1. 
For now, go ahead and name the project username_CSCI201L_Lab1. 
Press ‘Finish’. 
 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 12/28  
 
 
Part 4 – Hello World 
The time has finally come to write a program.  
Go to File->New…->Class 
 
You can name classes anything you want for homework assignments, but go ahead and make a 
‘HelloWorld’ class. Just type the name and press ‘Finish’. 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 13/28  
 
 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 14/28  
 
 
 
Eclipse will generate a HelloWorld.java file for us, as well as create a package in our project. Packages 
just correspond to directories on your file system, but they do have some significance that you’ll learn 
about in class. 
Now we need to make a main method.  
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 15/28  
 
 
Now, insert the following code. 
 
You can use System.out.print(Object); to print a String to the console. You can use 
System.out.println(Object); to print a String followed by a newline to the console. 
Now press the ‘Run’ button to execute the program!  
 
You should get the output in the Console tab at the bottom of the window. 
 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 16/28  
 
 
Part 5 – Debugging 
Type the following code into your main method: 
 
For some reason, this code doesn’t seem to work! It will run, but it throws an exception! 
 
Let’s see what’s happening by putting breakpoints on line 6 and 7. Do this by double clicking on the left 
side-bar. 
 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 17/28  
 
 
Click the Debug button.  
Press ‘Yes’. 
 
You will now see this perspective: 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 18/28  
 
 
We can see that s is still ‘Hello World!’, let’s resume…  
 
We can see that right after line 6 was executed, s turned to null! (What a surprise…) 
You can stop here, and go back to the normal perspective by clicking ‘Java’ in the upper-right corner. 
 
Note: If you have breakpoints set, pressing ‘Run’ will not trigger them. You MUST use Debug for the 
breakpoints to cause the program to stop. 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 19/28  
 
 
Part 6 – Exporting a Java Project 
Now let’s export our Java Project to a .zip archive. 
Make sure the project folder is highlighted, and go to File->Export. 
 
Select General->Archive File 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 20/28  
 
 
 
Press ‘Browse…’, and save the file with the same convention as you did with the project name. 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 21/28  
 
 
 
Press ‘Save’ and ‘Finish’ 
You will now have a .zip file containing the project. 
If you are worried that the project did not export correctly, simply re-import the project into a different 
workspace.  Importing projects is explained in the next section.  It is recommended that you do this for 
each assignment before submitting to ensure your program was exported correctly. If you can import 
your project into a fresh workspace and the program still works, the program will work for the grader. 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 22/28  
 
 
Part 7 – Importing a Java Project 
For the rest of the labs, we will use a Factory project that was prepared for you. Let’s go ahead and 
import this project. 
Download the project from the course website. 
Go to File -> Import… 
 
Select General-> Existing Projects into Workspace 
 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 23/28  
 
 
Select ‘Select archive file:’, and the Press ‘Browse…’ Select the zip file that you downloaded. 
 
Press ‘Finish’ 
You will now see the Factory project in your workspace. 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 24/28  
 
 
 
Let’s make sure everything works. Open the ‘Client’ and ‘Server’ Packages, and open up 
ClientFactory.java and FactoryServer.java. 
We need to run both of these programs. Both of these files contain main methods.  We will be learning 
about client and server programming in the future, so you don’t need to worry about how this is 
working for now. 
With FactoryServer.java open, press ‘Run’. 
 
 
With FactoryClient.java open, press ‘Run’. 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 25/28  
 
 
You should have two windows open. Press ‘Start Listening’ on the Factory Server, and ‘Connect’ on the 
Client. 
 
Note: If you get a firewall warning, select whatever you need to let these applications through. 
You should see these two screens: 
 
Press ‘Select Factory’ on the factory server to read in factory.txt 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 26/28  
 
 
You should see workers appear as shown below: 
 
You now have everything set up on your computer for all of the labs and homework for the semester! 
 
 
  
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 27/28  
 
 
Expand on This 
Expand on This sections are do-it-yourself sections that will require you to think about how to design a 
solution. There will be very little tutorial in this part, so use your creativity to complete this portion of 
the lab. Oftentimes, words that are in bold or sections IN ALL CAPS are very important to your 
understanding of this section. SECTIONS IN BOLD AND CAPS ARE PROBABLY EVEN MORE SO. 
In this particular lab, let's talk about Scanner. 
In C++, you used something like cin from the Standard Template Library (STL) to read in something from 
the console. In Java, there is no STL, so you will be using a Java object to do basic input/output (I/O). The 
Scanner behaves a lot like a basic buffer reader, such as cin, however, there are specific ways to use the 
Scanner that differs from cin.  
In order to do so, you first need to declare a new Scanner object. This means that you will need to 
import java.util.Scanner; as part of your project's imports.  
Once you declare a new Scanner object, you can start reading in items from the Java console. Your job is 
to make a simple program that will take in your username, and then print out your username with a nice 
message such as “Hi !” 
BUT BEFORE WE GET TO THAT: 
You will first need to duplicate your project. In the future, when you (inevitably) request a regrade from 
the CP who graded you, you will need to duplicate your project. This is because when an in-person 
regrade is done, the CP will have you download a fresh copy of your submitted code off Blackboard and 
then import it into Eclipse before demoing it to them.  
First, export your Lab1 code into a .zip file. Keep it somewhere safe in your computer.  
Then, you will need to rename your original project, otherwise Eclipse will not allow you to import your 
project. You can do this by right clicking your project's name and then clicking “Refactor” and then 
“Rename.” Rename your project to something like “Lab1_Expansion”. 
Next follow the same steps as you did to import the Factory code except select the Lab1 zip file that you 
exported earlier. 
 CSCI 201L Lab 1 
 Prof. Jeffrey Miller 
 28/28  
 
 
Finally, do the Scanner expansion IN YOUR Lab_Expansion PROJECT, NOT THE ORIGINAL Lab1 PROJECT. 
Once you are finished with the expansion, show it to your CP to get checked off for the lab.