Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How would I implement my classes into my program?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Benner




PostPosted: Wed Apr 10, 2013 9:01 pm   Post subject: How would I implement my classes into my program?

Hi. For a school project I was instructed to create a class(s) that contain methods and variables relating to a game, for example everything from appearance of a character, to stats such as that character's strength, health, etc. In my program I created two classes (class1 and class2) that have all of the methods and variables I need. I made another class that creates a swing interface (swingclass) to display the information and change it etc through methods in the two classes. Once I finish the code for swingclass I need to be able to open the interface in which it is able to interact with all the methods and variables in the 2 other classes.

My dilemma is I don't know what to do in terms of using my classes to create the program. I need my swingclass to have access to class1 and class2's methods. I need class1 and class2 to have access to eachother's methods. Right now my main method in my main class is empty and I don't know what to do. Any help would greatly be appreciated... thanks.

If you are familiar with RPG games, this will be helpful I assume. Class1 contains methods and variables about the character (name, gender, inventory, level, xp etc) while class2 contains methods and variables about the character's class (stats like strength, stamina, health). These two classes need to use eachother's methods. On top of this my guiclass needs to be able to use all of the methods in both of the classes.

Any help at all would greatly be appreciated. I'm sorry if my code looks like a mess. I'm think I should just paste the important bits but then again I'm not sure which parts are important for what I need to do.

MainClass
code:
package chapter7project;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;


public class Chapter7project {
   
    public static void Message(String message){     //pop up message
        JOptionPane.showMessageDialog(null,message);
    }
   
    public static void main(String[] args) {
        newCharacterSwing gui = new newCharacterSwing();
        character character = new character();
       
       
        Message("");
        Message("");
        Message("");
       
       
       }
       
    }



Class1
code:
package chapter7project;
import java.util.Scanner;

public class character {
   Scanner input = new Scanner(System.in);
   String name;
   boolean gender;
   int level;
   int xp;
   String[] inventory = new String[10];
   double[] xpToLvl = new double[90];
   Warrior charname;
   int killBoar;
   int damage;
   int goldLoot, baconLoot;
   int totalFood, totalGold;
   
   public character(){   
       level = 1;
       xp = 0;
       xpToLvl[1] = 50;
       charname = new Warrior();
       killBoar = 20; //xp given when you kill boar
       
       for (int i = 2; i <= 90; i++){
           xpToLvl[i-1] += xpToLvl[i-1]*0.2;
       }
   }
   
   public void buyBacon(){
       if (totalGold > 10){
           totalGold = totalGold -10;
           totalFood++;
       }
   }
   
   public void eatFood(){
       if (totalFood > 0){
           totalFood--;
           charname.changeHealth((int)(charname.maxHealth*0.15));
       }
   }
   
   public void changeFood(int amount){
       totalFood += amount;
   }
   
   public int getFood(){
          return(totalFood);
      }
   
   public void changeName(String name){
       this.name = name;
   }
   
   public String getName(){
       return(name);
   }
   
   public void changeGender(boolean gender){
       this.gender = gender;
   }
   
   public boolean getGender(){
       return(gender);
   }
   
   public void levelUp(){
       int remainder;
       if (xp >= (int)xpToLvl[level]){
           remainder = (xp - (int)xpToLvl[level]);
           xp = 0 + remainder;
           level++;
           charname.changeStrength((int)(charname.getStrength()*0.2));
           charname.changeCrit((int)(charname.getCrit()*0.2));
           charname.changeStamina((int)(charname.getStamina()*0.2));
           charname.maxHealth = ((int)(charname.maxHealth*0.2));
           charname.health = charname.maxHealth;
           
           System.out.println("Congratulations! You have reached level "+level+"!");
           System.out.println("Your Stength increased by "+(int)(charname.getStrength()/1.2)+"!");
           System.out.println("Your Stamina increased by "+(int)(charname.getStamina()/1.2)+"!");
           System.out.println("Your Critical Strike Chance increased by "+(int)(charname.getCrit()/1.2)+"!");
           System.out.println("Your Health increased by "+(int)(charname.getHealth()/1.2)+"!");
       }
                 
    }
   public void addItem(){
       int i;
       String addItemID = input.nextLine();
       for (i = 0; i <= 11; i++){
              if (inventory[i].equals("Empty")){
                  break;
              }
          }
       if (i == 11){
           System.out.println("There is no room in your bags.");
       } else {
          inventory[i] = addItemID;
       }
   }
   
