AP CS Song Solution

posted by: veldte 18 September 2010 No Comment

Below is my solution to the AP CS Assignment #1. Uses a secondary song.txt file which contains the copy+pasted song info. This is provided for educational purposes, you’ll understand it in a few months.

Oh, and as a word of advice, the teacher will be able to tell the difference between my code and yours. Do your own work, but make it look GOOD. There is a difference between good-looking code and bad-looking code.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Song {
    ArrayList song = new ArrayList();

    public Song() {
        readSong();
        writeSong();
    }

    public void readSong() {
        InputStream instream = getClass().getResourceAsStream("song.txt");
        InputStreamReader infile = new InputStreamReader(instream);
        BufferedReader inbuf = new BufferedReader(infile);
        String songLine;
        try {
            songLine = inbuf.readLine();
            while (songLine != null && songLine != "") {
                song.add(songLine);
                songLine = inbuf.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void writeSong() {
        for (int i = 0; i < song.size(); i++)
            System.out.println(song.get(i));
    }

    public static void main(String[] args) {
        new Song();
    }
}

On how you’re doing it… you want to make as few calls as possible without making a single macro-function which handles the song in a static context, as that design would fail and require rechecking and reorienting were the song to be modified. Therefore what you want is a sufficiently modular program which can be easily modified with minimal revision. Find that balancing point, and go with it. It honestly doesn’t matter so much now when all you have to do is retype “Oh I’m teh master programmer! Hoorah!” to “My ego is going up like a rocket! Go programming me!”, but when it becomes a sprawling change, such as with the applied algorithms which will be created later in AP CS, it becomes critical. You will make mistakes, and you will need to throw out parts or all of your code at some point. Again, make it look GOOD. If it looks ugly and complicated, there’s probably a better and more elegant way to do it.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>