Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is my code: //NPC Class: import java.util.Random; public class NPC { static int numberOfNPCs; private String name; private String race; private float xPos; private

image text in transcribedimage text in transcribedimage text in transcribed

Here is my code:

//NPC Class:

import java.util.Random;

public class NPC {

static int numberOfNPCs;

private String name;

private String race;

private float xPos;

private float yPos;

private String dialogue[];

private boolean hasSequentialDialogue;

private int nextDialogue;

//Constructor

public NPC() {

this.name = "no name";

dialogue=new String[1];

dialogue[0]="PLACE HOLDER TEXT";

this.race = "no race";

this.xPos = (float) 5;

this.yPos = (float) 5;

this.hasSequentialDialogue = false;

this.nextDialogue += 1;

}

//Multi Arguement

public NPC (String name, String race, float xPos, float yPos,String[] dialogue, boolean hasSequentialDialogue) {

this.name = name;

this.race = race;

this.xPos = xPos;

this.yPos = yPos;

this.dialogue = dialogue;

this.hasSequentialDialogue = hasSequentialDialogue;

}

public String talk(){

String output;

Random rand = new Random();

int input = rand.nextInt(dialogue.length);

output = dialogue[input];

return output;

}

public void walk(String direction){

direction.toLowerCase();

if(direction.equals("left")) {

setxPos(this.getxPos()-1);

}else if (direction.equals("right")){

setxPos(this.getxPos()+1);

}else if (direction.equals("up")){

setyPos(this.getyPos()+1);

}else if (direction.equals("down")) {

setyPos(this.getyPos()-1);}

}

//Getter and Setter

public String getName() {

return this.name;

}

public void setName(String name) {

this.name = name;

}

public String getRace() {

return this.race;

}

public void setRace(String race) {

this.race = race;

}

public float getxPos() {

return this.xPos;

}

public void setxPos(float xPos) {

this.xPos = xPos;

}

public float getyPos() {

return this.yPos;

}

public void setyPos(float yPos) {

this.yPos = yPos;

}

public String[] getDialogue() {

return this.dialogue;

}

public void setDialogue(String[] dialogue) {

this.dialogue = dialogue;

}

public boolean isHasSequentialDialogue() {

return this.hasSequentialDialogue;

}

public void setHasSequentialDialogue(boolean hasSequentialDialogue) {

this.hasSequentialDialogue = hasSequentialDialogue;

}

public int getNextDialogue() {

return this.nextDialogue;

}

public void setNextDialogue(int nextDialogue) {

this.nextDialogue = nextDialogue;

}

public String toString(){

String output = "Name: " + name + " " +

"Race: " +race + " " +

"Location: (" + getxPos() + "," + getyPos() + ") " +

"Sequential: " + isHasSequentialDialogue() + " " +

"Number of NPCs: " + numberOfNPCs + " "+

"Dialog: " + talk();

return output;

}

}

//NPCMain

import java.util.Scanner;

import java.util.ArrayList;

import java.util.InputMismatchException;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.Scanner;