   public void removeItem(){
       String removeItemID = input.nextLine();
       int i;
       for (i = 0; i <= 11; i++){
           if (inventory[i].equals(removeItemID)){
               inventory[i] = "Empty";
               System.out.println("Item removed from your bag.");
               break;
           }
       }
       if (i == 11){
           System.out.println("Item not found in bags.");
       }
   }
   
   public void killBoar(){
       xp += (xpToLvl[level]/5);
       damage = (int)(((charname.getHealth()/5)- 5 + 1)*Math.random()+5);   //damage between 20% base health and 5 damage.
       charname.changeHealth((-1)*(damage));
       goldLoot = (int)((10-5+1)*Math.random()+5);
       baconLoot = (int)((1-1+1)*Math.random()+1);
   }
   
 } //end class


Class2
code:
package chapter7project;


public class Warrior {
   double strength;
   double crit;
   double stamina;
   double health;
   double maxHealth;
   double spec; // 0,1,2 arms, prot, fury
 
   
   public Warrior(){
       strength = 10;
       crit = 10;
       stamina = 10;
       health = 100;
       maxHealth = 100;
   }
   
   public void changeStrength(int amount){
       strength += amount;
   }
 
   public void changeStamina(int amount){
       stamina = stamina + amount;
   }
   
    public void changeCrit(int amount){
       crit = crit + amount;
   }
 
   public void changeHealth(int amount){
      health = health + amount;
   }
   
   public void changeSpec(int spec){
       this.spec = spec;
   }
   
   public int getStrength(){
       return((int)strength);
       
   }
    public int getStamina(){
       return((int)stamina);
       
   }
     public int getCrit(){
       return((int)crit);
       
   }
      public int getHealth(){
       return((int)health);
   }
     
      public int getSpec(){
          return((int)spec);
      }
     
   
     
} //end class


SwingInterfaceClass
code:
package chapter7project;
import javax.swing.*;
import java.awt.event.*;

/*
 * p = prompt/label
 * b = button
 * t = textfield
 * r = radiobutton
 *
 * (component)(window)(number on window)
 *
 * E.g. b13 = (button 3 on window 1)
 */

/* ------Prompts/labels------
* P11(title)  P12(name) P13(gender)               
*
* P21(title) P22(hair) P23(hair colour) P24(beard/earrings)
*
* P31(title) p32(name) p33(health) p34(level) p35(xp/xpToLevel) p36(spec)
* p37(strength) p38(stamina) p39(crit)
* p41,p42,p43,p44,p45,p46 etc... for bags
* p51(Level up!) p52(you have reached level ...) p53(health) p54(strength) p55(stam) p56(crit)
* p61(you killed boar, loot)
* P71(title) p72(name) p73(health) p74(level) p75(xp/xpToLevel) p76(spec)
* p77(strength) p78(stamina) p79(crit)
* p81,p82,p83,p84,p85,p86 etc... for cheatBags
* p91(buying bacon) p92(gold)
*/

/*--------Buttons--------
* b11(next)
* b21(next)
* b31(bags) b32(kill boar) b33(eat food) b34(cheat mode) b35(quit)
*  b41(drop item) b42(close bags)
* b51(okay)
* b61(loot bacon) b62(loot gold) b63( close)
* b71(update stats) b72(bags) b73(normal mode) b74(quit)
* b81(add item) b82(remove item) b83(close)
* b91(buy bacon)
*/

/*------TextFields------
 * t11(enter name)
 *
 *
 * t41(drop item)
 *
 *
 * t71(name) t72(health) t73(level) t74(spec) t75(strength) t76(stamina) t77(crit)
 * t81(add item) t82(remove item)
 */

/*-----Radio Buttons------
 * r11(gender)
 * r21(hair) r22(colour) r23(beard/earrings)
 * r31(spec)
 */

/*
 * 1. new character: title, name, gender
 * 2. new character: title, hair, hair colour, beard/earrings
 * 3. Main: stats, health, level, xp, etc
 * 4. bags(main)
 * 5. level up!
 * 6. loot boar
 * 7. cheat mode
 * 8. cheat mode bags
 * 9. shop
 */

public class newCharacterSwing implements ActionListener {
   JFrame newCharF1, normalF2, manualF3, bagF4, levelupF5, lootF6, cheatF7, cheatBagsF8, shopF9;
   JPanel newCharP1, normalP2, manualP3, bagP4, levelupP5, lootP6, cheatP7, cheatBagsP8, shopP9;
   
   JLabel p11,p12,p13;   
   JLabel p21, p22, p23, p24;
   JLabel p31, p32, p33, p34, p35, p36, p37, p38, p39;
   JLabel p41, p42, p43, p44, p45, p46, p47, p48, p49, p410;
   JLabel p51, p52, p53, p54, p55, p56;
   JLabel p61;
   JLabel p71, p72, p73, p74, p75, p76, p77, p78, p79;
   JLabel p81, p82, p83, p84, p85, p86, p87, p88, p89, p810;
   JLabel p91, p92;
   
   JButton b11;
   JButton b21;
   JButton b31, b32, b33, b34, b35;
   JButton b41, b42;
   JButton b51;
   JButton b61, b62, b63;
   JButton b71, b72, b73, b74;
   JButton b81, b82, b83;
   JButton b91;
   
   JTextField t11;
   JTextField t41;
   JTextField t71, t72, t73, t74, t75, t76, g77;
   JTextField t81, t82;
   
   JRadioButton r11;
   JRadioButton r21, r22, r23;
   JRadioButton r31;
   
   String beardOrEarrings;
   
   character character = new character();
   
           
    public newCharacterSwing(){
    newCharF1 = new JFrame("Create a new Character");
    normalF2 = new JFrame("Create a new Character");
    manualF3 = new JFrame("Create a new Character");
    bagF4 = new JFrame("Bags");
    levelupF5 = new JFrame("You have leveled up!");
    lootF6 = new JFrame("You killed the boar!");
    cheatF7 = new JFrame("Cheat Mode (Main Menu)");
    cheatBagsF8 = new JFrame("Cheat Mode (Bags)");
   
    newCharP1 = new JPanel();
    normalP2 = new JPanel();
    manualP3 = new JPanel();
    bagP4 = new JPanel();
    levelupP5 = new JPanel();
    lootP6 = new JPanel();
   
    p11 = new JLabel("Create new character (Step 1 of 2)");
    p12 = new JLabel("Name:");
    p13 = new JLabel("Gender");
   
    p21 = new JLabel("Create new character (Step 2 of 2)");
    p22 = new JLabel("Hair Style");
    p22 = new JLabel("Hair Colour");
    p24 = new JLabel(beardOrEarrings);  //make sure to take care of this
   
    p31 = new JLabel("Main Menu"); //P31(title) p32(name) p33(health) p34(level) p35(xp/xpToLevel) p36(spec)
    p32 = new JLabel("--"+character.getName()+"--");
    p33 = new JLabel("");
    p34 = new JLabel("");
    p35 = new JLabel("");
    p36 = new JLabel("");
    p37 = new JLabel("");
    p38 = new JLabel("");
    p39 = new JLabel("");
   
    p41 = new JLabel("Bacon");
    p42 = new JLabel("Bacon");
    p43 = new JLabel("Bacon");
    p44 = new JLabel("Bacon");
    p45 = new JLabel("Bacon");
    p46 = new JLabel("Empty");
    p47 = new JLabel("Empty");
    p48 = new JLabel("Empty");
    p49 = new JLabel("Empty");
    p410 = new JLabel("Empty");
   
    p61 = new JLabel("You killed the boar. you took x damage, you gained x xp. ");
   
}
    }

