Return Highest Value of an Array of Objects

  • (9 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

120 Replies - 4179 Views - Last Post: 01 August 2017 - 03:47 AM Rate Topic: -----

#1 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Return Highest Value of an Array of Objects

Posted 28 July 2017 - 11:49 AM

Hi, I require help with my address book of athletes.

 

package Debug;

public class BanffMarathonRunner {

 // Instance Fields
 private String firstName;
 private String lastName;
 private int time;
 private int year;

 // Constructor
 public BanffMarathonRunner(String fN, String lN, int t, int y) {
  this.firstName = fN;
  this.lastName = lN;
  this.time = t;
  this.year = y;
 }

 // Getters
 public String getFullName() {
  return firstName + " " + lastName;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return firstName;
 }

 public int getYear() {
  return year;
 }
 
 public getFastestRunner() {
  return [b]???;[/b]
 }

 // Setters
 public void setFirstName(String fN) {
  firstName = fN.substring(0, 1).toUpperCase() + fN.substring(1).toLowerCase();
 }

 public void setLastName(String lN) {
  lastName = lN.substring(0, 1).toUpperCase() + lN.substring(1).toLowerCase();
 }
 
 public void setTime(int t) {
  time = t;
 }

 public void setYear(int y) {
  year = y;
 }

 // Main Method
 public static void main(String[] args) {

  // Create Objects
  BanffMarathonRunner[] runners = new BanffMarathonRunner[15];
  runners[0] = new BanffMarathonRunner("Elena", "Brandon", 341, 1);
  runners[1] = new BanffMarathonRunner("Thomas", "Molson", 273, 2);
  runners[2] = new BanffMarathonRunner("Hamilton", "Winn", 278, 5);
  runners[3] = new BanffMarathonRunner("Suzie", "Sarandin", 329, 7);
  runners[4] = new BanffMarathonRunner("Philip", "Winne", 445, 9);
  runners[5] = new BanffMarathonRunner("Alex", "Trebok", 275, 3);
  runners[6] = new BanffMarathonRunner("Emma", "Pivoto", 275, 4);
  runners[7] = new BanffMarathonRunner("John", "Lenthen", 243, 1);
  runners[8] = new BanffMarathonRunner("James", "Lean", 334, 1);
  runners[9] = new BanffMarathonRunner("Jane", "Ostin", 412, 1);
  runners[10] = new BanffMarathonRunner("Emily", "Car", 393, 4);
  runners[11] = new BanffMarathonRunner("Daniel", "Hamshire", 299, 4);
  runners[12] = new BanffMarathonRunner("Neda", "Bazdar", 343, 3);
  runners[13] = new BanffMarathonRunner("Aaron", "Smith", 317, 6);
  runners[14] = new BanffMarathonRunner("Kate", "Hen", 265, 8);

  // Show the output in console
  System.out.println("Time: " + BanffMarathonRunner.getFastestRunner(runners).getTime());

 }
}




As you can see I do not know how to find the fastest runner.

I was given the following instructions for this question:

public static BanffMarathonRunner getFastestRunner(BanffMarathonRunner[] runners)
public static BanffMarathonRunner getSecondFastestRunner(BanffMarathonRunner[] runners)
public static double getAverageTime(BanffMarathonRunner[] runners)
public static String getAboveAverageRunners(BanffMarathonRunner[] runners)

Thank you for any clarification.

Is This A Good Question/Topic? 0
  • +

Replies To: Return Highest Value of an Array of Objects

#2 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:07 PM

Quote

how to find the fastest runner

What field(s) in the BanffMarathonRunner class would be looked at to determine if one runner is faster than another?
Then compare those field(s) to find the one with the best value.
The code would use a loop to compare each BanffMarathonRunner object to find the fastest one.
Was This Post Helpful? 0
  • +
  • -

#3 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:21 PM

View PostNormR, on 28 July 2017 - 07:07 PM, said:

Quote

how to find the fastest runner

What field(s) in the BanffMarathonRunner class would be looked at to determine if one runner is faster than another?
Then compare those field(s) to find the one with the best value.
The code would use a loop to compare each BanffMarathonRunner object to find the fastest one.


Ok I would look at the time field.

How would I compare this field?

Would this be a for loop, how..
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • I wrote you an code
  • member icon


Reputation: 1237
  • View blog
  • Posts: 4,029
  • Joined: 12-May 09

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:24 PM

How would you do it as a human?
Was This Post Helpful? 2
  • +
  • -

#5 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:27 PM

View Postxclite, on 28 July 2017 - 07:24 PM, said:

How would you do it as a human?


I would look at anything with a > time in the comparison.
Was This Post Helpful? 0
  • +
  • -

#6 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:35 PM

Quote

How would I compare this field?

Since the field is numeric, you can use the arithmetic operators: <, >, == etc to compare two values.
Since the fields are private you will need a getTime method to return the value of the field.

This post has been edited by NormR: 28 July 2017 - 12:36 PM

Was This Post Helpful? 0
  • +
  • -

#7 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:42 PM

public getFastestRunner(BanffMarathonRunner[] runners) {
return getTime(runners);
}

I am getting nearer?
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10684
  • View blog
  • Posts: 18,298
  • Joined: 19-March 11

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:45 PM

You have a bag of pieces of paper, and each one has a number written on it. You are allowed to look at one piece of paper at a time, and you want to find the highest number.

How do you do it?
Was This Post Helpful? 1
  • +
  • -

#9 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:46 PM

No.
You need a loop to go through all the runner objects in the array.
Start with making the loop to access all the objects in the array.
Add a print statement inside the loop that prints the name and time for the current runner being accessed.

When that works, move on to the next step.

This post has been edited by NormR: 28 July 2017 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#10 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:49 PM

I would do if paper > number keep it.
Was This Post Helpful? 1
  • +
  • -

#11 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 12:57 PM

public static getFastestRunner(BanffMarathonRunner[] runners) {

for(int i = 0; i < runners.length; i++) {
System.out.print(i);
if (runners[i] > number)
}
return number
}

Am I getting better?
Was This Post Helpful? 0
  • +
  • -

#12 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 01:00 PM

Ok, that is a start.
Now add this part:

Quote

Add a print statement inside the loop that prints the name and time for the current runner being accessed.


Compile and execute the code and see what is printed.

This post has been edited by NormR: 28 July 2017 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#13 Glasir  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 06-June 17

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 01:07 PM

  
 for(int i = 0; i < runners.length; i++) {
	 number = 0;
	 if (i > number) {
		 System.out.print(runners.getFullName(i));
	 }
 return number;
 }
 } 



I get Cannot invoke getFullName(int) on the array type BanffMarathonRunner[]
Was This Post Helpful? 0
  • +
  • -

#14 NormR  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 644
  • View blog
  • Posts: 4,844
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 01:15 PM

Quote

Cannot invoke getFullName(int) on the array type BanffMarathonRunner[]

No. That is not how to get elements from an array.
Take a look at the tutorial: http://docs.oracle.c...lts/arrays.html
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10684
  • View blog
  • Posts: 18,298
  • Joined: 19-March 11

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 01:17 PM

Runners is not a runner.
Concretely, an egg carton is not an egg. If you want to make fried eggs for you and a bunch of friends, you do not break a carton of eggs, you break each egg in the carton.
Was This Post Helpful? 0
  • +
  • -

  • (9 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »