【问题标题】:Trying to understand sort method. Java Homework试图理解排序方法。 Java 作业
【发布时间】:2009-08-20 17:58:29
【问题描述】:

这是作业,但我需要“轻推”。我找不到如何按名称排序。

我的问题:(请保持初学者友好的答案)

  1. 到目前为止看起来正确吗?
  2. 如何排序?
  3. 有人对优化/清理这个有什么建议吗?

这是作业:

在第 6 周到期的库存计划第 2 部分检查点具有以下要求:

  1. 修改库存程序,使应用程序可以处理多个项目。使用数组来存储项目。

  2. 输出应一次显示一个产品的信息,包括项目编号、产品名称、库存单位数、每个单位的价格以及该产品的库存价值产品。

  3. 此外,输出应显示整个库存的价值。

  4. 创建一个计算整个库存价值的方法。

  5. 创建另一个方法以按产品名称对数组项进行排序。

为了满足这些要求,您需要将以下内容添加到您的 Inventory 类(而不是产品类)中:

1) 声明一个 Product 类型的数组(私有实例变量)

2) 在主 while 循环中,执行以下操作:

a. Instantiate a product object 
b. Populate the product object with the input from the console (like Inventory Part 1     does) 
c. Add the product object reference to the next element in your array 
d. Destroy the reference to the product object variable (note this object was added to your array so you can set the local variable that refers to the object to null)

3) 在主 while 循环之外,执行以下操作:

a. Call a method in your Inventory class to sort the array of Product objects. Note you need to sort an array of Product objects by the Product Name instance variable. To do this, you will need to create a separate .java file that implements the Comparator interface. You will pass the Product array and implemented Comparator interface class as arguments to the sort method of the Arrays class (part of the Java API). 
b. Create a For Loop that iterates through the array of Product objects (similar to the one I have below). Invoke the get method on each instance variable and format the output by calling printf. Sample For Loop to use:
for ( Product product : productArray )  
    {           Add statements here  
    }
c. Call a method in your Inventory class to calculate the total inventory value of all the Product objects in your array. 

这是我的代码

    public class InvTest2{// main method begins
       public static void main( String args[] ) {

        int version = 2;// Version number
        final int invLeng = 5;// Declare Inv length

       // Welcome message
       System.out.printf( "\n%s%d\n" , 
       "Welcome to the Inventory Program v.", version );

       Inv[] DVDs = new Inv[invLeng];
       DVDs[0] = new Inv("The Invisible Man", 0, 8.50); // new DVD constructor
       DVDs[1] = new Inv("The Matrix", 1, 17.99);
       DVDs[2] = new Inv("Se7en", 7, 12.99);
       DVDs[3] = new Inv("Oceans Eleven", 11, 9.99);
       DVDs[4] = new Inv("Hitch Hikers Guide to the Galaxy", 42, 18.69);

        // Display formatted results
        int c = 0;
        double runningValue = 0;
        System.out.printf( "\n%s\n", "Inventory of DVD movies");
        while(c != DVDs.length){
        System.out.printf( "\n\n%s%d\n%s%s\n%s%d\n%s%,.2f\n%s%,.2f\n",
        "Item Number:      ",c,      
        "DVD Title:        ",DVDs[c].getdvdTitle(),
        "Copies in stock:  ",DVDs[c].getdvdInStock(),
        "Price each disk:  $",DVDs[c].getdvdValue(),
        "Value of copies:  $",DVDs[c].getdvdStockValue());//End print
        runningValue += DVDs[c].getdvdStockValue();
        c++;
        }
        System.out.printf( "\n%s%,.2f\n", 
        "Collection Value: $",runningValue);

       }// end method main

}//end class Inventory1

这是库存

public class Inv {//Begin DVD class

   // Declare instance variables
   String dvdTitle;// Declare title as string
   int dvdInStock;// Declare dvdInStock as float
   double dvdValue;// Declare dvdValue as float

   // constructor initializes DVD information
   public Inv(String title, int inStock, double value) { // Initialize (clear) instance variables here
      dvdTitle = title;
      dvdInStock = inStock;
      dvdValue = value;
      } // end constructor

   public String getdvdTitle() {// public method to get the DVD name
      return dvdTitle;}// end method getdvdTitle

   public int getdvdInStock() {// public method to retrieve the dvdInStock
      return dvdInStock;} // end method get dvdInStock

   public double getdvdValue() {// public method to retrieve the dvdValue
      return dvdValue;} // end method get dvdValue

   public double getdvdStockValue() {// public method to dvdStockValue
      return ( dvdValue * dvdInStock );}// end method get dvdStockValue

   } // end class Inv

【问题讨论】:

  • 您的代码不符合讲师提供的要求。您可能希望从那里开始。
  • 对那个编辑破坏者感到抱歉。如果您觉得我做得不好,请回滚。
  • 寻求家庭作业帮助的问题在于,没有人愿意再做作业。你最好问一些关于你遇到的实际问题的更具体的问题
  • 我猜我很困惑。我认为我满足要求 1-4 好吗?
  • @Welbog= 我的第一次回滚!我知道现在哪里搞砸了。没问题! @mkoryak= 看起来对吗?如何排序?有什么清理小窍门吗?

标签: java


【解决方案1】:

该作业确切地告诉您要执行按名称排序的操作。

睡一觉,然后重新阅读“在主 while 循环之外,执行以下操作:”下的第一个要点。

【讨论】:

  • touche' 据我所知,我猜比较器部分并没有真正在读数中。这是主要问题,也是睡眠不足。
【解决方案2】:

使用Comparator 很简单,可以通过以下方式完成:

public class InvSortByName implements Comparator<Inv>{
    public int compare(Inv o1, Inv o2) {
        return o1.getdvdTitle().compareTo(o2.getdvdTitle());
    }
}

根据所提供的说明和您的代码,我相信您可能误解了这些指南。似乎应该有一个 main(...)、一个 Inventory 类和一个 DVD/Product 类。你目前只有其中的一部分。

您可以将 main 中的 while 循环替换为更简洁、更易于使用的 foreach。说明提到并提供了 for-each 的示例。应根据提供的说明从主循环中删除此循环。

您还应该查看一些基本的 Java 最佳实践和命名约定。

【讨论】:

  • 非常有帮助。感谢您的轻推先生!
  • 我也是这么看的;一个主程序循环,它有一个 Inventory 对象,可以在其中放置各种 Product 对象(此处为 DVD)。然后 Inventory 类可以有一个调用 Arrays.sort(T[], Comparator) 的排序方法。 (java.sun.com/j2se/1.5.0/docs/api/java/util/…
  • 或者您可以让您的 DVD 具有可比性,只需调用 Arrays.sort(T[])
  • @extraneon 好点!我正在考虑这样做,但按照教练的要求去做。还是好消息传下去!
【解决方案3】:

你确定你应该使用 Arrays.sort 方法吗?出于某种原因,在我看来,要求 5 要求您编写自己的排序方法。虽然我可能错了,但您可能需要在使用 Arrays.sort 之前进行验证。

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2010-11-20
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多