using arrays & methods with Object Oriented Programing Java
if (bikeRace[i] > bikeRace[0])
change to
if (bikeRace[i].getSpeed() > bikeRace[0].getSpeed())
Reasoning: You cannot numerically compare non-numerical objects.
George Putnam
Updated on June 04, 2022Comments
-
George Putnam almost 2 years
I am learning Java, with that being said, my knowledge is still in the crawling phase of learning java. I have read about using objects in arrays which is very powerful. I am trying to edit this simple example class and test class to show who is the fastest driver, and what is the fastest speed. There are two ways I was thinking on how to solve this. One was find the maximum speed, and find what array number (e.g. array[2]).
I have managed to capture the highest speed in a loop, and the array number. putting it into a method, and then calling it is giving confusing me big time. Any help is much appreciated.error code I am getting
TestBikeWithArray.java:73: error: bad operand types for binary operator '>'
if (bikeRace[i] > bikeRace[0])
^
first type: Bike
second type: Bike
1 errorTool completed with exit code 1
They are both Bike classes, I don't get it.public class Bike{ private String driver; private int gear; private int cadence; private int speed; private int id; private static int numberOfBikes = 0; public Bike(String theDriver, int startGear,int startCadence, int startSpeed){ driver = theDriver; gear = startGear; cadence = startCadence; speed = startSpeed; numberOfBikes++; id = numberOfBikes; } public String getDriver() {return driver;} public void setDriver(String d) {driver = d;} public int getGear(){return gear;} public void setGear(int g){gear = g;} public int getCadence(){return cadence;} public void setCadence(int c){cadence = c;} public int getSpeed(){return speed;} public void setSpeed(int s) {speed = s;} public int getID() {return id;} public static int getNumberOfBikes() {return numberOfBikes;} // method to print out contents of object public String toString () { StringBuffer sb = new StringBuffer(); sb.append("\nDriver: " + driver); sb.append("\nGear: " + gear); sb.append("\nCadence: " + cadence); sb.append("\nSpeed: " + speed); sb.append("\nID: " + id + "\n"); return (new String(sb)); } // end toString method } // end class
main test bikeimport java.util.Scanner; import javax.swing.JOptionPane; class TestBike { public static void main(String[] args) { String message =""; Bike[] bikeRace = new Bike[10]; bikeRace[0] = new Bike("Cersei Lannister", 0, 0, 0); bikeRace[1] = new Bike("Daenerys Targaryen", 10, 60, 22); bikeRace[2] = new Bike("Robb Stark", 21, 50, 15); //testing values //bikeRace[3] = new Bike("Melisandre", 31,90,155); //bikeRace[4] = new Bike("Margaery Tyrell", 2, 3, 100); bikeRace[0].setGear(3); for(int i=0; i < Bike.getNumberOfBikes(); i++) { System.out.println(bikeRace[i].toString()); //testing, must use a proper function call! //System.out.println(bikeRace[i].getDriver()); } // end for loop //find max speed & driver findTheFastestDriver //testing stuffs get all drivers /*for(int i=0; i < Bike.getNumberOfBikes(); i++) { System.out.println("Driver: " + bikeRace[i].getDriver()); } //testing get all speeds for(int i=0; i <Bike.getNumberOfBikes(); i++) { System.out.println("Speed: " + bikeRace[i].getSpeed()); } */ //****************************************************************/ //finding fastest bike without a method //fastest speed is 22 /*int fastestBike = bikeRace[0].getSpeed(); int fastestBikeIndex = 0; for(int i=0; i<Bike.getNumberOfBikes(); i++) { if (bikeRace[i].getSpeed() >= fastestBike) { fastestBike = bikeRace[i].getSpeed(); fastestBikeIndex = i; }//end if }//end for System.out.println("fastest Bike Speed: " + fastestBike + " " + " Bike index of " + fastestBikeIndex);*/ //******************************************************************** int fastestBikeIndex = findTheFastestDriver(bikeRace); System.out.println("\nThe fastest driver is " + bikeRace[fastestBikeIndex].getDriver() + " and the speed of " + bikeRace[fastestBikeIndex].getSpeed() + " mph."); System.out.println("\nNumber of Bikes: " + Bike.getNumberOfBikes() + "\n"); } //end of main public static int findTheFastestDriver(Bike[] bikeRace) { int fastestBike = bikeRace[0].getSpeed(); int fastestBikeIndex = 0; for(int i=0; i < Bike.getNumberOfBikes(); i++) { if (bikeRace[i].getSpeed() >= fastestBike) { fastestBike = bikeRace[i].getSpeed(); fastestBikeIndex = i; }//end if }//end for return fastestBikeIndex; } } //end of class
code works :) so happy.
-
Tim Bender about 11 yearsIn Java, you should implement
Comparable
and use the compareTo method to establish a natural ordering for sorting/comparing. -
Patashu about 11 years@Tim Bender's idea is good if (and only if) your objects have a natural ordering
-
George Putnam about 11 yearsthank you for the tip. So when calling the method back. int bikeIndex = findTheFastestDriver(bikeRace) System.out.print("The fastest driver " +bikeRace.getDriver[bikeIndex] + " and is going " + bikeRace.getSpeed[bikeIndex] + " mph."); Still the method call is not correct.
-
Ted Hopp about 11 years@TimBender - That's certainly one approach, but "should" is the the wrong word. It's not at all clear that
Bike
objects have a natural ordering. Even if they did, it's not clear that it should be based on speed. Why not onid
, or on the lexical order ofdriver
? -
George Putnam about 11 yearsI never thought of that Tim. I figured since I am looking for the fastest driver, look for the highest speed first. let me take a look again at the code.
-
George Putnam about 11 yearsThank very much for the explanations. I found the explanations very helpful. I need to practice more on methods, arrays, and objects to understand them more. practice makes perfect.
-
George Putnam about 11 yearsTimBender, wow on compareTo method. I didn't know about this tool, and reading more up on this subject. very cool, and very powerful. makes this a bit more simple. Will play around with this tomorrow.
-
Patashu about 11 years@George Putnam Another tool you should look into is
Comparator
. If a class doesn't have a single natural sort, you can define aComparator
for it and pass it to operations that expect one, such as sorting and making a PriorityQueue