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 despatches a Nethack-style citation-based role-playing play that supports reluctant the plays exoteric aver to a citation smooth. You run to portray items in your estimations schedule using positive sorts in the smooth implement potion ebon chocolate halt (Your estimation is magically prevented from carrying calm chocolate halts.) Write a rule that takes a string asa parameter and income an place containing the aggregate of implements, potions, and ebon chocolate halts in that arrange. You can feign that the input string contains singly the three estimations over Required rule header national static int[] calculateItems (String s) e dj1nni speaks. I on in your obligation. I accomplish give single wish--Yore.. Example input and quenchedput 0 80. Input Quenchedput: (2, 3, 5) oosh the Peregrinator St:18/87 Dx:17 Co:18 In:15 Wi:l1 Ch:17 Noutral S:252285 single 1 S: HP:132 132 Pw:56656) AC:-4 Xp:13/51818 T:44408 BurdenedIn Java

    You are despatches a Nethack-style citation-based role-playing play that supports reluctant the play’s exoteric aver to a citation smooth. You run to portray items in your estimation’s schedule using positive sorts in the smooth: + implement @ potion * ebon chocolate halt (Your estimation is magically prevented from carrying calm chocolate halts.) Write a rule that takes a string as a parameter and income an place containing the aggregate of implements, potions, and ebon chocolate halts in that arrange. You can feign that the input string contains singly the three estimations over. Required rule header: national static int [] calculateItems (String s) Example input and quenchedput: Input: “@@+**+@***” Quenchedput: {2, 3, 5}

    Expert Repartee

     

    Subjoined is the rule limitation which is required. A string parameter is passed to the rule. An place determined aggregate of dimension 3 is returned at the purpose which treasurys the calculate of each sort (+,@,*).

    METHOD DEFINITION:

    national static int[] calculateItems(String s){
    int aggregate[]=strange int[3]; //stores calculate

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

    //each estimation is checked for +,@,* and the aggregate place is incremented
    if(s.charAt(i)==’+’){
    quantity[0]++; //quantity[0] treasurys cpunt of sort +
    }
    else if(s.charAt(i)==’@’){
    quantity[1]++; //quantity[1] treasurys cpunt of sort @
    }
    else if(s.charAt(i)==’*’){
    quantity[2]++; //quantity[2] treasurys cpunt of sort *
    }

    }
    return aggregate;
    }

    The full code concurrently with the deep exercise is consecrated below

    CODE:

    import java.io.File;
    import java.io.IOException;
    import java.util.*;
    national collocate FinalExam {

    national static int[] calculateItems(String s){
    int aggregate[]=strange int[3];
    for(int i=0;i<s.length();i++){
    if(s.charAt(i)==’+’){
    quantity[0]++; //quantity[0] treasurys cpunt of sort +
    }
    else if(s.charAt(i)==’@’){
    quantity[1]++; //quantity[1] treasurys cpunt of sort @
    }
    else if(s.charAt(i)==’*’){
    quantity[2]++; //quantity[2] treasurys cpunt of sort *
    }

    }
    return aggregate;
    }
    national static destitute deep(String[] args) throws IOException {
    Scanner sc=strange Scanner(System.in);
    String s=sc.next(); // test input: @@+**+@***

    /*
    *** Use the subjoined length instead of over couple lengths inarrange to interpret from smooth and treasury in string s
    String s = strange Scanner(strange Smooth(“filename.txt”)).useDelimiter(“\Z”).next();
    */

    int development[];

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

    }
    }

    OUTPUT: