Java Student Class: Attributes, Methods, and Status

Classified in Computers

Written at on English with a size of 3.34 KB.

This document presents a Java class named Student, demonstrating object-oriented programming principles. It includes attributes, constructors, getters, setters, and methods to calculate average grades and determine student status.

Test Class

import java.io.*;
public class Test {
    public static void main(String[] args) throws IOException {
        Student student = new Student("John", true, 3, 2, 5, 43);
        student.show();
    }
}

Student Class

import java.io.*;
public class Student {
    private int note1, note2, note3;
    private String name;
    private int age;
    private boolean sex; // attributes

    public Student() {
        this.name = "NN";
        this.sex = false;
        this.note1 = 0;
        this.note2 = 0;
        this.note3 = 0;
        this.age = 2;
    }

    public Student(String name, boolean sex, int n1, int n2, int n3, int age) {
        this.name = name;
        this.sex = sex;
        this.note1 = n1;
        this.note2 = n2;
        this.note3 = n3;
        this.age = age; // constructor
    }

    public String getName() {
        return this.name; // return the value of the name
    }

    public boolean getSex() {
        return this.sex;
    }

    public int getNote1() {
        return this.note1;
    }

    public int getNote2() {
        return this.note2;
    }

    public int getNote3() {
        return this.note3;
    }

    public int getAge() {
        return this.age; // methods accessible
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSex(boolean sex) { // mutator, receives a data attribute
        this.sex = sex;
    }

    public void setNote1(int n1) {
        this.note1 = n1;
    }

    public void setNote2(int n2) {
        this.note2 = n2;
    }

    public void setNote3(int n3) {
        this.note3 = n3;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // Methods
    public float average() {
        float avg;
        avg = (float) (this.note1 + this.note2 + this.note3) / 3;
        return avg;
    }

    public int more() {
        int m = 0;
        if (this.note1 > m) {
            m = this.note1;
        }
        if (this.note2 > m) {
            m = this.note2;
        }
        if (this.note3 > m) {
            m = this.note3;
        }
        return m; // pseudo language
    }

    public void status() {
        float p = this.average(); // invoke the method average
        if (p > 5) {
            System.out.println("pass");
        } else {
            if (p < 3) {
                System.out.println("fail");
            } else {
                System.out.println("test method"); // order situation
            }
        }
    }

    public String getSexString() {
        String sx;
        if (this.sex == true) {
            sx = "MALE";
        } else {
            sx = "FEMALE";
        }
        return sx; // End method getSexString
    }

    public void show() {
        System.out.println("NAME: " + this.name);
        System.out.println("AGE: " + this.age);
        System.out.println("SEX: " + this.getSexString()); // to return something
        System.out.println("AVERAGE: " + this.average());
        System.out.println("STATUS:");
        this.status();
    } // End method show
} // End

Entradas relacionadas: