Skip to content

Module 2 OOP Fundamentals Part 2

2026-02-05 22:46

Tags: #java

Author: Duke Hsu


Part 2 Topic

  1. Creating Class
  2. Adding Class Attributes
  3. Adding Methods
  4. Instantiating Objects
  5. Sending Messages

Topic in the sample code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class PrjWritingCLass {
    public static void main(String[] args){
        Student duke = new Student();


        //with out parameter + with return values
        duke.enroll(20251266,"Louis Co","BS - CS"); //duke enrolled

        //whit parameter + no return values
        duke.displayInformation(20251266); //output duke information


        //with out return value 
        duke.enroll(); //no output 

        // no parameter , no return 
        duke.displayInformation(); //student ID = 20251234   print other information
    }
}

class Student{


    //attributes
    private int studentID = 0;
    private String name = "";
    private String course = "";


    //method with out parameter + with return
    public String enroll(){

        //this variables not used
        int studID = 20251234;
        String studName = "Duke Hsu";
        String studCourse = "BS - IT";

        return "The Student was enrolled!";
    }

    //method with parameters + with return 
    public String enroll(int studID, String studName,String studCourse){
        studentID = studID;
        name = studName;
        course = studCourse;

        return "The Student was enrolled!";
    }


    //method with parameter + with out return 
    public void displayInformation(int studID){
        if (studID==studentID){
            System.out.println("The student enrolled is " + name + " , with student ID: "+ studID + ", and course : "+ course + ".");
        }
        else{

            System.out.println("No enrolled student with the student ID: " +studID+ ".");
        }
    }


    //method with out parameter and return values
    public void displayInformation(){

        int studID = 20251234;

        if (studID==20251234){
            System.out.println("The student enrolled is " + name + " , with student ID: "+ studID + ", and course : "+ course + ".");
        }
        else{

            System.out.println("No enrolled student with the student ID: " +studID+ ".");
        }
    }


}

References: