Composition
Friday, 18 October 2024 21:36
When there is an object of another class defined in the class variables.
This object can be instantiated (do magic in Constructor)
Can be used to call methods from its original class
Note unless they explicitly state that Q3 inherits from Q2 then only you use the extends
Q2
Student
- name: String
- grade: integer
+ Constructor (inN: string, inG: integer)
+ getName() : String
+ getGrade(): String
+ toString() : String
public class Student
{
//Class variables
private String name;
private int grade;
//Class constructor
public Students(String inN, int inG)
{
name = inN;
grade = inG;
}
//Accessor methods
public String getName()
{
return name;
}
public int getGrade()
{
return grade;
}
//To String
@Override
public String toString()
{
return name + "\t" + grade;
}
}
Q3
Subjects
- subjectName: String
- students: Student
+ Constructor (inSN: string, inST: Student)
+ getSubjectName() : String
+ toString() : String
toString format: name <tab> grade <tab>subjectName
public class Subjects
{
private String subjectName;
private Student students; //Creating an object of the class
//Constructor to initialise
public Subjects(String inSN, Student inST)
{
subjectName = inSN;
students = inST;
}
//Accessor
public String getSubjectName()
{
return subjectName;
}
//toString: we have to call the toString using the students objects
//same principle as super.toString(), in this case we replace super with the class object
(students)
@Override
public String toString()
{
return students.toString() + "\t" + subjectName;
}
}
Ways they can ask you to read data from a text file:
name#grade
subject
combine.txt
Shriyan#12
Info Tech
Prishaylen#12
Maths
Preolan#12
Physics
They can sometimes as all different data on a new line.
Q4:
4.1 Create the SubjectManager class.
4.2 Create an array sArr to store a max of 50 Subject objects. Also create a variable size to keep
track of these objects. These variables should not be accessible outside the class.
4.3 Read the data from the text file: combined.txt
4.4 Create a toString for the array.
4.5 Create a method to sort the data based on the subject name in alphabetical order
4.6 Create a method called findStudent that will return a position (integer) of a subject object in the
array. Pass through a student's name as a parameter.
public class SubjectManager
{
//4.2
private Subjects sArr[] = new Subjects[50];
private int size = 0;
//4.3
public SubjectManager()
{
//Step 1: try+catch
try
{
//Step 2: Scanner + FileReader (import)
Scanner sc = new Scanner(new FileReader("combined.txt"));
//Step 3: while loop
while (sc.hasNextLine())
{
//Step 4: Line Scanner
String line = sc.nextLine();
Scanner scLine = new Scanner(line).useDelimiter("#");
//Step 5: Reading the data
//Delimited Data
String name = scLine.next();
int grade = scLine.nextInt();
//Data on new line
When there is data on new lines: you will use sc.nextLine()
If the data needs to be converted: Integer.parseInt(sc.nextLine())
->converts the string to int
String subject = sc.nextLine();
Because we have an object as a class variable, we have to instantiate a Students
object to be placed in the array.
Below: Creates a Students object to be placed in the array
Students students = new Students(name, grade);
//In general case: ClassName var = new ClassName(f1, f2, f3);
//Initialising the array
sArr[size] = new Subjects(subject, students);
size++;
}
sc.close();
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
@Override
public String toString()
{
String display = "";
for (int i = 0; i < size; i++)
{
display = display + sArr[i].toString() + "\n";
}
return display;
}
Step 1: create a void method sort
public void sort()
{
Step 2: create a for-loop (i loop)
for (int i = 0; i < size - 1; i++)
{
Step 3: j-loop (j = i + 1)
for (int j = i + 1; j < size; j++)
{
Step 4: if statement (alphabetical)
if (sArr[i].getSubjectName().compareToIgnoreCase(sArr[j].getSubjectName()) > 0)
{
Step 5: swap
Subjects temp = sArr[i];
sArr[i] = sArr[j];
sArr[j] = temp;
}
}
}
}
Step 1: create a method called findStudent and pass the name as a parameter
public int findStudent(String name)
{
Step 2: create a position variable (int pos = -1;)
int pos = -1;
Step 3: loop through the array
for (int i = 0; i < size; i++)
{
Step 4: if statement->checks if the object at the current position is equal to the data
Highlighted: references a student object, meaning we can call methods from that student
class
if (sArr[i].getStudent().getName().equals(name))
{
Step 5: Setting pos = i
pos = i;
}
}
Step 6: after the loop, return pos.
return pos;
}