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

This assignment deals with string manipulation problems. ? Use only the charAt,

ID: 3663370 • Letter: T

Question

This assignment deals with string manipulation problems.

?   Use only the charAt, equals, length, substring, and toUpperCase methods of the String class (see the Java API for details) in your solutions.
?   The methods you’re writing for this assignment simply make use of String objects. You’re not actually writing new methods for Java’s built-in String class! Thus, all your methods can be declared as static, just like most of the ones you wrote in COMP 1900.

? The 1980 computer game Rogue is a dungeon crawler that features procedurally generated levels and simple text-based “graphics” as shown below. Procedurally generated means that the levels are automatically produced by an algorithm and are different each time the game is played, as opposed to having levels manually designed by a human. Rogue inspired an entire genre of similar games called Roguelikes that have an active player base even today.

   |   vertical wall
   -   horizontal wall
   .   room space
   #   corridor
   +   doorway

You want to write some code to help you analyze Rogue levels. Because the levels contain only text characters, this is a great chance to practice working with strings!

A. ? Write a method countDoors(String s). This method takes a line from a Rogue level as a parameter, and it returns the number of doorways in that line.

Example:

countDoors("----+---- # ---+-----") should return 2.


B. ? Write a method countFeatures(String s). This is similar to the countDoors method, but it returns an array containing the number of vertical walls, horizontal walls, room spaces, corridors, and doorways (in that order).

Example:

countFeatures("----+---- # ---+-----") should return the array {0, 16, 0, 1, 2}.

Explanation / Answer

public static int countDoors(String str) {
       int noOfDoors = 0;
       int i = 0;
      
       while( i < str.length()) {
           if(str.charAt(i)== '+') {
               noOfDoors++;
           }
           i++;
       }
      
       return noOfDoors;
      
   }
  
   public static int[] countFeatures(String s) {
       int[] features = new int[5];
       int i = 0;
      
       while( i < s.length()) {
           if(s.charAt(i)== '|') {
               features[0]++;
           }else if(s.charAt(i)== '-') {
               features[1]++;
           }else if(s.charAt(i)== '.') {
               features[2]++;
           }else if(s.charAt(i)== '#') {
               features[3]++;
           }else{
               features[4]++;
           }
          
           i++;
       }
      
       return features;
   }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote