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
Related
i'm learning junit and problem have occured at the beginning.
At the start i want to initialize objected which will be used in tests.
But #BeforeClass doesn't do that.
public class InitTests {
private Croupier croupier;
private Player p1, p2;
#BeforeClass
public void setUp() {
croupier = new Croupier();
croupier.PlayersInit(5, 100);
p1 = croupier.getPlayer(0);
p2 = croupier.getPlayer(1);
} #Test // p1,p2, croupier = null, have no idea why.
public void PlayerInitTest() {
assertEquals(0, p1.getId());
assertEquals(1, p2.getId());
}}
Other classes :
public class Player {
private ArrayList<Card> hand = new ArrayList<>();
private int coins = 0;
private static int playerNumber = 0;
private int id;
private boolean inGame = true;
public Player(int coins) {
this.coins = coins;
id = ++playerNumber;
}
public int addCoins(int amount) {
coins+=amount;
return amount;
}
public int substractCoins(int substract) {
coins-=substract;
return substract;
}
public int getCoins() {
return coins;
}
public int getId() {
return id;
}
public boolean isInGame() {
return inGame;
}
public void setGameStatus(boolean status) {
if(getCoins() < 0 )
inGame = false;
else
inGame = status;
}
public void clearHand() {
hand.clear();
}}
public class Croupier {
private String name;
private ArrayList<Card> deck = new ArrayList<>();
private ArrayList<Player> allPlayers = new ArrayList<>();
private ArrayList<Player> actual = new ArrayList<>();
private int stack = 0;
private int bigPlayerStack = 0;
private int smallPlayerStack = 0;
public Croupier() {
System.out.println("tutej.");
}
public Croupier CroupierInit() {
// static
PlayersInit(5, 100);
return new Croupier();
}
private void CreateDeck() {
String[] suits = { "hearts", "spades", "diamonds", "clubs" };
for (int i = 0; i < suits.length; i++)
for (int j = 2; j < 15; j++)
deck.add(new Card(j, suits[i]));
}
private void DeckShuffle() {
Collections.shuffle(deck);
}
public boolean TurnPlayed() {
if (!preparedGame())
return false;
return true;
}
public void StartGame() {
preparedGame();
System.out.println("Game ended.");
}
public boolean preparedGame() {
clearTable();
if(!setActualPlayers())
return false;
setSmallAndBig();
takeFromSmallAndBig();
CreateDeck();
DeckShuffle();
return true;
}
// set players who are playing
public boolean setActualPlayers() {
for (Player e : allPlayers)
if (e.isInGame())
actual.add(e);
if (actual.size() < 2)
return false;
return true;
}
// take coisn from small and big blind
public void takeFromSmallAndBig() {
stack += actual.get(bigPlayerStack).substractCoins(20);
stack += actual.get(smallPlayerStack).substractCoins(10);
}
// set who has small or big blind
public void setSmallAndBig() {
bigPlayerStack++;
if (bigPlayerStack > actual.size())
bigPlayerStack = 0;
smallPlayerStack = bigPlayerStack - 1;
if(smallPlayerStack < 0 )
smallPlayerStack = actual.size() -1;
}
// clear table before game
public void clearTable() {
actual.clear();
for (Player e : allPlayers)
e.clearHand();
}
public void PlayersInit(int numberOfPlayers, int coins) {
for (int i = 0; i < numberOfPlayers; i++) {
allPlayers.add(new Player(coins));
}
}
public Player getPlayer(int index) {
return allPlayers.get(index);
}}
I'm sure these tests are correct because when i put setUp method ( code from that method) inside #Test it just works.
I hope it's a simple syntax problem which as a beginner can't set at the moment.
Greetings.
Your test class uses a mix of JUnit 4 and 5 annotations.
The JUnit 5 API is not compatible with prior versions of the library, and defines a new set of annotations. Assuming your test executes, you are probably using a JUnit 5 launcher and therefore need to use annotations from org.junit.jupiter.api package.
If you wish to use JUnit 5
Try #org.junit.jupiter.api.BeforeEach or#org.junit.jupiter.api.BeforeAll, with a static method for the latter. Their semantic is defined here.
If you wish to use JUnit 4
You need revert all the annotations to JUnit 4 - which implies using org.junit.Test.
Your JUnit launcher needs to be configured for JUnit 4.
I am trying to create a method for " winning percentage " in a player class. I know I need to incorporate total wins divided by total games played, but the code is meant to be simple so I cannot use complex code. (beginner project in computer science) Any useful feedback would be great as I have spent multiple days attempting this and getting no where. By the way, ties count as half a win.
Update: Implemented the getters into the getWinningPercentage method. Also calculated everything inside the getWinningPercentage and removed the setWinningPercentage considering it was useless code. Results were as follows:
Bob
5 wins, 1 losses, 2 ties
Winning percentage = 0.75
public class Player
{
private int numWins = 0;
private int numTies = 0;
private int numLosses = 0;
private String name;
public void setWins(int w)
{
numWins = w;
}
public int getWins()
{
return numWins;
}
public void setTies(int t)
{
numTies = t;
}
public int getTies()
{
return numTies;
}
public void setLosses(int L)
{
numLosses = L;
}
public int getLosses()
{
return numLosses;
}
public void setName(String n)
{
name = n;
}
public String getName()
{
return name;
}
public void incrementWins()
}
numWins++;
}
public void incrementTies()
{
numTies++;
}
public void incrementLosses()
{
numLosses++;
}
public double getWinningPercentage()
{
double totalGames = getWins() + getTies() + getLosses();
double totalWins = getWins() + (getTies() / 2.0);
double winningPercentage = (totalWins / totalGames);
return winningPercentage;
}
}
The winning percentage should be a calculated property, not a field, and not have a setter method. Instead there should only be a "getter" (public double getWinningPercentage()) method and you should calculate and return this value from within the method itself from the other fields that your class already has.
We should leave it up to you to create this method and formula yourself.
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
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 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!