Verifying Input

When creating constructors and adding setters, I’ve been taught to verify all input passed to the method. In working through the ProjectEuler exercises, I’ve made use of helper methods that hadformal parameters but didn’t necessarily verify the input (bad habit). With Problem8, I decided to put this into practice. The problem was: “find the greatest product of five consecutive digits in a 1000-digit number.’ I solved this problem using 2 for loops:

  1. With the first for loop, I created a String object that contained 5 characters at one time. With every iteration of the for loop, the sentinel would be updated by 1 and thus, the String would be updated to contain a new sequence of 5 characters e.g.  startingString.substring(i,i+4).
  2. Rather than perform the required computation in the 1st for loop which would made my program faster, I decided to create a helper method which would accept a String and return the product of all the digits in that String. 
  3. The first iteration of my helper method didn’t include any checks to verify that the String didn’t have any non-word characters which would have caused a NumberFormat exception (I converted the digits in the String object using Integer.parseInt(String name)).

     So, I added some to the helper method I used to make sure that the algorithm would return correct/appropriate output when provided input that fits the formal parameter specifications. 

  4. First, I stripped the String of all non-word characters with this command: digits.replaceAll(“\W”,””); An alternate and informative way to alert the user to the ‘bad’ input is via the try { } catch { } feature of Java.
  5. Another thing to check for is if, after stripping all non-word characters from the String passed to the helper method, the String’s length is 0 i.e. the String is an empty String, I would return 0 as the product of the String in that case instead of the value of Product which was initialized to 1. That was taken care of via a simple conditional statement. Here’s the code for my helper method:

Overall, it’ll be good practice for me to view my code with an eye to splitting off ‘tasks’ into helper methods and making sure that input is verified. 

Happy December break! :)

I’m thankful for family and thankful that my faculties are still sharp. 🙂 Long way of saying my semester ended on a very strong note.

Taking a break from stuffing myself, I thought of a crude solution to Problem 5 from Caution: With the way I solved the problem, I’ll run into an integer oveflow situation for N > 23. Without further ado, here’s the gist of my rough solution:

I’ve started using Github (@janoulle) and I know the basics of creating/cloning a repo, adding, committing and pushing. I may have to pony up for a private repo for placing my class assignments but the cheapskate in me thinks it might be better to move to Bitbucket which has free & unlimited private repos. 

Edit: Problem 6 was relatively straightforward and I solved that quickly. It requires summation formula which, if you’ve taken Calculus I or Discrete Math, should be easy to do. I’ve pushed it to the Github repo which contains  all my solutions.

Edit: Problem 7 is done.

Code Critiques

So, I finished the semester on a strong note: aced all 4 of my classes. Woohoo! 

The relevant CS courses I took were: Intro to Programming (using Java), Discrete Math and Calculus I. I heartily recommend this combination of classes because the concepts I learned in Discrete Math served me well in my Java class (think propositional & predicate logic) and my Calculus class (functions/relations)!

Project Euler

ProjectEuler is “a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve.” In other words, it is a collection of problems for inquiring minds.

I solved the first problem using Python and it was relatively straightforward

The next problem (Problem 2) was solved with Java and it builds on skills you used for solving Problem 1 and adds the Fibonacci algorithm to the mix:

I’ll be working on Problem 3 and will have my solution ready when it’s fit for public viewing.

Update: my solution is here. It’s bulky but it works. Phew.

Some Resources for Learning Java

In my quest to learn more about Java, here are some sites that I’ve found to be of use to me:

  1. The Java 7/6 API documentation – learn to love this site. You’ll refer to it endlessly as you run into problems or need to find methods for a task.
  2. The Java Tutorials – by Oracle. I found this helpful while reviewing for tests. You can also download them to your computer.
  3. – by Professor Fred Swartz. You can view the notes online or download them to your computer.
  4. Training sites like ElementK. ElementK is not a free site but I have access to their huge library of training materials via my school. So make sure you browse your Computer Science Department homepage for hidden gems like free software for students and free training resources. If your school has a “student hub” (my school has one powered by uPortal which is where I learned about the free training), start your search there.


Reading input from the console

In training materials from ElementK, I observed the use of to accept input from the keyboard. In my intro to Java class, I was taught to use the Scanner class in Java. What is the different between these two methods?

I’ll talk about the Scanner class although the Java API docs do a much better job of clarification. If you want to create a simple Java program where the user is prompted to enter words, characters or numbers, you need a class that allows you to do this. The Scanner class does not belong to the java.lang package to you need to import the Scanner class from java.util.Scanner using the import command at the top of your Java class.

import java.util.Scanner;