Solved programming problem

Monster

Source code

Question:

 

 1. Consider the Monster class.

 class Monster { String monsterName; int weight; String favouriteFood;  }  Monsters live in MonsterLand. We want store all the information about all the monsters in MonsterLand. Make an ArrayList that will store the information about all the monsters in MonsterLand. Add at least 10 dummy monsters in the ArrayList. Monsters are forgetful creatures and often enter their information more than once. The monsters in charge of making the list did not notice this and they allow the same information to be entered more than once.

2. Print all the elements of the ArrayList along with the number of monsters in MonsterLand.

3. The monster who was the second one to enter his information in the system came running. He had accidentally eaten one whole elephant and his weight has increased by 100 kg. Update the weight of this monster.

4. The fattest and the third fattest monster left the town to go elsewhere. Their information was erased from the list. Print the remaining monsters' information in descending order. Use the sort function for nding out the fattest and third fattest monster and if necessary, for other things.

Main Class: 

package monster;

import java.util.Scanner;

import java.util.ArrayList;

import java.util.Collections;

public class TestMonster {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        ArrayList<Monster> M = new ArrayList<>();

        for (int i = 0; i < 10; i++) {

            String name;

            String food;

            int weight;

            Monster newmonster = new Monster();

            System.out.println("Name: ");

            name = s.nextLine();

            System.out.println("Weight: ");

            weight = s.nextInt();

             s.nextLine();

            System.out.println("Food: ");

            food = s.nextLine();

            newmonster.setName(name);

            newmonster.setWeight(weight);

            newmonster.setfavouriteFood(food);

            M.add(newmonster);

        }

        Update u = new Update();

        u.setMonster(M);

        System.out.println("Monster 2 Weighr Updated.");

        u.updateWeight();

       

       

        Print p = new Print();

        p.setMonster(M);

        System.out.println("All Monster In Monster Land:");

        p.printMonster();

        Collections.sort(M, new Sortbyweight());

        M.remove(0);

        M.remove(1);///after delete 1st element all index decrese by 1

       

        System.out.println("Max and 3rd Max Weight Monster Info successfully Deleted.");

     

         p.setMonster(M);

         p.printMonsterRev();

    }

}

Monster Class:

package monster;

import java.util.*;

public class Monster {

    String monsterName;

    int weight;

    String favouriteFood;

    public void setName(String name) {

        this.monsterName = name;

    }

    public void setWeight(int weight) {

        this.weight = weight;

    }

    public void setfavouriteFood(String favouriteFood) {

        this.favouriteFood = favouriteFood;

    }

    public String getName() {

        return monsterName;

    }

    public int getWeight() {

        return weight;

    }

    public String getFood() {

        return favouriteFood;

    }

}

 

Update Class:

 

 

package monster;

 

import java.util.ArrayList;

 

/**

 *

 * @author Sabit

 */

public class Update 

{

    

   ArrayList<Monster>M = new ArrayList<>();

 

    

    public void setMonster(ArrayList M)

    {

        this.M = M;

    }  

    

    public void updateWeight()

    {

        M.get(1).setWeight(M.get(1).getWeight()+100);

    }

    

}

 

Sortbyweight Class:

 

package monster;

import java.util.*;

 

class Sortbyweight implements Comparator<Monster>

{

   

    public int compare(Monster a, Monster b)

    {

        return a.weight - b.weight;

    }

}

 

 

Print Class:

 

 

package monster;

 

import java.util.ArrayList;

 

 

public class Print extends Monster

{

  ArrayList<Monster>M = new ArrayList<>();

  Monster ms= new Monster();

    

    public void setMonster(ArrayList M)

    {

        this.M = M;

    }  

    

    public void printMonster()

    {

        int i=1;

        

        for(Monster x: M)

        {   

            System.out.println("Position: "+i++);

            ms=x;

            System.out.println("Name: "+ms.getName());

            System.out.println("Weight: "+ms.getWeight());

            System.out.println("Food: "+ms.getFood());

        }

    }

    

    public void printMonsterRev()

    {  

        System.out.println("Reverse: "); 

        for(int i =M.size()-1;i>=0;i--)

        {   

            

            System.out.println("Position: "+(i+1));

       

            System.out.println("Name: "+M.get(i).monsterName);

            System.out.println("Weight: "+M.get(i).weight);

            System.out.println("Food: "+M.get(i).favouriteFood);

        }

    }

}
Join the discussion

Add your comment