【发布时间】:2015-06-23 01:59:13
【问题描述】:
我一直在研究我的代码,但我被要求执行以下操作:
"静态方法调用ModifyPhonePrices()。该方法的详细信息如下:
该方法接受 3 个参数:一个二维手机数组和两个 双值。该方法还返回一个整数。
该方法将搜索整个数组以查找任何有价格的手机 匹配第二个参数,如果找到,该方法将替换价格 作为第三个参数传递的值的手机。”
我怎么可能创建一个既查找对象(即 Cellphone 对象)又查找双值的方法。我不需要两个不同的数组来实现吗?
public static double modifyCellPhone ( double x, double y)
那个,我明白。让我感到困惑的是其中包含二维数组。
为了清楚起见,这是我的代码:
import java.util.Random;
class Cellphone {
private String brand;
private long serialNumber;
private double Price;
public Cellphone (String br, long sN, double Pr)
{
brand= br;
serialNumber = sN;
Price = Pr;
}
public Cellphone(Cellphone aCellphone)
{
this(aCellphone.getbrand(), aCellphone.getserialNumber(), aCellphone.getPrice());
}
public String getbrand()
{
return brand;
}
public long getserialNumber()
{
return serialNumber;
}
public double getPrice()
{
return Price;
}
public void setBrand(String cellphoneBrand)
{
//allows to set the brand of the cellphone
brand = cellphoneBrand;
}
public void setSerialNumber(long SN)
{
// Sets the Serial Number of the cellphone
serialNumber = SN;
}
public void setPrice(double Pr)
{
// Sets the price of the cellphone
Price = Pr;
}
public String toString()
{
return this.brand + ", " + this.serialNumber + " " + this.Price;
}
public boolean equals(Cellphone phone)
{
if (Price == phone.Price && brand.equals(phone))
return true;
else
return false;}
public boolean equals2(Cellphone phone)
{
if (Price == phone.Price)
return true;
else
return false;
}
public boolean equals3(Cellphone phone)
{
if (brand.equals(phone));
return true;
}
}
public class ModifyCellPhone {
public static double ModifyPhonePrices (Cellphone[][], double x, double y)
{
return int r;
}
public static void main (String[] args)
{
Cellphone[][] cellphoneArr = new Cellphone[10] [10];
for (int i=0; i < cellphoneArr.length-1; i++)
{
for (int m=0; m < cellphoneArr[i].length; m++)
{
if (i % 3 == 0)
cellphoneArr[i][m]= new Cellphone("Samsung", 111111111 + (2 * (m+ i) +1), 500.4 + (m+ i));
else if (i % 3 == 1)
cellphoneArr[i][m] = new Cellphone("LG", 111111111 + (2 * (m+ i)), 500.6 + (m+ i));
else if (i % 3 == 2)
cellphoneArr[i][m] = new Cellphone("HTC", 11111111 + 2^(m+ i), 700 + (m+ i));
}
}
for (int y=9; y<cellphoneArr.length; y++)
{
for (int n=0; n<cellphoneArr[y].length; n++)
{
cellphoneArr[y][n] = new Cellphone(cellphoneArr [y-7][n]);
}
}
Random generator = new Random();
for (int i=0; i < cellphoneArr.length ; i++)
{
for (int m=0; m < cellphoneArr[i].length; m++)
{
double pick = generator.nextInt(199) + 100;
cellphoneArr[i][m].setPrice((int)pick);
}
}
for (int p = 0; p < cellphoneArr.length; p++)
{
// Loop and display sub-arrays.
for (int x = 0; x < cellphoneArr[p].length; x++)
{
System.out.print(cellphoneArr[p][x].getPrice() + " ");
}
System.out.println(" ");
}
}
}
编辑添加代码
【问题讨论】:
-
我们需要查看更多代码才能开始回答这个问题。
-
您能否提供更多代码。这有助于更好地理解您的问题。
-
我们至少需要 Cellphone 类的代码以及您对代码的尝试。
-
为什么数组是二维的?
-
你应该先阅读一本初学者编程书籍。看来您不了解面向对象编程(或任何编程)的基础知识。
标签: java static-methods