600.107 Introduction to Programming in Java
Homework #5 - Due 11:30pm on Monday 10/9

Overview

The primary goal of this assignment is to get practice working with loops and strings in Java. You'll also get some (more) practice writing pseudocode while thinking about processing tables of data. You'll need to download our posted HW5start.zip file to get the file(s) necessary to do this assignment.

Deliverables: As usual, you must submit all parts of the assignment together in one zip file called HW5-jhedLogin.zip, substituting your actual JHED/Blackboard login as the second part of the file name. Your solution to Part A should be a java program called Hw5a.java. Your solution to Part B should be a java program called Hw5b.java, that uses the structure we have provided (Hw5bStart.java) as a starting point.

Remember that you can submit multiple times on Blackboard; just make sure that your final submission includes all parts! Also remember that your Java code must compile (no errors), and it should be checkstyle compliant (with our configuration file). You can check your submission by downloading from MyGrades in Blackboard after submitting and making sure all was included in the zip file, and everything still compiles as submitted. (Do this now as practice for the graded projects where you will get a 0 if your submission is incomplete or does not compile!)


Part A: DNA Sequence Validation

Your first exercise in working with loops and strings in Java will be to write a program that can validate whether a particular string is a valid DNA sequence or not. A valid DNA sequence must start with the prefix 5'- and end with the suffix -3'. Between those it must have at least one character, and can only contain the valid bases A, C, G and T, any number of each in any possible order. The bases can be either upper or lower case - both are valid. No other characters may be included.

The overall operation of your program must be to prompt for (only) one DNA sequence, read the input, and then display "valid" or "invalid" depending on the result. Here are a few examples; you must follow this user interface exactly:

  Enter a DNA sequence: 5'-ACTACGAGTAC-3'
  valid

  Enter a DNA sequence: ACTACGAGTAC
  invalid

  Enter a DNA sequence: 3'ACTACGAGTAC5'
  invalid

  Enter a DNA sequence: 5'-aaAAaa-3'
  valid

  Enter a DNA sequence: 5'-actg-3'
  valid

  Enter a DNA sequence: 5'-EWKDIwjgNKAWOE-3'
  invalid

  Enter a DNA sequence: 5'-ACTGbCTGA-3'
  invalid

  Enter a DNA sequence: 5'-3'
  invalid

Hint: Not only can you use the indexOf String method to find the first occurrence of a character, but you can use it to find the starting index of the first occurrence of a (sub)string also. For example,

    String full = "this is a string";
    int where;
    where = full.indexOf("is");  // result is 2
    where = full.indexOf("ing");  // result is 13
    

Part B: Digit String Descriptions

As your second exercise in working with loops and strings in Java, you will be implementing the digit string description problem for which you wrote pseudocode in assignment 1, and using it to generate a sequence of digit strings. We will give you a skeleton program (Hw5b.java) that has two methods: main, and one to compute the description of a digit string. You'll need to fill in the details for both methods in order to get the program working.

The overall operation of your program (main method) must be to generate a sequence of digit strings that starts with the string "1". Each subsequent value will be the digital description of the previous string. Get user input for how many strings to generate. For example, if the user inputs 6, your program will generate the sequence below, printing each string on it's own line. Use this exact format for input and output. Reading aloud, you can see that each next line in the sequence is the digital description of the previous line.

  How many lines to generate? 6
  1
  11
  21
  1211
  111221
  312211

Your main method must prompt for and read the user input for how many lines of digit strings to generate. Starting with "1", it should then loop however many times necessary to get the requested number of output lines. The result from each time you call the dsDescribe method should become the parameter for the next time you call it.

The method (subroutine) to compute the description of a digit string is called dsDescribe in the starter file. It has a parameter that is a starting string, such as "13322442111". It must create and return a new string (called variable result in the starter code), that is a digit string description of the parameter value. For the example above, the result would be "112322241231". The subroutine is not supposed to print the result. Instead, the result will be returned to the main method, and should be printed there. Note that this method is more general purpose than how it is being used in main for this assignment: it should give the correct result no matter what (valid) digit string is passed to it as the parameter value. Here is a fairly simple algorithm that will almost work for this method - it's missing a step or two! (You can implement your own version or fix this one, but either way you must test to be sure it works correctly!)

Subroutine dsDescribe(digitString):
  initialize result to the empty string
  if digitString has no characters, return result
  set current to the first character in digitString
  set count to 1
  repeat for each subsequent digit in digitString:
    if digit is the same as current:
      add one to count
    otherwise:
      concatenate count to the end of result
      concatenate current to the end of result
      set count to 1
      set current to digit
  return result
  

Implementation Suggestion: first write the method that takes a digit string, creates its digital description, and displays the result, implementing the pseudocode above. Test it by calling it several times from main with various literal digit strings, similar to the formulas and choices programs in prior assignments. You can use the examples from hw01. Then add to the program so that it gets user input for how many sequences to generate, and prints the descriptions for the requested number of iterations, starting with "1" as the first string. Before submitting your program, instead of deleting the tests you wrote in main, simply comment them out by surrounding them with /* and */ comment markers.


General assignment requirements, style and submission details: