0% found this document useful (0 votes)
22 views5 pages

Experiment 3

programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Experiment 3

programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

评分与评语:

台州学院
电子与信息工程学院实验报告

班级 1 学号 2362720018 中文姓名 孙安家

实验课程: Java 程序设计基础

实验项目: Experiment 3 Java Arrays

NOTE:
When pasting code, please do not use a black background. Otherwise, when the
teacher prints your assignments at the end of the semester, it will be a mess of
black.

实验日期: 2024 年 6 月 19 日

Project: Locate the largest element

Problem Description:
(Exercise08_13) Write the following method that returns
the location of the largest element in a two-dimensional
array.

public static int[] locateLargest(double[][] a)

The return value is a one-dimensional array that contains


two elements. These two elements indicate the row and
column indices of the largest element in the two-
dimensional array. Write a test program that prompts the
user to enter a two-dimensional array and displays the
location of the largest element in the array. Here is a
sample run:

<Output>
Enter the number of rows and columns of the array: 3 4
Enter the array:
23.5 35 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is at (1, 2)
<End Output>

Analysis: (Describe the problem including input and output in your own
words.)

This Java program is designed to find the location (row and column indices) of the
largest element in a given two-dimensional array. Here's a breakdown of how the
program works:
Input:
 The user is prompted to enter the number of rows and columns for the array.
 Then, the user is asked to input the elements of the 2D array row by row.
 Processing:
 The program creates a 2D array (matrix) based on the specified number of rows
and columns.
 The user inputs the elements into this array.
 The locateLargest method is called, which iterates over the elements of the array
to find the largest value. It keeps track of the row and column indices where this
maximum value is found.
Output:
The program prints out the location (row and column indices) of the largest element in
the array.

Design: (Describe the major steps for solving the problem.)


1. Prompt user to enter rows and columns:
- Read and validate `rows` and `columns`.
2. Create the 2D array `matrix`:
- Initialize `matrix` with dimensions `rows` × `columns`.

3. Input array elements:


- Use nested loops to iterate through each element of `matrix`:
- Prompt user to enter each element and store it in `matrix[i][j]`.

4. Define function `locateLargest(matrix)`:


- Initialize `max` to `matrix[0][0]` and `location` to `[0, 0]`.
- Iterate through each element of `matrix` using nested loops:
- If current element > `max`:
- Update `max` to the current element.
- Update `location` to current indices `[row, column]`.

5. Print the result:


- Display the location of the largest element using `location` indices.

6. Close the scanner.

Coding: (Copy and Paste Source Code here. Format your code using Courier
10pts)
import java.util.Scanner;
public class Exercise_8_13{
public static void main(String[] args) {
//Create a scanner
Scanner scanner = new Scanner (System.in);
System.out.print("Enter the number of the rows and columns of
the array: ");
byte rows = scanner.nextByte();
byte columns = scanner.nextByte();
if (rows <1 || columns<1) {
System.err.print("Error : Invalid rows or columns.");
System.exit(0);
}
double[][] matrix = new double [rows][columns];
System.out.println("Enter the array : ");
for(byte i = 0; i < rows; i++)
for(byte j = 0; j < columns; j++)
matrix[i][j] = scanner.nextDouble();

int[] location = locateLargest(matrix);


System.out.print("The location of the largest element is at
("
+ location[0] + ',' + location[1] + ')');

//Close Scanner
scanner.close();
}
public static int[] locateLargest (double[][] a) {
int [] location = new int [2];
double max = a[0][0];
for(int row = a.length-1; row >= 0; row--){
for (int column = a [a.length-1].length-1; column>=0;
column--){
if(max < a[row][column]) {
location[0] = row;
location[1] = column;
max = a[row][column];
}
}
}
return location;
}
}

Testing: (Describe how you test this program)


1. Prepare test cases for various scenarios:
a. Create a small array (e.g., 2x2) with positive values.
b. Create an array with negative values.
c. Create an array with a mixture of positive and negative values.
d. Create arrays with different sizes (1x1, 3x3, 5x5, etc.).

2. Implement test cases using JUnit or manual testing:


a. For each test case, initialize the array and input the elements.
b. Run the program and verify that the output matches the expected result (position
of the largest element).

3. Validate edge cases:


a. Test with a single-element array (smallest possible).
b. Test with arrays containing duplicate maximum values.
c. Test with very large arrays to assess performance.

4. Evaluate program behavior:


a. Check for correct identification of the largest element's position.
b. Ensure that the program handles various array sizes and value distributions
correctly.
c. Measure execution time for larger arrays to ensure performance meets
expectations.

5. Adjust the program based on test results:


a. If any test case fails, debug and make necessary corrections to the program.
b. Optimize the program if performance issues are identified during testing.

6. Document test results and observations:


a. Record the outcomes of each test case (pass/fail).
b. Note any improvements or modifications made to the program.

You might also like