I'm not very good at English, but I'll try to explain what I'm trying to do.
So I have this class constructor here:
public class God {
public static ArrayList<God> gods = new ArrayList<God>();
private String name;
private PowerType powerType;
private AttackType attackType;
private ArrayList<ItemStack> abilities;
private Pantheon pantheon;
private GodClass godClass;
private List<Pro> pros;
private int favorCost;
private int gemCost;
public enum Pro {
HIGH_SINGLE_TARGET_DAMAGE, HIGH_MOBILITY, HIGH_AREA_DAMAGE, HIGH_CROWD_CONTROL, HIGH_DEFENSE, HIGH_SUSTAIN, PUSHER, HIGH_ATTACK_SPEED, HIGH_MOVEMENT_SPEED, GREAT_JUNGLER, MEDIUM_CROWD_CONTROL,
}
public enum GodClass {
ASSASSIN, GUARDIAN, HUNTER, MAGE, WARRIOR
}
public enum Pantheon {
CHINESE, EGYPTIAN, GREEK, HINDU, MAYAN, NORSE, ROMAN
}
public enum PowerType {
PHYSICAL, MAGICAL
}
public enum AttackType {
MELEE, RANGED
}
private int health;
private int mana;
private int speed;
private int range;
private double attackSpeed;
private int damage;
private int physicalProtection;
private int magicalProtection;
private int hp5;
private int mp5;
public God(String name, PowerType powerType, AttackType attackType,
ArrayList<ItemStack> abilities, Pantheon pantheon,
GodClass godClass, List<Pro> pros, int favorCost, int gemCost,
int health, int mana, int speed, int range, double attackSpeed,
int damage, int physicalProtection, int magicalProtection, int hp5,
int mp5) {
this.name = name;
this.powerType = powerType;
this.attackType = attackType;
this.abilities = abilities;
this.pantheon = pantheon;
this.godClass = godClass;
this.pros = pros;
this.favorCost = favorCost;
this.gemCost = gemCost;
this.health = health;
this.mana = mana;
this.speed = speed;
this.range = range;
this.attackSpeed = attackSpeed;
this.damage = damage;
this.physicalProtection = physicalProtection;
this.magicalProtection = magicalProtection;
this.hp5 = hp5;
this.mp5 = mp5;
gods.add(this);
}
This is okay for now, right?
If not, correct me please...
So let's suppose this is okay, I created some classes that extends the constructor God which are Vulcan, Loki and Athena
public class Vulcan extends God {
private Vulcan(String name, PowerType powerType, AttackType attackType,
ArrayList<ItemStack> abilities, Pantheon pantheon,
GodClass godClass, ArrayList<Pro> pros, int favorCost, int gemCost,
int health, int mana, int speed, int range, double attackSpeed,
int damage, int physicalProtection, int magicalProtection, int hp5,
int mp5) {
super("VULCAN", PowerType.MAGICAL, AttackType.RANGED, abilities,
Pantheon.ROMAN, GodClass.MAGE, Arrays.asList(
Pro.HIGH_AREA_DAMAGE, Pro.PUSHER), 5500, 200, 380, 245,
360, 55, 0.9, 34, 13, 30, 7, 5);
}
So now Vulcan class is going to be added to the gods ArrayList, right? correct me if I'm wrong.
Okay so when I print the list gods, I get an empty ArrayList.
Did I do something wrong? Or do I have to define the classes Vulcan, Loki and Athena? I'm really confused, help please.
When you say John, Michael and Levi, it seems to me that they should be an instance of the class Human rather than completely different classes.
Probably you need something like this, but not sure :
public Human {
public static ArrayList<Human> humans = new ArrayList<Human>();
private String name;
private int old;
public Human(String name, int old) {
this.name = name;
this.old = old;
humans.add(this);
}
public static void main(String args[])
{
Human john = new Human("John", 21);
Human michael = new Human("Michael", 31);
Human levi = new Human("Levi", 41);
System.out.println(Human.humans.size());
}
}
It would make much sense to have John, Levi and Michael as an instance of Human class.
Human john = new Human( "John", 23 );
Human levi = new Human( "Levi", 24 );
Human michael = new Human( "Michael", 25 );
This code can use to pass values to super class constructor by using super() statement on class John.
class John extends Human {
public John(String name, int old) {
super(name, old);
}
public static void main(String[] args) {
new John("John", 23);
new John("Michael", 31);
new John("Levi", 41);
}
}
Related
i am having some trouble with my code, the main class has String n, int s, int d, int i
public class Maincharacter {
static Dice dice = new Dice();
private final String name;
private final int strength;
private final int dexterity;
public int intelligence;
public Maincharacter(String n, int s, int d, int i){
this.name = n;
this.strength = s;
this.dexterity = d;
this.intelligence = i;
}
and i need to make 3 extended classes getting the same attributes but has differences in them which is
public class Mage extends Maincharacter {
static Dice dice = new Dice();
private int maxMagic;
public int currentMagic;
private int heal;
public Mage(String n, int s, int d){
super (n,s,d);
this.maxMagic = 100;
this.currentMagic = maxMagic;
}
I only want to get String n, int s, and int d, but the error says that it differs in length, that i also need to add int i to it.
Sorry if there is some confusion or it may be vague but i dont know how to code that much.
you have passed
super (n,s,d);
but super class constructor asking 4 parameters, so you should pass 4 parameters into Mage class constructor
super (n,s,d,/* another parameter should pass*/);
I have been creating a java text game but I am stuck trying to figure out on how to implement the last 2 methods. I want it to print out the items in the room and the name of the npc thats in the room (A sort of a Look function). I am not sure on how to go on about it. Any help would be appriciated.
Room[] place = new Room[]{station, UC, Ollies, lounge, palace, AT301};
Sword sword = new Sword();
Thing heal = new HealthPotion();
Thing armour = new Armour();
Thing trap = new Trap();
and for the NPC (Mike, Jake, Evil, Carl)
public abstract class Player{
//abstract attributes
private String name;
private int currentHealth;
private int maxHealth;
private int damage;
private Room currentRoom;
private int stack;
private int effect;
//Constructor for player
public Player(String name, int currentHealth, int maxHealth, int damage, int effect, int stack){
this.name = name;
this.currentHealth = currentHealth;
this.maxHealth = maxHealth;
this.damage = damage;
this.effect = effect;
this.stack = stack;
}
//getters
public String getName(){ return name;}
public int getCurrentHealth(){ return currentHealth;}
public int getMaxHealth(){ return maxHealth;}
public int getDamage(){ return damage;}
public Room getCurrentRoom(){ return currentRoom;}
public int getEffect(){ return effect;}
public int getStack(){ return stack;}
//setters
public void setCurrentHealth(int currentHealth){this.currentHealth = currentHealth;}
public void setMaxHealth(int maxHealth){this.maxHealth = maxHealth;}
public void setDamage(int damage){ this.damage = damage;}
public void setCurrentRoom(Room room){this.currentRoom = room;}
public void setEffect(int effect){ this.effect = effect;}
public void setStack(int stack){ this.stack = stack;}
public void enter(Room room){ this.currentRoom = room;}
//abstract method because each player has a different attack;
public void takeDamage(int damage){ setCurrentHealth(this.currentHealth-damage);}
public boolean isDead(){
if(this.currentHealth<=0){ return true;}
return false;
}
}
I was able to make everything functional except the Look function for the player. I can't figure out how to go on about it.
Room is a vector of items right ? If so you can do a function on the player class that when it is called it goes to the vector of the room you're in and simply print out the items that are in the vector, something like this:
String lookAround(){
ArrayList temp = (ArrayList)getCurrentRoom(); //returns the array containing the items in the current room
for(Thing i : temp){
i.getDescription(); //Method present in all classes that come from Thing that prints out the name of the item and/or its caracheteristics
}
}
In the array of the room you should try to include the name of all players in the room including yourself so that you can print out everyone present in the room
Hope this helps
I would recommend you first of all creating a class Npc with attribute name and add it to the room.
I'm stumped. I created a method called "makeBed" in the (BedRoom) class that calls another method in the (Bed) class called "make". But when I try to call the method via bedroom.makeBed(); it does not work. I put a // in the part i'm trying to get to work.
Main
public class Main {
Wall wall1 = new Wall("West");
Wall wall2 = new Wall("East");
Wall wall3 = new Wall("South");
Wall wall4 = new Wall("North");
Ceiling ceiling = new Ceiling(12, 55);
Bed bed = new Bed("Modern", 4, 3, 2, 1);
Lamp lamp = new Lamp("classic", false, 75);
BedRoom bedroom = new BedRoom("test",wall1,wall2,wall3,wall4,ceiling,bed,lamp);
bedroom.makeBed(); //bedroom.makeBed(); does not work
//It does not show the public method "makeBed" in the BedRoom class
}
BedRoom Class
public class BedRoom {
private String name;
private Wall wall1;
private Wall wall2;
private Wall wall3;
private Wall wall4;
private Ceiling ceiling;
private Bed bed;
private Lamp lamp;
public BedRoom(String name, Wall wall1, Wall wall2, Wall wall3, Wall wall4, Ceiling ceiling, Bed bed, Lamp lamp) {
this.name = name;
this.wall1 = wall1;
this.wall2 = wall2;
this.wall3 = wall3;
this.wall4 = wall4;
this.ceiling = ceiling;
this.bed = bed;
this.lamp = lamp;
}
public Lamp getLamp(){
return this.lamp;
}
public void makeBed(){ //This is the method I'm trying to access
System.out.println("Bedroom -> Making bed");
bed.make();
}
}
Bed Class
public class Bed {
private String style;
private int pillows;
private int height;
private int sheets;
private int quilt;
public Bed(String style, int pillows, int height, int sheets, int quilt) {
this.style = style;
this.pillows = pillows;
this.height = height;
this.sheets = sheets;
this.quilt = quilt;
}
public void make(){
System.out.println("Bed -< Making"); //Method I'm trying to call
}
public String getStyle() {
return style;
}
public int getPillows() {
return pillows;
}
public int getHeight() {
return height;
}
public int getSheets() {
return sheets;
}
public int getQuilt() {
return quilt;
}
}
I want to say this is it... You don't have your code wrapped into a method so it not compiling right.
public class Main {
public static void main(String[] args) {
Wall wall1 = new Wall("West");
Wall wall2 = new Wall("East");
Wall wall3 = new Wall("South");
Wall wall4 = new Wall("North");
Ceiling ceiling = new Ceiling(12, 55);
Bed bed = new Bed("Modern", 4, 3, 2, 1);
Lamp lamp = new Lamp("classic", false, 75);
BedRoom bedroom = new BedRoom("test",wall1,wall2,wall3,wall4,ceiling,bed,lamp);
bedroom.makeBed(); //bedroom.makeBed(); does not work
//It does not show the public method "makeBed" in the BedRoom class
}
}
Maybe that will work.
This is on the assumption that THIS is your class you want to initilize the whole script from, if not change public static void main(String[] args){} into public Main() {}
So I'm working on a Space Invaders theme project, and I have most of my classes up and running and have started on the animation. Part of the process is the ship's weapons.
I have a class for the weapons, as below (Focus on the constructor):
/**
* #(#)Weapon.java
*
*
* #author Tristan Nel - 18179460
* #version 1.00 2015/3/4
*/
public class Weapon {
private String type;
private int damage;
private int rof; //rate of fire
private int orientation;
private int firingStage; //0 - not firing ; 1 - flash & recoil ; 2 - bullet
private String[] sprites; //Set of sprite image file names
public Weapon() {
}
public Weapon(String type, int damage, int rof, int orientation, int firingStage, String[] sprites)
{
this.type = type;
this.damage = damage;
this.rof = rof;
this.orientation = orientation;
this.firingStage = firingStage;
this.sprites = sprites;
}
//GET and SET Methods
public void setType(String type)
{
this.type = type;
}
public void setDamage(int damage)
{
this.damage = damage;
}
public void setROF(int rof)
{
this.rof = rof;
}
public void setOrientation(int orientation)
{
this.orientation = orientation;
}
public void setFiringStage(int firingStage)
{
this.firingStage = firingStage;
}
public void setSprites(String[] sprites)
{
this.sprites = sprites;
}
public String getType()
{
return this.type;
}
public int getDamage()
{
return this.damage;
}
public int getROF()
{
return this.rof;
}
public int getOrientation()
{
return this.orientation;
}
public int getFiringStage()
{
return this.firingStage;
}
public String[] getSprites()
{
return this.sprites;
}
}
In another class, which handles all elements on the game screen to be animated, I want to have a global array of hardcoded Weapon types that can be accessed as needed without fuss. I have attempted to do so at the top of the contents of the class:
/**
* #(#)GameScreen.java
*
*
* #author Tristan Nel - 18179460
* #version 1.00 2015/3/4
*/
import java.util.Scanner;
import java.io.*;
public class GameScreen {
private static final String HIGH_SCORE_FILE = "highScore.txt";
//Available Weapons
//UPDATED SINCE ORIGINAL POST
public static final Weapon[] WEAPONS = new Weapon[4];
WEAPONS[0] = new Weapon("Machinegun", 10, 20, 0, 0, {Graphics.MG_L_NORM, Graphics.MG_R_NORM});
WEAPONS[1] = new Weapon("Plasma MG", 20, 20, 0, 0, {Graphics.PMG_L_NORM, Graphics.PMG_R_NORM});
WEAPONS[2] = new Weapon("Photon Cannon", 40, 5, 0, 0, {Graphics.PC_L_NORM, Graphics.PC_R_NORM});
WEAPONS[3] = new Weapon("Alien Destabilizer", 60, 10, 0, 0, {Graphics.AD_L_NORM, Graphics.AD_R_NORM});
private Ship defender;
private Weapon equipped;
//private Invader[] aliens;
//private Bullet[] bullets;
private int score;
private int highscore;
private int lives;
public GameScreen() {
}
public GameScreen(Ship defender, int score, int lives)
{
this.defender = defender;
this.score = score;
this.lives = lives;
}
public void loadHighscore()
{
try
{
Scanner sc = new Scanner(new File(HIGH_SCORE_FILE));
this.highscore = Integer.parseInt(sc.next());
sc.close();
}
catch(FileNotFoundException fnf)
{
System.out.println(fnf);
this.highscore = 0;
}
}
public void saveHighScore(int highscore)
{
try
{
FileWriter write = new FileWriter(HIGH_SCORE_FILE);
PrintWriter pw = new PrintWriter(write);
pw.print(this.highscore);
pw.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
//GET and SET methods
public void setDefender(Ship defender)
{
this.defender = defender;
}
public void setScore(int score)
{
this.score = score;
}
public void setLives(int lives)
{
this.lives = lives;
}
public Ship getDefender()
{
return this.defender;
}
public int getScore()
{
return this.score;
}
public int getLives()
{
return this.lives;
}
}
This gives me the following error messages on each line that I try to add another element to the array:
UPDATED
https://drive.google.com/file/d/0B7ye7Ul2JDG2NDFDRTJNM1FCd0U/view?usp=sharing
It is highly frustrating..
I read somewhere that you have to create an object within a method? (Eg. main() )
But I tried that in my driver class and it made no difference...
Will appreciate any help/advice (:
There are multiple issues
You cannot have arbitrary code in the body of your class, e.g. the WEAPONS[0] = calls. However, you can initialize the array directly using new Type[]{} syntax. You could also use a static initializer static {} but this is not recommended.
Also, you need to use the constructor via new keyword, it's not just a method, i.e. new Weapon() not Weapon()
You cannot declare arrays using {}, i.e. new String[]{{Graphics.MG_L_NORM, Graphics.MG_R_NORM}} not {Graphics.MG_L_NORM, Graphics.MG_R_NORM}
Working version
public static final Weapon[] WEAPONS = new Weapon[] {
new Weapon("Machinegun", 10, 20, 0, 0, new String []{Graphics.MG_L_NORM, Graphics.MG_R_NORM}),
new Weapon("Plasma MG", 20, 20, 0, 0, new String []{Graphics.PMG_L_NORM, Graphics.PMG_R_NORM}),
new Weapon("Photon Cannon", 40, 5, 0, 0, new String []{Graphics.PC_L_NORM, Graphics.PC_R_NORM}),
new Weapon("Alien Destabilizer", 60, 10, 0, 0, new String []{Graphics.AD_L_NORM, Graphics.AD_R_NORM})
};
Actually I put it wrongly before but it looks like you need to call the constructor using the new operator like this.
arrayName[0] = new Weapon();
Since those classes seem somewhat static, something else to look into is using enums for this. This will help avoid complications when you have to search for a particular Weapon. A better design would be to have a WeaponType enum containing all the static immutable data in relation to a Weapon and have the Weapon class contain all the state data.
I'm not sure how eloquently I can really explain what I don't understand/need help with, I'm still Very new to Object Oriented Programming. This is regarding my coursework and I don't expect anyone to do it for me, I just need help understanding how to move on, and if I'm even on the right track.
Ok, so on to my question. Basically, I am attempting to create an arraylist which will hold a few objects which themselves has a bunch of information(obviously), my spec said to create an abstract class, which will be extended by my constructor class, which I did. The abstract class has a few variables (decided by spec) But I dont know how to move them over to my extended class.
I'll post my code below, and I hope it makes sense. I'd be very thankful for any help you all could provide. I'm very confused right now.
Basically, I would love to know, A) How do I create an object in my arraylist which will be able to contain everything in SportsClub and FootballClub, and preferably all the variables user inputted.
And B) I don't know how to print The object, When I print right now I get coursework.FootballClub#49233bdc, Which I'm sure there's a reason for but I need the information in the objects to display, E.g. name. And if possible to sort the results by alphabetical order with respect to name? I hope this is all written ok. Sorry and Thank you in advance.
package coursework;
import java.util.*;
/**
*
* #author w1469384
*/
public class PremierLeagueManager implements LeagueManager{
public static void main(String[] args) {
Scanner c1 = new Scanner(System.in);
Scanner c2 = new Scanner(System.in);
ArrayList<FootballClub> PL = new ArrayList<FootballClub>();
int choice;
System.out.println("Enter 1; To create a club, 2; To Delete a Club, 3; To display all clubs and 99 to close the program");
choice = c1.nextInt();
//Creates and adds a new FootballClub Object
while (choice != 99){
if (choice == 1){
System.out.println("Please Enter The games played for the club");
int played = c1.nextInt();
System.out.println("Please enter the number of wins");
int wins = c1.nextInt();
System.out.println("please enter the number of losses");
int losses = c1.nextInt();
System.out.println("please enter the number of draws");
int draws = c1.nextInt();
System.out.println("please enter the number of goals for");
int goalsFor = c1.nextInt();
System.out.println("please enter the number of goals against");
int goalsAgainst = c1.nextInt();
FootballClub club = new FootballClub(played, wins, losses, draws, goalsFor, goalsAgainst);
PL.add(club);
System.out.println("check");
}
//Deletes a FootballClub Object
if (choice == 2){
}
//Displays all Football Clubs in the PremierLeague array
if (choice == 3){
System.out.println(PL);
}
//Closes the Program 1
choice = c1.nextInt();
}
}
}
public abstract class SportsClub {
public String name;
public String location;
public int capacity;
public void setName(String Name){
name = Name;
}
public void setLocation(String Location){
location = Location;
}
public void setCapacity(int Capacity){
capacity = Capacity;
}
public String getName(){
return name;
}
public String getLocation(){
return location;
}
public int getCapacity(){
return capacity;
}
}
public class FootballClub extends SportsClub {
//Statistics for the club.
int played;
int wins;
int losses;
int draws;
int goalsFor;
int goalsAgainst;
public FootballClub(int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst){
played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;
}
public void setPlayed(int newPlayed){
played = newPlayed;
}
public void setWins(int newWins){
wins = newWins;
}
public void setLosses(int newLosses){
losses = newLosses;
}
public void setDraws(int newDraws){
draws = newDraws;
}
public void setGoalsFor(int newGoalsFor){
goalsFor = newGoalsFor;
}
public void setGoalsAgainst(int newGoalsAgainst){
goalsAgainst = newGoalsAgainst;
}
public int getPlayed(){
return played;
}
public int getWins(){
return wins;
}
public int getLosses(){
return losses;
}
public int getDraws(){
return draws;
}
public int getGoalsFor(){
return goalsFor;
}
public int getGoalsAgainst(){
return goalsAgainst;
}
}
FootballClub inherits the variables declared in SportsClub so you can set them as you please.
public FootballClub(
int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst,
String inName, String inLocation, int inCapacity
) {
played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;
// set the variables from the superclass
name = inName;
location = inLocation;
capacity = inCapacity;
}
FootballClub also inherits the methods declared in SportsClub so you can use the setters and getters too.
Normally you would create a constructor for SportsClub that sets these and then call that constructor from the FootballClub constructor.
// in SportsClub
protected SportsClub(
String inName, String inLocation, int inCapacity
) {
name = inName;
location = inLocation;
capacity = inCapacity;
}
// in FootballClub
public FootballClub(
int gPlayed, int gWins, int gLosses, int gDraws, int gFor, int gAgainst,
String inName, String inLocation, int inCapacity
) {
super(inName, inLocation, inCapacity);
played = gPlayed;
wins = gWins;
losses = gLosses;
draws = gDraws;
goalsFor = gFor;
goalsAgainst = gAgainst;
}
You should also make your member variables protected or private if you are using setters and getters.
I don't know how to print The object
You need to override toString. There is a short tutorial here.
Also unrelated side note: all Java variable identifiers should start with a lowercase letter.
When you have a method like this:
public void setName(String Name) { name = Name; }
It should be:
public void setName(String inName) { name = inName; }
Or:
public void setName(String name){ this.name = name; }