讲解 program、Java设计编程辅导
Coding Style Requirements
Coding Style
Rule Bad Good Penalty
Add a space after commas int num1,num2; int num1, num2; -2
Add a space between operators and
operands
sum=5+3; sum = 5 + 3; -2
Align the { } vertically (new line style) if(num > 0){
}
if(num > 0)
{
}
Graded 0
Add a block after if, while and for
when they contain one statement
if(num > 0)
x++;
if(num > 0)
{
x++;
}
Graded 0
Indent your code appropriately if(num > 0)
{
x++;
}
if(num > 0)
{
x++;
}
Graded 0
Unnecessary continue statements
will be penalized
while(k > 5)
{
if(k > 3)
{
i++;
}
else
{
continue;
}
}
while(k > 5)
{
if(k > 3)
{
i++;
}
}
-10
Variable Naming
Rule Bad Good Penalty
- Single letter names not allowed
except for loop counters or temporary
variables
int n; int number; -3
- Should always start with a lowercase float Ratio; float ratio; -3
- Use abbreviations when they can be
understood
- number n num -3
- customer first name n firstName -3
name custFirstName -3
- quantity q, qt qty, quantity -3
- sentence s, sent sentence -3
- number of students n, nos numStudents,
number
-3
Java - Assignment 04 (100 pts)
Instructions
• Write each exercise in its own method. Uncomment the function calls in main() to activate each
exercise. Refer to "Assignment01" if necessary.
• For each exercise, use comments to write pseudocode with at least one additional refinement
step. Include the pseudocode at the beginning of each method.
• Submit only one file containing all of your code. Multiple files not allowed!
• The knowledge required for this assignment is in the textbook / slides and your notes. You are
not allowed to use any additional resources, websites, external help... Doing so will be
considered plagiarism and / or cheating! If you need help, talk to a CIS tutor or to the professor.
• But you are allowed to assist your friends during the lab session in the presence of the professor.
• If you are having problems unrelated to the assignment (for example, you are unable to find a
specific function in your IDE, …), you can go ahead and check it on the Internet.
• Do not use functions/techniques that have not been covered in class yet. Doing so might be
mistaken for plagiarism and/or defeats the purpose of the exercise and will be graded 0.
• You are required to perform at least one submission at the end of the lab session, even if it’s
incomplete. You can complete it later and resubmit it; the latest one will be graded.
• Test all your methods at least 3 times in main() by calling them with different parameters
Exercise 01 (25 pts)
Write a method public static void distinct(int[] arrNumbers) that receives an array
of ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly
one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number from
the input array and store it in a local array if it is new. If the number is already in the local array, ignore
it.) At the end, the local array will contain the distinct numbers. Here is a sample run of the program:
(assuming that the input array contains 1 2 3 2 1 6 3 4 5 2)
The number of distinct number is 6
The distinct numbers are: 1 2 3 6 4 5
Exercise 02 (30 points)
Use the following formula to compute the standard deviation of n numbers:
𝑑𝑒𝑣𝑖𝑎𝑡𝑖𝑜𝑛 = √
where mean is the average of the n numbers.
To compute the standard deviation with this formula, you have to store the individual numbers using an
array, so that they can be used after the mean is obtained. Your program should contain the following
methods:
/** Compute the deviation of double values */
public static double deviation(double[] arrNumbers)
/** Compute the mean of an array of double values */
public static double mean(double[] arrNumbers)
To test this program, fill the array with ten numbers and output the mean and standard deviation as shown
in the sample run. You can confirm your values by trying the same numbers in Excel and computing the
standard deviation there.
Sample run: (where the values are 1.9 2.5 3.7 2 1 6 3 4 5 2)
The mean is 3.11
The standard deviation is 1.55738
Exercise 03 (45 points)
Write a method findColumnWithLargestSum() which receives an m x n matrix of integers (2D
array) and returns the column (an array) that has the largest sum.
Notes:
• Find the largest sum by column
• Return the entire column, not the index of the column. The column is a 1D array