You are viewing our Forum Archives. To view or take place in current topics click here.
Java help + rep
Posted:

Java help + repPosted:

Gavin-
  • Blind Luck
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
So my problem is I am trying to reduce the strength atribute of the bad guy by -8 everytime i click the good guy button.

Pic of program :

[ Register or Signin to view external links. ]


Bad Guy Class :

package gavin.me;

public class BadGuy {
   
   String name;
   int Strength;
   int ammo;
   boolean alive;
   
   
   
   public BadGuy()
   {
      name = "Gavin";
      Strength = 100;
      ammo = 50;
   }
   
   
   public  BadGuy(String name , int Strength , int ammo)
   {
      this.name = name;
      this.Strength = Strength;
      this.ammo = ammo;
   }
   
   public void setName(String name)
   {
      this.name = name;
   }
   
   public void setStrength(int Strength)
   {
      this.Strength = Strength;
   }
   
   public void setAmmo(int ammo)
   {
      this.ammo = ammo;
   }
   
   public String getName()
   {
      return name;
   }
   
   public boolean isALive()
   {
      return alive;
   }
   
   public int getStrength()
   {

      return Strength;
   }
   
   public int getAmmo()
   {
      return ammo;
   }

   public int getShot() {

      Strength = -8;

      return Strength;



   }













   public String toString()
   {
      return "BadGuy:" + getName() + "Strength:" + getStrength() + "Ammo:" + getAmmo();
   }
   

}



Good Guy Class :

package gavin.me;

public class GoodGuy {
   
   String name;
   int Strength;
   int ammo;
   boolean alive;
   
   
   
   public GoodGuy()
   {
      name = "Micheal";
      Strength = 100;
      ammo = 50;
   }
   
   
   public  GoodGuy(String name , int Strength , int ammo)
   {
      this.name = name;
      this.Strength = Strength;
      this.ammo = ammo;
   }
   
   public void setName(String name)
   {
      this.name = name;
   }
   
   public void setStrength(int Strength)
   {
      this.Strength = Strength;
   }
   
   public void setAmmo(int ammo)
   {
      this.ammo = ammo;
   }
   
   public String getName()
   {
      return name;
   }
   
   public int getStrength()
   {
      return Strength;
   }
   
   public boolean isAlive()
   {
      return alive;
   }
   
   public int getAmmo()
   {
      return ammo;
   }

   public int getShot() {

      return Strength = -1;


   }


   
   public String toString()
   {
      return "GoodGuy:" + getName() + "Strength:" + getStrength() + "Ammo:" + getAmmo();
   }
   
   
   

}






Game Tester Class :

package gavin.me;



import javafx.beans.property.adapter.JavaBeanObjectProperty;
import jdk.nashorn.internal.scripts.JO;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import static com.sun.java.accessibility.util.AWTEventMonitor.addActionListener;

public class GameTester extends JFrame implements ActionListener   {
   
   JButton j1;
   JButton j2;
   JTextArea area;
   JTextArea area2;
   
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   GoodGuy good = new GoodGuy();
   BadGuy bad = new BadGuy();


   public GameTester()
   {






         Container cPane = getContentPane();

         cPane.setLayout(new FlowLayout());




       j1 = new JButton("GoodGuy");
       j2 = new JButton("BadGuy");



       j1.addActionListener(this);
       j2.addActionListener(this);


       cPane.add(j2);
      cPane.add(j1);


      area = new JTextArea();
      area2 = new JTextArea();

      area.append(bthetechgame.orgString());
      area2.append(good.toString());


      cPane.add(area);
      cPane.add(area2);

      setSize(375,375);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setTitle("GoodGuy vs BadGuy");
      setVisible(true);
      setLocationRelativeTo(null);
      setResizable(false);


      
   }

   public void actionPerformed(ActionEvent e)
   {
      if(e.getActionCommand().equals("GoodGuy")) {

           bad.getShot();



      }

      else if(e.getActionCommand().equals("BadGuy"))
      {
         good.getShot();
      }
   }



   
      public static void main(String args[])
      {

         new GameTester();
      }


}




The getShot() method in the good guy and badguy classes in the method i will use!

Thanks!
#2. Posted:
-Deano
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
You have



Strength = -8;



This should be
"Strength -= 8;"
or
"Strength = Strength - 8;"


You are just setting its strength to -8 when you call the shot method.

Same thing for the GoodGuy's method, you need to have
"Strength -= 1;"
or
"Strength = Strength - 1;"
#3. Posted:
Gavin-
  • Ninja
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
-Deano wrote You have



Strength = -8;



This should be
"Strength -= 8;"
or
"Strength = Strength - 8;"


You are just setting its strength to -8 when you call the shot method.

Same thing for the GoodGuy's method, you need to have
"Strength -= 1;"
or
"Strength = Strength - 1;"



Unfortunately it did not work
#4. Posted:
7en
  • V5 Launch
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
Status: Offline
Joined: Aug 16, 201211Year Member
Posts: 598
Reputation Power: 29
You are modifying the object's field, but you are not reflecting this change on the JTextArea too. You will have to update the text area's contents separately.

Also, the good and bad guy classes are identical - do they really need to be different classes? They should at least extend just extend from a common parent.
#5. Posted:
Gavin-
  • TTG Fanatic
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
7en wrote You are modifying the object's field, but you are not reflecting this change on the JTextArea too. You will have to update the text area's contents separately.

Also, the good and bad guy classes are identical - do they really need to be different classes? They should at least extend just extend from a common parent.


Ya that was bad coding by me ill extend the bad guy class , also i updated the JTextareas and it works now
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.