静态方法问题
Static method issues
我研究我的代码有一段时间了,我被要求执行以下操作:
”调用ModifyPhonePrices()的静态方法。该方法的详细信息如下:
该方法接受 3 个参数:一个二维单元格 phone 数组,以及两个
双值。该方法还returns一个整数。
该方法将搜索整个数组以查找具有价格的任何单元格 phone
匹配第二个参数,如果找到,该方法将替换价格
该单元格 phone 的值作为第三个参数传递。"
我怎么可能创建一个同时查找对象(即 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(" ");
}
}
}
编辑添加代码
这里有一些框架代码可以帮助您入门:
"static method called ModifyPhonePrices(). The details of this method are as follows:
The method accepts 3 parameters: a 2-dimentional array of cell phones, and two double values. The method also returns an integer.
static int modifyCellPhone(CellPhone[][] cp, double x, double y)
{
The method will search the entire array looking for any cell phone with a price that matches the 2nd parameter, and if found, the method will replace the price of that cell phone with the value passed as the 3rd parameter."
// start a nested for loop on the array, and search
}
我不确定为什么你的数组是二维的。也许您可以对此进行扩展,以便我澄清我的答案。
我研究我的代码有一段时间了,我被要求执行以下操作:
”调用ModifyPhonePrices()的静态方法。该方法的详细信息如下:
该方法接受 3 个参数:一个二维单元格 phone 数组,以及两个 双值。该方法还returns一个整数。
该方法将搜索整个数组以查找具有价格的任何单元格 phone 匹配第二个参数,如果找到,该方法将替换价格 该单元格 phone 的值作为第三个参数传递。"
我怎么可能创建一个同时查找对象(即 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(" ");
}
}
}
编辑添加代码
这里有一些框架代码可以帮助您入门:
"static method called ModifyPhonePrices(). The details of this method are as follows:
The method accepts 3 parameters: a 2-dimentional array of cell phones, and two double values. The method also returns an integer.
static int modifyCellPhone(CellPhone[][] cp, double x, double y)
{
The method will search the entire array looking for any cell phone with a price that matches the 2nd parameter, and if found, the method will replace the price of that cell phone with the value passed as the 3rd parameter."
// start a nested for loop on the array, and search
}
我不确定为什么你的数组是二维的。也许您可以对此进行扩展,以便我澄清我的答案。