【发布时间】:2018-09-14 11:04:39
【问题描述】:
在我的一个课程中,我们创建了一个程序,它随机创建 10 辆具有价格和星级的汽车。现在,程序创建了 10 个对象,然后创建了另一个将 10 个对象进行比较的对象。它按星级搜索,按星级对 10 进行排序,然后对 10 个对象进行二分搜索。我一直在尝试通过在每个对象中添加汽车制造商名称来改进它,但一直弄乱程序。该程序有两个类
import java.util.*;
import java.util.Scanner;
public class Sorter{
static Car [] ary; // declare
final static int NUM_CARS = 10;
public static void main() {
//Scanner rating = new Scanner(System.in);
//int r = rating.nextInt();
Car key = new Car();
ary = new Car[NUM_CARS]; // initialize
int i;
int position;
for (i=0; i<NUM_CARS; i++) {
ary [ i ] = new Car();
}
System.out.println("Unsorted:");
System.out.println(Arrays.toString(ary));
System.out.println("Sequential search for " + key);
ary[0].reset();
position = sequentialSearch(key)+1; //add one to the index position to display the position on screen that the user's desired search is first found
System.out.println("Total comparisons: "
+ ary[0].getAllCount());
System.out.println("Found a position: " +position);
System.out.println();
System.out.println("Sorted:");
ary[0].reset();
Arrays.sort(ary);
System.out.println("Total comparisons: "
+ ary[0].getAllCount());
System.out.println(Arrays.toString(ary));
System.out.println("Binary search for " + key);
ary[0].reset();
position = binarySearch(key)+1; //add one to the index position to display the position on screen that the user's desired search is first found
System.out.println("Found a position: " +position);
System.out.println("Total comparisons: "
+ ary[0].getAllCount());
}
public static int binarySearch (Car keyCar) {
return Arrays.binarySearch(ary,keyCar);
}
public static int sequentialSearch (Car keyCar) {
int pos = -1;
int i;
int answer;
for (i=0; i<ary.length; i++) {
answer = ary[i].compareTo(keyCar);
if (answer==0) {
pos = i;
break;
}
}
return pos;
}
}
二等
import java.util.*;
/**
* in class work
*/
public class Car implements Comparable<Car> {
// instance variables - replace the example below with your own
int stars;
double price;
String name [] = {"Ford", "Dodge", "Chevrolet", "Honda", "Toyota", "VW", "Hyundai"};
int myCounter;
static int allCounter;
/**
* Constructor for objects of class Sorter
*/
public Car() {
Random generator = new Random ();
price = generator.nextDouble()*50000 + 50000;
stars = generator.nextInt(5)+1;
reset();
}
public String toString()
{
return String.format("$%,7.0f(%d stars)",price,stars);
//$ put dollar sign infront
//up to 7 digits
//, puts commas
}
/**
* Resets counter to zero
*/
public void reset(){
myCounter=0;
allCounter=0;
}
public int getMyCount() {
return myCounter;
}
public int getAllCount() {
return allCounter;
}
/**
* @param other A house to compare to.
* @return Returns 0 if they are equal
*/
public int compareTo(Car other)
{
myCounter++;
allCounter++;
if (this.stars < other.stars) {
return -1;
} else if (this.stars> other.stars) {
return +1;
}
return 0; // equals
}
}
如何使对象显示价格、星级和制造商? 此外,我想做但尚未尝试的另一件事是制作它,以便用户可以输入他们想要的星级,它只会显示那些汽车。这部分是我想在有时间的时候尝试的下一件事情。
【问题讨论】:
-
我没有尝试过很多不同的东西,因为我不确定要使用什么代码。在公共 Car() 我试着把 name = generator.nextLine(name.length);但它说“找不到符号”
-
把你尝试过的细节放在问题本身中。
-
@TrippKinetics 对不起,我是这个网站的新手,所以我按 Enter 跳过一行,但它发布了它。
标签: java arrays sorting search binary-search