public class NPCMain {

public static void main(String [] args) {

Scanner scan = new Scanner(System.in);

String dialog1[] = {"I found myself at the gate of an ancient ruin in search of the Ring of Mimir.",

"Using the ancient Ellian language, I was able to enter it alongside my grandfater.",

"But as I touched the ring, my magic power was absorbed by it.",

"The ring disappeared, and now my quest is to find the ring and gain my power back."};

String dialog2[] = {"As a master swordsman, I am quick on my feet.",

"my only weakness is magic, but in my mind, there's nothing a sharp piece of steel can't beat.",

"Now I serve as a leader for a search party,",

"in search for two things: the magic power in my kingdom, and my missing sister"};

String dialog3[] = {"I am a marksman with ranged weapons but can call on the elements to fuel my magic attacks as well.",

"When cornered though, I have no problem striking enemies with my awesome kicks.",

"I am also able to fly through the air with my double jump. ",

"By harnessing the power of the natural world, I have the capability to further enhance my basic attacks and various skills "};

//Test Objects - DONE

NPC npc = new NPC();

NPC npc1 = new NPC("Aisha", "Asian", 23.4f, 26.9f, dialog1, true);

NPC npc2 = new NPC("Elsword", "Asian", 10.8f, 9.7f, dialog2, false);

NPC npc3 = new NPC("Rena", "N/A", 40.1f, 21.9f, dialog3, false);

ArrayList listNPC = new ArrayList();

listNPC.add(npc);

listNPC.add(npc1);

listNPC.add(npc2);

listNPC.add(npc3);

while(true){

int input = Menu1(listNPC);

if(input==1)

Menu2(npc);

else if(input==2)

Menu2(npc1);

else if(input==3)

Menu2(npc2);

else if(input==4)

Menu2(npc3);

else if(input == listNPC.size() + 1) {

CreateNPC(listNPC);

}

else if(input == listNPC.size() +2) {

System.out.println("Goodbye~");

System.exit(0);

}

}}

public static int Menu1(ArrayList listNPC) {

Scanner scan = new Scanner(System.in);

System.out.println("Choose from the Menu: ");

for (int i = 0; i

System.out.println((i + 1) + ". " + listNPC.get(i).getName());

}

System.out.println(listNPC.size() + 1 + ". Create new NPC");

System.out.println(listNPC.size() + 2 + ". ");

int input = scan.nextInt();

return input;

}

public static void Menu2(NPC npc) {

Scanner sc=null;

while(true){

System.out.println("Test Menu For :"+npc.getName());

System.out.println("1. Display NPC Information");

System.out.println("2. Talk to NPC");

System.out.println("3. Make NPC to walk up");

System.out.println("4. Make NPC to walk down");

System.out.println("5. Make NPC to walk left");

System.out.println("6. Make NPC to walk right");

System.out.println("7. Go Back to privous Menu");

System.out.println("Enter The number choice(1-7) :");

sc=new Scanner(System.in);

int choice=sc.nextInt();

if(choice==1)

System.out.println(npc.toString());

else if(choice==2)

System.out.println(npc.getName()+ " said: "+npc.talk());

else if(choice==3) {

npc.walk("up");

System.out.println(npc.getName() + " has walked up and the location is: (" + npc.getxPos() + "," + npc.getyPos() + ")");

}

else if(choice==4) {

npc.walk("down");

System.out.println(npc.getName() + " has walked down and the location is: (" + npc.getxPos() + "," + npc.getyPos() + ")");}

else if(choice==5) {

npc.walk("left");

System.out.println( npc.getName() + " has walked left and the location is: (" + npc.getxPos() + "," + npc.getyPos() + ")");}

else if(choice==6) {

npc.walk("right");

System.out.println(npc.getName() + " has walked right and the location is: (" + npc.getxPos() + "," + npc.getyPos() + ")");}

else if(choice==7)

break;

else

System.out.println("invalid choice try again");

}

}

public static void CreateNPC(ArrayList listNPC) {

Scanner sc = new Scanner(System.in);

Scanner sc1 = new Scanner(System.in);

String name,race;

float xPos;

float yPos;

boolean whether;

System.out.println("Please enter the NPC name: ");

name = sc.nextLine();

System.out.println("Please enter the NPC race: ");

race = sc.nextLine();

System.out.println("Please enter the NPC xPos and yPos: ");

xPos = sc.nextFloat();

yPos = sc.nextFloat();

System.out.println("If your NPC has sequential dialogue, enter 'true', if your NPC has random dialogue, enter 'false' ");

whether = sc.hasNextBoolean();

System.out.println("How many?");

int num;

num = sc1.nextInt();

System.out.println("Enter your dialogues: ");

String dialogue[] = new String[num];

for(int i =0; i

dialogue[i]=sc1.next();

}

NPC Newnpc = new NPC(name,race,xPos,yPos,dialogue,whether);

System.out.println(Newnpc);

listNPC.add(Newnpc);

}

please help me edit my code so that:

(1)numberOfNPCs: Add a static data field to your NPC class which will be an integer. This data field will have NO getters and setters. This data field shall increase by 1 every time a new NPC object is created. Remember that all static data is shared among all instances of your class. All classes should have the same value for this data field if you implement it correct. HINT: Only change this value in the constructors.

(2)

getDialogue(): Update this getter to return a deep copy of the dialogue array. Changes to the returned copy SHOULD NOT affect the array stored in the dialogue data field.

toString(): Update your toString() method to show the current value of numberOfNPCs. Add this information right before the printout of the dialogue. Follow the same general formatting as the toString() output example from the previous assignment.

walk(): Just to make this official, update your walk() function. Since the coordinates cannot be negative and since our default values are VERY negative. When the user calls walk for the first time, set x and y to 5 and 5 respectively. After that, verify that the direction your NPC can walk stays in the positive range and then set the final value if valid.

(3).Creating Custom NPCs:

Write a method which allows the user to create a new NPC object. The method should request the following information:

NPC name

Race

xPos and yPos

If the NPC will have sequential or random dialogue

Next will be to ask the user for the dialogue for the NPC. Ask the user how many lines of dialogue they will be entering. Use this value as the size of the dialogue array. Now have the user enter the dialogue one line at a time, until they have entered all the lines of dialogue.

BIG HINT: Recall what happens when you mix any of the Scanner methods with the nextLine() method and how to fix this. If you don't remember this behavior, you may find that your program will skip over requesting certain information. Think this through very carefully.

Once all of the information has been gathered, create a new NPC object, return it from this method, and store it in the ArrayList of NPC objects.

Please read all of the help that I needed. I am really stuck on this.

good answers will be rated high.

General Description: For this week's assignment, you will add some new features to your NPC project from last week Package Declaration and General Setup Please rename your package to "hw1_and_hw2" it you follow the prompts given by Eclipse, it should automatically update the package declarations of all files. Deductions will be given for programs which do not follow this requirement . Please include a multi-line comment at the top of each java file with your name and a briet description of what that class does. e See the Triangle java source code file for an example of this The NPc Class Additions: Data Fields: . numberONPCs Add a static data field to your NPC class which will be an integer. This data field will have NO getters and setters. This data field shall increase by 1 every time a new NPC object is created. Remember that all static data is shared among all instances of your class. All classes should have the same value for this data field if you implement it correct. HINT Only change this value in the constructors Methods getDialogue() Update this getter to return a deep copy of the dialogue array Changes to the retumed copy SHOULD NOT afect the array stored in the dialogue data field to String(): Update your toString) method to show the current value of numberONPCs. Add this information right before the printout of the dialogue. Follow the same general formatting as the tostringo output example from the previous assignment walk(): Just to make this official, update your walko function. Since the coordinates cannot be negative and since our default values are VERY negative. When the user calls walk for the first time, set x and y to 5 and 5 respectively. After that, verity that the direction your NPC can walk stays in the positive range and then set the final value if valid The NPCMain Class Additions: NPC ArrayList Store all of your NPC objects inside of an ArrayList o HINT For the initial 3 NPC objects, have a method which creates these objects, places them into an ArrayList, and then returns a reference to the ArrayList containing these objects. This will be the storage ArrayList that you will use for the rest of the testing Write a method which allows the user to create a new NPC object. The method should request the following information: Creating Custom NPCs: NPC name . Race XPos and yPos

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions