-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRFVolcanoes.java
More file actions
197 lines (175 loc) · 4.92 KB
/
Copy pathPRFVolcanoes.java
File metadata and controls
197 lines (175 loc) · 4.92 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* Lab 7B: PRFVolcanoeses.java
* Author: Te-Feng (Dylan) Pan
* Last Modified: 03/17/2013
* -------------------------------
* Objectives: Understand passing Objects - making & using an equals,
* toString, compareTo methods, plus a copy constructor.
* Please see Lab requirements.
*/
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Reports information of various volcanoes from around the world.
*/
public class PRFVolcanoes
{
private String name, country, type;
private int height, eruptYear;
/**
* Constructor for PRFVolcanoes.
*/
public PRFVolcanoes(String nam, String cty, String typ, int hgt, int yr)
{
name = nam;
country = cty;
type = typ;
height = hgt;
eruptYear = yr;
}
/**
* Copy constructor for PRFVolcanoes.
*/
public PRFVolcanoes(PRFVolcanoes volc)
{
name = "Santa Maria";
country = "Guatemala";
type = "Strato";
height = 12375;
eruptYear = 2011;
}
/**
* Returns name of volcano.
*/
public String getName()
{
return name;
}
/**
* Returns country/continent of volcano.
*/
public String getCountry()
{
return country;
}
/**
* Returns type of volcano.
*/
public String getType()
{
return type;
}
/**
* Returns height of volcano.
*/
public int getHeight()
{
return height;
}
/**
* Returns last eruption year of volcano.
*/
public int getYear()
{
return eruptYear;
}
/**
* Prints out information about volcanoes with formatting
*/
public String toString()
{
DecimalFormat formatter = new DecimalFormat("#,###");
return this.getName() + " " + this.getCountry() + " " + formatter.format(this.getHeight()) +
" " + this.getYear() + " " + this.getType();
}
/**
* Tests if volcanoes are equal
*/
public boolean equals(PRFVolcanoes volc)
{
if (volc.getName().equals(this.getName()) && volc.getCountry().equals(this.getCountry()) &&
volc.getHeight() == this.getHeight())
return true;
else
return false;
}
/**
* Compares the height of two volcanoes
*/
public int compareTo(PRFVolcanoes volc)
{
if (this.getHeight() < volc.getHeight())
return -1;
else if (this.getHeight() == volc.getHeight())
return 0;
else// if (this.getHeight() > volc.getHeight())
return 1;
}
/**
* Tests all of this nifty volcano stuff.
*/
public static void main(String[] args)
{
String nameInput, countryInput, typeInput;
int yearInput, heightInput;
Scanner keyboard = new Scanner(System.in);
//First volcano
System.out.println("Enter first volcano name:");
nameInput = keyboard.nextLine();
System.out.println("Enter country/continent:");
countryInput = keyboard.nextLine();
System.out.println("Enter height in feet:");
heightInput = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter year of last eruption:");
yearInput = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter type/shape of volcano:");
typeInput = keyboard.nextLine();
PRFVolcanoes volc1 = new PRFVolcanoes(nameInput, countryInput, typeInput, heightInput, yearInput);
//Second volcano
System.out.println("Enter second volcano name:");
nameInput = keyboard.nextLine();
System.out.println("Enter country/continent:");
countryInput = keyboard.nextLine();
System.out.println("Enter height in feet:");
heightInput = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter year of last eruption:");
yearInput = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter type/shape of volcano:");
typeInput = keyboard.nextLine();
PRFVolcanoes volc2 = new PRFVolcanoes(nameInput, countryInput, typeInput, heightInput, yearInput);
if (!volc1.equals(volc2))
{
if (volc1.compareTo(volc2) == -1)
System.out.println(volc1.getName() + " is shorter than " + volc2.getName() + "!\n");
else if (volc1.compareTo(volc2) == 0)
System.out.println(volc1.getName() + " is the same height as " + volc2.getName() + "!\n");
else if (volc1.compareTo(volc2) == 1)
System.out.println(volc1.getName() + " is taller than " + volc2.getName() + "!\n");
}
else
System.out.println("The volcanoes are equal!\n");
System.out.println("Name Location Current Height Last Eruption Type/Shape");
System.out.println(volc1);
System.out.println(volc2 + "\n");
//Third volcano
PRFVolcanoes volc3 = new PRFVolcanoes(volc1);
if (!volc1.equals(volc3))
{
if (volc1.compareTo(volc3) == -1)
System.out.println(volc1.getName() + " is shorter than " + volc3.getName() + "!\n");
else if (volc1.compareTo(volc3) == 0)
System.out.println(volc1.getName() + " is the same height as " + volc3.getName() + "!\n");
else if (volc1.compareTo(volc3) == 1)
System.out.println(volc1.getName() + " is taller than " + volc3.getName() + "!\n");
}
else
System.out.println("The volcanoes are equal!\n");
System.out.println("Name Location Current Height Last Eruption Type/Shape");
System.out.println(volc1);
System.out.println(volc3);
}
}