Click here to Skip to main content
15,887,436 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Java project I am working on and I get this error:

The method findTreasure(Treasure) is undefined for the type Leprechaun.

I am trying to find out why am I getting this when I do have it in my code? Can some please look this over for me and let me know where I goofed? Here is my code:

Java
package mythical.controllers;

import java.text.NumberFormat;

import mythical.model.Leprechaun;
import mythical.model.Treasure;

/**
 * This is the driver for the informal test application
 * 
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * ~~ DO NOT MODIFY THE CODE INSIDE TestDriver ~~
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 * @author	Kwesi Hopkins
 * @version	9/12/2016
 *
 */
public class LeprechaunDemo {
	private Treasure treasurePail;
	private Treasure treasureChest;
	private Leprechaun patti;
	private NumberFormat currencyFormatter;

	/**
	 * Creates a new LeprechaunDemo object with 2 Treasure
	 * 	objects and an instance of a Leprechaun
	 */
	public LeprechaunDemo() {
		this.treasurePail = new Treasure(48);
		this.treasureChest = new Treasure(250);
		this.patti = new Leprechaun(100.0);
		this.currencyFormatter = NumberFormat.getCurrencyInstance();
	}
	
	/**
	 * Runs a set of tests for the Leprechaun's behavior
	 */
	public void testLeprechaun() {
		this.describeLeprechaun("Patti's initial value", 100.00);
		
		System.out.println("Patti says:");
		System.out.println("\tExpected:\t\"Top o' the mornin to ya!\"");
		System.out.println("\tActual:\t\t\"" + this.patti.getGreeting() + "\"");
		System.out.println();
		
		this.patti.findTreasure(this.treasurePail);
		this.describeLeprechaun("Patti's value after finding the small treasure", 11735.2);
		System.out.println("Small treasure's value after being found:");
		System.out.println("\tExpected:\t$0.00");
		System.out.println("\tActual:\t\t" + this.currencyFormatter.format(this.treasurePail.getValue()));
		System.out.println();
		
		this.patti.meetPerson();
		this.describeLeprechaun("Patti's value after giving away first time", 7627.88);
		
		this.patti.meetPerson();
		this.describeLeprechaun("Patti's value after giving away second time", 4958.122);
		
		this.patti.findTreasure(this.treasureChest);
		this.describeLeprechaun("Patti's value after finding the large treasure", 65558.122);
		System.out.println("Large treasure's value after being found:");
		System.out.println("\tExpected:\t$0.00");
		System.out.println("\tActual:\t\t" + this.currencyFormatter.format(this.treasureChest.getValue()));
	}
	
	/**
	 * Helper method that accepts a message and the expected value
	 * 
	 * @param	message			The message to be displayed
	 * @param	expectedValue	The expected value
	 */
	public void describeLeprechaun(String message, double expectedValue) {
		System.out.println(message);
		System.out.println("\tExpected: \t" + this.currencyFormatter.format(expectedValue));
		System.out.println("\tActual: \t" + this.currencyFormatter.format(this.patti.getAssetValue()));
		System.out.println();
	}
}


Here is the class code:

Java
package mythical.model;

public class Leprechaun{
    private int treasurePail;
    private int treasureChest;
    private double patti;

    public Leprechaun(){
        
        this.treasurePail = 0; 
        this.treasureChest = 0; 
        this.patti = 0;
    }
    
    public Leprechaun(double patti){
        this.patti = patti;
    }
    
    public void chooseTreasurePail(){
        
        this.treasurePail = this.treasurePail + 48;
    }
    
    public void chooseTreasureChest(){
        this.treasureChest = this.treasureChest + 250; 
    }
    
    public void choosePatti(){
        this.patti = this.patti + 100.00;
    }

    public String getGreeting() {
    	return "Top o' the mornin to ya!";
    }
    
    public double getAssetValue(){
    	return this.patti;
    }

    public int meetPerson(){
    	return this.treasurePail;
    }
}


What I have tried:

I have searched my code up and down to find my mistakes.
Posted
Updated 13-Sep-16 9:14am

1 solution

Your error is pretty clear. Leprechaun does not contain a method called findTreasure. In your leprechaun class you have the following methods

C#
public void chooseTreasurePail
   public void chooseTreasureChest
   public void choosePatti
   public String getGreeting
   public double getAssetValue
   public int meetPerson



There is no findTreasure method in your Leprechaun class. If you were to add

Java
public void findTreasure(Treasure yourparam)



Then your error should go away.
 
Share this answer
 
v3
Comments
Computer Wiz99 13-Sep-16 15:49pm    
David_Wimbley, Thanks for the help but I am sill getting the same error.
[no name] 13-Sep-16 16:24pm    
If you believe his code, the findTreasure method would be passed a Treasure object not a double.
David_Wimbley 13-Sep-16 18:56pm    
I was hoping common sense would prevail but you are correct. I've updated my answer.
[no name] 13-Sep-16 20:54pm    
Common sense in QA? Don't be ridiculous. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900