Ryanrob327 / CSA

MIT License
0 stars 0 forks source link

TRI 2 Final #6

Open Ryanrob327 opened 8 months ago

Ryanrob327 commented 8 months ago

Q4 - Methods and Control Structures

The fourth FRQ the question was unique for using interfaces to create what is essentially a template for future methods and classes. In this example I create an interface to act as a template for my final project, where instead of an arbitrary group of numbers, I am using crops for my interface to represent. As you can see, there are many different methods for each crop, like getSpecies() which returns the species of crop being planted like corn, rice, etc. You also have getFertilizer() and getHydration() Which determine how well the specific crop performs and depending on the performance of said crop it will effect calculateProfit() and getEcoPoints().

public interface Crop {
    // Method to calculate the profit generated by the crop
    double calculateProfit();

    // Getter method for the species of the crop
    String getSpecies();

    // Getter method for the eco points earned by cultivating the crop
    int getEcoPoints();

    // Getter method for the hydration required by the crop
    int getHydration();

    // Getter method for the fertilizer needed by the crop
    String getFertilizer();

    // Method to check if the crop is ready for harvesting
    boolean isReadyForHarvest();

    // Method to harvest the crop
    void harvest();
}

Q2 - Classes

The second FRQ the question was unique for using objects and constructors to initialize the class, here we use a constructor to initialize all of our variables similar to how HiddenWord that took in a string to set to the instance variable. Here, however, we use multiple variables to access anywhere in the class. Additinally, Each method provides a specific behavior or information related to the corn crop, such as calculating profit, accessing species information, determining eco points, hydration requirements, fertilizer needs, checking readiness for harvest, and harvesting.


public class Corn implements Crop {
    private String species;
    private int ecoPoints;
    private int hydration;
    private String fertilizer;
    private boolean readyForHarvest;

    // Constructor
    public Corn(String species, int ecoPoints, int hydration, String fertilizer) {
        this.species = species;
        this.ecoPoints = ecoPoints;
        this.hydration = hydration;
        this.fertilizer = fertilizer;
        this.readyForHarvest = false; // Corn is not ready for harvest initially
    }

    @Override
    public double calculateProfit() {
        // Example: Profit calculation based on corn's characteristics
        // For simplicity, let's assume a fixed profit value for corn
        return 100.0;
    }

    @Override
    public String getSpecies() {
        return species;
    }

    @Override
    public int getEcoPoints() {
        return ecoPoints;
    }

    @Override
    public int getHydration() {
        return hydration;
    }

    @Override
    public String getFertilizer() {
        return fertilizer;
    }

    @Override
    public boolean isReadyForHarvest() {
        return readyForHarvest;
    }

    @Override
    public void harvest() {
        // Example: Harvesting corn
        System.out.println("Harvesting corn...");
        readyForHarvest = true;
    }
}

Q1 - 2D arrays

The second FRQ the question was unique for using 2D arrays, however just like before, the FRQ was only good for storing arbitrary numbers, this class utilizes a 2d array to store the corn object from before like you would in a real life field. The Field class encapsulates a 2D array representing a farm field where crops can be planted and harvested. Each element in the grid can hold either a crop object or be null. This class demonstrates the practical application of 2D arrays in modeling real-world scenarios, such as agricultural management systems. By organizing crop data in a structured grid format, it enables efficient storage, manipulation, and retrieval of crop information, facilitating tasks such as planting, harvesting, and crop management within the field.

public class Field {
    private Crop[][] grid;

    // Constructor
    public Field(int numRows, int numCols) {
        grid = new Crop[numRows][numCols];
    }

    // Method to plant corn at a specific location in the field
    public void plantCron(int row, int col, Corn corn) {
        if (isValidLocation(row, col)) {
            grid[row][col] = corn;
        } else {
            System.out.println("Invalid location for planting corn.");
        }
    }

    // Method to check if a location is valid within the field
    private boolean isValidLocation(int row, int col) {
        return row >= 0 && row < grid.length && col >= 0 && col < grid[0].length;
    }

    // Method to get the crop at a specific location in the field
    public Crop getCrop(int row, int col) {
        if (isValidLocation(row, col)) {
            return grid[row][col];
        } else {
            System.out.println("Invalid location.");
            return null;
        }
    }

    // Method to harvest wheat at a specific location in the field
    public void harvestWheat(int row, int col) {
        if (isValidLocation(row, col) && grid[row][col] instanceof Corn) {
            Corn corn = (Corn) grid[row][col];
            if (corn.isReadyForHarvest()) {
                corn.harvest();
                grid[row][col] = null; // Remove harvested wheat from the field
            } else {
                System.out.println("Corn is not ready for harvest yet.");
            }
        } else {
            System.out.println("No corn planted at this location.");
        }
    }
}

Q3 - Array lists

The third FRQ the question was unique for how annoying and time consuming it was. However, this example was neither of those, the FarmerScorer class manages two ArrayLists: one for storing eco points and another for storing money earned by the farmer. The addScore method allows adding new entries for eco points and money to the respective ArrayLists. The calculateTotalScore method computes the total score for the farmer by summing up the money earned and assigning a weight to the eco points (assuming they contribute half to the score). This implementation demonstrates how ArrayLists can be used to manage and process dynamic data in a scoring system for a farming simulator.

import java.util.ArrayList;

public class FarmerScorer {
    private ArrayList<Integer> ecoPointsList;
    private ArrayList<Double> moneyList;

    public FarmerScorer() {
        ecoPointsList = new ArrayList<>();
        moneyList = new ArrayList<>();
    }

    public void addScore(int ecoPoints, double money) {
        ecoPointsList.add(ecoPoints);
        moneyList.add(money);
    }

    public double calculateTotalScore() {
        double totalScore = 0;
        for (double money : moneyList) {
            totalScore += money;
        }
        for (int ecoPoints : ecoPointsList) {
            totalScore += ecoPoints * 0.5; // Assuming eco points contribute half to the score
        }
        return totalScore;
    }
}
JishnuS420 commented 8 months ago

Grader: Jishnu Singiresu

FRQ Score Comments
FRQ 1 0.95/.9 FRQ was well completed and is fully functional, listed the type of the FRQ and answered all the parts of the question and lft comments in code to help guide through the thinking
FRQ 2 0.95/.9 FRQ was fully completed and again, good comments that are simple but help show the thinking and answered all the parts to the questions and made sure to have fully functional code
FRQ 3 0.92/.9 FRQ was completed, simple and good comments and answered all the parts, really unqiue way of displaying the results of the code, showed the removed column instead of whole array
FRQ 4 0.95/.9 FRQ was completed thoroughly and code is fully functional and displays the right results and everything and yet again, good comments that show the code and explain what was going on
Connections 3.8/3.6 He explained in great detail and showed all the aspects of the code that correlates with the right parts of the question and then he also ordered it in an order that strcutural made sense and alllowed for a smooth presentation.
Total 7.57/7.2 below

Glows:

Grows: