Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3) Colorado State University Logo | Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)
Computer Science

Lab 9 & 10 - Traveling Alchemist the Almost MUD

In this assignment, we focus heavily on String and text manipulation. This is based on the old MUDs, Muli - User Dungeons, which are some of the first examples of virtual worlds online. This program also includes GIS positioning data (latitude and longitude) and a formula used to calculate distances in GIS applications. As such, through the game we are learning about GIS and also some of the first MMORPGs around.

** NOTE: This is a multi day long lab worth additional points **

In this lab you will learn how to use several different String methods such as .substring() and .indexOf() if you need some further information on how to use String methods click here or here

What You’ll Learn

  • String methods
  • Program flow control
  • Pattern Matching

Step 0 - Reading the Code

For this assignment, you will see that there are two different files by clicking the dropdown above the editor. The files are as follows.

  1. AlchemistHelper.java
    This file is the one you will edit. We already put the method signatures in the file, and some ‘dummy’ information so the program will compile. One important method to note is ‘testMethod’. You will put your tests in this method as you write your code, as it is executed first in the program flow.
  2. TravelingAlchemist.java
    This file looks very complex, but really it has to do with reading the files, and printing information to the screen. It is worth looking through it all, both to find the easter egg built into the program, and to see examples of String methods. After you turn in the assignment, you may want to mess with this file. There are todo parts for every skill level in it, from splash screens to save features.

As with all Step 0s you should put your name and email in AlchemistHelper.java

Step 1 - getCityInfo

For getCityInfo, you will be pulling a substring from a much larger String. The String may change, but in general, the format is

Fort Collins,40°35'6.9288"N,105°5'3.9084"W;Denver,39°44'31.3548"N,104°59'29.5116"W;Boulder,40°0'53.9424"N,105°16'13.9656"W

That looks complicated, but what we are really looking at the semi-colons. If we ignore the complexity, the String could simply look like:

SomeCity;AnotherCity;YetAnotherCity

This second String is easier to think about. The goal of getCityInfo is to take in the String of the city we are looking for - basically the first part of the city string, the list of cities, and the deliminator (which will be a semi-colon ; in the above case).

As such, if someone is looking for

fort

then

Fort Collins,40°35'6.9288"N,105°5'3.9084"W

is returned (notice, no semi-colon).

Let’s get started!

Writing getCityInfo

You have three parameters, the cityList, destination, and the deliminator. The method signature is already done for you, so best to find it in the code below. Also know that given a String, you can take its substring, and you can also search for the indexOf a certain character, which is essential for writing this.

  • First when writing this method, know we want it to be case-insensitive. While the choice is yours, we found .toLowerCase() to be useful (which is a String method) - as it forced the city name in cityList to be lowercase.

  • Understanding destination - this is the ‘start’ location of the substring. As such:
      int start = cityListLower.indexOf(destinationLower); 
    

    will give you the starting index of the city name, assuming you have created a lowercase version of both cityList and destination.

  • The end index is trickier. .indexOf(String, int) will search for the index of the String starting at location int. This could be extremely useful in helping you find the deliminator after the start location! However, what if the city is the last one in the list? Notice there is no deliminator after the last city!

  • Taking both the start and end index, return the substring. Reminder that substrings the end index is exclusive so the end index provided will not be included in the substring.

HINT to make this method go easier, put in println’s throughout the method as you figure out the indices. It also helps to write down a String, and the write the index locations under the string, so you can see the relationships.

Testing getCityInfo

If you go to the testMethod in the file, you will see an example of how to test getCityInfo. Uncomment it. I would also put in other options searching for denver to test what you wrote with different input.

It is important to note that the cityInfo line can change: cities may not be there, the order of the cities may change, and new ones can be added.

Step 2 - getCityName

getCityName takes in a cityData String that consists of the format:

<name>,<latitude>,<longitude>

And returns just the name portion of the String.

Does this look familiar? Look at what the last method you wrote returned. Since this format will be exactly the same for every cityData that is passed in, you can use substring, and index of to get the portion from the String.

Writing getCityName

This method returns a substring of the cityData from 0 to the indexOf of the deliminator that is passed into the method. You can do this method in one or three lines.

Testing getCityName

You will see a sample in testMethod that you need to uncomment. Feel free to try with other cityData info.

Step 3 - getCityLongitude

getCityLongitude takes in a cityData String that consists of the format:

<name>,<latitude>,<longitude>

and returns just the longitude portion of the String. In this case, you are returning from the deliminator to the end of the String. It is important to note that substring with one parameter takes from that location to the end of the line!

So String str = “hello world”; str.substring(5) returns world.

Writing getCityLongitude

Return the substring of the cityData from the .lastIndexOf() of the deliminator to the end of the String.

Testing getCityLongitude

You will see a sample in testMethod that you need to uncomment. Feel free to try with other cityData info.

Step 4 - getCityLatitude

getCityLongitude takes in a cityData String that consists of the format:

<name>,<latitude>,<longitude>

and returns just the latitude portion of the String.

This is harder as you are grabbing the middle of the String. However, reading what you did in getCityName, you know how to get the first indexOf the deliminator and the lastIndexOf the deliminator from reading getLongitude. Take those two ideas to figure out the range of your substring.

Writing getCityLatitude

Return the substring of the cityData from the indexOf of the deliminator to the to the lastIndexOf the deliminator. You will need to think of you OB1 errors on the first index since you do not want to include the deliminator in the substring. Think, do you want that location, or do you want that location+1?

Testing getCityLongitude

You will see a sample in testMethod that you need to uncomment. Feel free to try with other cityData info.

Step 5 - convertDegreesToDecimals

For convertDegreesToDecimals, you need to combine all the skills you practiced above. You are given a degree based coordinate. For example:

40°35'6.9288"N

You will know that has a set pattern to it. And you can use the DEGREE, MINUTE, SECOND constants at the top - to help figure out the splits in the pattern. It is safe to assume all degrees will be given in that format. Using the format to your advantage, you will calculate the decimal value of the coordinate which in the above examples case is:

40.585258

This is figured out by the following formula:

degree value + (minute value / 60) + (second value / 3600)  * 1 or -1 depending on direction

For example: 105°5’3.9084”W would be 105 + (5 / 60) + (3.9084/ 3600) and then you would multiply it by -1 to make it the negative coordinate.

Note that ‘S’ and ‘W’ are both the negative coordinates when looking at latitude and longitude.

Writing convertDegreesToDecimals

  • First split up the String into the different parts. Hint, I would print out the parts just to see if you split it correctly.
  • Convert each part to the double value by using Double.parseValue(String) where String changes based on what you are looking at. For example:
        String degree = strValue.substring(0, strValue.indexOf(DEGREE));
        double deg = Double.parseDouble(degree);
    
  • You will need to now do the following for seconds and minutes
  • Use the formula above to figure out the overall decimal value.
  • Look at the last character and if it is West or South you need to make it a negative value. If negative, multiple the result from the above formula by -1.
  • return the final value.

Testing getCityLongitude

You will see a sample in testMethod that you need to uncomment. Feel free to try with other coordinates.

Step 6 - Play the game

You should have a functioning game now! Play it. Try putting ? into the prompt to see what your options are. Did you find the easter egg?

Step 7 - Turning in

Once you have run the program and are satisfied it is working, you may submit for grading. Note that you only get ten submission attempts, so make sure it is mostly working before you submit. We will bypass the testMethod in our tests, running our own tests on each method you wrote.

Computer Science Department

279 Computer Science Building
1100 Centre Avenue
Fort Collins, CO 80523
Phone: (970) 491-5792
Fax: (970) 491-2466

Spring 21: CS 150 - Culture and Coding (AUCC 3B/GT-AH3)

Survey of computer science, formal logic, and computational thinking. Explores the historical, gender, and cultural perspectives on the role of technology in society. Includes learning a basic programming language. Students will be expected to write small programs, and construct written arguments on ways in which technology influences our modern culture. Previous computer science experience not necessary.