I am finally setting out on my own instead of following courses online and trying to create something on my own, it's just a simple text based dungeon type of game.
However I know I'm messing up when I'm passing in the new values from one class to another class, the class where I try to set the values to pass to another
package com.company;
import java.util.Random;
import java.util.Scanner;
public class CharBuilder {
static public Random randNum = new Random();
static public Scanner sc = new Scanner(System.in);
private String mCharacterName;
private int mStrength = 6;
private int mIntelligence = 6;
private int mConstitution = 6;
private int mDexterity = 6;
private int mHitPoints = 40;
private int mManaPoints = 40;
private String mCharClass;
private CharBuilder hero;
public CharBuilder(String charClass,int str, int intl, int con, int dex, int hp, int mp) {
mCharClass = charClass;
mStrength = str;
mIntelligence = intl;
mConstitution = con;
mDexterity = dex;
mHitPoints = hp;
mManaPoints = mp;
}
public CharBuilder() {
}
public CharBuilder(String charClass) {
mCharClass = charClass;
}
public void characterNameCreator() {
System.out.println("Greetings, what is your name adventurer? ");
mCharacterName = sc.nextLine();
System.out.printf("I see, so your name is %s very well, very well \n", mCharacterName);
}
public void createHero() {
hero = new CharBuilder(mCharClass,mStrength,mIntelligence,mConstitution,mDexterity,mHitPoints,mManaPoints);
String acceptedAnswers = "warrior wizard thief";
System.out.printf("%s would you say you are more of a warrior, wizard or more of the thievery type \n", mCharacterName);
String classAnswer = sc.nextLine();
boolean isAcceptableClass = classAnswer.contains(acceptedAnswers);
do {
if (classAnswer.equalsIgnoreCase("warrior")) {
mCharClass = "warrior";
hero.setStr(mStrength + 3);
hero.setIntelligence(mIntelligence - 1);
hero.setmConstitution(mConstitution + 4);
hero.setmDexterity(mDexterity + 1);
hero.setHitPoints(mHitPoints + 20);
hero.setManaPoints(mManaPoints - 10);
} else if (classAnswer.equalsIgnoreCase("Wizard")) {
mCharClass = "wizard";
hero.setStr(mStrength -1);
hero.setIntelligence(mIntelligence + 3);
hero.setmConstitution(mConstitution + 2);
hero.setmDexterity(mDexterity);
hero.setHitPoints(mHitPoints - 10);
hero.setManaPoints(mManaPoints + 30);
} else if (classAnswer.equalsIgnoreCase("thief") || classAnswer.equalsIgnoreCase("thievery")) {
mCharClass = "thief";
hero.setStr(mStrength + 2);
hero.setIntelligence(mIntelligence + 1);
hero.setmConstitution(mConstitution + 1);
hero.setmDexterity( mDexterity + 5);
hero.setHitPoints(mHitPoints + 10);
hero.setManaPoints(mManaPoints + 10);
} else {
System.out.println("I'm sorry, that is not an acceptable class, please pick warrior, wizard, or thief");
createHero();
}
}while (isAcceptableClass);
}
public int getStrength() {
return mStrength;
}
public int getIntelligence() {
return mIntelligence;
}
public int getConsitution() {
return mConstitution;
}
public int getDex() {
return mDexterity;
}
public int getHP() {
return mHitPoints;
}
public int getMP() {
return mManaPoints;
}
public CharBuilder getHero() {
return hero;
}
public void setStr(int str) {
mStrength = str;
}
public void setIntelligence(int intl) {
mIntelligence = intl;
}
public void setmConstitution(int con) {
mConstitution = con;
}
public void setmDexterity(int dex) {
mDexterity = dex;
}
public void setHitPoints( int hitPoints) {
mHitPoints = hitPoints;
}
public void setManaPoints( int manaPoints) {
mManaPoints = manaPoints;
}
public void setmCharClass(String charClass) {
mCharClass = charClass;
}
}
and the class where I'm trying to pass the variables into doesn't seem to be getting anything as it is giving me a nullPointerException so I know there's no value when I ask it go it, and I've included that class below,
package com.company;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Combat {
Scanner sc = CharBuilder.sc;
Random randNum = CharBuilder.randNum;
List<Enemy> enemyList = new Enemy().createList();
Enemy enemy = enemyList.get(randNum.nextInt(enemyList.size()));
CharBuilder character = new CharBuilder().getHero();
int charStrength = character.getStrength();
int charHp = character.getHP();
int enemyHp = enemy.getHP();
int enemyStr =enemy.getStr();
String enemyName = enemy.getName();
public void initiateCombat() {
System.out.printf("A %s appears, it has %d hitpoints, and it's strength is %s \n", enemyName, enemyHp, enemyStr);
}
public void playersTurn() {
System.out.println("It is your turn, your available actions are attack, or use a skill");
String actionAnswer = sc.nextLine();
if (actionAnswer.equalsIgnoreCase("attack")) {
enemyHp -= charStrength;
System.out.printf("You attack the enemy, doing %s damage, the enemy currently has %s hp left \n", charStrength,
enemyHp);
}
else {
System.out.println("Sorry, please choose attack or skill ");
playersTurn();
}
}
public void enemyTurn() {
charHp -= enemyStr;
System.out.printf("It is the enemy's turn, it attacks doing %s damage, your current hp is %s \n", enemyStr, charHp);
}
public void combat() {
while(enemyHp >= 0 && charHp >= 0) {
initiateCombat();
playersTurn();
if (enemyHp <= 0) {
System.out.println("You have defeated the enemy, congratulations");
break;
}
enemyTurn();
}
}
}
any tips for a new guy on what I am doing wrong, and how I can get these values over? I know that I could just declare the hero static but is there a better way?
I'm getting NullPointerException in the first possible area where they try to pull data from the CharBuilder class. Which is
int charStrength = character.getStrength();
You need to call createHero() on a CharBuilder before you call getHero() in your constructor.
Your code should be,
List<Enemy> enemyList = new Enemy().createList();
Enemy enemy = enemyList.get(randNum.nextInt(enemyList.size()));
CharBuilder cb = new CharBuilder();
cb.createHero();
CharBuilder character = cb.getHero();
int charStrength = character.getStrength();
int charHp = character.getHP();
int enemyHp = enemy.getHP();
int enemyStr =enemy.getStr();
String enemyName = enemy.getName();
The reason for this is because you need to execute createHero() in order to initialize the hero object.
Further go more on this, you are initializing CharBuilder inside CharBuilder which is not a good practice when we think in terms of object orientation. One alternative is to use the singleton pattern here.
Lets see why you have NPE issue:
Take a look at here
public CharBuilder() {
}
The first constructor is a default constructor. It only initializes the bare minimum and lets users set the rest with getters and setters.
CharBuilder character = new CharBuilder().getHero();
some setters is missing here?
int charStrength = character.getStrength();
As you see in your code, you did not set anything to get it after wards
In addition, There are three common reasons to define a default constructor:
To construct an object with default values.
To initialize an object that doesn't need parameters in that initialization process.
To redefine the scope of the constructor. Making the constructor private will prevent anyone but the class itself from constructing an
object.
If you want to make sure that any instance created is always valid and any member variables are always initialized,then you would define the constructor which initializes all the required member variables.
In your case, you can use createHero function before getHero because createHero sets the values up.
Suggestion:
Plaese take a look at builder pattern for designing a better class
More Info About Builder Pattern
As has already been pointed out, you are not calling createHero, which generates the instance of CharBuilder which getHero returns. Thus returning a null value.
The problem seems to steam from a misunderstanding of what a builder should do. A builder should take raw material and build something, it shouldn't return a copy of it self.
For example...
public class CharBuilder {
static public Random randNum = new Random();
private String mCharacterName;
private int mStrength = 6;
private int mIntelligence = 6;
private int mConstitution = 6;
private int mDexterity = 6;
private int mHitPoints = 40;
private int mManaPoints = 40;
private String mCharClass;
private String classType;
public Character createHero() {
// Valid the properties you have been given...
if ("warrior".equalsIgnoreCase(classType)) {
mCharClass = "warrior";
mStrength = mStrength + 3;
mIntelligence = mIntelligence - 1;
mConstitution = mConstitution + 4;
mDexterity = mDexterity + 1;
mHitPoints = mHitPoints + 20;
mManaPoints = mManaPoints - 10;
} else if ("Wizard".equalsIgnoreCase(classType)) {
mCharClass = "wizard";
mStrength = (mStrength - 1);
mIntelligence = (mIntelligence + 3);
mConstitution = (mConstitution + 2);
mDexterity = (mDexterity);
mHitPoints = (mHitPoints - 10);
mManaPoints = (mManaPoints + 30);
} else if ("thief".equalsIgnoreCase(classType) || "thievery".equalsIgnoreCase(classType)) {
mCharClass = "thief";
mStrength = (mStrength + 2);
mIntelligence = (mIntelligence + 1);
mConstitution = (mConstitution + 1);
mDexterity = (mDexterity + 5);
mHitPoints = (mHitPoints + 10);
mManaPoints = (mManaPoints + 10);
} else {
throw new UnsupportedOperationException("Unknown class");
}
return new DefaultCharacter(mCharacterName, mStrength, mIntelligence, mConstitution, mDexterity, mHitPoints, mManaPoints, mCharClass);
}
public CharBuilder setCharacterName(String mCharacterName) {
this.mCharacterName = mCharacterName;
return this;
}
public CharBuilder setStrength(int mStrength) {
this.mStrength = mStrength;
return this;
}
public CharBuilder setIntelligence(int mIntelligence) {
this.mIntelligence = mIntelligence;
return this;
}
public CharBuilder setConstitution(int mConstitution) {
this.mConstitution = mConstitution;
return this;
}
public CharBuilder setDexterity(int mDexterity) {
this.mDexterity = mDexterity;
return this;
}
public CharBuilder setHitPoints(int mHitPoints) {
this.mHitPoints = mHitPoints;
return this;
}
public CharBuilder setManaPoints(int mManaPoints) {
this.mManaPoints = mManaPoints;
return this;
}
public CharBuilder setCharClass(String mCharClass) {
this.mCharClass = mCharClass;
return this;
}
public CharBuilder setClassType(String classType) {
this.classType = classType;
return this;
}
}
You can use method chaining to make it easier to call, for example
Character character = new CharBuilder().
setCharClass("Wizard").
setCharacterName("Bob").
setConstitution(10).
setDexterity(10).
setHitPoints(10).
setIntelligence(10).
setManaPoints(10).
setStrength(10).
createHero();
The builder shouldn't be asking questions of the user, this information should already have been obtained and simply given to the builder. You should only call the methods which you need in order to build character, so many of the methods in the above example might be omitted in favor of default values
Character character = new CharBuilder().
setCharClass("Wizard").
setCharacterName("Bob").
createHero();
The builder should validate the properties it has been given and throw an Exception if one or required properties are missing (like the name or class)
I, personally, like to work with interfaces, rather then implementations, it allows you to define the public contract that a object can have, for example...
public interface Character {
public String getCharacterName();
public int getStrength();
public int getIntelligence();
public int getConstitution();
public int getDexterity();
public int getHitPoints();
public int getanaPoints();
public String getCharClass();
}
Which can be wrapped in
public class DefaultCharacter implements Character {
private final String mCharacterName;
private final int mStrength;
private final int mIntelligence;
private final int mConstitution;
private final int mDexterity;
private final int mHitPoints;
private final int mManaPoints;
private final String mCharClass;
public Character(String mCharacterName, int mStrength, int mIntelligence, int mConstitution, int mDexterity, int mHitPoints, int mManaPoints, String mCharClass) {
this.mCharacterName = mCharacterName;
this.mStrength = mStrength;
this.mIntelligence = mIntelligence;
this.mConstitution = mConstitution;
this.mDexterity = mDexterity;
this.mHitPoints = mHitPoints;
this.mManaPoints = mManaPoints;
this.mCharClass = mCharClass;
}
public String getCharacterName() {
return mCharacterName;
}
public int getStrength() {
return mStrength;
}
public int getIntelligence() {
return mIntelligence;
}
public int getConstitution() {
return mConstitution;
}
public int getDexterity() {
return mDexterity;
}
public int getHitPoints() {
return mHitPoints;
}
public int getManaPoints() {
return mManaPoints;
}
public String getCharClass() {
return mCharClass;
}
}
Now, because the CharBuilder only says it will return a Character, you can change the physical implementation anyway you like
In this way, it's pretty hard to run into the problem you are having, because either the builder is going to return a valid Character or throw an Exception
Take a closer look at the Builder Pattern for more details
Related
Stupid question about Java basics, but I cannot beat it for days already.
I have a class with initialized data. Randomizer is here.
public class AddContract {
private Data data = new Data();
class Data {
final String CONNUM = String.valueOf(randInt());
public int randInt() {
int min = 1;
int max = 7777;
return (int) (Math.random() * max + min);
}
}
And I have another class:
public class ContractsPage {
public ContractsPage FindContractByContractNumber() {
AddContract.Data buffer = new AddContract.Data();
this.contractNumberFilter.clear();
this.contractNumberFilter.sendKeys(buffer.CONNUM);
return this;
}
}
Pls don't judge how it looks now coz I was trying many variants how to make it work.
So, I want get the same instanse of randInt in class ContractsPage as it is in class AddContract. But Object through new generates "new" rng.
tl;dr:
With random I get var=5 in Class1, I want to adress the same var=5 in Class2.
do the random oprtaions in constructor of the class and keep it in an instance variable.
public class AddContract {
private Data data = new Data();
private String bufferCONNUMStr = String.valueOf(data.randInt());
public String bufferCONNUM() {
return bufferCONNUMStr;
}
}
public class AddContract {
private Data data = new Data();
public String bufferCONNUM() {
return data.CONNUM;
}
public String bufferPURCHNUM() {
return data.PURCHASENUM;
}
class Data {
final String CONNUM = String.valueOf(randInt());
final String PURCHASENUM = String.valueOf(randInt());
public final int randInt() {
int min = 1;
int max = 7777;
return (int) (Math.random() * max + min);
}
}
}
Apologies if this is trivial to most but I just can't figure this issue out!!
I am creating a mock game where I have a start, end, and hops along. There are portals where if you go on a white portal you jump further ahead and there are black ones where you go backwards. I have set up the class as a POJO;
private int totalSize;
private int minDice;
private int maxDice;
private int whitePortalStart;
private int whitePortalEnd;
private int blackPortalStart;
private int blackPortalEnd;
private int startPosition = 1;
private int currentPosition;
public GameObject(){}
public int getTotalSize() {
return totalSize;
}
public void setTotalSize(int totalSize) throws Exception {
if(totalSize <= 0){
throw new Exception("Can't have a total distance of less than or equal to 0");
} else {
this.totalSize = totalSize;
}
}
public int getMinDice() {
return minDice;
}
public void setMinDice(int minDice) throws Exception {
if(minDice <= 0){
throw new Exception("Can't have a min dice value of less than or equal to 0");
} else {
this.minDice = minDice;
}
}
public int getMaxDice() {
return maxDice;
}
public void setMaxDice(int maxDice) throws Exception {
if(getMinDice() > maxDice){
throw new Exception("Cant have minimum dice number greater than the larger dice number");
} else {
this.maxDice = maxDice;
}
}
public int getWhitePortalStart() {
return whitePortalStart;
}
public void setWhitePortalStart(int whitePortalStart) throws Exception {
this.whitePortalStart = whitePortalStart;
}
public int getWhitePortalEnd() {
return whitePortalEnd;
}
public void setWhitePortalEnd(int whitePortalEnd) throws Exception {
this.whitePortalEnd = whitePortalEnd;
}
public int getBlackPortalStart() {
return blackPortalStart;
}
public void setBlackPortalStart(int blackPortalStart) throws Exception {
this.blackPortalStart = blackPortalStart;
}
public int getBlackPortalEnd() {
return blackPortalEnd;
}
public void setBlackPortalEnd(int blackPortalEnd) throws Exception {
this.blackPortalEnd = blackPortalEnd;
}
public GameObject builder(int n) throws Exception {
setTotalSize(n);
return this;
}
public GameObject whitePortal(int m, int o) throws Exception {
setWhitePortalStart(m);
setWhitePortalEnd(o);
return this;
}
public GameObject blackPortal(int o, int m) throws Exception {
setBlackPortalStart(o);
setBlackPortalEnd(m);
return this;
}
public GameObject dice(int i, int j) throws Exception {
setMinDice(i);
setMaxDice(j);
return this;
}
public int rollDice(){
Random random = new Random();
int min = getMinDice();
int max = getMaxDice();
return random.nextInt(max - min + 1) + min;
}
public void build(){
int totalDistance = getTotalSize();
currentPosition = startPosition;
while(currentPosition < totalDistance){
int diceValue = rollDice();
if(currentPosition + diceValue > getTotalSize()){
System.out.println("CurrentPosition : " + (currentPosition + diceValue) + ", is larger than the total size of the road - " + totalSize);
continue;
} else if(currentPosition + diceValue == getWhitePortalStart()){
System.out.println("You landed on a white portal. Advancing from position " + (currentPosition + diceValue) + " to " + getWhitePortalEnd());
currentPosition = getWhitePortalEnd();
} else if(currentPosition + diceValue == getBlackPortalStart()){
System.out.println("You landed on a black portal. Moving from position " + (currentPosition + diceValue) + " to " + getBlackPortalEnd());
currentPosition = getBlackPortalEnd();
} else {
System.out.println("You landed on " + (currentPosition + diceValue));
currentPosition += diceValue;
}
}
}
So in my main method I call the it like create and call this class like;
WorldOfOz oz = new WorldOfOz();
oz.go.builder(30)
.dice(1, 4)
.whitePortal(5, 12)
.blackPortal(13, 2)
.build();
My issue is when I want to add in more than 1 whitePortal/blackPortal
WorldOfOz oz = new WorldOfOz();
oz.go.builder(30)
.dice(1, 4)
.whitePortal(5, 12)
.whitePortal(18, 26)
.blackPortal(13, 2)
.build();
The values 18 - 26 override 5 - 12. How can I set this up so I can have multiple white and black portals?
It seems that your data structure is not enough to solve this problem.
You need to define a collection of whitePortals and a collection of blackPortals. If you do so calling the method whitePortal(5, 12) add a new white portal insted of setting the white portal values of the only white existing portal.
You need to define a class Portal
public class Portal {
private int portalStart;
private int portalEnd;
...
public Portal(int s, int e) {
this.portalStart = s;
this.portalEnd = e;
}
}
Then you can use it in the GameObject as follow:
public GameObject {
List<Portal> whitePortals;
List<Portal> blackPortals;
public GameObject() {
whitePortals = new ArrayList<Portal>();
blackPortals = new ArrayList<Portal>();
}
public GameObject addWhitePortal(int m, int o) throws Exception {
whitePortals.add(new Portal(m, o));
return this;
}
...
// You need to change other methods to follow a different data structure
}
Well, you can use the following approach:
Introduce a new "Portal" type with start/end attributes
Replace white/black portal attributes in your class with lists for white and black portals (or any other type of collection you like)
Replace getWhite/Black methods with access to lists
Refactor whitePortal and blackPortal method to create new instances of a portal object and add them to an appropriate collection
You can, of course, use arrays instead of collections, but that's a bit more cumbersome.
Also, assuming portals are collections, you probably need to add helper methods for operating on those. Depending on what your actual needs are.
public class Portal
{
private int start;
private int end;
public Portal(int start, int end) { ... }
public getStart() {...}
public getEnd() {...}
public setStart(int end) {...}
public setEnd(int start) {...}
}
public class GameObject
{
...
private List<Portal> whitePortals = new ArrayList<Portal>();
private List<Portal> blackPortals = new ArrayList<Portal>();
...
public GameObject whitePortal(int m, int o) throws Exception {
whitePortals.add(new Portal(m, o));
return this;
}
public GameObject blackPortal(int o, int m) throws Exception {
blackPortals.add(new Portal(m, o));
return this;
}
...
}
emphasized textI've been working on this problem for a while and managed to get rid of almost all the errors on this class. This error keeps saying I'm missing method body or declare abstract but I just don't see it. I've managed to complete another class almost similar to this but this one seems to be acting strangely. Can someone please help me out? Thank you if you do.
import java.util.Scanner;
public class HockeyPlayer extends StudentAthlete
{
Scanner keyboard = new Scanner(System.in);
public static void main (String [] args)
{
HockeyPlayer athlete1 = new HockeyPlayer("Dave", 111111, 15, 3.2, 2, 3);
athlete1.writeOutput();
}
private int assist = 0;
private int goal = 0;
public HockeyPlayer()
{
super();
goal = 0;
assist = 0;
}
public int getAssist()
{
return assist;
}
public void setAssist(int newAssist)
{
if (0 >= newAssist)
{
assist = newAssist;
}
else
{
System.out.println("Invalid Assists");
System.out.println("Please enter a valid Assists");
int tempAssist = keyboard.nextInt();
setAssist(tempAssist);
}
}
public int getGoal()
{
return goal;
}
public int setGoal(int newGoal)
{
if (0 >= newGoal)
{
goal = newGoal;
}
else
{
System.out.println("Invalid Goals");
System.out.println("Please enter a valid Goals");
int tempGoal = keyboard.nextInt();
setGoal(tempGoal);
}
}
public HockeyPlayer(String initialName, int initialStudentNumber, int initialJersey, double initialGpa, int initialGoal, int initialAssist)
{
super (initialName, initialStudentNumber,initialJersey, initialGpa);
setGoal(initialGoal);
setAssist(initialAssist);
}
public HockeyPlayer(String initialName, int initialStudentNumber, int initialJersey, double initialGpa)
{
super (initialName, initialStudentNumber, initialJersey, initialGpa);
goal = 0;
assist= 0;
}
public HockeyPlayer(String initialName, int initialStudentNumber)
{
super (initialName, initialStudentNumber);
goal = 0;
assist = 0;
}
public HockeyPlayer(String initialName)
{
super(initialName);
goal = 0;
assist = 0;
}
public void writeOutput(); // THE ERROR OCCURS HERE
{
super.writeOutput();
System.out.println("Goals: " + goal);
system.out.println("Assists: " + assist);
}
}
change
public int setGoal(int newGoal)
to
public void setGoal(int newGoal)
Setter methods usually don't have a return type (and based on the fact that you don't try to return anything, you probably didn't intend it to have an int return type).
Also change
public void writeOutput();
to
public void writeOutput()
I'm doing an assignment for my computer science class.
I've done quite a bit of the assignment, but I'm having a little bit of trouble pulling the individual variables from the classes. We are just getting into classes and objects and this is our first assignment regarding them so I don't completely understand all of it. So far I've been able to print out the teams, but I haven't been able to pull the individual wins, losses, OTL and OTW so that I can compute whether or not each individual team is a winning team.
What I have done so far is create a class called winningRecord and getPoints, which returns a boolean deciding whether it's a winning team or not. (The formula for a winning team is if the points are > Games Played * 1.5 (as that is an even record).
I don't know how to pull the stats, as it has to be written in the HockeyTeam class. I have set it up so that the constructor sets the variables publicly so that the can be accessed, but as far as accessing them, I'm stumped.
As far as storing them once I am able to access them, would I just make a parallel method that has the points for each team, with just one digit assigned to each bin?
Here is all of the code, thanks for looking.
public class A1Q2fixed {
public static void main(String[] parms) { // main method
processHockeyTeams();
}
/*****************************/
public static void processHockeyTeams() { // processing method
boolean[] winningRecord;
HockeyTeam[] hockeyTeams;
hockeyTeams = createTeams();
printTeams(hockeyTeams);
System.out.print("*********************\n");
printWinningTeams();
winningRecord = HockeyTeam.winningRecord(hockeyTeams);
// printWinningTeams(hockeyTeams);
}
/*********************************/
public static void printTeams(HockeyTeam[] hockeyTeams) {
for (int i = 0; i < hockeyTeams.length; i++) {
System.out.println(hockeyTeams[i]);
}
}
public static void printWinningTeams() {
}
public static HockeyTeam[] createTeams() {
HockeyTeam[] teams;
HockeyTeam team;
int count;
teams = new HockeyTeam[HockeyTeams.getNumberTeams()];
team = HockeyTeams.getTeam();
for (count = 0; (count < teams.length) && (team != null); count++) {
teams[count] = team;
team = HockeyTeams.getTeam();
}
return teams;
}
}
/* hockey team class *******/
class HockeyTeam {
public String name;
public int wins;
public int otw;
public int otl;
public int losses;
public HockeyTeam(String name, int wins, int otw, int otl, int losses) {
this.name = name;
this.wins = wins;
this.otw = otw;
this.otl = otl;
this.losses = losses;
}
public String toString() {
System.out.println(name);
return " W:" + wins + " OTW:" + otw + " OTL:" + otl + " L:" + losses;
}
public static boolean[] winningRecord(HockeyTeam[] hockeyTeam) {
boolean array[] = new boolean[hockeyTeam.length];
String name;
int wins;
int otw;
int otl;
int losses;
for (int i = 0; i < hockeyTeam.length; i++) {
System.out.println(HockeyTeam.name);
}
return array;
}
public static int getPoints() {
int points = 0;
return points;
}
}
/* hockey teams class *******************/
class HockeyTeams {
private static int count = 0;
private static HockeyTeam[] hockeyTeams = {
new HockeyTeam("Canada", 5, 3, 0, 0),
new HockeyTeam("Russia", 5, 1, 1, 2),
new HockeyTeam("Finland", 3, 2, 1, 3),
new HockeyTeam("Sweden", 4, 1, 1, 4),
new HockeyTeam("USA", 1, 2, 2, 3), };
public static int getNumberTeams() {
return hockeyTeams.length;
}
public static HockeyTeam getTeam() {
HockeyTeam hockeyTeam;
hockeyTeam = null;
if (count < hockeyTeams.length) {
hockeyTeam = hockeyTeams[count];
count++;
}
return hockeyTeam;
}
}
Thanks,
Matt.
Sorry but I was only able to understand only a part of your question,from what I understood it seems you are not able to access individual wins, losses, OTL and OTW. I hope this answers your question if not please clarify a bit
To access OTL,OTW have a loop as below:
public class A1Q2fixed
{
public static void main(String[] parms) // main method
{
processHockeyTeams();
}
/*****************************/
public static void processHockeyTeams() // processing method
{
boolean[] winningRecord;
HockeyTeam[] hockeyTeams;
hockeyTeams = createTeams();
printTeams(hockeyTeams);
System.out.print("*********************\n");
printWinningTeams();
winningRecord = HockeyTeam.winningRecord(hockeyTeams);
for(HockeyTeam h:hockeyTeams)
{
System.out.println(h.losses);//To access and print losses
System.out.println(h.otw);//To access and print otw
System.out.println(h.otl);//To access and print otl
}
// printWinningTeams(hockeyTeams);
}
/*********************************/
public static void printTeams(HockeyTeam[] hockeyTeams)
{
for (int i = 0; i < hockeyTeams.length; i++)
{
System.out.println(hockeyTeams[i]);
}
}
public static void printWinningTeams()
{
}
public static HockeyTeam[] createTeams()
{
HockeyTeam[] teams;
HockeyTeam team;
int count;
teams = new HockeyTeam[HockeyTeams.getNumberTeams()];
team = HockeyTeams.getTeam();
for (count=0; (count<teams.length) && (team!=null); count++)
{
teams[count] = team;
team = HockeyTeams.getTeam();
}
return teams;
}
}
Also declare name as Static in HockeyTeam
I am trying to add weapons to a player inventory. It's kind of hard to explain, so I'll try my best. What I have are a class for each weapon, a class for Combat, and a class for the Player. I am trying to get it to where when the Random number equals a certain number, it will add a weapon to the player inventory. I will put my code Below.
Combat Class:
public class Combat {
M4 m4 = new M4();
M16 m16 = new M16();
M9 m9 = new M9();
Glock glock = new Glock();
SCAR Scar = new SCAR();
Player player = new Player();
final int chanceOfDrop = 3;
static boolean[] hasWeapon = {false, true};
public static int ranNumberGen(int chanceOfDrop) {
return (int) (Math.random()*5);
}
private void enemyDead() {
boolean canDrop = false;
if(ranNumberGen(chanceOfDrop)==0){
canDrop = true;
}
if(canDrop == true){
if(ranNumberGen(0) == 1) {
Player.addInvetory(m4.weaponName(wepName), m4.weaponAmmo(wepAmmo)); //Issues here. wepName & wepAmmo cannot be resolved into variable
//Should I just delete the line?
//Trying to get it to add the weapon M4 to the player inventory.
//Maybe use an ArrayList? If so I need a couple pointers on how to implement this.
}
}
}
}
M4 Class:
public class M4 implements Armory {
//Weapon classes are practically identical except for differences in the name wepDamage and wepAmmo.
public Integer weaponAmmo(int wepAmmo) {
wepAmmo = 10;
return wepAmmo;
}
public Integer weaponDamage(int wepDamage) {
wepDamage = 5;
return wepDamage;
}
public String weaponName(String wepName) {
wepName = "M4";
return wepName;
}
Player Class:
public class Player {
public static int health = 100;
//Player Class.
public static void addInvetory(String wepName, int wepAmmo) {
Player.addInvetory(wepName, wepAmmo);
}
public static void removeInventory(String wepName, int wepAmmo) {
Player.addInvetory(wepName, wepAmmo);
}
public static void removeAll(String wepName, int wepAmmo) {
Player.removeAll(wepName, wepAmmo);
}
Interface:
public interface Armory {
//Interface implemented by all of the weapons classes.
public Integer weaponAmmo(int wepAmmo);
public Integer weaponDamage(int wepDamage);
public String weaponName(String wepName);
Hope you can help!
class Weapon {
private final String name;
private final int damage;
private final int ammo;
public Weapon(final String name,final int damage,final int ammo) {
this.name = name;
this.damage = damage;
this.ammo = ammo;
}
public Weapon clone() {
return new Weapon(this.name,this.damage,this.ammo);
}
public String getName() {
return this.name;
}
public int getAmmo() {
return this.ammo;
}
public int getDamage() {
return this.damage;
}
}
class WeaponFactory {
static WeaponFactory factory;
public static WeaponFactory getWeaponFactory() {
if(factory == null) {
factory = new WeaponFactory();
}
return factory;
}
private ArrayList<Weapon> weapons = new ArrayList<Weapon>();
private Random random;
private WeaponFactory() {
//TODO: Fix Ammo and Damage
weapons.add(new Weapon("M4",0,0));
weapons.add(new Weapon("M16",0,0));
weapons.add(new Weapon("M9",0,0));
weapons.add(new Weapon("Glock",0,0));
weapons.add(new Weapon("SCAR",0,0));
}
public Weapon getWeapon() {
int w = random.nextInt(weapons.length);
return weapons.get(w).clone();
}
}
class Combat {
...
private void enemyDead() {
if(ranNumberGen(chanceOfDrop)==0){
Player.addInventory(WeaponFactory.getWeaponFactory().getWeapon());
}
}
}
You can use an array of Armory and the generate a random number from 0 to the size of the array as an index to the array to decide which weapon to add.
Okay dude, since your question about creating a programming language was closed, I'm answering it through here:
I think that your idea is great! Don't give up on it, yet don't get too excited. I would try all the options that you have heard of(interpreted route AND the Compiled route). If you can get either of those to work, then you may proceed to go into further detail with the language creation. It's going to take a while though. Be patient!