初学者到Java:构造函数和排序 [英] Beginner to Java: Constructors and Sorts

查看:44
本文介绍了初学者到Java:构造函数和排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名初学程序员,遇到构造函数和类似问题。更糟糕的是,我不习惯在两个不同的课程中编程。这让我的头脑旋转,试图追踪不同的名字。


项目背后的想法是我将拥有两组阵列。一个具有项目名称,另一个具有所述项目的相应价格。将询问用户他们希望如何查看已排序的信息。 (按字母顺序按名称或按数字按价格排序。)然后我将它们排序(取决于用户输入)并显示它们。


现在,我知道我需要两个这个和构造函数的不同类。我是否在构造函数中使用一个类中的get / set定义数组并在驱动程序类中进行排序?任何示例,链接或建议都将非常感激。我会在正确的方向上轻轻推动我的荣誉。


~Missy

I am a beginner programmer and am having trouble with constructors and sorts. What''s worse is that I am not used to programming in two different classes. It''s making my head spin looking back and forth trying to trace the different names.

The idea behind the project is that I''m going to have two sets of arrays. One with item names and the other with the corresponding prices of said items. The user will be asked how they wish to see the information sorted. (Either alphabetically by the names or numerically by price.) Then I have them sorted (depending on the user input) and display them.

Now, I know I''ll need two different classes for this and a constructor. Do I define the arrays in the constructor with a get/set in one class and sort in the driver class? Any examples, links, or advice would be GREATLY appreciated. I will give kudos for even the slightest nudge in the right direction.

~Missy

推荐答案

有什么理由可以解释为什么你想要两个阵列?如果你有一个代表文章名称和价格的类会更容易。还有一个单个文章数组,你对它进行了分类显示。
Is there some reason why you want two arrays? It would be a lot easier if you had a class representing the name and price of an article. And a single array of articles which you sorted ad displayed.


我认为我应该为项目使用两个数组。让我为你抓住一些标准。我已经开始将一些代码放在一起了,但我很困惑我在哪里声明了数组对象。另外,有人告诉我,泡泡分类是尝试这个的好方法,但我从来没有用过它。任何人都可以给我这方面的信息吗?


这个项目需要2个班级。第一个是数据定义类,第二个类包含main。

头等名项目:


数据字段仅为2,请记住这些数据字段应该是私有的。


String itemName;

double itemPrice;


此类的公共方法如下:


构造函数采用String和double作为初始化数据字段的输入参数。


getItemName

setItemName

getItemPrice

setItemPrice


现在进入名为CoffeeDriver的第二个类,它将包含main方法。


Driver类所需的方法:


main - 将使用上面的数据创建一个Item Objects数组来设置每个项目的信息。此信息不会来自项目的用户。此外,主要应该从用户请求他们希望按价格或按项目名称查看数据项目列表。对此请求使用JOptionpane。您可能想要包含一个问题,询问用户是否已完成该程序?


sortName此方法将按项目名称对项目对象数组进行排序。请记住,在创建排序时,在比较String值时,不能使用==。您将需要使用compareTo()String方法。见下面的注释。您还必须记住,您只会比较项目名称的字符串,但是当您需要交换排序时,您将交换包含名称和价格的完整对象,而不仅仅是itemNames。此方法将按项目名称顺序显示对象数组。显示将包括itemName和itemPrice。


sortPrice:此方法将按价格对项目对象数组进行排序。再次记住,当您需要交换价格时,您将交换整个对象而不仅仅是价格。同样,此方法将按价格顺序显示对象数组。显示将包括itemName和itemPrice。
I think I''m supposed to use two arrays for the project. Let me grab some of the criteria for you. I have started to put some of the code together, but I''m confused as to where I declare the array objects. Also, I was told that bubble sorting would be a good way to try this, but I''ve never used it. Can anyone give me information on that?

This project will require 2 classes. The first will be the data definition class and the second class will contain the main.
First class name Item:

Data Fields are 2 only and remember these data fields should be private.

String itemName;
double itemPrice;

The public methods for this class will be the following:

A constructor the takes a String and a double as the input arguments to initialize the data fields.

getItemName
setItemName
getItemPrice
setItemPrice

Now on to the second class to be called CoffeeDriver which will contain the main method.

Methods required for the Driver class:

main - will create an array of Item Objects using the data above to set each item’s information. This information will NOT come from the user of the project. In addition, main should request from the user which order they wish to see the data items list, by price or by item name. Use a JOptionpane for this request. You might want to include a question to ask the user if they are finished with the program??

sortName this method will sort the array of item objects by the item name. Remember when creating your sort that when you are comparing String values you cannot use ==. You will want to use the compareTo() String method. See note below. You must also keep in mind that you will compare on the String of the item name only, but when you need to swap for the sort, you will be swapping a complete object which includes the name and price, not just the itemNames. This method will display the array of objects in item name order. The display will include both the itemName and the itemPrice.

sortPrice: this method will sort the array of item objects by price. Again remember, when you need to swap the prices, you are swapping the entire object and not just the prices. Again this method will display the array of objects in price order. The display will include both the itemName and the itemPrice.



此项目将需要2个类。第一个是数据定义类,第二个类包含main。
This project will require 2 classes. The first will be the data definition class and the second class will contain the main.



这使它听起来更像我描述的。 数据定义是指数据定义。是一个类(我称之为文章),包括两个名称和价格。


正如驱动程序类的描述所说: 将使用上面的数据创建项目对象的数组。只涉及一个数组。


我建议你从数据定义类开始,因为这是最直接的:一个有几个实例字段的类(每个都带有get /设置mutator方法)和构造函数。如果你遇到问题,请发布代码。


然后你可以开始编写驱动程序类,其三个(静态?)方法完全按照赋值中的描述进行。实现排序的丑陋细节可以在维基百科或您的教科书上找到。


再次重复赋值描述中的要点:驱动程序类的main()方法开头创建另一个(数据)类的单个实例数组。

This makes it sound even more like I described. The "data definition" is a class (what I was calling an article) that includes both the name and the price.

As the description of the driver class says, it: "will create an array of Item Objects using the data above". There is only a single array involved.

I suggest you start with the data definition class as this is the most straight forward: a class with a couple of instance fields (each with get/set mutator methods) and a constructor. Post code if you get stuck.

Then you can start writing the driver class whose three (static?) methods do exactly what was described in the assignment. The ugly details of implementing the sort can be found on Wikipedia or your textbook.

Again to repeat the point made in the assignment description: the main() method of the driver class begins by creating a single array of instances of the other (data) class.


这篇关于初学者到Java:构造函数和排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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