Java Help!! I need help building class main write a program that will create an
ID: 3577688 • Letter: J
Question
Java Help!! I need help building class main write a program that will create an Array or ArrayList of Project objects.
1.Implement the Comparable interface on your Project class and have it sort by the project name. Sort your collection of Project objects with the default sort and print them to the screen.
2.Create a Comparator object that will compare Project objects based on the project ID. Sort your collection of Project objects with the comparator and print them to the screen.
3.Create a comparator object that will compare Project objects based on the text length of the project description. Sort your collection of Project objects with this comparator and print them to the screen.
4.Sort your collection by project name in reverse alphabetical order, using a lambda expression, and print them to screen.
5.Sort your collection by project ID from largest to smallest using the Comparator.comparing() method, and print them to the screen
6.Sort your collection by Active Flag, then project name, using a method of your choosing, and then print them to the screen.
Before printing the collection at each step, print a header line that explains which method is being used.
project -------------
import java.util.Comparator;
public class Project implements Comparable, Comparator { int projectId; boolean active; String projectName; String projectDescription; public Project(int id, String status, String name, String description) { projectId = id; active = status.equals("A"); projectName = name; projectDescription = description; } Project() { } public int getProjectID() { return projectId; } public String getProjectName() { return projectName; } public String getProjectDescription() { return projectDescription; } public boolean getActiveFlag() { return active; } public void display() { System.out.println("Project ID: " + projectId); if (active == true) { System.out.println("Active flag: " + "A"); } else { System.out.println("Active flag: " + "I"); } System.out.println("Project Name: " + projectName); System.out.println("Project Description: " + projectDescription); } @Override public int compareTo(Project p) { return (this.getProjectName().compareTo(p.getProjectName())); } @Override public int compare(Project p1, Project p2) { return p1.projectId - p2.projectId; } }
ProjectDAO ---------------------
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ProjectDAO { private String fileName = "project_data.txt"; List projectList = new ArrayList<>(); public ProjectDAO(String file) throws FileNotFoundException { fileName = file; Scanner infile = new Scanner(new FileReader(file)); while (infile.hasNextLine()) { String str[] = infile.nextLine().split("|"); projectList.add(new Project(Integer.parseInt(str[0]), str[1], str[2], str[3])); } } public Project getProject(int id) { return projectList.get(id); } public void sortByProjectID(List projectList) { Collections.sort(projectList, new Project()); } public void sortByProjectName(List projectList) { Collections.sort(projectList); } public void PrintProjectDetails() { for (int i = 0; i < projectList.size(); i++) { projectList.get(i).display(); } } }
Explanation / Answer
package dec13;
public class Project implements Comparable {
int projectId;
boolean active;
String projectName;
String projectDescription;
public Project(int id, String status, String name, String description) {
projectId = id;
active = status.equals("A");
projectName = name;
projectDescription = description;
}
Project() {
}
public int getProjectID() {
return projectId;
}
public String getProjectName() {
return projectName;
}
public String getProjectDescription() {
return projectDescription;
}
public boolean getActiveFlag() {
return active;
}
public void display() {
System.out.println("Project ID: " + projectId);
if (active == true) {
System.out.println("Active flag: " + "A");
} else {
System.out.println("Active flag: " + "I");
}
System.out.println("Project Name: " + projectName);
System.out.println("Project Description: " + projectDescription);
}
@Override
public int compareTo(Object p) {
return (this.getProjectName().compareTo(((Project) p).getProjectName()));
}
}
-------------------------
package dec13;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ProjectDAO {
private String fileName = "project_data.txt";
List projectList = new ArrayList<>();
public List getProjectList() {
return projectList;
}
public void setProjectList(List projectList) {
this.projectList = projectList;
}
public ProjectDAO(String file) throws FileNotFoundException {
fileName = file;
Scanner infile = new Scanner(new FileReader(file));
while (infile.hasNextLine()) {
String s = infile.nextLine();
String str[] = s.split("\|");
projectList.add(new Project(Integer.parseInt(str[0]), str[1],
str[2], str[3]));
}
}
public Project getProject(int id) {
return (Project) projectList.get(id);
}
public void sortByProjectID(List projectList) {
Collections.sort(projectList, new SortByProjectIdComparator());
}
public void sortByProjectName(List projectList) {
Collections.sort(projectList);
}
public void PrintProjectDetails() {
for (int i = 0; i < projectList.size(); i++) {
((Project) projectList.get(i)).display();
}
}
}
----------------------------
package dec13;
import java.util.Comparator;
public class SortByProjectIdComparator implements Comparator {
@Override
public int compare(Object p1, Object p2) {
return ((Project) p1).projectId - ((Project) p2).projectId;
}
}
-----------------------------------------
package dec13;
import java.util.Comparator;
class SortByProjectDescriptionComparator implements Comparator {
@Override
public int compare(Object p1, Object p2) {
return ((Project) p1).projectDescription.length()
- ((Project) p2).projectDescription.length();
}
}
----------------------------------------------
import java.io.FileNotFoundException;
public class Tester {
public static void main(String[] args) throws FileNotFoundException {
ProjectDAO dao = new ProjectDAO("input1.txt");
dao.PrintProjectDetails();
dao.sortByProjectID(dao.getProjectList());
dao.sortByProjectName(dao.getProjectList());
dao.PrintProjectDetails();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.