0% found this document useful (0 votes)
24 views2 pages

Studentid Name Gpa: Package Public Class Int Double Public Int Double

The document defines a Student class with fields for student ID, name, and GPA. It includes a constructor, getter and setter methods for GPA, and a display method. The TestStudent1 class creates two Student objects, sets their fields, and calls the display method to output their information.

Uploaded by

Ari Hendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Studentid Name Gpa: Package Public Class Int Double Public Int Double

The document defines a Student class with fields for student ID, name, and GPA. It includes a constructor, getter and setter methods for GPA, and a display method. The TestStudent1 class creates two Student objects, sets their fields, and calls the display method to output their information.

Uploaded by

Ari Hendra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

package mypackage;

public class Student {


int studentID;
String name;
double gpa;
public Student(int sid, String n, double gpa) {
studentID = sid;
name = n;
this.gpa = gpa;
}
public double getGpa(){
return gpa;
}
public void setGpa(double gpa){
if(gpa<0.0){
System.out.println("Invalid gpa");
} else {
this.gpa = gpa;
}
}
public void displayStudentInfo() {
System.out.println("Student ID:" + studentID);
System.out.println("Nama:" + name);
System.out.println("GPA" + gpa);
}
}
package mypackage;
public class TestStudent1 {

//
//
//
//
//
//

public static void main(String[] args) {


Student s1 = new Student(12345, "Andi", 7.9);
s1.studentID = 12345;
s1.name = "Andi";
s1.gpa = 7.9;
System.out.println("Student ID:" + s1.studentID);
System.out.println("Nama:" + s1.name);
System.out.println("GPA" + s1.gpa);
s1.setGpa(-7.9);
s1.displayStudentInfo();

//
//
//
//
//
//

Student s2 = new Student(123456, "Budi", 8.6);


s2.studentID = 123456;
s2.name = "Budi";
s2.gpa = 8.6;
System.out.println("Student ID:" + s2.studentID);
System.out.println("Nama:" + s2.name);
System.out.println("GPA" + s2.gpa);
s2.displayStudentInfo();

}
}

You might also like