ITE3101留学生辅导、讲解Java程序、辅导SphereDistances、Java编程设计讲解

- 首页 >> Java编程

ITE3101 - Introduction to Programming Assignment (2018/19) Version 1

D:\ST201819\IP\EA\EA1_Assig\EA1_Question_v1.docx Page 1

ITE3101 Introduction to Programming

EA1 - Programming Assignment Hand-out: 26-Oct-2018

Deadline: 27-Nov-2018, 5:00pm

Important Notice to Students

1. This assignment should be done by individual student. All downloaded materials are not

allowed.

2. Plagiarism will be treated seriously. All assignments that have been found involved

wholly or partly in plagiarism (no matter these assignments are from the original authors or

from the plagiarists) will score ZERO marks.

3. Your program must be compiled and run with ONLY the following commands for Java

JDK6 or above.

C:\...\bin\javac SphereDistances.java

C:\...\bin\java SphereDistances

4. Your program must be structured and well commented.

The first few lines in the source file must be comments stating the name of the source file,

student name, student ID, course name, course code, and brief description of your

program. Marks will be deducted if such comments are not included.

/********************************************************

Program File: SphereDistances.java

Programmer: Chan Tai Man (180000000)

Programme: IT114… - HD in …

Description: This program is …

********************************************************/

5. A sample program, Sample.java, is provided for your reference. You can use the program

codes to read data from a text file, e.g. spheres.txt.

6. Test your program with the given data file, spheres.txt.

7. Put all your program codes in a SINGLE Java source file named as

SphereDistances.java. Submit your UNZIPPED source file to the Moodle link

EA1_Submission.

8. This is part of End-of-Module Assessment (EA). The weight of this assignment is 20% of

the module total assessment.

ITE3101 - Introduction to ProgrammingAssignment (2018/19) Version 1

D:\ST201819\IP\EA\EA1_Assig\EA1_Question_v1.docx Page 2

Calculating Distances of Spheres on a 3-D Space

Background

We use x-, y- and z- coordinates and radii to represent spheres on a 3-D space. We can measure

the distance (d1,2) of any two distinct spheres, S1(x1, y1,z1,r1) and S2(x2,y2,z2,r2) by the following

formulas (see Figure 2):

1,2 = √(1 2)

2 + (12)

2 + (12)

2 12

Tasks

Write a Java program to do the following tasks:

1. Read a set of Si(xi,yi,zi,ri) parameters representing some spheres from a text file,

e.g. spheres.txt.

-94 11 63 6

1 3 -88 10

-91 -97 -43 5

-28 27 75 7

2. Write a Java method to calculate the distance of two spheres by passing their

parameters as inputs. (15 marks)

3. Display the spheres as shown in part (A) in Appendix A. (15 marks)

4. Display the shortest and the longest distances (together with their spheres)

as shown in part (B) in Appendix A. (30 marks)

5. Display the 10 shortest distances (together with their spheres) as shown

in part (C) in Appendix A. (10 marks)

Other Assessment Criteria

Problem solving techniques, e.g. algorithms, data structures, and

Java programming technique, e.g. statements, control structures, etc. (10 marks)

Programming style. (10 marks)

Testing (10 marks)

Resources

Please download the following files from Moodle:

Sample.java

spheres.txt

ITE3101 - Introduction to ProgrammingAssignment (2018/19) Version 1

D:\ST201819\IP\EA\EA1_Assig\EA1_Question_v1.docx Page 3

Appendix A – Expected output of your program with the given spheres.txt file

Name: CHAN Tai Man (180000000) IT114112/1A

Input Spheres:

(-94,11,63,6) (1,3,-88,10) (-88,-96,80,3) (99,49,77,2)

(41,-92,-9,4) (-47,-77,-59,6) (-54,-33,52,4) (-65,83,-80,2)

(-91,-97,-43,5) (-28,27,75,7)

The shortest distance is 39, between (-47,-77,-59,6) and (-91,-97,-43,5)

The longest distance is 260, between (99,49,77,2) and (-91,-97,-43,5)

The 10 shortest distances:

Rank Sphere 1 Sphere 2 Distance

1 (-47,-77,-59,6) (-91,-97,-43,5) 39

2 (-94,11,63,6) (-54,-33,52,4) 50

3 (-94,11,63,6) (-28,27,75,7) 55

4 (-54,-33,52,4) (-28,27,75,7) 58

5 (-88,-96,80,3) (-54,-33,52,4) 69

6 (1,3,-88,10) (-47,-77,-59,6) 81

7 (41,-92,-9,4) (-47,-77,-59,6) 92

8 (1,3,-88,10) (-65,83,-80,2) 92

9 (-94,11,63,6) (-88,-96,80,3) 99

10 (-47,-77,-59,6) (-54,-33,52,4) 109

Figure 1. Example output

Figure 2. Distance between two spheres in a 3-D space

--- END ---

(A) Show inputs in

4 spheres each

line

(B) Show the shortest and

the longest distances

(C) Show the 10

shortest distances

ITE3101 - Introduction to ProgrammingAssignment (2018/19) Version 1

D:\ST201819\IP\EA\EA1_Assig\EA1_Question_v1.docx Page 4

Sample.java

import java.io.*;

import java.util.*;

public class Sample {


final static int MAX_NO_SPHERES = 12; // maximum number of input spheres

final static int X = 0; // array index for x-coordinates

final static int Y = 1; // array index for y-coordinates

final static int Z = 2; // array index for z-coordinates

final static int R = 3; // array index for radii


public static void main( String[] args ) throws Exception {

int p[][] = new int[MAX_NO_SPHERES][4]; // array to store spheres parameters

int nSpheres; // number of spheres read


nSpheres = readSpheresFile( "spheres.txt", p );


System.out.println( "Spheres read from spheres.txt" );

for (int i=0; i<nSpheres; i++)

System.out.println( p[i][X] + "," + p[i][Y] + "," + p[i][Z] + "," + p[i][R] );

}

/***************************************************************************

This is a method to read the parameters (x-, y-, z-coordinates & radii)

of spheres from a text file.

Inputs:

filename is the file name of the input file

Outputs:

spheres[][] is a 2-dimensional array to store parameters

e.g. spheres[0][0] stores the x-coordinate of the 1st sphere

spheres[0][1] stores the y-coordinate of the 1st sphere

spheres[0][2] stores the z-coordinate of the 1st sphere

spheres[0][3] stores the radius of the 1st sphere

...

spheres[n-1][0] stores the x-coordinate of the last sphere

spheres[n-1][1] stores the y-coordinate of the last sphere

spheres[n-1][2] stores the z-coordinate of the last sphere

spheres[n-1][3] stores the radius of the last sphere

Return value:

the total number of spheres read

***************************************************************************/

public static int readSpheresFile( String filename, int[][] spheres ) throws Exception {

// Create a File instance and a Scanner for the input file

File inFile = new File( filename );

Scanner input = new Scanner( inFile );

// Read parameters of spheres from the input file

int nSpheres = 0; // count number of spheres read

while (input.hasNext()) {

spheres[nSpheres][X] = input.nextInt(); // read x-coordinate

spheres[nSpheres][Y] = input.nextInt(); // read y-coordinate

spheres[nSpheres][Z] = input.nextInt(); // read z-coordinate

spheres[nSpheres][R] = input.nextInt(); // read radius

nSpheres++;

}

// Close the input file

input.close();

return nSpheres;

}

}


站长地图