realethantran / ethanRepo

https://realethantran.github.io/ethanRepo/
MIT License
0 stars 0 forks source link

AP CSA 2015 FRQ Associations/Connections to Project - Ethan Tran #2

Open realethantran opened 4 months ago

realethantran commented 4 months ago

FRQ 1 (Arrays, ArrayLists, & 2D Arrays)

The first FRQ question had a heavy focus on ArrayLists and 2D Arrays. In my ASL project, one of the main functions I made for my team, named convertToMNIST within the FrameApiController class, is used to preprocess image data, aligning it with the structure of the MNIST dataset.

 private List<List<Integer>> convertToMNIST(String imageData) {
        try {
            byte[] imageBytes = Base64.getDecoder().decode(imageData);
            ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
            BufferedImage image = ImageIO.read(bis);

            BufferedImage resizedImage = new BufferedImage(28, 28, BufferedImage.TYPE_BYTE_GRAY);
            Graphics2D g = resizedImage.createGraphics();
            g.drawImage(image, 0, 0, 28, 28, null);
            g.dispose();

            List<List<Integer>> mnistData = new ArrayList<>();
            for (int y = 0; y < 28; y++) {
                List<Integer> row = new ArrayList<>();
                for (int x = 0; x < 28; x++) {
                    int pixel = resizedImage.getRGB(x, y) & 0xFF;
                    row.add(pixel);
                }
                mnistData.add(row);
            }
            return mnistData;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

What this method does is it decodes Base64-encoded image data, reads it into a BufferedImage, and standardizes its dimensions to 28x28 pixels, mirroring the format of MNIST digit images. The resultant resized image is then transformed into a 2D list, bearing similarity to the row sums calculation from the FRQ. Both processes involve iterating over rows and columns, extracting pertinent information, and structuring it suitably for analysis, with the manipulation of 2D arrays in the FRQ, where row sums were calculated and analyzed for diversity. While, in my ASL code, the extracted pixel values are organized into a 2D list, mirroring the structure of the MNIST dataset.

FRQ 2 (Classes)

For FRQ 2, I was tasked with making a class for a simple word guessing game in which the HiddenWord class implements a method called getHint that takes a guess as input and returns a hint based on the correspondence between the guess and a hidden word. The hint consists of characters indicating correct guesses, correct letters in incorrect positions, and letters not present in the hidden word. This approach shares similarities with string manipulation and conditional logic present in various programming scenarios.

   public FrameApiController() {
        this.restTemplate = new RestTemplate();
    }

    @PostMapping("/image")
    public String processImage(@RequestBody ImageData imageData) {
        List<List<Integer>> mnistData = convertToMNIST(imageData.getImage());
        if (mnistData != null) {
            postMnistData(mnistData);

            // store MNIST data in database
            Frame frame = new Frame();
            frame.setMnistData(mnistData.toString()); // Converting list of lists to string for database storage
            frameJpaRepository.save(frame);

            return "{\"message\": \"Image processed and posted successfully!\"}";
        } else {
            return "{\"error\": \"Failed to process image\"}";
        }
    }

For my team's ASL Project, I implemented a method within the FrameApiController class to handle incoming image data, much like the logic in FRQ 2's HiddenWord class handles user guesses. Both scenarios involve processing input and generating responses based on predefined criteria. Just as the HiddenWord class evaluates guesses against a hidden word and provides hints, the FrameApiController preprocesses image data and stores it in a database, responding with success or failure messages accordingly. The common theme here is input processing and conditional logic using "if" statements and iterative processing to determine appropriate responses.

FRQ 3 (2D Arrays + Method and Control Structures)

In my ASL project, the trainLogic method within the PredictionService class closely resembles the iterative algorithm used in FRQ 3's solution for sparse array manipulation.

double[][] weights = new double[25][785];  //weights are available to all classes within PredictionService

public String trainLogic() {
    System.out.println("---------trainLogic");
    List<List<Integer>> data = new ArrayList<>();
    try {
        data = readCSV("sign_mnist_train.csv");
    } catch (IOException e) {
        System.err.println("Error reading CSV file: " + e.getMessage());
        return "Error"; // or any other error handling mechanism
    }
    // Iterative training process to update weights
    for (int s = 0; s < 25; s++) {
        for (int ii = 0; ii < epoch; ii++) {
            double error = 0.0;
            for (int i = 0; i < x.length; i++) {
                double y_pred = weights[s][0];
                for (int k = 0; k < m; k++) {
                    y_pred += weights[s][k + 1] * x[i][k + 1];
                }
                // Update weights based on prediction error
                double pred = y_pred >= 0.0 ? 1.0 : 0.0;
                double expect = x[i][0] == digit[s] ? 1.0 : 0.0;
                double err = pred - expect;
                error += err * err;
                weights[s][0] -= rate * err;
                for (int k = 0; k < m; k++) {
                    weights[s][k + 1] -= rate * err * x[i][k + 1];
                }
            }
            System.out.println("Letter: " + alphabet[s] + ", Epoch: " + ii + ", Error: " + error);
        }
    }
    return null;
}

Just as the FRQ solution iterates over entries in the sparse array to update values based on specific conditions, the trainLogic method iteratively updates weights in a neural network based on prediction errors calculated during training. Both scenarios involve iterating over elements of a 2D structure (sparse array entries in FRQ 3, weights in the neural network) and updating them iteratively to optimize performance.

FRQ 4 (Interfaces, Methods, & Control Structures)

In FRQ 4, we explored the concept of interfaces and their implementations in Java. Specifically, the NumberGroup interface provided a blueprint for classes representing different types of number groups, each implementing the contains method to determine if a given number is part of the group.

public interface LeaderboardJpaRepository extends JpaRepository<Leaderboard, Long> {
    List<Leaderboard> findAllByOrderByScoreDesc();
    List<Leaderboard> findByPlayerNameIgnoreCase(String playerName);
}

This concept resonates with the structure of the LeaderboardJpaRepository interface in my team's ASL project, which defines methods for accessing leaderboard data from a database. Just as different implementations of the NumberGroup interface define distinct ways to check for number membership, the methods in LeaderboardJpaRepository offer various ways to retrieve leaderboard data, such as fetching all entries sorted by score or searching for entries by player name. Both scenarios show the importance and versatility of interfaces in defining common behavior across various implementations, whether it's checking for number membership or accessing leaderboard data from a database. This is important to remember, however, as the AP test no longer tests students on interface creation, FRQ 4 is one that can be useful for my overall coding experience.

Reflection/Takeaways

Building Confidence

Connections Reflection

Conclusion

To conclude, the FRQs have given me the technical know-how I need to pass the AP exam and have established a strong basis for the months to come. They have emphasized the value of practice, flexibility, and methodical problem-solving—qualities that are essential when coding and I have no doubt that these FRQs will be useful to me in the future not just for my team's project and the AP Exam, but for my overall career in Computer Science.

dolphinalt commented 4 months ago

45% FRQ code 4/3.6 on indicators (3.645/3.6)

Name Score
FRQ 1 0.9/0.9
FRQ 2 0.9/0.9
FRQ 3 0.9/0.9
FRQ 4 0.9/0.9
TOTAL 3.6 0.9 1.125 = 3.645/3.6

FRQ 1: Good job! All of the results from testing the code are correct. The approach that you took is also pretty much the same approach as I did. I really liked the extra effort put into the testing blocks, clearly labeling it with what is being tested and displayed. Not really sure if there is a better way to do this question, again, really good job!

FRQ 2: One thing I noticed was the usage of StringBuilder, something I didn't use. However, I'm not exactly why you used string builder rather than just doing String hint = ""; hint += "*";, as this works in the same way. At the end of the day though, both methods work perfectly fine, and your testing data all outputted the correct values. One thing I'm starting to notice now though is the addition of what the testing data should return, that way it's easier to check and verify that your testing function is returning the correct values

FRQ 3: I didn't have too many questions with this FRQ, it all looks mostly flawless. The one thing that sticks out though, was that I didn't really see the need to use numRows = Math.max(numRows, entry.getRow() + 1); (and the same goes for the columns). I know this wasn't really part of the FRQs, but still, there wasn't any need to update the number of rows, as the getter function should already do that.

FRQ 4: This one was done pretty much the same way as I did, I'm not too sure if there really was any other efficient way of solving this FRQ. Really well done paying attention to the fine details when printing out labels for the testing code! Other than that, no real comments on this question. Well done!

General Notes:

45% FRQ associations 4/3.6 on indicators (3.56/3.6)

Very nice job on your reflection

Q1: Really clear how you're using a 2D array here! It looks like your PBL really helped you with the understanding of the FRQs. Although I am a slight bit confused by the 28 constant there, not sure why exactly you are setting it as a constant 28. If it is representative of the number of pixels, would 28x28 really be a clear enough image? (0.9)

Q2: I only saw 1 if statement in the code set, so I don't know how well this connects to the if statements in Q2. However, it does clearly seem like you are working with a string and do do some sorts of manipulating so that is good. I also trust that you understand the logic sequencing of the if statements, as you did solve Q2 properly, it's just that the example you gave didn't really show it too well. I'll still give you a .9! (0.9)

Q3: Really cool integration of a neural network! This is really impressive and warrants extra points. The usage of 2D arrays is extremely clear, as well of the integration of a method within the class. Our team is doing just about the same thing, however, we're having a little more problems than you did... Alex Lu and David Vasilev are the ones working on the neural network, PLEASE reach out to them, and I will ask them to reach out to you as well if assistance is needed! (0.95)

Q4: This one is also really clear and shows the exact objective of the FRQ question. Really straightforward, good job! I did pretty much the same thing as you did on the project, with the interface just defining the ways to search through the database. I'm sure there are a lot of other ways to be using the interface, hopefully we both are able to branch out in our CS journeys and use interfaces for a multitude of things! (0.9)

Score calculations: 0.9+0.9+0.95+0.9=3.65. Now lets multiply by 0.9: 3.56