讲解 program、辅导 Java语言编程
Java test 2
Important Notes
1. The assignment is an individual project, to be finished on one’s own effort.
2. 20% mark deduction will be given for late submission within 2 days, and 0 for even later;
3. Plagiarism is strictly forbidden, regardless of the role in the process. Notably, ten consecutive
lines of identical codes are treated as plagiarism. Using AI to directly generate code will also
be regarded as plagiarism. Depending on the seriousness of the plagiarism, 30% − 100% marks
will be deducted.
Marking Criterion
1. The full score of the assignment is 300 marks.
2. Three java programs are to be submitted. Each program will be evaluated with several unseen
test cases. A submission obtains the full score if and only if both programs pass all test cases.
Running Environment
1. The submissions will be evaluated in the OJ system running Java SDK 21. It is the students’
responsibility to make sure that his/her submissions are compatible with the OJ system.
2. The submission is only allowed to import four packages of (java.lang.*; java.util.*; java.math.*;
java.io.*) included in Java SDK, and StdIn / StdOut from textbook. No other packages are
allowed.
3. All students will have an opportunity to test their programs in the OJ platform prior to the
official submission.
Submission Guidelines
1. You will get your grade only if you submit your code both on OJ and on bb on time.
2. For bb submission, you need to directly upload your java file on bb. That is, your submission
should be M atrix.java, LongestCommonP ref ix.java, and P ower.java. Wrong submission
format will receive 10% mark deduction.
3. Inconsistency with or violation from the guideline leads to marks deduction.
4. All students are reminded to read this assignment document carefully and in detail. No argument will be accepted on issues that have been specified in this document.
1
Programs
There are 3 independent programs in this assignment, and each is worth 100 points.
Matrix
Write a Java program ”Matrix.java” that can perform matrix addition and multiplication operation.
Input: The first line is a positive integer T, followed by T groups of input data. For each group, the
first line includes 4 integers m, n, p, q, and r, each separated by space. The following m lines give
the elements of the first matrix, and each line has n space-separated integers, which form matrix A.
Then the following p lines give the elements of the second matrix, and each line has q space-separated
integers, which form matrix B. All input matrices and operations are valid, so it’s not necessary to
consider robustness of your program.
Output: For each group, if r equals 0, output AB; else output A + B. In the output, each element should be separated by a space. There’s no extra space at the end of a line, or extra blank row
between two groups of output.
For all test cases, 1 ≤ T ≤ 10, 1 ≤ m, n, p, q ≤ 30, r ∈ {0, 1}, −500 ≤ elements in matrix ≤ 500.
Hint: Write two functions for addition and multiplication.
An example of console input Expected console output
2
2 2 2 2 0
1 -1
2 1
5 4
0 3
1 3 1 3 1
6 8 9
-6 -8 -9
5 1
10 11
0 0 0
LongestCommonPrefix
You are given n strings consisting of only English letters and digits. Write a Java program ”LongestCommonPrefix.java” that takes the strings and output the longest common prefix of the strings. S
is a common prefix if all the strings starts with S, and the longest common prefix is the longest S
you can find.
Input: The first line is a positive integer n. Then, for the following n lines, each contains a string
with only English letters and digits.
Output: The longest common prefix in a line. If the strings have no common prefix, output a
blank row.
For all test cases, 1 ≤ n ≤ 30, and 1 ≤ the length of each string ≤ 30.
2
An example of console input Expected console output
3
flower
flow
flight
fl
An example of console input Expected console output
3
hello
world
CSC2003
Power
Given 2 integers m, n, write a Java program “Power.java” that tests whether m = n
x
, where x can be
any positive integer, in a recursive way. The input includes 1 line with two integers m, n separated by
a space. The output should be ”true” if m = n
x
, else ”false”. For all test cases, 1 ≤ m, n ≤ 1000000.
An example of console input Expected console output
8192 2 true
3