ITE3101 – Introduction to ProgrammingAssignment (2021/22) Version 1
ITE3101_Assignment_Question_GA_2021.docx
Page 1
ITE3101 Introduction to Programming
EA1 – Programming Assignment Hand-out: 11/11/2021 9:30
Deadline: 02/12/2021 17:30
Important Notice to Students
- This assignment should be done by individual student. All downloaded materials are not
allowed. - 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. - 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 - 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 …
…
/ - 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. - Test your program with the given data file, points.txt.
- 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. - 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 (2021/22) Version 1
ITE3101_Assignment_Question_GA_2021.docx
Page 2
Calculating Distances of Spheres on a 3-D Space
Background
We use x-, y- and z- coordinates to represent spheres on a 3-D space. We can measure the
distance (d1,2) of any two distinct spheres, S1(x1, y1,z1) and S2(x2,y2,z2) by the following formulas:
𝑑1,2 = √(𝑥1 − 𝑥2) -
- (𝑦1 − 𝑦2)
-
- (𝑧1 − 𝑧2)
2
Figure 0. Distance between two spheres in a 3-D space
Tasks
Write a Java program to do the following tasks:
- (𝑧1 − 𝑧2)
- Read a set of Si(xi,yi,zi) parameters representing some spheres from a text file,
e.g. points.txt.
-80 10 65 - 13 -78
… - -6 35
- -39 -33
ITE3101 – Introduction to ProgrammingAssignment (2021/22) Version 1
ITE3101_Assignment_Question_GA_2021.docx
Page 3 - When the program starts, Main Menu will be shown as follows: (5 masks)
Name: CHAN Tai Man (210000000) IT114206/1D
Main Menu
1) Display the spheres data
2) Calculate the distance
3) Display the 10 shortest distance
4) Credit
9) Quit
You have to handle the following user input:
User Input Action
1
(15 marks)
Display the All spheres data. (Show data in 3 spheres each line)
(See Figure 1. Sphere Data)
Proceeds to Main Menu
2
(15 marks)
Prompt the user to enter 2 indexes, and calculate the distance of two spheres
(See Figure 2. calculate the distance)
Proceeds to Main Menu
3
(20 marks)
Display the 10 shortest distances (together with their spheres)
(See Figure 3. 10 shortest distances)
Proceeds to Main Menu
4
(10 masks)
Display the Credit (Design by you, include your description, your name)
(See Figure 4. Credit)
Proceeds to Main Menu
9
(5 masks)
Confirm whether the user chooses to Quit the program
(See Figure 5. Quit)
[1 – Yes] to Quit program , [0 – No] to Main Menu
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 (2021/22) Version 1
ITE3101_Assignment_Question_GA_2021.docx
Page 4
Appendix A – Expected output of your program with the given spheres.txt file
Name: CHAN Tai Man (210000000) IT114206/1D
Main Menu
1) Display the spheres data
2) Calculate the distance
3) Display the 10 shortest distance
4) Credit
9) Quit
Figure 0. Main Menu - Sphere Data
Total 15 Spheres
S1 [-94,11,63] S2 [1,3,-88] S3 [-88,-96,80]
S4 [99,49,77] S5 [41,-92,-9] S6 [-47,-77,-59]
S7 [-54,-33,52] S8 [-65,83,-80] S9 [-91,-97,-43]
S10 [-28,27,75] S11 [65,12,20] S12 [-55,45,28]
S13 [39,-98,1] S14 [-99,9,10] S15 [0,0,0]
–\ Enter <<any key>> to return Main Menu\–
Figure 1. Sphere Data - Calculate the distance
Please enter the First sphere index(1-15): 2
Please enter the Secord sphere index(1-15): 10
The distance is 162,
between S2 [1,13,-78] and S10 [-18,39,81]
–\ Enter <<any key>> to return Main Menu\–
Figure 2. Calculate distance - Display 10 Shortest distances
The 10 shortest distances:
Rank 1st Sphere 2nd Sphere Distance - S12 S15 40
- S13 S14 40
- S4 S11 41
- S6 S9 48
- S11 S13 48
- S5 S15 56
- S1 S7 57
- S11 S14 62
- S1 S10 70
- S3 S7 72
–\ Enter <<any key>> to return Main Menu\–
Figure 3. Shortest distances
ITE3101 – Introduction to ProgrammingAssignment (2021/22) Version 1
ITE3101_Assignment_Question_GA_2021.docx
Page 5 -
Credit
Program File: SphereDistances.java
Programmer: Chan Tai Man (210000000)
Programme: IT114… – HD in …
Description: This program is …
–\ Enter <<any key>> to return Main Menu\–
Figure 4. Credit - Quit
End of program
Figure 5. Quit
— END — -
Design by yourself
ITE3101 – Introduction to ProgrammingAssignment (2021/22) Version 1
ITE3101_Assignment_Question_GA_2021.docx
Page 6
Sample.java
import java.io.File;
import java.util.Scanner;
public class Sample {final static int MAX_NO_POINTS = 20; // maximum number of input points
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-coordinatespublic static void main(String[] args ) throws Exception {
int p[][] = new intMAX_NO_POINTS; // array to store the points parameters
int nPoints; // number of points readnPoints = readPointsFile(“points.txt”, p);
System.out.println(“Points read from points.txt”);
for (int i=0; i<nPoints; i++)
System.out.println(pi + “,” + pi + “,” + pi);
}
/*
This is a method to read the parameters (x-, y-, z-coordinates)
of points from a text file.
Inputs:
filename is the file name of the input file
Outputs:
points[][] is a 2-dimensional array to store parameters
e.g. points0 stores the x-coordinate of the 1st point
points0 stores the y-coordinate of the 1st point
points0 stores the z-coordinate of the 1st point
…
pointsn-1 stores the x-coordinate of the last point
pointsn-1 stores the y-coordinate of the last point
pointsn-1 stores the z-coordinate of the last point
Return value:
the total number of points read
*/
public static int readPointsFile(String filename, int[][] points) 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 points from the input file
int nPoints = 0; // count number of points read
while (input.hasNext()) {
pointsnPoints = input.nextInt(); // read x-coordinate
pointsnPoints = input.nextInt(); // read y-coordinate
pointsnPoints = input.nextInt(); // read z-coordinate
nPoints++;
}
// Close the input file
input.close();
return nPoints;
}
}