Welcome to JAVA!(第8章课后习题)

8.1Following the example of the Circle class in &8.2,design a class named Rectangle to represent a rectangle.The class contains:
Two double data fields named width and height that specify the width and height of the rectangle.The default values are 1 for both width and height.
  • A no-arg constructor that creates a default rectangle.
  • A constructor that creates a rectangle with the specified width and height.
  • A method named getArea() that returns the area of this rectangle.
  • A method named getPerimeter() that returns the perimeter.
代码如下
public class Rectangle {
	public static void main(String[] args) {
		Rectangle R1 = new Rectangle();
		System.out.println("The width of the Rectangle is " + R1.width);
		System.out.println("The height of the Rectangle is " + R1.height);
		System.out.println("The Area of the Rectangle is " + R1.getArea());
		System.out.println("The Perimeter of the Rectangle is " + R1.getPerimeter());
		
		Rectangle R2 = new Rectangle(100,20);
		System.out.println("The width of the Rectangle is " + R2.width);
		System.out.println("The height of the Rectangle is " + R2.height);
		System.out.println("The Area of the Rectangle is " + R2.getArea());
		System.out.println("The Perimeter of the Rectangle is " + R2.getPerimeter());
	}
	double width,height;
	Rectangle()
	{
		width=1;
		height=1;
	}
	Rectangle(double newWidth,double newHeight)
	{
		width=newWidth;
		height=newHeight;
	}
	double getArea()
	{
		return width*height;
	}
	
	double getPerimeter()
	{
		return 2*(width+height);
	}
}
运行结果:
/*output:
The width of the Rectangle is 1.0
The height of the Rectangle is 1.0
The Area of the Rectangle is 1.0
The Perimeter of the Rectangle is 4.0
The width of the Rectangle is 100.0
The height of the Rectangle is 20.0
The Area of the Rectangle is 2000.0
The Perimeter of the Rectangle is 240.0
*///~

8.13Design a class named Location for location a maximal value and its location in a two-dimensional array,The class contains public data fields row,column,and maxValue that store the maximal value and its indices in a two dimensional array with row and column as int type and maxValue as double type.
Write the following method that returns the location of the largest element in a two-dimensional array.
public static Location locateLargest(double[][] a)
The return value is an instance of Location.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.
代码如下:

Main.java
import java.util.Scanner;
public class Main {
	public static void main(String[] args)
	{
		Main L1=new Main();  
        Location L2=new Location(); 
		double[][] a=new double[10][10];
		System.out.print("Enter the numberof rows and columns of the array: ");
		Scanner input = new Scanner(System.in);
		int rows = input.nextInt();
		int columns = input.nextInt();
		System.out.println("Enter the array:");
		for (int i = 0; i < rows; i++)
		{
			for (int j=0; j<columns;j++ )
			{
				a[i][j]=input.nextDouble();
				
			}
		}
		L2 = L1.locateLargest(a); 
		System.out.println("The location of the largest element is "+ L2.maxValue+" at (" + L2.row+"," + L2.column+")"); 
	}
	public static Location locateLargest(double[][] a)
	{
		Location L = new Location();
		for(int i = 0;i<a.length;i++){
			for(int j=0;j<a[i].length;j++){
				if(a[i][j]> L.maxValue){
					L.row=i;
					L.column=j;
					L.maxValue=a[i][j];
				}
			}
		}
	return L;
	
	}
}


Location.java

class Location  
{  
    int row,column;  
    double maxValue;  
    void setRow(int i)  
    {  
        row=i;  
    }  
    void setColumn(int j)  
    {  
        column=j;  
    }  
}  

运行结果:
/*output:
 * Enter the numberof rows and columns of the array: 3 4
Enter the array:
23.5 32 2 10
4.5 3 45 3.5
35 44 5.5 9.6
The location of the largest element is 45.0 at (1,2)
*///~


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值