Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 35

26 Feb 2024

Radford Burger
Agenda
 Sorting an Arraylist of custom objects
 GUI - Introduction to swing
Sorting Arraylist of objects
with Comparator in
separate class
 We will define another class that will implement
the Comparator interface of the type of our
custom object.
Sorting Arraylist of objects
with Comparator in
separate class
 This class will override the compare method of
the Comparator interface which accepts two
objects of the Employee class as parameters and
returns the comparison value according to our
requirement whether we want to sort the array in
ascending or descending order and on which
attribute, we want to sort the list.
Sorting Arraylist with Comparator class:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
class MySalarySort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getEmpSalary( ) – o2.getEmpSalary( )); //ascending)
}
Sorting Arraylist with Comparator class:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));

Collections.sort(employeeList, new MySalarySort( ));


}//end main
}//end class
class MySalarySort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getEmpSalary( )-o2.getEmpSalary( ));
}
Sorting Arraylist with Comparator class:
public class EmployeeListProgram {
example
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
Collections.sort(employeeList, new
MySalarySort(
for (Employee employee));
: employeeList) {
message.append("SORTED ON SALARY\n");
message.append("Name: ").append(employee.getEmpName()).append("\n");
message.append("Surname: ").append(employee.getEmpSurname()).append("\
n");
message.append("Salary: ").append(employee.getEmpSalary()).append("\n\n");
}
JOptionPane.showMessageDialog(null, message.toString());
}//end main
}//end class
Sorting Arraylist with Comparator class:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
class MySalarySort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o2.getEmpSalary( ) - o1.getEmpSalary( )); //descending)
}
Sorting Arraylist with Anonymous
Comparator interface: example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
class MySalarySort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getEmpSalary( ) – o2.getEmpSalary( )); //ascending)
}
Sorting Arraylist with Anonymous
Comparator interface: example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
Collections.sort(employeeList, new Comparator<Employee>( ){
@Override
public int compare(Employee o1, Employee o2) {
return (int) (o1.getEmpSalary( ) – o2.getEmpSalary( )); //ascending)
}

}//end main
}//end class
Sorting Arraylist with Lambda expression:
example
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
Collections.sort(employeeList, (o1, o2) -> (int) (o1.getEmpSalary( ) –
o2.getEmpSalary( )); //ascending)

}//end main
}//end class
Sorting Arraylist with Comparator class:
example with Strings
public class EmployeeListProgram {
public static void main(String[] args) {
ArrayList<Employee> employeeList = new ArrayList<>( );
// populate arraylist with hard-coded Employee objects
employeeList.add(new Employee("Kaya", "Mabula", 29, 8400.0));
employeeList.add(new Employee("Sisanda", "Bhele", 34, 3500.0));
employeeList.add(new Employee("Julian", "Lawson", 26, 6450.0));
employeeList.add(new Employee("Bongi", "Adams", 33, 5400.0));
employeeList.add(new Employee("Marie", "Botha", 23, 7450.0));
}//end main
}//end class
class MySurnameSort implements Comparator<Employee>{
@Override
public int compare(Employee o1, Employee o2) {
return (int)
(o1.getEmpSurname( ).compareTo(o2.getEmpSurname( ))); //ascending)
}
Practical exercise for this
week
 Watch video: Java 8 Lambda - Sort List (ArrayList) in
Ascending and Descending Order Comparator Examples
https://www.youtube.com/watch?v=2DOzWaVen4Q

 Complete Prac 3 – sorting of Arraylists


Graphical User Interface
javax.swing.*
(Gui)
Where do we start?....LottoGui.java
(LottoProject source code is uploaded to BB)
Lotto Gui
Step 1: Design
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and
it’s functionality
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and
it’s functionality

Decide how these


components will be
arranged. Choose a layout.
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and
it’s functionality
textfield
Decide how these
textfield
components will be
arranged. Choose a layout.
textfield
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and
it’s functionality

Decide how these b b b


components will be u u u
arranged. Choose a layout. t t t
t t t
o o o
n n n
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and c c
it’s functionality h h
e e
Decide how these c c
components will be k k
arranged. Choose a layout. b b
o o
x x
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and
it’s functionality
L
Decide how these a
components will be b
arranged. Choose a layout. e
l
Lotto Gui
Step 1: Design

Decide what components


are required for the
particular application and
it’s functionality

Decide how these


components will be
arranged. Choose a layout.
labels
Lotto Gui
Step 1: Design

Default layout manager for


JFrame is BorderLayout

Divides frame into 5


sections: WEST, EAST
Lotto Gui
Step 1: Design

Default layout manager for


JFrame is BorderLayout

Divides frame into 5


sections: SOUTH
Lotto Gui
Step 1: Design

Default layout manager for


JFrame is BorderLayout

Divides frame into 5


sections: CENTER
Lotto Gui
Step 1: Design
Step 2: Declare
variables for different
components
Lotto Gui
Step 1: Design
Step 2: Declare
variables for different
components
Step 3: Instantiate
variables in constructor
Lotto Gui
Step 1: Design
Step 2: Declare
variables for different
components
Step 3: Instantiate
variables in constructor
Step 4: add components
to panels and frame
Lotto Gui
Step 1: Design
Step 2: Declare
variables for different
components
Step 3: Instantiate
variables in constructor
Step 4: add components
to panels and frame
Step 5: add event
listeners
Lotto Gui
Step 1: Design
Step 2: Declare
variables for different
components
Step 3: Instantiate
variables in constructor
Step 4: add components
to panels and frame
Step 5: add event
listeners
Step 6: handle events
Lotto Gui
Step 1: Design
Step 2: Declare
variables for different
components
Step 3: Instantiate
variables in constructor
Step 4: add components
to panels and frame
Step 5: add event
listeners
Step 6: handle events
Step 7: construct GUI
object from main
Practical exercise
a) Try to add some GUI
components to the
NORTH area.
Practical exercise
a) Try to add some GUI
components to the
NORTH area.

b) Redesign the layout


of the entire GUI. The
current functionality
must remain but you
can provide additional.
Practical exercise for this
week
 Experiment with different layouts by
making changes to LottoGui.java
Conclusion

Please check BB regularly for any


announcements
If you have any questions/queries please
contact me via email:
burgerr@cput.ac.za
Please use your cput email for all
correspondence with your lecturers

You might also like