Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MonsterAttack class in Java Each object of this class represents information abo

ID: 3791786 • Letter: M

Question

MonsterAttack class in Java

Each object of this class represents information about a single attack. The data on each attack includes an integer attack id, three ints representing the day of the month, the month, and the year of the attack, the name of the monster involved (assume that each attack involves only one monster), the location of the attack (String), and the name of the person reporting the attack (String).

MonsterAttack needs a constructor that sets all the instance fields listed above using its parameters. It should take the three components of the date as a single String in the format MM/DD/YYYY (eg, 01/31/2010). Use String's split method to split this String on the slashes, parse the resulting Strings to ints, and use them to set the values of the month, day, and year.

Monster attack also needs getters and setters for all the data fields and a toString() that uses the instance data to create and return a String similar to this: "Attack # 125: Godzilla attacked Tokyo on 6/25/2009. Reported by Kyohei Yamane."

Explanation / Answer

Hi buddy, please find the below java program

import java.util.*;
import java.lang.*;
import java.io.*;


class MonsterAttack{
    private int attackId, day, month, year;
    private String monsterName, location;
    MonsterAttack(int attackId, String date, String monsterName, String location){
        this.attackId = attackId;
        this.monsterName = monsterName;
        //Parsing the dates
        String ds[] = date.trim().split("/");
        this.month = Integer.parseInt(ds[0]);
        this.day = Integer.parseInt(ds[1]);
        this.year = Integer.parseInt(ds[2]);
        this.location = location;
    }
    //Getter methods
    public int getAttackId(){
        return attackId;
    }
    public int getDay(){
        return day;
    }
    public int getMonth(){
        return month;
    }
    public int getYear(){
        return year;
    }
    public String getMonsterName(){
        return monsterName;
    }
    public String getLocation(){
        return location;
    }
    //Setter methods
    public void setAttackId(int attackId){
        this.attackId = attackId;
    }
    public void setDay(int day){
        this.day = day;
    }
    public void setMonth(int month){
        this.month = month;
    }
    public void setYear(int year){
        this.year = year;
    }
    public void setMonsterName(String monsterName ){
        this.monsterName = monsterName;
    }
    public void setLocation(String location){
        this.location = location;
    }
    //This method converts the variables into string
    @Override
    public String toString(){
        return "Attack # "+attackId+" "+monsterName+" Attacked "+location+" on "+month+"/"+day+"/"+year+" Reported by Kyohei Yamane.";
    }
}
class Main
{
   public static void main (String[] args) throws java.lang.Exception
   {
       MonsterAttack attack = new MonsterAttack(120,"02/14/2017","Argentinosaurus","Newyork");
       System.out.println(attack);
   }
}


OUTPUT :

Attack # 120 Argentinosaurus Attacked Newyork on 2/14/2017 Reported by Kyohei Yamane.