Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CSCI 270 – Lab 1 
Java Review & Eclipse Introduction 
Objectives 
• Refresh your Java skills! 
• Review basic Java, file I/O, and arrays 
• Learn how to use the Eclipse IDE 
• Gain familiarity with UML diagrams and abstract classes 
Lab Assignment 
You will write a program that helps keep track of student lab scores. I have sketched out how I 
want the program to function using the UML diagram below.  
 
I will give you the code for the abstract class LabScoresAbstract. You will implement the class 
LabScores.   
Here is an overview of the program you will write. The method readNames reads student names 
from a file into the one-dimensional array names and the method readScores reads integers (lab 
scores) from a second file into the two-dimensional array scores.  
The data in the files represents a set of lab scores for students in a CS class. The first line of the 
names.txt file will be an integer indicating the number of lines of data that follow. The first line 
of the scores.txt file will have two integers indicating the number of rows and columns, 
respectively. Below is a sample of what the data might look like in a spreadsheet and how it 
would be stored in the two files. 
 
  
  
Names             L0  L1  L2  L3  L4  L5  L6  L7  L8  L9 
---------------   -------------------------------------- 
Cow, Clarabelle   88  52  75  75  98  90  79  90  93  93 
Duck, Donald      95  82  85  94  93  98  94  92  85  97 
Goffy, Max        99  86  103 102 100 98  103 102 93  101 
Mouse, Minnie     94  88  84  89  98  92  85  74  84  77 
Mouse, Mickey     92  85  84  93  94  94  83  100 95  76 
 
names.txt 
5 
Cow, Clarabelle     
Duck, Donald        
Goffy, Max          
Mouse, Minnie      
Mouse, Mickey      
 
scores.txt 
5  10 
88  52  75  75  98  90  79  90  93  93 
95  82  85  94  93  98  94  92  85  97 
99  86  103 102  100 98 103 102 93  101 
94  88  84  89  98  92  85  74  84  77 
92  85  84  93  94  94  83  100 95  76 
The method studentAverage returns a student's average lab score. For example, 
studentAverage(0) will return Clarabelle's average lab score. The method labAverage returns the 
average score for one of the labs. For example, labAverage(0) will return the average of the first 
column (lab L0) of integers.  
The method studentAverages will create text file that contains the student names followed by 
the student’s average lab score (one name and average lab score per line). The method 
labAverages will create a file that has a lab number (e.g., Lab0) followed by the lab average, 
with each lab number and average on a separate line.  
You must have javadoc comments for all six methods.  
Note: The class LabScores does not have a main method. You will write a separate program 
(e.g., Lab1Main) that has a main method and creates an instance of the class LabScores. You 
will use the main method to test the methods in LabScores.  
Before Lab Assignment 
1. Create an Eclipse project for this lab 
2. Import the class LabScoresAbstract into your project 
3. Create the subclass LabScores that extend the abstract class LabScoresAbstract. Stub out 
all the methods in the UML diagram. If a method you stub out returns a value just return 
0 or null. Add javadoc comments for all the methods. There is an example of javadoc 
comments in the abstract class LabScoresAbstract. 
4. Write the class that contains the main method (e.g., Lab1Main) and in the main method 
instantiate an instance of the class LabScores.  
  
In-Lab Assignment 
I suggest that you write and test one method at a time and you write and test the methods in the 
following order. The first thing you must do in the main method is instantiate an instance of the 
class LabScores. 
1. Write the method readNames. Call the method from main and pass it the file name 
names.txt. Test that it is working correctly. 
2. Write the method readScores. Call the method from main and pass it the file name 
scores.txt. Test that it is working correctly. 
3. Write the method studentAverage. Call the method from inside main and test that it is 
working correctly.  
4. Write the method labAverage.  Call the method from inside main and test that it is 
working correctly.  
5. Write the method studentAverages. Call the method from inside main and pass it the file 
name studentAve.txt. 
6. Write the method labAverages. Call the method from inside main and pass it the file 
name labAve.txt. Test that it is working correctly. 
Hint: For testing you may want to add getter (accessor) methods  for the private attributes 
(fields).  
Post-Lab Assignment 
Complete the lab and don’t forget to include javadoc comments. 
Appendix: Using the Eclipse IDE 
The Eclipse IDE is a freely available, open-source Integrated Development Environment (IDE) 
for Java and other languages. It is available from http://www.eclipse.org. In this lab assignment, 
you'll write a Java program using Eclipse and, in the process, become familiar with the essential 
concepts behind the IDE. 
Workspaces and Projects 
Two essential concepts in Eclipse are the workspace and the project. A workspace is essentially 
just a directory (folder) where Eclipse will store metadata about projects. The projects 
themselves can be stored within the workspace or elsewhere. For this class, we recommend 
storing your projects within the workspace. A Java project is a folder that contains all the 
necessary files for building a single Java program. For the purposes of this class, we recommend 
that you use one single workspace for all your lab assignments and create a new project for each 
assignment. 
When you first start Eclipse, you will be asked to select a workspace. If this is your first-time 
starting Eclipse, you can choose a new directory to contain your workspace (if the directory does 
not exist, Eclipse will create it). Otherwise, select your pre-existing workspace directory. Please 
remember that this directory is not the same as your project directory. A workspace can contain 
many projects. 
I suggest you create a folder for this class and place the Eclipse workspace in that folder. For 
example, C:\CS270\eclipseWS. 
 If this is the first time that you've opened Eclipse in this workspace, you'll be presented with the 
"Welcome" screen. Close the "Welcome" tab (click the "X" next to the word "Welcome" in the 
tab bar), and then you should be presented with the Eclipse workbench. It should look like the 
screen below: 
 
Creating a New Project 
To create a project, select "New" -> "Java Project" from the "File" menu, or use the drop-down 
menu from the icon in the upper left corner (shown below). 
 
In the dialog that appears, enter a name for the project. For this lab, let us use Lab1. Then click 
the "Next" button. Make sure that the checkbox labeled Create module-info.java 
file is NOT selected. See image below. Now click “Finish”. 
 Project Organization 
Eclipse will automatically create a folder named src within your project. All Java source code 
files should be placed within this folder. 
Creating a Java Class 
To create a new Java file, go to "File" -> "New" -> "Class". Or use the menu shown in the 
screenshot below: 
 
In the "New Java Class" dialog, make sure that you choose the src folder as the "Source 
folder", the "Package" can be blank, and be sure to fill in the "Name" field with the name of your 
new Java class. Then click the "Finish" button. 
Running your Program 
To run your Java program, you need to create a "Run Configuration." A simple way to do so is to 
have Eclipse automatically generate it for you. To do so, right click on the Java class that 
contains your main method and choose "Run As" -> "Java Application." This will execute your 
program and create a run configuration (if it hasn't already done so). 
When executing the program in the future (once the run configuration exists), you can select the 
run configuration from the menu connected to the green arrow (play) button in the menu bar: .