试图理解排序方法。 Java家庭作业 [英] Trying to understand sort method. Java Homework

查看:131
本文介绍了试图理解排序方法。 Java家庭作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是家庭作业,但我需要轻推。我找不到如何按姓名排序。

This is homework, but I need a "nudge". I can't find how to sort by name.

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

My Questions: (Please keep answers beginner friendly)


  1. 到目前为止看起来是否合适?

  2. 如何排序?

  3. 有没有人有优化/清理这个的建议?

这是作业:


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

The Inventory Program Part 2 checkpoint that is due in week 6 has the following requirements:


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

  1. Modify the Inventory Program so the application can handle multiple items. Use an array to store the items.

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

The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product.

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

In addition, the output should display the value of the entire inventory.

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

Create a method to calculate the value of the entire inventory.

创建另一种方法,按产品名称对数组项进行排序。

Create another method to sort the array items by the name of the product.

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

To satisfy these requirements, you will need to add the following to your Inventory class (not product class):

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

1) Declare an array of type Product (a private instance variable)

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

2) Inside your main while loop, do the following:

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循环之外,执行以下操作:

3) Outside of your main while loop, do the following:

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


推荐答案

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

Using the Comparator is easy and can be accomplished with something like this :

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

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

I believe, based on the instructions provided and your code, that you may be misunderstanding the guidelines. It seems that there should be a main(...), an Inventory class, and a DVD/Product class. You currently only have part of that.

你可以用更干净,更容易使用的方式替换你主体中的while循环 foreach 。说明中提到并提供了for-each的示例。应根据提供的说明从main中删除此循环。

You could replace the while loop in your main with a much cleaner and easier to use foreach. The instructions mention and provided an example of the for-each. This loop should be removed from the main based on the instructions provided.

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

You should also review some of the basic Java best practices and naming conventions.

这篇关于试图理解排序方法。 Java家庭作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