您的位置:首页 > 其它

8.13 ** (The Location class)

2015-10-27 20:28 183 查看
问题及代码:

Main.java
package first;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int row, column ;
System.out
.print("Enter the number of tows anf columns of the array: ");
Scanner cin = new Scanner(System.in);
row = cin.nextInt();
column = cin.nextInt();
System.out.println("Enter the array: ");
double [][]array = new double[row][column];
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
array[i][j]=cin.nextDouble();
}
}
Location a=new Location(row,column,array);
Point x=new Point();
x=a.getmaxPoint();
System.out.println("The location of the largest element is "+(int)a.max +" at ("+(int)x.getx()+","+(int)x.gety()+")");
}
}

Location.java

package first;

public class  Location {
public int row, column;
double [][]array;
double max;
Point maxPoint=new Point();
Location (int a,int b,double a1[][])
{
row=a;
column=b;
array=a1;
}
Point getmaxPoint()
{
max=array[0][0];
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
if(array[i][j]>max)
{
max=array[i][j];
maxPoint.setPoint(i,j);
}
}
}
return maxPoint;
}
}

Point.java

package first;

public class Point {
double a,b;
Point()
{
a=0;
b=0;
}
Point(double x,double y)
{
a=x;
b=y;
}
void setPoint(double x,double y)
{
a=x;
b=y;
}
double getx()
{
return a;
}
double gety()
{
return b;
}
}

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: