Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Module1 Exercise4 UsingAClassToCreateRecords

The document provides instructions for submitting a programming exercise and discusses creating a class to represent student records. It includes examples of using a Student class to define attributes and methods for student data, and a StudentList class to manage an array of Student objects.

Uploaded by

Adrian Nabua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Module1 Exercise4 UsingAClassToCreateRecords

The document provides instructions for submitting a programming exercise and discusses creating a class to represent student records. It includes examples of using a Student class to define attributes and methods for student data, and a StudentList class to manage an array of Student objects.

Uploaded by

Adrian Nabua
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Computer Programming 2 Exercise 4

Using a class to create records

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.

To submit your work:


1. Open your project in IntelliJ IDEA.
2. Check all necessary packages and classes in the project. Countercheck if all your
programs are running properly.
3. Once everything is in place, you are to archive your project by accessing File menu
then choose Export | Project to Zip file…
4. Specify the location where you will create the archive file (remember the location).
Open your file browser and go to the location you specified, you should have a file
named after your project with a zip extension (e.g. DelaCruzJuan9300.zip).
5. Upload the file into the submission box provided by the faculty-in-charge in the
Google Classroom.

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.

First Approach: Use parallel arrays


Second Approach: Use an array (one dimensional array) of objects

Parallel Arrays
Arrays that are processed in parallel because the elements are associated

First Name Middle Name name Last Name Age Grade Point Average

Juan Abad Cruz 24 91.25

Mark Lee Kuan 25 92.30

Anne Dimo Lopez 23 90.87

Carla Park Somi 24 88.90

… … … … …
Formulating Record Structure Using Java
• A record is composed of fields.

• A field is an attribute of a record.

• 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.

--------------------------------------------------------------------------------------------------------------------

// Example of a Reference Class


public class Student {
private String firstName = "";
private String midName = "";
private String lastName = "";
private int age;
private double gPA;

/**
* 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;

public class StudentList {


static Scanner keyboard = new Scanner(System.in);

public static void main(String[] args) {


Student[] list;
int number;

System.out.print("How many students will be listed? "); number =


Integer.parseInt(keyboard.nextLine());

list = new Student[number];

System.out.println("Enter the student information."); for (int x = 0; x <


list.length; x++) {
System.out.println("For student " + (x + 1) + " :"); list[x] = readStudent();
}

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);
}

private static void showList(Student[] studs) { for (int x = 0; x


< studs.length; x++) {
System.out.println(studs[x].toString()); }
}

private static Student readStudent() {


System.out.print("first name: ");
String f = keyboard.nextLine();
System.out.print("middle name: ");
String m = keyboard.nextLine();
System.out.print("last name: ");
String l = keyboard.nextLine();
System.out.print("age: ");
int a = Integer.parseInt(keyboard.nextLine());
System.out.print("grade point average: ");
double g = Double.parseDouble(keyboard.nextLine()); Student s =

new Student(f, m, l, a, g);

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>

<Constructors and Method Members>

The following is an example of a class diagram that presents he composition of


Reference Class called student.
Student

-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.

You might also like