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 11 of 11

Thread: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    CSC 275 Lab 7

    Instructor: Joshua L. Smith

    Question Subject Line: CSC 275 Online Lab 7 Question

    Answer Subject Line: CSC 275 Online Lab 7 Solution


    You are to develop an entire program that simulates the way a game is handled in the random creation of its characters. Your program should have the ability to create one of 3 types of character. The choices should be an ogre, archer and a swordsman. Each of these characters will share many of the same types of attributes such as name, life and energy and these should be contained in the Super class that we will call Entity (because Character is already a class). Only the archer and the swordsman are playable, which leaves the ogre to be a Non-Player Character (NPC). This means that the Ogre will be a subclass of the Entity, while the archer and swordsman will be subclasses of Playable which is a subclass of Entity. Below is the diagram.



    The Playable class should extend the Entity class and should contain some additional methods that dictate the movement of the character by the user. Note that we will NOT be tracking the movement of the characters to help simplify the lab. The playable class should have a method for moving the character in the 4 standard directions (no diagonals allowed). So up, down, left and right.

    Class Name: Entity
    Class Level (global) Variables:
    strName - String
    intLife - Integer
    intEnergy - Integer

    Method Name: getStrName
    Parameters: None
    Desired Result: Accessor
    Data Returned: strName

    Method Name: setStrName
    Parameters: name
    Desired Result: Mutator
    Data Returned: none

    Method Name: getIntLife
    Parameters: None
    Desired Result: Accessor
    Data Returned: intLife

    Method Name: setIntLife
    Parameters: life
    Desired Result: Mutator
    Data Returned: none

    Method Name: getIntEnergy
    Parameters: None
    Desired Result: Accessor
    Data Returned: intEnergy

    Method Name: setIntEnergy
    Parameters: energy
    Desired Result: Mutator
    Data Returned: none


    Class Name: Playable
    Method Name: moveUp
    Parameters: None
    Data Returned: “ has moved up one space”

    Method Name: moveDown
    Parameters: None
    Data Returned: “ has moved down one space”

    Method Name: moveLeft
    Parameters: None
    Data Returned: “ has moved left one space”

    Method Name: moveRight
    Parameters: None
    Data Returned: “ has moved right one space”


    Class Name: Ogre
    Method Name: Attack
    Parameters: damageToInflict – Integer, playerToAttack - Entity
    Desired Result: Attack an entity for x amount of damage
    Data Returned: None

    Class Name: Archer
    Method Name: Attack
    Parameters: damageToInflict - In-teger, playerToAttack - Entity
    Desired Result: Attack an entity for x amount of damage
    Data Returned: None

    Class Name: Swordsman
    Method Name: Attack
    Parameters: damageToInflict – Integer, playerToAttack - Entity
    Desired Result: Attack an entity for x amount of damage
    Data Returned: None


    Above is my assignment that I must do.
    --------------------------------------------------------------------------------------------

    Below is the pseudo code that my professor want us to use.

     
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
     
    public class GameDriver {
    	static ArrayList<Entity> myList = new ArrayList<Entity>();
     
    	public static void main(String[] args) {
     
    		Random randomGenerator = new Random();
    		for (int idx = 0; idx <= 5; idx++) {
    			int randomInt = randomGenerator.nextInt(3);
    			Entity temp = new Entity();
    			switch (randomInt) {
    				case 0:
    					temp = new Archer();
    					temp.setStrName("Archer" + idx);
    					//System.out.println(temp.getStrName());
    					break;
    				case 1:
    					temp = new Swordsman();
    					temp.setStrName("Swordsman" + idx);
    					//System.out.println(temp.getStrName());
    					break;
    				default:
    					temp = new Ogre();
    					temp.setStrName("Ogre" + idx);
    					//System.out.println(temp.getStrName());
    					break;
    			}
    			myList.add(temp);
    		}
     
    		while(true){
    			// This leaves the possibility of a player attacking themselves.
    			int randomInt = randomGenerator.nextInt(5);
    			Entity player1 = myList.get(randomInt);
    			System.out.println("Player 1: " + player1.getStrName());
    			randomInt = randomGenerator.nextInt(5);
    			Entity player2 = myList.get(randomInt);
    			System.out.println("Player 2: " + player2.getStrName());
     
     
    			if(player1 instanceof Archer){
    				randomInt = randomGenerator.nextInt(1);
    				if(randomInt == 0){ // Move
    					randomInt = randomGenerator.nextInt(4);
    					switch(randomInt){
    						case 0: System.out.println(player1.getStrName() + ((Archer)player1).moveDown()); break;
    						case 1: System.out.println(player1.getStrName() + ((Archer)player1).moveUp()); break;
    						case 2: System.out.println(player1.getStrName() + ((Archer)player1).moveRight()); break;
    						default: System.out.println(player1.getStrName() + ((Archer)player1).moveLeft()); break;
    					}
    				}else{ // Attack
    					System.out.println("Attacking");
    					randomInt = randomGenerator.nextInt(5);
    					((Archer)player1).attack(randomInt, player2);
    				}
    			}else if(player1 instanceof Swordsman){
    				randomInt = randomGenerator.nextInt(1);
    				if(randomInt == 0){ // Move
    					randomInt = randomGenerator.nextInt(4);
    					switch(randomInt){
    					case 0: System.out.println(player1.getStrName() + ((Swordsman)player1).moveDown()); break;
    					case 1: System.out.println(player1.getStrName() + ((Swordsman)player1).moveUp()); break;
    					case 2: System.out.println(player1.getStrName() + ((Swordsman)player1).moveRight()); break;
    					default: System.out.println(player1.getStrName() + ((Swordsman)player1).moveLeft()); break;
    					}
    				}else{ // Attack
    					System.out.println("Attacking");
    					randomInt = randomGenerator.nextInt(5);
    					((Swordsman)player1).attack(randomInt, player2);
    				}
    			}else{ // Player is an ogre
    				System.out.println("Attacking");
    				randomInt = randomGenerator.nextInt(5);
    				((Ogre)player1).attack(randomInt, player2);
    			}
     
    			Scanner input = new Scanner(System.in);
    			System.out.println("Please enter 1 to continue, 0 to quit.");
    			int keepGoing = input.nextInt();
    			if(keepGoing == 0){
    				break;
    			}
    		}
    	}
    }

    -----------------------------------------------------------------------------------------

    Here is the class I'm currently creating.


     
    public class entity
    {
     
       static String strName;
       static int intLife;
       static int intEnergy;
     
     
       public static void setStrName( String strName )
       {
            strName = strName;
       }
     
       public String getStrName()
       {
        return strName;    
       }
     
       public static void setIntLife( int IntLifejr)
       {
           intLife = IntLifejr;
       }
     
        public int getIntLife()
       {
           return intLife;
       }
     
       public static void setIntEnergy( int intEnergy )
       {
            intEnergy = intEnergy;
       }
     
           public int setIntEnergy()
       {
           return intEnergy;
       }
     
     
     
     
     
     
    }



    I emailed my professor on what exactly he wants me to do with this problem. He is prone for responding to emails late. So I wanted to ask you the community to see what I should do exactly. Or least try to figure out what he wants.

    I see he wants to me to make multiple classes. But I don't know if I should extend my entity class or make nested classes. Any idea guys?

    Thanks again!


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    No, not nested classes. Subclass by definition is a class that is an extension of another super class.

    Class names should be capitalized, but that's what you were told to do in the instructions. Read and follow them carefully.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Rain_Maker (October 19th, 2013)

  4. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    GregBrannon,

    Thanks for the reply. I figured I would do that. I was over thinking it I guess. I'll come back to the post if I have anymore questions.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    Also the class shouldn't have static variables. Each instance of the class will want its own variables.
    This is confusing:
    Class Level (global) Variables:
    Did your prof define that as static?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    Hello everyone,


    I made great progress on this assignment. Here is what I have so far.



     
     
     
    import java.util.ArrayList;
    import java.util.Random;
    import java.util.Scanner;
     
    public class GameDriver {
     
        static ArrayList<Entity> myList = new ArrayList<Entity>();
     
    	public static void main(String[] args) {
     
    		Random randomGenerator = new Random();
    		for (int idx = 0; idx <= 5; idx++) {
     
    		    int randomInt = randomGenerator.nextInt(3);
    			Entity temp = new Entity();
     
    			switch (randomInt) {
    				case 0:
    					temp = new Archer();
    					temp.setStrName("Archer" + idx);
    					//System.out.println(temp.getStrName());
    					break;
    				case 1:
    					temp = new Swordsman();
    					temp.setStrName("Swordsman" + idx);
    					//System.out.println(temp.getStrName());
    					break;
    				default:
    					temp = new Ogre();
    					temp.setStrName("Ogre" + idx);
    					//System.out.println(temp.getStrName());
    					break;
    			}
    			myList.add(temp);
    		}
     
    		while(true){
    			// This leaves the possibility of a player attacking themselves.
    			int randomInt = randomGenerator.nextInt(5);
    			Entity player1 = myList.get(randomInt);
    			System.out.println("Player 1: " + player1.getStrName());
    			randomInt = randomGenerator.nextInt(5);
    			Entity player2 = myList.get(randomInt);
    			System.out.println("Player 2: " + player2.getStrName());
     
     
    			if(player1 instanceof Archer){
     
    			    randomInt = randomGenerator.nextInt(1);
     
    				if(randomInt == 0){ // Move
    					randomInt = randomGenerator.nextInt(4);
    					switch(randomInt){
    	                    case 0: System.out.println(player1.getStrName() + ((Archer)player1).moveDown()); break;
    			    case 1: System.out.println(player1.getStrName() + ((Archer)player1).moveUp()); break;
    		            case 2: System.out.println(player1.getStrName() + ((Archer)player1).moveRight()); break;
    		           default: System.out.println(player1.getStrName() + ((Archer)player1).moveLeft()); break;
    					}
    				}else{ // Attack
    					System.out.println("Attacking");
    					randomInt = randomGenerator.nextInt(5);
    					((Archer)player1).attack(randomInt, player2);
    				}
     
    			}else if(player1 instanceof Swordsman){
     
    			    randomInt = randomGenerator.nextInt(1);
     
    				if(randomInt == 0){ // Move
    					randomInt = randomGenerator.nextInt(4);
     
    					switch(randomInt)
    					{
    			case 0: System.out.println(player1.getStrName() + ((Swordsman)player1).moveDown());   break;
    			case 1: System.out.println(player1.getStrName() + ((Swordsman)player1).moveUp()); break;
    			case 2: System.out.println(player1.getStrName() + ((Swordsman)player1).moveRight()); break;
    			default: System.out.println(player1.getStrName() + ((Swordsman)player1).moveLeft()); break;
    					}
     
    				}
    				else{ // Attack
    					System.out.println("Attacking");
    					randomInt = randomGenerator.nextInt(5);
    					((Swordsman)player1).attack(randomInt, player2);
    				}
     
    			}else{ // Player is an ogre
    				System.out.println("Attacking");
    				randomInt = randomGenerator.nextInt(5);
    				((Ogre)player1).attack(randomInt, player2);
    			}
     
    			Scanner input = new Scanner(System.in);
    			System.out.println("Please enter 1 to continue, 0 to quit.");
    			int keepGoing = input.nextInt();
     
    			if(keepGoing == 0){
    				break;
    			}
    		}
    	}
    }
     
     
     
     
     
     
    public class Entity
    {
     
       static String strName;
       static int intLife;
       static int intEnergy;
     
     
       public static void setStrName( String strName )
       {
            strName = strName;
       }
     
       public String getStrName()
       {
        return strName;    
       }
     
       public static void setIntLife( int IntLifejr)
       {
           intLife = IntLifejr;
       }
     
        public int getIntLife()
       {
           return intLife;
       }
     
       public static void setIntEnergy( int intEnergy )
       {
            intEnergy = intEnergy;
       }
     
           public int setIntEnergy()
       {
           return intEnergy;
       }
     
     
     
     
     
     
    }
     
    public class Player extends Entity
     
    {
     
            public String moveUp()
        {
            String moveUpString = " Has moved up one space. ";
            return moveUpString ;
     
        }
     
           public String moveDown()
        {
            String moveDownString = " Has moved down one space. ";
            return moveDownString;
     
        }
     
            public String moveLeft()
        {
            String moveLeftString = " Has moved left one space. ";
            return moveLeftString;
        }
     
               public String moveRight()
        {
            String moveRightString = " Has moved right one space. ";
            return moveRightString;
        }
     
     
    }

    I'm waiting for a response from my teacher and class mates. But I wanted your guys opinion. Please look below.

    Class Name: Ogre
    Method Name: Attack
    Parameters: damageToInflict – Integer, playerToAttack - Entity
    Desired Result: Attack an entity for x amount of damage
    Data Returned: None

    Class Name: Archer
    Method Name: Attack
    Parameters: damageToInflict - Integer, playerToAttack - Entity
    Desired Result: Attack an entity for x amount of damage
    Data Returned: None

    Class Name: Swordsman
    Method Name: Attack
    Parameters: damageToInflict – Integer, playerToAttack - Entity
    Desired Result: Attack an entity for x amount of damage
    Data Returned: None


    What in the heck is going with these parameters for these character methods? I'm not familiar with the syntax. It looks like he is performing operations in my parameters and then returning them. Any idea what might be going on?

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    It looks like he is performing operations in my parameters . . .
    What are you talking about? Who is 'he', and what is 'he' doing?

  8. #7
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    My professor GregBrannon,

    Sorry maybe I wasn't clear. If you look at the parameter row for each method i.e ogre, archer, swordsman you will see that it looks like he(my professor) is performing operations. Instead of performing the operation in the body it looks like its being done in the parameters of the methods. Does that make sense now? : )

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    I admit stupidity and concede. Doesn't matter.

  10. #9
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    122
    My Mood
    Amazed
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    Quote Originally Posted by GregBrannon View Post
    I admit stupidity and concede. Doesn't matter.
    What do you mean?

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    It meant I didn't understand, didn't want you to waste your time explaining it to me, and that I was going to sleep. Well, the last part wasn't communicated, but that was the reason. I've reread:
    What in the heck is going with these parameters for these character methods? I'm not familiar with the syntax. It looks like he is performing operations in my parameters and then returning them. Any idea what might be going on?
    hoping for total enlightenment, but it's not happening. It sounds like you're confused about something your professor is doing, but I don't understand the mechanism or the context. Please point to the code you're confused about or describe the process that causes this transformation resulting in your confusion. Either there's something you're not telling us, or I'm being thick and not seeing the obvious.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help understanding a Lab. Don't know if I should make a nested class or extend the class itself.

    It help if you would copy the full contents of the console from when you execute the program and paste it here.
    Add some comments following the output describing what is wrong with it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Class loader wont let me extend classes...?
    By sci4me in forum Java Theory & Questions
    Replies: 9
    Last Post: May 19th, 2013, 12:47 PM
  2. Replies: 2
    Last Post: November 18th, 2012, 02:09 PM
  3. need to make basic class and implementation class (base class without void main)
    By javanewbie101 in forum Object Oriented Programming
    Replies: 1
    Last Post: September 19th, 2012, 08:03 PM
  4. Don't know how to structurate a class.
    By diesel in forum Object Oriented Programming
    Replies: 0
    Last Post: May 29th, 2012, 12:50 PM
  5. [SOLVED] How to make a class be static if it's the main class.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 10th, 2012, 05:14 AM