Bifalgorithm post · Computer science one · Example

What does the completed Java Student class example contain?

What does the completed Java Student class example contain? (Computer science one · Example) — "We present the full and completed Student class in Code Sample…

Ideas in this flowchart

  • Completed Java Student class
  • Student data fields
  • firstName and lastName store the student's names
  • id stores the student's identifier
  • gpa stores the student's grade point average
  • Student constructors
  • The full constructor initializes the name, ID, and GPA
  • The no-argument constructor initializes null names, ID 0, and GPA 0.0
  • The two-name constructor initializes the names with default ID and GPA values
  • Accessor methods
  • Getter methods return the first name, last name, ID, and GPA
  • getFormattedName returns the name as Last, First
  • getGpaAsPercentage scales the GPA from a 4.0 scale to a percentage
  • Object representation and comparison methods
  • toString returns a formatted representation containing the name, ID, and GPA
  • hashCode computes a hash from the names, GPA, and ID
  • equals compares Student objects by their names, GPA, and ID
Feed/

What does the completed Java Student class example contain?

No cover

Channel

Computer science one

1 page

"We present the full and completed Student class in Code Sample 34.1.\n\npackage unl.cse;\n\npublic class Student {\n\nprivate String firstName;\n\nprivate String lastName;\n\nprivate int id;\n\nprivate double gpa;\n\npublic Student(String firstName, String lastName, int id, double gpa) {\n\nthis.firstName = firstName;\n\nthis.lastName = lastName;\n\nthis.id = id;\n\nthis.gpa = gpa;\n\n}\n\npublic Student() {\n\nthis(null, null, 0, 0.0);\n\n}\n\npublic Student(String firstName, String lastName) {\n\nthis(firstName, lastName, 0, 0.0);\n\n}\n\npublic String getFirstName() {\n\nreturn firstName;\n\n}\n\npublic String getLastName() {\n\nreturn lastName;\n\n}\n\npublic int getId() {\n\nreturn id;\n\n}\n\npublic double getGpa() {\n\nreturn gpa;\n\n}\n\n/**\n\n* Returns a formatted String of the Student's\n\n* name as Last, First.\n\n*/\n\npublic String getFormattedName() {\n\nreturn lastName + \", \" + firstName;\n\n}\n\n/**\n\n* Scales the GPA, which is assumed to be on a\n\n* 4.0 scale to a percentage.\n\n*/\n\npublic double getGpaAsPercentage() {\n\nreturn gpa / 4.0;\n\n}\n\n@Override\n\npublic String toString() {\n\nreturn String.format(\"%s, %s (ID = %d); %.2f\",\n\nthis.lastName,\n\nthis.firstName,\n\nthis.id,\n\nthis.gpa);\n\n}\n\n@Override\n\npublic int hashCode() {\n\nfinal int prime = 31;\n\nint result = 1;\n\nresult = prime * result + ((firstName == null) ? 0 : firstName.hashCode());\n\nlong temp;\n\ntemp = Double.doubleToLongBits(gpa);\n\nresult = prime * result + (int) (temp ^ (temp >>> 32));\n\nresult = prime * result + id;\n\nresult = prime * result + ((lastName == null) ? 0 : lastName.hashCode());\n\nreturn result;\n\n}\n\n@Override\n\npublic boolean equals(Object obj) {\n\nif (this == obj) {\n\nreturn true;\n\n}\n\nif (obj == null) {\n\nreturn false;\n\n}\n\nif (!(obj instanceof Student)) {\n\nreturn false;\n\n}\n\nStudent other = (Student) obj;\n\nif (firstName == null) {\n\nif (other.firstName != null) {\n\nreturn false;\n\n}\n\n} else if (!firstName.equals(other.firstName)) {\n\nreturn false;\n\n}\n\nif (Double.doubleToLongBits(gpa) != Double.doubleToLongBits(other.gpa)) {\n\nreturn false;\n\n}\n\nif (id != other.id) {\n\nreturn false;\n\n}\n\nif (lastName == null) {\n\nif (other.lastName != null) {\n\nreturn false;\n\n}\n\n} else if (!lastName.equals(other.lastName)) {\n\nreturn false;\n\n}\n\nreturn true;\n\n}\n\n}\n\nCode Sample 34.1.: The completed Java Student class."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.