在这个的前面我要怎么打印出topDisplay.mDisplayContent.compareTo(display.mDisplayContent)的值和display.mDisplayContent和mDisplayContent.compareTo(display.mDisplayContent)的值 if (!canUpdate && topDisplay.mDisplayContent.compareTo(display.mDisplayContent) < 0)
时间: 2023-02-08 22:30:35 浏览: 108
如果需要打印出topDisplay.mDisplayContent.compareTo(display.mDisplayContent)的值和display.mDisplayContent和mDisplayContent.compareTo(display.mDisplayContent)的值,可以在if语句前添加以下代码:
System.out.println("topDisplay.mDisplayContent.compareTo(display.mDisplayContent)的值为:" + topDisplay.mDisplayContent.compareTo(display.mDisplayContent));
System.out.println("display.mDisplayContent的值为:" + display.mDisplayContent);
System.out.println("topDisplay.mDisplayContent的值为:" + topDisplay.mDisplayContent);
注意: 前提是 mDisplayContent 是 String 类型的变量。
相关问题
【实践训练】 1. 定义抽象类 Shape,其中包含抽象方法 double getPeremeterO求周长和 double getArea0求面积。 定义一个矩形类,继承此抽象类,并自行扩充成员变量和方法,定义一个方法 一次直接显示长和宽、周长和面积。 定义一个测试类,测试矩形类。 2. 定义一个接口 CompareObject,具有方法 compareTo0用于比较两个对象。 定义一个类 Position 从 CompareObject 派生而来,有x和y属性表示其坐标, 该类实现 compare To0方法,用于比较两个对象距离原点(0,0)的距离。 定义测试类测试 Position 类的 compareTo0方法。
1.
```
// Shape.java
public abstract class Shape {
public abstract double getPerimeter();
public abstract double getArea();
}
// Rectangle.java
public class Rectangle extends Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public void display() {
System.out.println("Length: " + length);
System.out.println("Width: " + width);
System.out.println("Perimeter: " + getPerimeter());
System.out.println("Area: " + getArea());
}
@Override
public double getPerimeter() {
return 2 * (length + width);
}
@Override
public double getArea() {
return length * width;
}
}
// Test.java
public class Test {
public static void main(String[] args) {
Rectangle rect = new Rectangle(5, 10);
rect.display();
}
}
```
2.
```
// CompareObject.java
public interface CompareObject {
public int compareTo(Object o);
}
// Position.java
public class Position implements CompareObject {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Object o) {
Position other = (Position) o;
double distance1 = Math.sqrt(x * x + y * y);
double distance2 = Math.sqrt(other.x * other.x + other.y * other.y);
if (distance1 < distance2) {
return -1;
} else if (distance1 > distance2) {
return 1;
} else {
return 0;
}
}
}
// Test.java
public class Test {
public static void main(String[] args) {
Position p1 = new Position(3, 4);
Position p2 = new Position(5, 12);
int result = p1.compareTo(p2);
if (result < 0) {
System.out.println("p1 is closer to the origin.");
} else if (result > 0) {
System.out.println("p2 is closer to the origin.");
} else {
System.out.println("p1 and p2 are equidistant from the origin.");
}
}
}
```
Extend the application above to be able to measure the efficiency of the implemented sorting algorithm in term of number of comparisons performed by the algorithm. The application should display the total number of comparisons performed by the algorithm for a given collection of items. [10 marks]
Here is an updated implementation of the NetBeans application that includes the ability to measure the efficiency of the sorting algorithm in terms of the number of comparisons performed:
```java
import java.util.ArrayList;
public class MyArrayList<T extends Comparable<T>> extends ArrayList<T> {
private int numComparisons;
public void bubbleSortDescending() {
int n = this.size();
boolean swapped;
do {
swapped = false;
for (int i = 1; i < n; i++) {
if (this.get(i - 1).compareTo(this.get(i)) < 0) {
T temp = this.get(i - 1);
this.set(i - 1, this.get(i));
this.set(i, temp);
swapped = true;
}
numComparisons++;
}
n--;
} while (swapped);
}
public int getNumComparisons() {
return numComparisons;
}
public static void main(String[] args) {
MyArrayList<Integer> list = new MyArrayList<Integer>();
list.add(7);
list.add(2);
list.add(9);
list.add(4);
list.add(5);
list.add(1);
list.add(6);
list.add(8);
list.add(3);
list.add(10);
System.out.println("Original list: " + list);
list.bubbleSortDescending();
System.out.println("Sorted list: " + list);
System.out.println("Number of comparisons: " + list.getNumComparisons());
}
}
```
In this updated implementation, we add a private instance variable `numComparisons` to keep track of the number of comparisons performed by the sorting algorithm. We increment this variable every time a comparison is made in the `bubbleSortDescending` method.
We also add a getter method `getNumComparisons` to retrieve the value of `numComparisons` from outside the class.
In the `main` method, after sorting the list, we print out the number of comparisons performed by the algorithm using the `getNumComparisons` method.
阅读全文