-3

My name is earth000.

I want to change the order of people and I want to everyoneAT()to be linked.

Explaining in detail with public class Lead,

white, zap black, zap yellow, kaboom

      ↓ li.swap()

black, zap yellow, kaboom white, zap

Please check Hunter, Swordman, clergyman and Group. The other code is a copy of the problem statement.



public class Human {
  private String name;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Human() {
    setName("no name");
  }

  public Human(String name) {
    this();
    setName(name);
  }

}

/////////////

public class Hunter extends Human{
   
  public Hunter(){}

  public Hunter(String name){
    super(name);
  }
  public String attack(){
    return "Yaa"; 
  }
}

///////

public class Swordman extends Hunter{
  public Swordman(){
  
  }
  public Swordman(String name){
    super(name);
  }
    
  public String attack(){
    return "zap";
  }
}

/////////////////

public class Clergyman extends Hunter{
 
  public clergyman(){  } 

  public clergyman(String name){
    super(name);
  }
  
  public String attack(){
    return "kaboom";
  }
}

///////////

import java.util.StringJoiner;
import java.util.ArrayList;
public class Humans <T extends Human> {
  ArrayList<T> people;
  public Humans() {
    people = new ArrayList<>();
  }
  public void add(T one) {
    people.add(one);
  }
  public int size() {
    return people.size();
  }

  @Override

  public String toString() {
    StringJoiner j = new StringJoiner(", ");
    for (T one : people)
      j.add(one.getName());
    return j.toString();
  }
}

///////////

import java.util.StringJoiner;
public class Group extends Humans<Hunter>{
  public Group(){
    super();
  }
  public void swap(){
   
    String lead = people(0);
      
    for(int i=0;i<size()-1;i++){
      people(i) = people(i+1);
    }

    people(size()-1) = lead;

    if(size()<=1){return;}
       
  }
  public String everyoneAT(){
    StringJoiner j = new StringJoiner(", ");
    for(T one : people){
       j.add(one.Attack());
     return j.toString();
    }
  }
}


//////////////////////First challenge


import java.human.*;
public class First{
  public static void main(String[] args){
    Group li = new Group();
    li.add(new Swordman("white"));
    li.add(new Swordman("black"));
    li.add(new Clergyman("yellow"));
    System.out.println(li);
    System.out.println(li.everyoneAT());
    li.swap();
    System.out.println(li);
    System.out.println(li.everyoneAT());
  }
}


/////////Second challenge

import java.Human.*;
public class Second{
  public static void main(String[] args){
    Group li = new Group();
    System.out.println(li);
    System.out.println(li.everyoneAT());
    li.swap();
    System.out.println(li);
    System.out.println(li.everyoneAT());
  }
}


///Third challenge

import java.human*;
public class Third{
  public static void main(String[] args){
    Group li = new Group();
    li.add(new Swordman("white"));
    System.out.println(li);
    System.out.println(li.everyoneAT());
    li.swap();
    System.out.println(li);
    System.out.println(li.everyoneAT());
  }
}


earth000
  • 1
  • 3

1 Answers1

0

You did some minor mistakes in your Group class.

First of all, it's necessary to understand the correct way to retrieve an element from an ArrayList by its index. You should retrieve as people.get(0). Also, in your case, people is a list of Hunters, so you cannot assign it to a String class. You should assign it to an Hunter type as follow:

    Hunter lead = people.get(0);

Also, to add an item to a specific index in an ArrayList, you should call add passing the index as first parameter. So, your method should look like this:

    public void swap() {
        Hunter lead = people.get(0);

        for (int i = 0; i < size() - 1; i++) {
            people.add(i, people.get(i + 1));
        }

        people.add(size() - 1, lead);

        if (size() <= 1) {
          return;
        }
    }

Your everyoneAT method also has a mistake. people is a list of Hunter, so your for loop should look like this:

    for (Hunter one : people) {
        j.add(one.attack());
    }
Community
  • 1
  • 1
Vitor Cavalcanti
  • 311
  • 3
  • 13