厦大计算机系Java程序设计实验(二)

习题1 最大子方阵

题目描述:给定一个由0,1组成的n×n方阵(n在运行时提醒用户输入),判断其中由全1组成的最大子方阵的左上角位置和阶数。例如用户输入n为5,随机产生的方阵如下:


要求编写方法实现上述功能,返回值是一个包含3个元素的数组,依次表示行下标、列下标、阶数。

方法原型:public static int[] findLargestBlock(int[][]m)

解题思路:阶数从1一直到n,遍历每一种可能的子矩阵,看其是否全为1,如果该子矩阵的阶数比之前知道的要大,则更新之,否则继续考察其它子矩阵。

源代码

import java.util.Scanner;

public class Test
{   
    public static int[] findLargestBlock(int[][] m)
    {
        int[] maxSubmatrix = {0, 0, 0};
        
        for(int order = 1;order <= m.length;++order)
        {
            for(int i = 0;i <= m.length - order;++i)
            {
                for(int j = 0;j <= m[0].length - order;++j)
                {
                    boolean isOne = true;
                    for(int r = i;r < i + order && isOne == true;++r)
                    {
                        for(int c = j;c < j + order && isOne == true;++c)
                        {
                            if(m[r][c] == 0)
                                isOne = false;
                        }
                    }
                    if(isOne == true && order > maxSubmatrix[0])
                    {
                        maxSubmatrix[0] = order;
                        maxSubmatrix[1] = i;
                        maxSubmatrix[2] = j;
                    }
                }
            }
        }
        
        return maxSubmatrix;
    }
    
    public static void main(String[] args)
    {
        int n = 0;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Please input n: ");
        n = input.nextInt();
        
        int[][] m = new int[n][n];
        for(int i = 0;i < n;++i)
        {
            for(int j = 0;j < n;++j)
            {
                m[i][j] = (int)(Math.random() * 2);
            }
        }
        
        System.out.println("The matrix is: ");
        for(int i = 0;i < n;++i)
        {
            for(int j = 0;j < n;++j)
            {
                System.out.print(m[i][j] + " ");
            }
            System.out.print("\n");
        }
        
        int subMatrix[] = new int[3];
        subMatrix = findLargestBlock(m);
        
        System.out.println("The max submatrix is " + subMatrix[0] + " order, in (" + subMatrix[1] + ", " +  subMatrix[2] + ")");
    }
}

测试样例

Please input n: 5
The matrix is: 
1 1 1 0 1 
1 0 1 1 0 
0 1 1 1 0 
0 1 0 1 1 
1 0 1 1 0 
The max submatrix is 2 order, in (1, 2)

习题2 二次方程类

题目描述:设计一个二次方程类QuadraticEquation,用于处理形如ax2 + bx + c = 0 (a≠0)的二次方程,成员如下:

–       私有成员a, b, c用于存储系数;

–       含三个参数的构造方法,用于传入a, b, c;

–       三个方法getA(), getB(), getC(),用于传出系数;

–       一个方法getDiscriminant()用于传出b2 - 4ac的值;

–       两个方法getRoot1()和getRoot2()用来返回方程的两个根。注意方程可能没有实根,所以返回值定义为String。

请提供一个测试类,测试上述所有方法。

解题思路:自定义的类QuadraticEquation中,包括了读入、读出数据的函数,计算判别式的函数,以及求根函数。在测试类Test中,依次进行检测。

源代码

import java.util.Scanner;

class QuadraticEquation
{
    double a, b, c;
    
    void updateA(double newA)
    {
        a = newA;
    }
    
    void updateB(double newB)
    {
        b = newB;
    }
    
    void updateC(double newC)
    {
        c = newC;
    }
    
    double getA()
    {
        return a;
    }
    
    double getB()
    {
        return b;
    }
    
    double getC()
    {
        return c;
    }
    
    double getDiscriminant()
    {
        return b * b - 4 * a * c;
    }
    
    String getRoot1()
    {
        String root1;
        
        if(getDiscriminant() < 0)
        {
            root1 = "No real root";
        }
        else
        {
            root1 = Double.toString((-b + Math.sqrt(getDiscriminant())) / (2 * a));
        }
        
        return root1;
    }
    
    String getRoot2()
    {
        String root2;
        
        if(getDiscriminant() < 0)
        {
            root2 = "No real root";
        }
        else
        {
            root2 = Double.toString((-b - Math.sqrt(getDiscriminant())) / (2 * a));
        }
        
        return root2;
    }
}

public class Test
{
    
    public static void main(String[] args)
    {
        double a, b, c;
        QuadraticEquation eq = new QuadraticEquation();
        Scanner input = new Scanner(System.in);
        
        System.out.print("Please input the coefficients: ");
        a = input.nextDouble();
        b = input.nextDouble();
        c = input.nextDouble();
        eq.updateA(a);
        eq.updateB(b);
        eq.updateC(c);
        
        System.out.println("The coefficients are: " + eq.getA() + " "+ eq.getB() + " " + eq.getC());
        
        System.out.println("The discriminant is: " + eq.getDiscriminant());
        
        System.out.println("Root1: " + eq.getRoot1());
        System.out.println("Root2: " + eq.getRoot2());
    }
}

测试样例

样例1:

Please input the coefficients: 1 -2 1
The coefficients are: 1.0 -2.0 1.0
The discriminant is: 0.0
Root1: 1.0
Root2: 1.0

样例2:

Please input the coefficients: 1 -4 1
The coefficients are: 1.0 -4.0 1.0
The discriminant is: 12.0
Root1: 3.732050807568877
Root2: 0.2679491924311228

样例3:

Please input the coefficients: 1 0 1
The coefficients are: 1.0 0.0 1.0
The discriminant is: -4.0
Root1: No real root
Root2: No real root

习题3  2*2线性方程组

题目描述:设计一个类LinearEquation用于处理如下的2*2线性方程组,成员包含:

–       私有成员a, b, c, d, e, f;

–       一个6参数构造方法,用于传入a, b, c, d, e, f;

–       6个getter用于返回a, b, c, d, e,f,例如getA(),getB(), …;

–       一个方法isSolvable()用于判定方程是否有解,有则返回true,否则false;

–       方法getX()和getY()返回一组解。

请提供一个测试类,测试上述所有方法。


解题思路:自定义的类LinearEquation中包括了方程组的系数、构造方法、读出系数方法、有解判断方法、求解方法,在测试类Test中逐一进行测试。

源代码

import java.util.Scanner;

class LinearEquation
{
    double a, b, c, d, e, f;
    
    LinearEquation(double newA, double newB, double newC, double newD, double newE, double newF)
    {
        a = newA;
        b = newB;
        c = newC;
        d = newD;
        e = newE;
        f = newF;
    }
    
    double getA()
    {
        return a;
    }
    
    double getB()
    {
        return b;
    }
    
    double getC()
    {
        return c;
    }
    
    double getD()
    {
        return d;
    }
    
    double getE()
    {
        return e;
    }
    
    double getF()
    {
        return f;
    }
    
    boolean isSolvable()
    {
        return (a * d - b * c != 0);
    }
    
    String getX()
    {
        if(isSolvable() == true)
        {
            double x = (e * d - b * f) / (a * d - b * c);
            return Double.toString(x);
        }
        else
        {
            return "The equation has no solution";
        }
    }
    
    String getY()
    {
        if(isSolvable() == true)
        {
            double y = (a * f - e * c) / (a * d - b * c);
            return Double.toString(y);
        }
        else
        {
            return "The equation has no solution";
        }
    }
}

public class Test
{
    public static void main(String[] args)
    {
        double a, b, c, d, e, f;
        Scanner input = new Scanner(System.in);
        
        System.out.print("Please input the coefficients: ");
        a = input.nextDouble();
        b = input.nextDouble();
        c = input.nextDouble();
        d = input.nextDouble();
        e = input.nextDouble();
        f = input.nextDouble();
        
        LinearEquation eq = new LinearEquation(a, b, c, d, e, f);
        
        System.out.println("The coefficients are: " + eq.getA() + " " +
                eq.getB() + " " +
                eq.getC() + " " +
                eq.getD() + " " +
                eq.getE() + " " +
                eq.getF() + " ");
        
        System.out.println("The equation can be solved: " + eq.isSolvable());
        
        System.out.println("Root X = " + eq.getX());
        System.out.println("Root Y = " + eq.getY());
    }
}

测试样例

样例1:

Please input the coefficients: 1 2 1 4 -2 3
The coefficients are: 1.0 2.0 1.0 4.0 -2.0 3.0 
The equation can be solved: true
Root X = -7.0
Root Y = 2.5

样例2:

Please input the coefficients: 1 1 2 2 1 2
The coefficients are: 1.0 1.0 2.0 2.0 1.0 2.0 
The equation can be solved: false
Root X = The equation has no solution
Root Y = The equation has no solution

习题4 Location类

题目描述:定义一个Location类,用于搜索二维数组的最大元素出现的位置和值。位置用公有的整型成员变量row, col表示,最大值用公有的浮点型成员变量maxValue表示。一个成员方法用来求解二维数组的最大元素及其位置,原型如下:

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

例如数组为{{1,2,3},{8,9,9,5},{4,3,5,7,8}},最大元素为9,位置是(1,1)。注意最大值不止一个的时候,只记录第一次出现的位置。

请提供一个测试类,测试上述方法。

解题思路:逐一扫描矩阵中的各个元素,找到最大值并记录其下标。在调用时,使用了成员函数的引用。

源代码

import java.util.Scanner;

class Location
{
    int row;
    int column;
    double maxValue;
    
    public static Location locateLargest(double[][] a)
    {
        Location loc = new Location();
        loc.row = 0;
        loc.column = 0;
        loc.maxValue = Double.MIN_VALUE;
        
        for(int i = 0;i < a.length;++i)
        {
            for(int j = 0;j < a[0].length;++j)
            {
                if(a[i][j] > loc.maxValue)
                {
                    loc.maxValue = a[i][j];
                    loc.row = i;
                    loc.column = j;
                }
            }
        }
        return loc;
    }
}

public class Test
{ 
    public static void main(String[] args)
    {
        final int M = 10;
        final int N = 10;
        
        double[][] m = new double[M][N];
        Scanner input = new Scanner(System.in);
        int row, column;
        
        System.out.print("Enter the number of rows and columns of the array: ");
        row = input.nextInt();
        column = input.nextInt();
        
        System.out.println("Enter the array: ");
        for(int i = 0;i < row;++i)
        {
            for(int j = 0;j < column;++j)
            {
                m[i][j] = input.nextDouble();
            }
        }
        
        Location loc;
        loc = Location.locateLargest(m);
        System.out.println("The location of the largest element is " + loc.maxValue +
                " at (" + loc.row + ", " + loc.column + ")");
    }
}

测试样例

Enter the number of rows and columns of the array: 3 4
Enter the array: 
34.5 12 3 5.2
12 45 3 23.1
0.9 1 5 7.8
The location of the largest element is 45.0 at (1, 1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值