CS106B Assignments 1 to 3

I’m loving C++ and CS106B so far. Learning a lot of new stuff like the collection classes – vector, stack, queue, maps and sets. And…..

Recursion!!!! Took a while to wrap my head around it. I still haven’t actually “gotten” it but at least I’m not totally clueless about it. I’m doing the newer assignments for recursion as I am using the newer CS106B libraries.

So here are the links to the assignents 1, 2 and 3 that I have done so far.

Assignment1 – Simple C++

Assignment2 – ADTs/Collection Classes

Assignment3 – Recursion

Hopefully I’ll be posting each of the next assignments as a seperate posts and talk a little about the challenges I faced.

Setting up CS106B on Linux

Since I have switched to Linux completely, setting up C++ for CS106B was a little tricky. The SEE version of the course only offers IDE for Windows and Mac. No linux supported. But if we check the course sites for the past couple of terms, Linux is supported. The only problem is that the instructor is not Julie Zelinski for the recent versions and the assignments are a bit different from that offered in the open courseware version.

So, setting up an IDE in Linux is a bit of work if one wants to do the old assignment using the new libraries. I found two solutions and here they are –

  1. Using the Old Libraries
    A very helpful gentleman has put up a version of CS106B libraries used in the SEE course for Linux. Here is the link – http://sourceforge.net/projects/progabstrlib/
    These are relatively easy to setup and use, if one follows the readme file included.The only issue with these libraries occurs if you are following the course along with the latest CS106BX reader (which I would recommend as it is the updated version). The libraries used in the reader are the updated ones and have different functions and syntax.
  2. Using the Latest Libraries and the new Reader –
    Follow these steps:-
    * Download the linux files for all the assignments from http://web.stanford.edu/class/archive/cs/cs106b/cs106b.1136/
    * Create a new directory for any c++ program you want to work on using the new libraries
    * Copy the “StanfordCPPLib” to your directory from any of the assignments you downloaded
    * Copy spl.jar and Makefile to the same directory
    * Create your own cpp file in the same directory (for example, name.cpp)
    * Now you should have your cpp file, the “StanfordCPPLib” directory, spl.jar and the Makefile in your project directory
    * Open Makefile in gedit or any other text editor.
    * The Makefile would have the name of the original project whose Makefile you copied. Replace all instances of that name with the name of your cpp file
    For ex. if the project you copied the Makefile from was  “0-Warmup” and your cpp file is name.cpp then replace all instances of “0-Warmup” with “name”
    * Now, when you are ready to compile the program simply open the terminal and cd to the directory that contains all these files and type “make filename” (for ex. “make name”)
    * If there are no compile errors you will get back to the prompt, then type “./filename” and your program should run.

Hope that is helpful and not too confusing. If someone reads this and doesn’t understand any part or if something doesn’t work, please feel free to comment and I will try my best to help.

Short Review of CS106A

Trying my hand at CS has been an enlightening and fascinating experience. I had been curious about computer programming since my high school days but I hadn’t felt so attracted to it before. The instructor of CS106A, Mehran Sahami, really shows how programming is an art and how it is full of creativity. Using logic to solve small yet tricky problems is the most gratifying thing ever.

The course material in CS106A is truly for a beginner. It is only the basics and nothing more. Neither should it be. Learning to code is an overwhelming experience and it can be an ordeal at the start. But sticking with the course makes it a much smoother and fun ride. The material covers only introductory aspects of CS but also drills in the learner an appreciation for good software engineering. As Julie Zelinsky (instructor for the next course CS106B) says in her intro lecture, it’s easy to hack at the keyboard and produce a working program by trial and error, but , writing well structured, designed and documented code is good software engineering.

So, that is my biggest takeaway from CS106A, along with all the basics and programming knowledge of java. I watched all the lectures multiple times. I read the course reader from cover to cover and also did a lot of the exercises at the back of each chapter. And, I did all the assignments and section handout problems. A fruitful endeavour, in my opinion.

Now, onto CS106B with Julie Zelinski, to learn the cooler and harder stuff!

Done with CS106A! Assignments #5 and #7

I’m finally done with CS106A. I did all the assignments and all the section handout problems. It was an incredibly fun and fascinating experience. Learned loads, but it still feels like I’ve just begun to scratch the surface of computer science.

The hardest part of the experience was coming up with logical solutions to the tricky problems. I will talk about that in an upcoming post, the struggle of coming up with an effective and “efficient” algorithm at the introductory level of programming. I mention the introductory level because I know there are methods and algorithms I will soon learn that have efficiency as their main concern. Anyways,

A BIG thank you to Mehran Sahami. I don’t know if he will ever read this post but I would like to say that he is the reason I got interested in this field. I began watching the first lecture out of curiosity and got hooked to his charming and jolly teaching methodology. So, my utmost gratitude to him.

I worked on Assignment #6 but lost it after formatting my hard disk by mistake. I switched to Ubuntu/Linux from Windows and didn’t realize that it would wipe my whole disk instead of a single partition.

But here are assignments #5 Yahtzee! and #7 FacePamphlet. The latter is only the console version which includes all the major parts of the assignment except the graphical elements as I didn’t find them to be particularly important for this application and this is mainly for practising database management at a small scale.

Assignment#5 – Yahtzee

Assignment#7 – FacePamphlet

Assignment #4 – Hangman

Hangman_1Assigment Problem Link

So, I’m done with the Hangman assignment of CS106A. It was comparatively easier than the breakout assignment. It did not have as many moving parts (pun intended). But it was great fun to work on it, nevertheless.

The graphics in the game aren’t flashy. Just the normal version. Placing the graphics on the canvas the a little frustrating for me. But the logical part of the game was fun and went kinda smoothly.

I’m not pasting the whole code here as there are 3 files associated with this project. Here are the gist links for the code.

Hangman.java

HangmanCanvas.java

HangmanLexicon.java

Here is a playable version of the game – Download

*my hangman does not look like the header image!
photo courtesy of barryfunenglish.com

CS106A Assignment #3 Breakout

screen

This is the first big assignment / program that I have worked on. And it was a blast doing it completely on my own. The only thing I had  to refer to google for, was where to place the addMouseListener().

Here is the link to the assignment – http://see.stanford.edu/materials/icspmcs106a/19-assignment-3-breakout.pdf

The biggest challenge was to manage the size of the program and decomposing it effectively. I have done my best but I’m sure there are better ways to implement all this. If anyone reading this has better solutions then please comment below.

Most of the strategies used are explained in the comments of the code. Some comments are simpler as the code itself is easily readable (at least I hope it is :D).

I did all the hard work of working out the game’s mechanics on paper first. Then, I dived into eclipse and coded for about an hour, slowly realizing that the ideas that work on paper do not translate directly to the actual code because I forget a lot of little things. For example, I forgot to add the brick separation while setting up the bricks. But nothing that “game-breaking”.

I have also added most of the extensions mentioned in the assignment including messages, sound, kicker and keeping score. I want to add another layer of increasing the difficulty by making bricks descend down the canvas slowly as the game progresses. I think it can be implemented by making the bricks a single GCompound subclass and then using the move() method. I shall implement it at a later date.

For now, I’m moving onto the next section.

Here is the gist github link for the code – Breakout

Methods

Image

Loved studying this chapter and the related material. Here are the code for the more trickier exercises at the end of the chapter.


 

Quadratic Equation:

ImageI’ve explained my strategy in the comments. I would have liked to return both the solutions back to the run method for further manipulation, but I don’t know how to return two variables from a method. (Or if its even possible). I’m sure there is some way to use arrays to achieve that but I haven’t yet touched the topic of arrays so I’ll leave it for later.

quad_soln

askYesNoQuestion

8_questThis problem wasn’t that tricky but it needed some thought as it was the first predicate method I wrote. I used the loop-and-a-half trick for this method so that the return statement breaks the while(true) loop.

8_ans
And the output is something like this:
8_out

And 1 more —-

isPrime

10_questI did the brute method myself but had to refer to a math website for the more efficient strategy. Again, everything is explained in the comments.

10_ans

 

Welcome.Intro

Hi,

Welcome to my blog where I will be documenting and recording my experiences as I learn to code. I have been working through the Stanford SEE CS106A course, Programming Methodology. It is an introductory course that teaches good software engineering practices. The instructor is Mehran Sahami and he is one of the best teachers I have ever studied under (even though we have never made acquaintance!). The language which the course uses is Java. The course does not teach the ins and outs of Java. Instead it uses java to help start learning programming fundamentals as a whole.

I am already done with the first two assignments of the course (which I might post in the future). So, I’m starting this blog from assignment 3 onwards. I will be posting my strategy and code for each assignment. There is also a course reader for CS106A, The Art and Science of Java by Eric Roberts. I am studying it along with the lectures and handouts. I’m currently going through the 5th chapter of the book named “Methods”. Each chapter has some exercises at the end, which I shall be attempting and posting the resulting code and approach here.

I am loving the coding process and it is fun to learn all about it. I hope this blog can help me keep myself on track and committed. I also hope, if anyone else is taking the open course, they would chime in with their own code or strategies to attempt the assignments. And lastly, this blog might be helpful for someone who is stuck at a particular exercise or assignment. As I have gotten help from other blogs and sites when I have been stuck.

So,

while (CodingIsFun) {
this blog will keep updating;
}