Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Not getting the right outputs.

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not getting the right outputs.

    I'm making an armor class. I also made a test for it to ensure it works, but something seems to be wrong. I'm not getting the expect outputs when I ask it to give me a println of the variables.

    This is the armor class:

    import java.util.Random;
    public class Armor
    {
    //fields
     
    CharacterRace charRace;
    String armorName;
    public int cost;
    public int armorBonus;
    public int armorCheckPenalty;
    public int arcaneSpellFailChance;
    public int speedFast;
    public int speedSlow;
    public int weight;
     
    //initialize
    public Armor(String armorName, int cost, int armorBonus, int armorCheckPenalty, int arcaneSpellFailChance, int speedFast, int speedSlow, int weight){
    this.armorName=armorName;
    cost=cost;
    armorBonus=armorBonus;
    armorCheckPenalty=armorCheckPenalty;
    arcaneSpellFailChance=arcaneSpellFailChance;
    speedFast=speedFast;
    speedSlow=speedSlow;
    weight=weight;
    }
    public int getSpeed(CharacterRace charRace)
    {
    int total=0;
    if(charRace==CharacterRace.HUMAN){
    total=speedFast;
    }else if(charRace==CharacterRace.ELF){
    total=speedFast;
    }else if(charRace==CharacterRace.HALFELF){
    total=speedFast;
    }else if(charRace==CharacterRace.HALFORC){
    total=speedFast;
    }else if(charRace==CharacterRace.DWARVE){
    total=speedSlow;
    }else if(charRace==CharacterRace.GNOME){
    total=speedSlow;
    }else if(charRace==CharacterRace.HALFLING){
    total=speedSlow;
    }
    return total;
    }
    public boolean arcaneSpellFailed()
    {
    Random rand= new Random ();
    int spellnumber=rand.nextInt(100)+1;
    boolean spellsuccess=true;
    if(spellnumber<arcaneSpellFailChance){
    spellsuccess=false;
    }
    return spellsuccess;
    }
     
    //methods
    }

    This is the armor test:

    public class ArmorTester
    {
    public static void main(String[] args)
    {
    Armor myLeather = new Armor( "Leather", 10, 2, 0, 10, 30, 20, 15);
    Armor myChainmail = new Armor( "Chainmail", 150, 6, 5, 30, 20, 15, 40);
    Armor myPlate = new Armor( "Full Plate", 1500, 9, 6, 35, 20, 15, 50);
    System.out.println(myLeather.getSpeed(CharacterRace.HUMAN));
    System.out.println(myChainmail.getSpeed(CharacterRace.HALFORC));
    System.out.println(myPlate.getSpeed(CharacterRace.GNOME));
    System.out.println( myLeather.arcaneSpellFailed());
    System.out.println( myChainmail.arcaneSpellFailed());
    System.out.println( myPlate.arcaneSpellFailed());
    }
    }

    And this is the CharacterRace class enum:

    public enum CharacterRace
    {
        HUMAN, ELF, HALFELF, HALFORC, DWARVE, GNOME, HALFLING
    }//end enum

    And lastly, this is what the armor test prints out:

    0
    0
    0
    true
    true
    true

    I never get false (I should sometimes) and those 0s should be

    30
    30
    15

    What's wrong with my code?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Not getting the right outputs.

    The println()s should include an ID String so you can find which println() printed which line of output:
    System.out.println("theVar= "+ theVar);

    BTW The posted code has lost its formatting. Nested statements are not indented which makes the code hard to read.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not getting the right outputs.

    I'm assuming you mean change the test, so as to understand what variables are being shown. I got this as my output

    Leather Speed: 0
    Chainmail Speed: 0
    Full Plate Speed: 0
    Leather arcane spell succuss: true
    Chainmail arcane spell succuss: true
    Full Plate arcane spell succuss: true

    Still shouldn't be the case.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Not getting the right outputs.

    The posted code has lost its formatting. Nested statements are not indented which makes the code hard to read.
    Can you fix the formatting of the code?

    Try debugging the code by adding some println statements that print out the values of the variables used in the if statements and passed to the method.

    Also add an else at the end of the if/else if chain that prints out a message saying that none of the preceding if statements were true.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Not getting the right outputs.

    Your mistake is that your constructor's parameter names are the same as your class variable names. Java looks at variables with the closest scope. So when you say:
    cost = cost;
    You are assigning the parameter variable to itself. You are NOT modifying the class variable. To modify the class variable, use the "this" keyword:
    this.cost = cost;

    Also, fun fact: since you are using Enums in your getSpeed() method, you can actually clean it up quite nicely using something called a switch statement.
    Here is a comparison of your code:
    public int getSpeed(CharacterRace charRace)
    {
    	int total=0;
    	if(charRace==CharacterRace.HUMAN){
    		total=speedFast;
    	}else if(charRace==CharacterRace.ELF){
    		total=speedFast;
    	}else if(charRace==CharacterRace.HALFELF){
    		total=speedFast;
    	}else if(charRace==CharacterRace.HALFORC){
    		total=speedFast;
    	}else if(charRace==CharacterRace.DWARVE){
    		total=speedSlow;
    	}else if(charRace==CharacterRace.GNOME){
    		total=speedSlow;
    	}else if(charRace==CharacterRace.HALFLING){
    		total=speedSlow;
    	}
    	return total;
    }
    verse a switch statement which produces the same result:
    public int getSpeed(CharacterRace charRace) {
    	int total = 0;
    	switch(charRace) {
    		case HUMAN:
    		case ELF:
    		case HALFELF:
    		case HALFORC:
    			total = speedFast;
    			break;
    		case DWARVE:
    		case GNOME:
    		case HALFLING:
    			total = speedSlow;
    			break;
    	}
    	return total;
    }
    The main advantage to the switch statement is that it allows you to compress all of those if/else clauses into a quick and easy-to-read list of condition statements.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. MuliThreading Unable to get outputs but many NPES.....
    By tanchiwoo in forum What's Wrong With My Code?
    Replies: 12
    Last Post: February 17th, 2014, 01:18 PM
  2. Help, text reader outputs high count value!
    By Lashickk in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 31st, 2013, 09:14 PM
  3. Why do I get 0 in the outputs?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2012, 12:00 PM
  4. Code only outputs 1's
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 27th, 2012, 09:26 PM
  5. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 04:57 AM