Assignment Chef icon Assignment Chef
All English tutorials

Programming lesson

Building Your First Java Class: A Guppy Simulation for COMP 2522

Learn to implement the Guppy class for COMP 2522 Assignment 1, covering Java constants, constructors, instance variables, and encapsulation with a real-world ichthyology simulation.

Java OOP tutorial COMP 2522 assignment Guppy class Java Java constants example Java constructors instance variables Java encapsulation in Java object-oriented programming Java programming for beginners software development iterative ichthyology simulation Java validation techniques static variables Java coding assignment help Java project tutorial

Introduction to Java Object-Oriented Programming

Welcome to your first COMP 2522 assignment! In this tutorial, you will apply what you've learned as you migrate from Python to Java by implementing the Guppy class for a larger project. Developing software is often a highly iterative process, involving repeated cycles of designing, coding, testing, and integrating small increments. By deconstructing a large project into smaller, manageable tasks, we can abstract away portions and focus on separate, solvable problems. This approach is used in real-world applications, from AI chatbots to mobile apps. Think of it like building a viral app like TikTok's recommendation engine—you start with a simple algorithm, test it, and iterate based on user feedback.

Understanding the Problem Domain

You've been hired to generate software for ichthyologists at the Vancouver Aquarium studying a new invasive guppy species (Poecilia). The scientists need to simulate the fish's reproductive rate to assess threats to native fauna. Object-oriented analysis and design are well-suited for this because requirements are incomplete and changing. In the first iteration, you'll abstract away complicated logic and write a fundamental class: Guppy. This mirrors how AI models like GPT are built—starting with basic layers and iterating to improve performance.

Step 1: Declaring Symbolic Constants

In Java, symbolic constants are static final variables. They replace magic numbers and improve code readability. For the Guppy class, you need nine constants:

  • YOUNG_FISH_AGE_IN_WEEKS = 10
  • MATURE_FISH_AGE_IN_WEEKS = 30
  • MAXIMUM_AGE_IN_WEEKS = 50
  • MINIMUM_WATER_VOLUME_ML = 250.0
  • DEFAULT_GENUS = "Poecilia"
  • DEFAULT_SPECIES = "reticulata"
  • DEFAULT_HEALTH_COEFFICIENT = 0.5
  • MINIMUM_HEALTH_COEFFICIENT = 0.0
  • MAXIMUM_HEALTH_COEFFICIENT = 1.0

These constants define boundaries for age, health, and default values. Using them ensures your code is maintainable—a key practice in software development for finance apps or gaming leaderboards.

Step 2: Defining Instance Variables

Instance variables represent the state of each Guppy object. You need eight private variables:

  • genus (String)
  • species (String)
  • ageInWeeks (int)
  • isFemale (boolean)
  • generationNumber (int)
  • isAlive (boolean)
  • healthCoefficient (double)
  • identificationNumber (int)

Additionally, a static variable numberOfGuppiesBorn counts all Guppy objects created. This is incremented in constructors and assigned to identificationNumber. Static variables are shared across all instances—like a global counter in a multiplayer game tracking total players.

Step 3: Implementing Constructors

Constructors initialize new Guppy objects. You need two constructors:

Zero-Parameter Constructor

This constructor sets default values:

public Guppy() {
    genus = DEFAULT_GENUS;
    species = DEFAULT_SPECIES;
    ageInWeeks = 0;
    isFemale = true;
    isAlive = true;
    generationNumber = 0;
    healthCoefficient = DEFAULT_HEALTH_COEFFICIENT;
    numberOfGuppiesBorn++;
    identificationNumber = numberOfGuppiesBorn;
}

Parameterized Constructor

This constructor accepts parameters and includes validation. For example, if newAgeInWeeks is negative, set age to 0. If newHealthCoefficient is out of bounds, clamp it to the nearest bound. Format newGenus with capital first letter and lowercase rest; format newSpecies as lowercase. Always set isAlive to true. Increment the static counter and assign ID.

public Guppy(String newGenus, String newSpecies, int newAgeInWeeks, boolean newIsFemale, int newGenerationNumber, double newHealthCoefficient) {
    // Format genus: first letter uppercase, rest lowercase
    genus = newGenus.substring(0,1).toUpperCase() + newGenus.substring(1).toLowerCase();
    // Format species: all lowercase
    species = newSpecies.toLowerCase();
    // Validate age
    ageInWeeks = (newAgeInWeeks < 0) ? 0 : newAgeInWeeks;
    isFemale = newIsFemale;
    // Validate generation number
    generationNumber = (newGenerationNumber < 0) ? 1 : newGenerationNumber;
    // Validate health coefficient
    if (newHealthCoefficient < MINIMUM_HEALTH_COEFFICIENT) {
        healthCoefficient = MINIMUM_HEALTH_COEFFICIENT;
    } else if (newHealthCoefficient > MAXIMUM_HEALTH_COEFFICIENT) {
        healthCoefficient = MAXIMUM_HEALTH_COEFFICIENT;
    } else {
        healthCoefficient = newHealthCoefficient;
    }
    isAlive = true;
    numberOfGuppiesBorn++;
    identificationNumber = numberOfGuppiesBorn;
}

Validation is crucial—like checking user input in an AI app to avoid crashes.

Step 4: The incrementAge() Method

This method increases the guppy's age by one week. If age exceeds MAXIMUM_AGE_IN_WEEKS, the guppy dies (set isAlive to false). This mimics real-world aging—similar to a character in a game losing health over time.

public void incrementAge() {
    ageInWeeks++;
    if (ageInWeeks > MAXIMUM_AGE_IN_WEEKS) {
        isAlive = false;
    }
}

Connecting to Trends and Real-World Examples

This iterative development process is used everywhere: from building AI models like ChatGPT to developing finance apps like Robinhood. For instance, when creating a gaming leaderboard, you start with a simple score class, then add features like player stats and rankings. Similarly, the Guppy class is your foundation for simulating population dynamics—a concept used in epidemiology and ecology AI tools.

Conclusion

By completing this assignment, you've practiced core Java OOP concepts: constants, instance variables, constructors, validation, and static members. These skills are essential for any software developer, whether you're building school projects or professional applications. Remember to follow code style guidelines and test with provided JUnit tests. Good luck!