-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRationalNumber.java
67 lines (57 loc) · 1.74 KB
/
RationalNumber.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import javax.swing.*;
import javax.swing.plaf.synth.SynthTextAreaUI;
import java.sql.SQLOutput;
import java.sql.Types;
public class Food implements Comparable<Food> {
private static final String[] TYPES = {"Apple", "Pear", "Cookie", "Grape"};
String ftype;
int fcal;
public Food(String type, int calories) {
for(int i= 0; i<TYPES.length; i++) {
if (TYPES[i].equals(type)) {
this.ftype = type;
this.fcal = calories;
}
}
}
public String toString(){
String back="";
back= back+this.ftype+" "+this.fcal;
return back;
}
public int compareTo(Food o) {
int p1= this.ftype.length()*this.fcal;
int p2= o.ftype.length()*o.fcal;
if (p1<p2){
return -1;
}
else if (p2<p1){
return 1;
}
return 0;
}
public String getType() {
return this.ftype;
}
public int getCalories() {
return this.fcal;
}
public static Food[] createSortedRandomList(int n) {
Food food[]= new Food[n];
for(int i=0; i<food.length; i++){
int trand= (int)(Math.random()*4);
int calrand= (int)(Math.random()*1000);
Food f= new Food(TYPES[trand], calrand );
food[i]= f;
}
java.util.Arrays.sort(food);
return food;
}
public static void main(String[] args) {
Food a= new Food("Apples", 30);
Food b= new Food("Pear", 500);
//Food food[]= Food.createSortedRandomList(3);
//System.out.println(java.util.Arrays.toString(food));
System.out.println(a.compareTo(b));
}
}