I am having an issue with a couple of programs - one I am to debug and the other that I need to write.
The first one I am to take a user inputted planet name, convert it to uppercase and then run it through an enum. What did I do wrong? import java.util.*;
public class DebugNine4 {
enum Planet {
MERCURY, VENUS, EARTH, MARS, JUPITER, SATURN, URANUS, NEPTUNE
};
public static void main(String[] args) {
Planet planet;
String userEntry;
int position;
int comparison;
Scanner input = new Scanner(System.in);
System.out.print("Enter a planet in our solar system >> ");
planet = input.nextLine().toUpperCase();
planet = Planet.valueOf(planet);
System.out.println("You entered " + planet);
position = planet.ordinal();
System.out.println(planet + " is " + (position + 1)
+ " planet(s) from the sun");
}
}
The 2nd one is the output is not going through like I want it. It is coming back with:
Cut Shampoo
and not with the info I need it to do - be sortable by name of service, cost and time. Where did I go wrong on this code?
- First you will need to create a Service object with three data fields.
Next you will need to create an array of 6 Service objects, populating them with the data from table 9.6, page 420 in the book, like:
service[0] = new Service("Cut",8.00,15);
Using the Scanner object, collect the response from the question:
"Sort services by (S)ervice, (P)rice, or (T)ime"
Return all of the information in 3 formatted columns listed in the correct order based on the input like:
Sorted by time:
Trim $6.00 5 minutes Shampoo $4.00 10 minutes
Code:
public class SalonReport {
public static void main(String[] args) {
// services listing with time and cost
Service[] myService = new Service[6];
myService[0] = new Service("Cut", 8.00, 15);
myService[1] = new Service("Shampoo", 4.00, 10);
myService[2] = new Service("Manicure", 18.00, 30);
myService[3] = new Service("Style", 48.00, 55);
myService[4] = new Service("Permanent", 18.00, 35);
myService[5] = new Service("Trim", 6.00, 5);
SortDescription(myService, myService.length);
System.out.println(myService[0].getServiceType() + " "
+ myService[1].getServiceType());
}
public static void SortDescription(Service[] array, int len) {
int a;
int b;
Service temp;
for (a = 0; a < len; ++a)
for (b = 0; b < len - 1; ++b) {
if (array[b].getServiceType().compareTo(
array[b + 1].getServiceType()) > 0)
;
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}
}
class Service {
// declaring parameters
String servDescript;
double price;
int avgMin;
public Service(String s, double p, int m) { // constructor
servDescript = s;
price = p;
avgMin = m;
}
// method returning requested item -
public String getServiceType() {
return servDescript;
}
public double getPrice() {
return price;
}
public int getMinutes() {
return avgMin;
}
}