Module1 Exercise4 UsingAClassToCreateRecords
Module1 Exercise4 UsingAClassToCreateRecords
Instruction:
If you have already created a project (working folder) in IntelliJ IDEA using the convention
below then open it, otherwise, create one:
<Last name><First name><class code> Ex. Dela CruzJuan9300
In this project, create the package prelim if it is not yet created. You are to place the
class/es needed for this activity in this package.
Required:
Create a similar application program. Include appropriate data members for your Student
class (i.e. modify the given examples). Show a sample output of your application.
Parallel Arrays
Arrays that are processed in parallel because the elements are associated
First Name Middle Name name Last Name Age Grade Point Average
… … … … …
Formulating Record Structure Using Java
• A record is composed of fields.
• Create a class that will serve as a template for a record (Reference Class) o
The class variables of a Reference Class define the attributes/fields. o
The Student class shown below is an example of a Reference class. As
illustrated, the attributes of a student are firstName, midName, lastName,
age and gPA.
• The Reference Class may be used in any program (another class). The StudentList
class shown below is an example of a class that uses the Student class.
--------------------------------------------------------------------------------------------------------------------
/**
* Constructs a student named Adel Man Tamano, 39 years old and with 95.0 grade
point average.
*/
public Student() {
firstName = "Adel";
midName = "Man";
lastName = "Tamano";
age = 39;
gPA = 95.0;
}
/**
* Constructs a student with first name f, middle name m, last name l, age a and grade
point average gpa.
*/
public Student(String f, String m, String l, int a, double gpa) { firstName = f;
midName = m;
lastName = l;
age = a;
gPA = gpa;
}
/**
* Returns the first name of the student */
public String getFirstName() {
return firstName;
}
/**
* Returns the last name of the student */
public String getLastName() {
return lastName;
}
/**
* Returns the middle name of the student */
public String getMidName() {
return midName;
}
/**
* Returns the age of the student
*/
public int getAge() {
return age;
}
/**
* Returns the grade point average of the student */
public double getGPA() {
return gPA;
}
/**
* Sets the first name of the student to fName */
public void setFirstName(String fName) { firstName
= fName;
}
/**
* Sets the last name of the student to lName */
public void setLastName(String lName) {
lastName = lName;
}
/**
* Sets the middle name of the student to mName
*/
public void setMidName(String mName) {
midName = mName;
}
/**
* Sets the age of the student to a
*/
public void setAge(int a) {
age = a;
}
/**
* Sets the grade point average of a student to g
*/
public void setGPA(double g) {
gPA = g;
}
/**
* Returns a string showing the name, age and grade point average of a student
*/
public String toString() {
return (firstName + " " + midName + " " + lastName + "," + age + "," + gPA);
}
/**
* Returns true if this student is the same as student other else it returns false.
**/
public boolean equals(Student other) {
boolean r = false;
r = (firstName.equalsIgnoreCase(other.getFirstName())); // improve this part
return r;
}
}
--------------------------------------------------------------------------------------------------------------------
// Example of a class that uses the Student Class (Reference Class) import
java.util.Scanner;
System.out.println();
System.out.println("Unsorted List");
showList(list);
System.out.println();
System.out.println("Sorted List");
sortList(list);
showList(list);
System.exit(0);
}
return s;
}
// Warning! using the last name as the sort key is not sufficient for a realistic set of
records
// This method needs improvement
private static void sortList(Student[] s) {
Student temp;
int minIndex = 0;
for (int x = 0; x < s.length - 1; x++) {
minIndex = x;
for (int y = x + 1; y < s.length; y++) {
if
(s[minIndex].getLastName().compareToIgnoreCase(s[y].getLastName()) > 0)
minIndex = y;
}
if (minIndex != x) {
temp = s[x];
s[x] = s[minIndex];
s[minIndex] = temp;
}
}
} // end of sortList method
} // end of class
--------------------------------------------------------------------------------------------------------------------
Discussions
Creating a Reference Class
• Composition of a class
<modifier(s)> class <NameOfClass> {
<data_members> a.k.a. attributes/instance variables
<constructor(s)>
<method_members> a.k.a properties
}
• The Unified Modelling Language (UML)
The UML is a framework that provides a tool for creating visual representation of
the design of a Class along with other tools for the representation of the architecture
and implementation of a software system.
The Class Diagram included in the UML is the tool for the visual representation of
the design of a class. Hence, the Class Diagram is introduced in this course for the
purpose of communicating the design of a class.The other diagrams e.g. Activity
Diagram, Use Case Diagram, etc.) belonging to the UML framework may be studied
in other courses.
• The Class Diagram is a rectangular box that is divided in three parts as shown below.
The name of the class is indicated in first part of the rectangular box following a
centered orientation. The data members or instance variables indicated in the
second part of the box. Finally, the constructors and the method members of the
class are indicated in the third part of the box.
<NameOfClass>
<Instance Variables>
-firstName: String
-lastName: String
-idNumber: String
+Student()
+Student(firstName: String, lastName:String, idNumber: String)
+setFirstName(firstName:String):void
+setLastName(lastName:String):void
+setIdNumber(number: String):void
+getFirstName():String
+getLastName():String
+getIdNumber():String
+toString():String
In the Class Diagram, the minus symbol (-) is used to specify that the member is declared as
private in the implementation of the Class. On the other hand, the plus symbol(+) is used to
specify that the member is declared as public.