Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If any one can help fix my code from arraylist to array and full fill this requirements. // import java.util.Random; public class NPC { static

image text in transcribedimage text in transcribed

If any one can help fix my code from arraylist to array and full fill this requirements.

//

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 = 0;

// 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;

numberOfNPCs++; // increment numberOfNPCs++ when NPC is added, i have

// incremented numberOfNPCs at the last because if an

// exception will be thrown while constructing NPC

// object numberOfNPCs would not get incremented;

}

// Multi Argument

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;

numberOfNPCs++; // increment numberOfNPCs++ when NPC is added, i have

// incremented numberOfNPCs at the last because if an

// exception will be thrown while constructing NPC

// object numberOfNPCs would not get incremented;

}

public String talk() {

String output;

if (this.hasSequentialDialogue == true) {

if (this.nextDialogue

output = dialogue[nextDialogue];

this.nextDialogue++;

return output;

} else {

this.nextDialogue = 0;

this.nextDialogue++;

return dialogue[0];

}

} else {

Random rand = new Random();

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

output = dialogue[input];

return output;

}

}

public void walk(String direction) {

direction = direction.toLowerCase(); // also assign it to direction when

// you do lower case.

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

// as the position is to remain in positive only, hence resetting it

// to 5 if it will reach less than zero

if (this.getxPos() - 1

this.setxPos(5);

else

setxPos(this.getxPos() - 1);

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

// In this case position is only going in positive direction, hence

// let it go

this.setxPos(this.getxPos() + 1);

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

// In this case position is only going in positive direction, hence

// let it go

setyPos(this.getyPos() + 1);

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

// as the position is to remain in positive only, hence resetting it

// to 5 if it will reach less than zero

if (this.getyPos() - 1

this.setyPos(5);

else

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() {

// please note that dialogue is an array of String objects, and Since

// Strings are immutable in Java, one cannot modify your a String

// Object,

// however if you still want to return deep copy, below is how you can

// do it, and preserve your dialogue array from outside interference

// once created

String[] copyOfDialogue = new String[this.dialogue.length];

// copy the contents of dialogue and return

for (int i = 0; i

copyOfDialogue[i] = this.dialogue[i];

}

return copyOfDialogue;

}

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;

}

// numberOfNPCs already correct in toString()

public String toString() {

String output = "Name: " + name + " " + "Race: " + race + " " + "Location: (" + getxPos() + "," + getyPos()

+ ") " + "Sequential: " + isHasSequentialDialogue() + " " + "Number of NPCs: " + numberOfNPCs + " "

+ "Dialog: " + talk();

return output;

}

}

NPCMain.java

import java.util.ArrayList;

import java.util.InputMismatchException;

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 > 4 && listNPC.size() > 4 && input

Menu2(listNPC.get(input-1));

}

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

NPC newNPC = CreateNPC(listNPC);

if(newNPC != null) {

listNPC.add(newNPC);

}

} 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 NPC CreateNPC(ArrayList listNPC) {

Scanner sc = new Scanner(System.in);

Scanner sc1 = new Scanner(System.in);

String name, race;

float xPos = 0;

float yPos = 0;

boolean whether = false;

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");

boolean isxPosEntered = false;

while (!isxPosEntered) {

try {

xPos = sc.nextFloat();

isxPosEntered = true;

} catch (InputMismatchException e) {

sc.nextLine(); // clear buffer, it is very important as if you do not do this, it will not prompt user for re-entering the new value and consider the value only

System.out.println("Enter a integer or a float value only..");

}

}

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

boolean isyPosEntered = false;

while (!isyPosEntered) {

try {

yPos = sc.nextFloat();

isyPosEntered = true;

} catch (InputMismatchException e) {

sc.nextLine(); // clear buffer, it is very important as if you do not do this, it will not prompt user for re-entering the new value and consider the value only

System.out.println("Enter a integer or a float value only..");

}

}

System.out.println(

"If your NPC has sequential dialogue, enter 'true', if your NPC has random dialogue, enter 'false' ");

boolean isBooleanEntered = false;

while (!isBooleanEntered) {

try {

whether = sc.nextBoolean();

isBooleanEntered = true;

} catch (InputMismatchException e) {

sc.nextLine(); // clear buffer, it is very important as if you do not do this, it will not prompt user for re-entering the new value and consider the value only

System.out.println("Enter only true or false");

}

}

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

int num = 0;

boolean isNumEntered = false;

while (!isNumEntered) {

try {

num = sc1.nextInt();

isNumEntered = true;

} catch (InputMismatchException e) {

sc1.nextLine(); // clear buffer, it is very important as if you do not do this, it will not prompt user for re-entering the new value and consider the value only

System.out.println("Enter only integer..");

}

}

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);

return Newnpc;

}

}

Data Fields: numberOfNPCs: Add a static data field to your NPC class which will be an integer. This data field will only have a getter, no setter. 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 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. The NpcMain Class Additions: NPC Array: o Store all of your NPC objects inside of an array of type NPC. HINT: For the initial 3 NPC objects, have a method which creates these objects, places them into an array, and then returns a reference to the array containing these objects. This will be the storage array that you will use for the rest of the testing. Create a static method in your NpcMain class called resizeNpcArray: o This method shall accept an array of NPC objects as input. o The purpose of this method is the expand our NPC array by one, making room for a new NPC object to be added. This method shall create a new empty array of NPC objects that is +1 size of the original NPC array that is passed to the method. o This method shall copy the data from the input array to the new array. If the input array has 4 NPC objects at indexes 0 - 3, then the new empty array will have a size of 5 and the 4 existing objects will be copied to indexes 0 - 3 of the new array, with index 4 of the new array remaining empty to make room for adding a new NPC object. . You must use the System.arraycopy(src, src_index, dest, dest_index, length) method to copy the data from the original array to the new array. This method shall return the new array now populated with the NPC objects previously created, and with an empty space at the end to add a new NPC object. Creating Custom NPCs: o Write a static method called createNewNPC() that takes no parameters. o This method shall allow the user to create a new NPC object and return the new NPC object from the method. The createNewNPC() method should request the following information: NPC name Race Pos and y Pos 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 0 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. O 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. o Once all of the information has been gathered, create a new NPC object, return it from this method, and store it in the array of NPC objects. HINT: You will need to make sure your resize method is fully functional and that it can correctly create space in a new array of NPC objects to make room to add the object created by the createNewNPC() method. Dynamically Updating Menu 1 Now that the user has the ability to create custom NPC objects, you will need to dynamically generate the text and options for Menu 1. o Hopefully you already thought about this and have implemented code in your Homework 03 to accommodate this change. Example if there are 4 NPC objects, then the first 4 options of Menu 1 should be the names of the 4 NPCs. The last two options will always be to create a new NPC and to exit the program. Example menu text below: Choose an NPC 1. 2. Insert second npc name here> 3. Insert third npc name here> 4. 5. Create new NPC. 6. Exit Program Enter the number choice (1-6): Example if there are 7 NPC objects, then the first 7 options of Menu 1 should be the names of the 7 NPCs. The last two options will always be to create a new NPC and to exit the program. Choose an NPC 1. Insert first npc name here 2. 3. Insert third npc name here> 4. Insert fourth npc name here> 5. 6. Insert sixth npc name here> 7. 8. Create new NPC. 9. Exit Program Enter the number choice (1-9): HINT: Properly breaking your NPCMain file into smaller, more manageable methods will go a VERY long way to helping you implement the NPC creation feature. A good rule of thumb is this: if you cannot succinctly describe what your method is doing in one or two sentences, it probably can be broken down into smaller methods. Also, I really like this person's post on Stack Overflow about method length. Give it a read please, you may find it very informative

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

Students also viewed these Databases questions

Question

1. Are my sources credible?

Answered: 1 week ago

Question

3. Are my sources accurate?

Answered: 1 week ago

Question

1. Is it a topic you are interested in and know something about?

Answered: 1 week ago