Sponsor
Sponsor
Sponsor
sponsor
Panphobia




PostPosted: Wed Apr 10, 2013 9:43 pm   Post subject: RE:How would I implement my classes into my program?

Wouldn't it be easier to make the character class abstract, just wondering that's how I'd do it, make Character class abstract and then Warrior class would extend it, and you could just call the super class in the warrior constructor, and by the way what have you already learned about OOP?
Benner




PostPosted: Wed Apr 10, 2013 9:58 pm   Post subject: Re: How would I implement my classes into my program?

I'm new to programming and I don't know a lot of java. All I know about OOP at this point is how to create a basic class and use them from the class which I created an object for them in. If this is beyond my level right now I guess I could do something more simple for my project. I'll look up information of extending classes and abstract classes though and hopefully that will help me with what i want to do. thanks
Panphobia




PostPosted: Wed Apr 10, 2013 10:19 pm   Post subject: RE:How would I implement my classes into my program?

If you have any trouble understanding my explanation please tell me, I just re read it and cringed.
Benner




PostPosted: Thu Apr 11, 2013 4:10 pm   Post subject: Re: How would I implement my classes into my program?

My question was worse, don't worry lol. I found a website that explains inheritance and polymorphism and I used my knew knowledge in my program! thanks Very Happy
Benner




PostPosted: Fri Apr 12, 2013 5:05 pm   Post subject: Re: How would I implement my classes into my program?

Hi, I'm not sure if I should create a new thread or not... I am getting a error that I've read is caused by an infinite loop.

/* Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at chapter7project.Chapter7project.<init>(Chapter7project.java:113)
at chapter7project.GUI.<init>(GUI.java:6)
this repeats it self 100+ times
*/

My main class contains the code for creating my swing gui. I then have a class called GUI that contains methods to update different parts of the gui. GUI extends my main class. my main class creates an object of GUI. I believe this is causing an infinite loop. When I remove the code that instantiates the GUI object the error goes away. Is there something I can do to fix this? I can probably just put the methods into my main class but I would like to know how to fix/avoid this for future reference. below is the part of my code that I believe is the focus, but Ill put a pastebin link to the full code of the two classes just in case. Thanks

main class code
code:

public class Chapter7project {
   
   GUI update;         
   
   public Chapter7project(){
    player = new Warrior();
    update = new GUI();

//---------------------------
class windowOne implements ActionListener{         
       @Override
       public void actionPerformed(ActionEvent event){
           String eventName = event.getActionCommand();
           String St11 = t11.getText();
           
           if (eventName.equals("b11")){
         player.changeName(St11);
 [b]        update.updateAllLabels();                                                                        //uses GUI object[/b]
         newCharF1.setVisible(false);
         CharF2.setVisible(true);
           }
       }   
   }


GUI Class
code:

 public class GUI extends Chapter7project{
     
     public void updateAllLabels(){
         p11 = new JLabel("Create new character (Step 1 of 2)");
    p12.setText("Name:");
    p13.setText("Gender");
   
    p21.setText("Create new character (Step 2 of 2)");
    p22.setText("Hair Style");
    p22.setText("Hair Colour");
    p24.setText("Beard / Earrings");  //make sure to take care of this
   
    p31.setText("Main Menu"); //P31(title) p32(name) p33(health) p34(level) p35(xp/xpToLevel) p36(spec)
    p32.setText(player.getName());
    p33.setText("Health: "+player.getHealth()+" / "+player.getMaxHealth());
    p34.setText("Level: "+player.getLevel());
    p35.setText(player.getXp()+" XP / "+player.getXpToLvl());
    p36.setText("Spec: "+player.getSpec());
    p37.setText("Strength: "+player.getStrength());
    p38.setText("Stamina: "+player.getStamina());
    p39.setText("Crit: "+player.getCrit());
   
    p41.setText(player.getInventory(1));
    p42.setText(player.getInventory(2));
    p43.setText(player.getInventory(3));
    p44.setText(player.getInventory(4));
    p45.setText(player.getInventory(5));
    p46.setText(player.getInventory(6));
    p47.setText(player.getInventory(7));
    p48.setText(player.getInventory(8));
    p49.setText(player.getInventory(9));
    p410.setText(player.getInventory(10));
   
    p51.setText("Level up!");
    p52.setText("You have reached level "+player.getLevel()+".");
    p53.setText("Your strength has increased to "+player.getStrength());
    p54.setText("Your stamina has increased to "+player.getStamina());
    p55.setText("Your crit has increased to "+player.getCrit());
    p56.setText("Your health has increased to "+player.getHealth());
   
    p61.setText("You killed the boar. You took "+player.getDamageTaken()+"damage. You gained "+(player.getLevel()/5)+" XP.");
   
    p71.setText("Cheat Mode");
    p72.setText("Name:");
    p73.setText("Health:");
    p74.setText("Level:");
    p77.setText("Strength:");
    p78.setText("Stamina:");
    p79.setText("Crit:");
   
    p81.setText(player.getInventory(1));
    p82.setText(player.getInventory(2));
    p83.setText(player.getInventory(3));
    p84.setText(player.getInventory(4));
    p85.setText(player.getInventory(5));
    p86.setText(player.getInventory(6));
    p87.setText(player.getInventory(7));
    p88.setText(player.getInventory(8));
    p89.setText(player.getInventory(9));
    p810.setText(player.getInventory(10));
    p811.setText("Add Item:");
    p812.setText("Remove Item:");
    p813.setText(" ");
   
    p91.setText("Shop");
    p92.setText("Bacon: 10 gold");
    p93.setText("Your gold: "+player.getTotalGold());
         
     }
     
     public void updateAllButtons(){
         
     }
   
}


Main class full code: http://pastebin.com/knkPwiJX
GUI full code: http://pastebin.com/FhYvbDyK

In my main class I put -----------------------------GUI object beside the parts that I referenced above

I hope this isn't asking for too much Embarassed
wtd




PostPosted: Fri Apr 12, 2013 6:39 pm   Post subject: RE:How would I implement my classes into my program?

code:
    p81.setText(player.getInventory(1));
    p82.setText(player.getInventory(2));
    p83.setText(player.getInventory(3));
    p84.setText(player.getInventory(4));
    p85.setText(player.getInventory(5));
    p86.setText(player.getInventory(6));
    p87.setText(player.getInventory(7));
    p88.setText(player.getInventory(8));
    p89.setText(player.getInventory(9));
    p810.setText(player.getInventory(10));


It causes me almost physical discomfort to see this. Arrays are your friends. Smile
Benner




PostPosted: Fri Apr 12, 2013 6:58 pm   Post subject: Re: How would I implement my classes into my program?

creating arrays for swing components never occurred to me... the amount of time you just saved me is huge...

thank you Very Happy

I'm aware my code is very bad, I'm a beginner cut me some slack lol
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Apr 12, 2013 7:07 pm   Post subject: RE:How would I implement my classes into my program?

If I cut new programmers slack, they'd learn less, so... no.
Benner




PostPosted: Fri Apr 12, 2013 7:22 pm   Post subject: Re: How would I implement my classes into my program?

dont get me wrong, I want any and all criticism... I want to get better! It's just that this problem has kept me from continuing my project for a few hours now and I can't continue until I fix it.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: