Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a JAVA project that has code in it to start but no code in the two classes it gives you. I am new to JAVA and really need help on this. I added my code to the Treasure class and now I am getting: Syntax error on tokens, delete these tokens and Syntax error, insert ";" to complete BlockStatements. Why am I getting these errors? What did I miss?


Here is the already Java code:
Java
package mythical.controllers;

import java.text.NumberFormat;

import mythical.model.Treasure;

/**
 * This class is used to informally test the Treasure class.
 * 	NOTE: This class won't compile until the Treasure class
 * 	has been completed correctly
 * 
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * ~~ DO NOT MODIFY THE CODE INSIDE TreasureDemo ~~
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 * @author	Kwesi Hopkins
 * @version	09/12/2016
 *
 */
public class TreasureDemo {
	private Treasure largeTreasure;
	private Treasure smallTreasure;
	private NumberFormat currencyFormatter;	
	
	/**
	 * Creates a new TreasureDemo object with 2 Treasures
	 */
	public TreasureDemo() {
		this.largeTreasure = new Treasure(105);
		this.smallTreasure = new Treasure(10);
		this.currencyFormatter = NumberFormat.getCurrencyInstance();
	}
	
	/**
	 * This method will print information about each Treasure,
	 * 	have each Treasure be claimed, and print information 
	 * 	about each (newly found) Treasure
	 */
	public void testTreasure() {
		this.describeTreasure2("New large treasure", this.largeTreasure, 105, 25452.0);
		
		this.describeTreasure2("New small treasure", this.smallTreasure, 10, 2424.0);
		
		System.out.println();
		System.out.println("~~~~ The treasures get found ~~~~");
		this.largeTreasure.getsFound();
		this.smallTreasure.getsFound();
		
		System.out.println();
		this.describeTreasure2("Found large treasure", this.largeTreasure, 0, 0.0);
		
		this.describeTreasure2("Found small treasure", this.smallTreasure, 0, 0.0);
		System.out.println();
	}
	
	/**
	 * Helper method that accepts a message and the expected value
	 * 
	 * @param	message			The message to be displayed
	 * @param	theTreasure		The treasure to be described
	 * @param	expectedWeight	The treasure's expected weight
	 * @param	expectedValue	The treasure's expected value
	 */
	public void describeTreasure2(String message, Treasure theTreasure, 
			int expectedWeight, double expectedValue) {
		System.out.println(message);
		System.out.println("\tExpected weight: \t" + expectedWeight);
		System.out.println("\tActual weight: \t\t" + theTreasure.getWeight());	
		
		System.out.println("\tExpected value: \t" + this.currencyFormatter.format(expectedValue));
		System.out.println("\tActual value: \t\t" + this.currencyFormatter.format(theTreasure.getValue()));
		System.out.println();
	}
}


Here is the Class:

Java
package mythical.model;

public class Treasure {
	
		private int theTreasure;
		    private int largeTreasure;
		    private int smallTreasure;
		
		 public Treasure() {
		        this.theTreasure = 0;
		        this.largeTreasure = 0;
		        this.smallTreasure = 0;
		        
		        
		    }
		    
		    public Treasure(int largeTreasure){
		        this.largeTreasure = largeTreasure;
		        
		    }
		    
		    public void getLargeTreasure(){
		        this.largeTreasure = this.largeTreasure + 105;
		    }
		    
		    public void getSmallTreasure(){
		        this.smallTreasure = this.smallTreasure + 10;
		    }
		        
		    public int getWeight(){
		        return this.largeTreasure;
		    }
		    
		    public double getValue(){
		        return this.largeTreasure * 242.40;
		    }
		                
		    public int getsFound(){
		        return this.largeTreasure;

		    }

		    public int expectedWeight(){
		        return this.largeTreasure + 105;
		    }
		    
		}
	}
}


Please help me.

What I have tried:

I have tried different ways to start the constructor but no luck. New to JAVA.
Posted
Updated 28-Nov-17 1:11am
v2
Comments
[no name] 13-Sep-16 10:30am    
Please do not expect people to try and teach you Java like they have taught you C# over the years and question after question. Like you have already been told over and over, get yourself a book on java programming and work through it. Learn the material. Then you won't have to come back here over and and over and ask the same questions over and over.

Start here:
Providing Constructors for Your Classes[^]
Constructor in Java - Javatpoint[^]
Java For Complete Beginners - class constructor[^]

Java
public class Treasure {
	int tres;
	public Treasure(int countOftres){
		tres = countOftres;
	}
}
 
Share this answer
 
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

So start by re-reading your course notes and / or the book so far. The information you need will be there - and you will learn this basic stuff a lot better by working it out for yourself than by being given a "solution" with no context.
Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!
 
Share this answer
 
I found out what I did wrong within my code. Here is the corrected code.

Java
public class Treasure {
	private int theTreasure;
    private int largeTreasure;
    private int smallTreasure;

public Treasure(){
    this.theTreasure = 0;
    this.largeTreasure = 0;
    this.smallTreasure = 0;
}
public Treasure(int largeTreasure){
    this.largeTreasure = largeTreasure;
}
public void getLargeTreasure(){
    this.largeTreasure = this.largeTreasure + 105;
}
public void getSmallTreasure(){
    this.smallTreasure = this.smallTreasure + 10;
}
public int getWeight(){
    return this.largeTreasure;
}
public double getValue(){
    return this.largeTreasure * 242.40;
}
public int getsFound(){
    return this.largeTreasure;
}
public int expectedWeight(){
    return this.largeTreasure + 105;
}

}
 
Share this answer
 
Comments
Richard MacCutchan 13-Sep-16 11:37am    
That is not good design. Property getters (e.g. getLargeTreasure) should not be modifying values in the instantiated class. They are supposed to return the current value of the property. Use setters to update values. See The Java Tutorials[^] for some useful training materials.
Maciej Los 13-Sep-16 13:22pm    
Agree!
Computer Wiz99 13-Sep-16 15:06pm    
Yeah, I could use that but I can't. Can't use what we didn't go over. It might not be a good design but i am trying.
Richard MacCutchan 13-Sep-16 15:47pm    
Well if this is what you are being taught then you need to find another school and quickly.

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