【问题标题】:Static method issues静态方法问题
【发布时间】: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


【解决方案1】:

这里有一些框架代码可以帮助您入门:

"静态方法调用ModifyPhonePrices()。该方法的详细信息如下:

该方法接受 3 个参数:一个二维手机数组和两个双精度值。该方法还返回一个整数。

static int modifyCellPhone(CellPhone[][] cp, double x, double y)
{

该方法将在整个数组中搜索价格与第二个参数匹配的任何手机,如果找到,该方法将用作为第三个参数传递的值替换该手机的价格。"

    // start a nested for loop on the array, and search
    
}

我不确定为什么您的数组是二维的。也许您可以对此进行扩展,以便我澄清我的答案。

【讨论】:

  • 我添加了代码,我最初担心的是如何创建一个静态方法,可以同时包含双精度和对象,因为它们是两个完全不同的东西。
  • @JSm,当你创建一个二维数组来保存手机时,向我解释一下你在 main 方法中要做什么。为什么是二维?
  • 因为我被要求创建 10 x 10 数组。因此二维。 Cellphone cellphoneArr [10][10] = new Cellphone 应该创建 100 个为 null 的对象,然后我的循环初始化它
  • 好的。所以一个维度应该包含对手机对象的引用。你用另一个维度做什么?
  • 嗯,我从来没有被这样教过。有人告诉我,第一维手机 [10] 指向第二维数组,它是指向对象的指针(每个“盒子”(在我的脑海中)都指向一个对象)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多