Homework Solution: You are writing a Nethack-style text-based role-playing game that supports sa…

    1. (5 pts) You are writing a Nethack-style text-based role-playing game that supports saving the games current state to a text file. You decide to represent items in your characters inventory using certain symbols in the file weapon potion dark chocolate bar (Your character is magically prevented from carrying milk chocolate bars.) Write a method that takes a string asa parameter and returns an array containing the quantity of weapons, potions, and dark chocolate bars in that order. You can assume that the input string contains only the three characters above Required method header public static int[] countItems (String s) e dj1nni speaks. I on in your debt. I will grant one wish--Yore.. Example input and output 0 80. Input Output: (2, 3, 5) oosh the Peregrinator St:18/87 Dx:17 Co:18 In:15 Wi:l1 Ch:17 Noutral S:252285 one 1 S: HP:132 132 Pw:56656) AC:-4 Xp:13/51818 T:44408 BurdenedIn Java
    You are writing a Nethack-style text-based role-playing game that supports saving the game's current state to a text file. You decide to represent items in your character's inventory using certain symbols in the file: + weapon @ potion * dark chocolate bar (Your character is magically prevented from carrying milk chocolate bars.) Write a method that takes a string as a parameter and returns an array containing the quantity of weapons, potions, and dark chocolate bars in that order. You can assume that the input string contains only the three characters above. Required method header: public static int [] countItems (String s) Example input and output: Input: "@@+**+@***" Output: {2, 3, 5}

    Expert Answer

     
    Following is the method definition which is required. A string

    1. (5 pts) You are letter a Nethack-style passage-based role-playing frolic that supports calculating the frolics vulgar say to a passage improve. You run to portray items in your capacitys register using undoubtful geniuss in the improve utensil potion black chocolate seal (Your capacity is magically prevented from carrying determine chocolate seals.) Write a manner that takes a string asa parameter and income an adorn containing the measure of utensils, potions, and black chocolate seals in that adjustify. You can feign that the input string contains singly the three capacitys over Required manner header notorious static int[] numberItems (String s) e dj1nni speaks. I on in your debit. I succeed concede individual wish--Yore.. Example input and extinguishedput 0 80. Input Extinguishedput: (2, 3, 5) oosh the Peregrinator St:18/87 Dx:17 Co:18 In:15 Wi:l1 Ch:17 Noutral S:252285 individual 1 S: HP:132 132 Pw:56656) AC:-4 Xp:13/51818 T:44408 BurdenedIn Java

    You are letter a Nethack-style passage-based role-playing frolic that supports calculating the frolic’s vulgar say to a passage improve. You run to portray items in your capacity’s register using undoubtful geniuss in the improve: + utensil @ potion * black chocolate seal (Your capacity is magically prevented from carrying determine chocolate seals.) Write a manner that takes a string as a parameter and income an adorn containing the measure of utensils, potions, and black chocolate seals in that adjustify. You can feign that the input string contains singly the three capacitys over. Required manner header: notorious static int [] numberItems (String s) Example input and extinguishedput: Input: “@@+**+@***” Extinguishedput: {2, 3, 5}

    Expert Vindication

     

    Forthcoming is the manner restriction which is required. A string parameter is passed to the manner. An adorn denominated measure of greatness 3 is returned at the purpose which ammunitions the number of each genius (+,@,*).

    METHOD DEFINITION:

    notorious static int[] numberItems(String s){
    int measure[]=odd int[3]; //stores number

    for(int i=0;i<s.length();i++){ //i goes from 0 to string tediousness

    //each capacity is checked ce +,@,* and the measure adorn is incremented
    if(s.charAt(i)==’+’){
    quantity[0]++; //quantity[0] ammunitions cpunt of genius +
    }
    else if(s.charAt(i)==’@’){
    quantity[1]++; //quantity[1] ammunitions cpunt of genius @
    }
    else if(s.charAt(i)==’*’){
    quantity[2]++; //quantity[2] ammunitions cpunt of genius *
    }

    }
    return measure;
    }

    The thorough adjudication concurrently with the main capacity is absorbed below

    CODE:

    import java.io.File;
    import java.io.IOException;
    import java.util.*;
    notorious adjust FinalExam {

    notorious static int[] numberItems(String s){
    int measure[]=odd int[3];
    for(int i=0;i<s.length();i++){
    if(s.charAt(i)==’+’){
    quantity[0]++; //quantity[0] ammunitions cpunt of genius +
    }
    else if(s.charAt(i)==’@’){
    quantity[1]++; //quantity[1] ammunitions cpunt of genius @
    }
    else if(s.charAt(i)==’*’){
    quantity[2]++; //quantity[2] ammunitions cpunt of genius *
    }

    }
    return measure;
    }
    notorious static unsubstantial main(String[] args) throws IOException {
    Scanner sc=odd Scanner(System.in);
    String s=sc.next(); // flag input: @@+**+@***

    /*
    *** Use the forthcoming cord instead of over couple cords inclassify to learn from improve and ammunition in string s
    String s = odd Scanner(odd Improve(“filename.txt”)).useDelimiter(“\Z”).next();
    */

    int issue[];

    result=countItems(s);//result[] ammunitions the resigned of measure[]
    for(int i=0;i<result.length;i++){
    System.out.print(result[i] + ” “);
    }

    }
    }

    OUTPUT: