The World class has an attribute named creatures, which is an ArrayList of objec
ID: 3840295 • Letter: T
Question
The World class has an attribute named creatures, which is an ArrayList of objects of type Creature. The Creature, Wizard and Warrior classes have the attributes as well as the getter and setter methods you designed in Problem 1. Write a method, for the World class, named findPowerfulWizards that receives one parameter (a number of potions), and creates and returns a new ArrayList of Wizard objects containing only those wizards whose number of potions is larger than the parameter. (For partial credit, have your method just find and return the number of wizards whose number of potions is more than the parameter.)Explanation / Answer
package chegg;
import java.util.ArrayList;
import java.util.Iterator;
public class World {
ArrayList<Wizard> res=new ArrayList<Wizard>();
static ArrayList<Wizard> wal=new ArrayList<Wizard>();
public static void main(String[] arg)
{
Wizard w1=new Wizard();
w1.setWizardName("x");
w1.setNoOfPotions(3);
Wizard w2=new Wizard();
w2.setWizardName("Y");
w2.setNoOfPotions(5);
Wizard w3=new Wizard();
w3.setWizardName("z");
w3.setNoOfPotions(10);
wal.add(w1);
wal.add(w2);
wal.add(w3);
}
public ArrayList<Wizard> findPowerfulWizard(int noOfPotions)
{
Wizard temp=new Wizard();
Iterator<Wizard> itr=wal.iterator();
while(itr.hasNext())
{
temp=itr.next();
if(temp.getNoOfPotions()>noOfPotions)
res.add(temp);
}
return res;
}
}
class Creature
{
}
class Wizard
{
int noOfPotions=0;
String wizardName;
public int getNoOfPotions() {
return noOfPotions;
}
public void setNoOfPotions(int noOfPotions) {
this.noOfPotions = noOfPotions;
}
public String getWizardName() {
return wizardName;
}
public void setWizardName(String wizardName) {
this.wizardName = wizardName;
}
}
class Warrior
{
}
package chegg;
import java.util.ArrayList;
import java.util.Iterator;
public class World {
ArrayList<Wizard> res=new ArrayList<Wizard>();
static ArrayList<Wizard> wal=new ArrayList<Wizard>();
public static void main(String[] arg)
{
Wizard w1=new Wizard();
w1.setWizardName("x");
w1.setNoOfPotions(3);
Wizard w2=new Wizard();
w2.setWizardName("Y");
w2.setNoOfPotions(5);
Wizard w3=new Wizard();
w3.setWizardName("z");
w3.setNoOfPotions(10);
wal.add(w1);
wal.add(w2);
wal.add(w3);
}
public ArrayList<Wizard> findPowerfulWizard(int noOfPotions)
{
Wizard temp=new Wizard();
Iterator<Wizard> itr=wal.iterator();
while(itr.hasNext())
{
temp=itr.next();
if(temp.getNoOfPotions()>noOfPotions)
res.add(temp);
}
return res;
}
}
class Creature
{
}
class Wizard
{
int noOfPotions=0;
String wizardName;
public int getNoOfPotions() {
return noOfPotions;
}
public void setNoOfPotions(int noOfPotions) {
this.noOfPotions = noOfPotions;
}
public String getWizardName() {
return wizardName;
}
public void setWizardName(String wizardName) {
this.wizardName = wizardName;
}
}
class Warrior
{
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.