Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Assignments

CSCI125Code


Person.java

/**
 * This class stores a person who it appears is taking classes or something.
 * 
 * @author Chuck Cusack
 * @version 9/4/24
 */
public class Person
{
    private String name;
    private int age;
    private String code;
    private int credits;
    
    /**
     * Constructor for objects of class Person
     * @param myName the name of the person
     * @param myAge the age of the person
     */
    public Person(String myName,int myAge)
    {
        name = myName;
        age = myAge;
        createCode();
        credits = 0;
    }
    
    /**
     * Adds credits to the student.
     * 
     * @param creditsToAdd the number of credits to add to the student.
     */
    public void addCredits(int creditsToAdd) {
        if(creditsToAdd>0) {
            credits = credits + creditsToAdd; 
        } else {
            System.out.println("You tried to add "+creditsToAdd+
            " which is not a positive number.");
        }
    }
    
    /**
     * Set the number of credits for the person to 0 and return the 
     * number of credits they had.
     * 
     * @return the number of credits the person had.
     */
    public int resetCredits() {
        int creditsToReturn = credits;
        credits = 0;
        return creditsToReturn;
    }
    
    /**
     * @return the age of the person
     */
    public int getAge() {
        return age;
    }
    
    /**
     * Set the age of the person to a new value.
     * 
     * @param newAge the new age of the person.
     */
    public void setAge(int newAge) {
        age = newAge;
    }
    
    /**
     * @return the name of the person.
     */
    public String getName() {
        return name;
    }
   
    /**
     * Create and update the code of the person.
     */
    public void createCode() {
        int nameLength = name.length();
        if(nameLength>=5) {
            code = name.substring(0,1)+name.substring(2,3)+name.substring(4,5);
        } else if(nameLength>=3) {
            code = name.substring(0,1)+name.substring(2,3);
        } else if(nameLength>=1) {
            code = name.substring(0,1);
        } else {
            code="";
        }
    }
    
    /**
     * Prints out the details about the person
     */
    public void printDetails() {
        System.out.println("The name of this person is "+name);
        System.out.println("The age of this person is "+age);
        System.out.println("The credits of this person is "+credits);
        System.out.println("The code of this person is "+code);
    }
}