Return Highest Value of an Array of Objects

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

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

#31 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 - 05:30 PM

Do you mean that I should have a for loop in my main?
Was This Post Helpful? 0
  • +
  • -

#32 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 05:43 PM

The loop would be where you are going to do the search for the best time.
Putting it in a separate method like many of your posts show would be ok.
Was This Post Helpful? 0
  • +
  • -

#33 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 - 05:53 PM

Should I put

for(int i = 0; i < runners.length; i++){
	 BanffMarathonRunner runner = runners[i]; 
	 System.out.println();
	 } 


inside another for loop?
Was This Post Helpful? 0
  • +
  • -

#34 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 05:55 PM

No, that is the loop that can be used to do the search.
Now add statements inside the loop to get the name and the time from the runner object.

What is the empty println() statement for? Why print 15 blank lines?
Was This Post Helpful? 0
  • +
  • -

#35 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 - 06:05 PM

I added

System.out.println(getFullName());



to have

Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Kate Hen
Fastest runner: Kate Hen 265
Was This Post Helpful? 0
  • +
  • -

#36 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 06:13 PM

There are 15 runner objects in the array. The loop should access each one in turn and use the runner variable to access each objects contents.
For example:
 System.out.println("year="+runner.getYear()); //  show this runner object's year
  


Now do that for the name and time.

This post has been edited by NormR: 28 July 2017 - 06:14 PM

Was This Post Helpful? 0
  • +
  • -

#37 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 - 06:21 PM

Thats what I don't know how to do

 
 public static String getFastestRunner(BanffMarathonRunner[] runners) {
	 String name = ""; 
	 
	 for(int i = 0; i < runners.length; i++){
	 BanffMarathonRunner runner = runners[i]; 
	 System.out.println(getFullName() + " year = " + runner.getYear());
	 }
	 return name;
	 }



I'm not looping through each runner, how would I do that?
Was This Post Helpful? 0
  • +
  • -

#38 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 06:27 PM

Quote

I'm not looping through each runner, how would I do that?

Yes the code loops through all the runners in the array and prints the year for each runner.
Change the code so it prints the name and the time for each runner just like the year is accessed and printed.
Was This Post Helpful? 0
  • +
  • -

#39 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 - 06:35 PM

View PostNormR, on 29 July 2017 - 01:27 AM, said:

Quote

I'm not looping through each runner, how would I do that?

Yes the code loops through all the runners in the array and prints the year for each runner.
Change the code so it prints the name and the time for each runner just like the year is accessed and printed.


I have the name and time only for one runner

System.out.println(getFullName() + " Time = " + runner.getTime() + 
			 " Year = " + runner.getYear());



Gives me

Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Kate Hen Time = 265 Year = 8
Fastest runner: Kate Hen 265
Was This Post Helpful? 0
  • +
  • -

#40 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 06:51 PM

Please post all the code so we can see what the program is.
Was This Post Helpful? 0
  • +
  • -

#41 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 - 06:58 PM

public class BanffMarathonRunner {

 // Instance Fields
 private static String firstName;
 private static String lastName;
 private static int time;
 private static 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 static String getFullName() {
  return firstName + " " + lastName;
 }

 public String getFirstName() {
  return firstName;
 }

 public String getLastName() {
  return firstName;
 }

 public int getYear() {
  return year;
 }

 public static int getTime() {
  return time;
 }

 public static String getFastestRunner(BanffMarathonRunner[] runners) {
	 String name = ""; 
	 
	 for(int i = 0; i < runners.length; i++){
	 BanffMarathonRunner runner = runners[i]; 
	 System.out.println(getFullName() + " Time = " + runner.getTime() + 
			 " Year = " + runner.getYear());
	 }
	 return name;
	 }

 // 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("Fastest runner: " + BanffMarathonRunner.getFastestRunner(runners)
  + BanffMarathonRunner.getFullName() + " " + BanffMarathonRunner.getTime());

 }
}



Was This Post Helpful? 0
  • +
  • -

#42 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 07:08 PM

Remove all the static modifiers for the fields in the class. and for all the get methods
Static means there is only one value for all objects of the class. You want each object to have its own value

This post has been edited by NormR: 28 July 2017 - 07:09 PM

Was This Post Helpful? 0
  • +
  • -

#43 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 - 07:19 PM

Ok I have done this, although the reason it was there is I get errors now.

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 int getTime() {
  return time;
 }

 public static String getFastestRunner(BanffMarathonRunner[] runners) {
	 String name = ""; 
	 
	 for(int i = 0; i < runners.length; i++){
	 BanffMarathonRunner runner = runners[i]; 
	 System.out.println(getFullName() + " Time = " + runner.getTime() + 
			 " Year = " + runner.getYear());
	 }
	 return name;
	 }

 // 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("Fastest runner: " + BanffMarathonRunner.getFastestRunner(runners)
  + BanffMarathonRunner.getFullName() + " " + BanffMarathonRunner.getTime());

 }
}


Was This Post Helpful? 0
  • +
  • -

#44 NormR  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 635
  • View blog
  • Posts: 4,824
  • Joined: 25-December 13

Re: Return Highest Value of an Array of Objects

Posted 28 July 2017 - 07:28 PM

Copy the error messages and paste them here.
I am done for tonight. Back tomorrow
Was This Post Helpful? 0
  • +
  • -

#45 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 - 07:33 PM

Thank you for your patience!

BanffMarathonRunner.java:46: error: non-static method getFullName() cannot be referenced from a static context
	 System.out.println(getFullName() + " Time = " + runner.getTime() + 
	                    ^
BanffMarathonRunner.java:93: error: non-static method getFullName() cannot be referenced from a static context
  + BanffMarathonRunner.getFullName() + " " + BanffMarathonRunner.getTime());
                       ^
BanffMarathonRunner.java:93: error: non-static method getTime() cannot be referenced from a static context
  + BanffMarathonRunner.getFullName() + " " + BanffMarathonRunner.getTime());


Was This Post Helpful? 0
  • +
  • -

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