0

Following the Oracle Acadamy Java Fundamentals and I walked into a wall(I dont understand how its explained in the acadamy)

The Try It/Solve It:

  1. Create a method nicestFish that takes in two fish as parameters, compares the friendliness level of two fish, and returns the fish with the higher friendliness. Test this method with the fish defined in problem 6.

Main:

public class index { public static void main(String[] args) { Fish Amber = new Fish("AngelFish", 5); Fish James = new Fish("Guppy", 3); } } 

Class:

public class Fish { String typeOfFish; int friendliness; public Fish(){ typeOfFish = "unknown"; friendliness = 3; } public Fish(String t, int f) { this.typeOfFish = t; this.friendliness = f; } public int getFriendliness(){ return friendliness; } } 
8
  • 3
    So, what have you tried? Have you at least tried declaring the nicestFish method? What do you think it should do? Commented Oct 8, 2015 at 18:36
  • Are you looking for compareTo method ? Commented Oct 8, 2015 at 18:37
  • Im not sure how to proceed with making the method for comparinson. I dont know how to call/get the value of the friendliness value from Amber and James. I do know how to use compareto Commented Oct 8, 2015 at 18:39
  • So you're in problem 7 of some kind of tutorial and "it"/"they" never showed you how to access fields of an object? Commented Oct 8, 2015 at 18:41
  • You get the friendliness of a fish by saying "fish, get friendliness" in Javaese: fish.getFriendliness() Commented Oct 8, 2015 at 18:42

1 Answer 1

3

I'm guessing they want you to make a static comparison method, since they specify it takes two Fish as arguments

public class Fish { ... public static Fish nicestFish(Fish f1, Fish f2) { //find out which fish is nicest, and return it } ... } 

To call a static method, you do not need an object reference:

Fish fish1 = ... Fish fish2 = ... Fish nicest = Fish.nicestFish(fish1, fish2); 
Sign up to request clarification or add additional context in comments.

1 Comment

Good start, and great teaching tool of not providing full solution. Thumbs Up (and vote up).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.