question archive The Author Class The Author class represents an author who wrote and published at least one paper that is available at the Paper DB
Subject:Computer SciencePrice: Bought3
The Author Class
The Author class represents an author who wrote and published at least one paper that is available at the Paper DB.
Do the following in the Author class:
The Paper Class
The Paper class represents a paper that is available at the Paper DB and a registered author of the Paper DB can download this paper.
Do the following in the Paper class:
The PaperDB Class
The class called PaperDB represents the online database where authors can register themselves to share their papers and download papers published by other authors. The class should have the following attributes:
Create the following methods:
Go back to the Author class and add these methods:
1. "How to a paper" by Ains Tain, John Doe, published in ABC conference
Now you should test your code. To do so, run the PaperDBTestProgram1.java file and compare your results to the expected results below.
Here is the output that you should see:
Status: DBLP Paper DB with 0 registered authors
All registerd Authors: []
All downloadable Papers:
None
Status: DBLP Paper DB with 4 registered authors
All registerd Authors: [Ains Tain: 3 papers (registered), Bugs Bunny: 3 papers (registered), Robin Road: 2 papers (registered), Peter Pan: 2 papers (registered)]
All downloadable Papers:
1. "How to a paper" by Ains Tain, John Doe, published in ABC conference
2. "A Nice Paper" by Ains Tain, published in SODA II
3. "Very Short paper" by Ains Tain, published in IEEE Mini
4. "How to survive first year comp sci" by Bugs Bunny, published in WALCOMP
5. "Neural Nets: Say Hi to Our Future Machine Overlords" by Bugs Bunny, published in CYBORG
6. "Useless graphs and how to draw them" by Bugs Bunny, published in Graph Drawing
7. "A boring paper" by Robin Road, published in ABC conference
8. "A Funny Paper" by Peter Pan, Robin Road, published in CALLDAM
9. "Art of selling your paper" by Peter Pan, published in XYZ conference
10. "A Funny Paper" by Peter Pan, Robin Road, published in CALLDAM
Go back to the Author class and add these methods:
Now, run the PaperDBTestProgram2.java file and compare your results to the expected results below.
Author with name Ains Tain: Ains Tain: 3 papers (registered)
All papers by Ains Tain
1. "How to a paper" by Ains Tain, John Doe, published in ABC conference
2. "A Nice Paper" by Ains Tain, published in SODA II
3. "Very Short paper" by Ains Tain, published in IEEE Mini
Author with name Ains Tain: Ains Tain: 4 papers (registered)
All papers by Ains Tain after adding P10
1. "How to a paper" by Ains Tain, John Doe, published in ABC conference
2. "A Nice Paper" by Ains Tain, published in SODA II
3. "Very Short paper" by Ains Tain, published in IEEE Mini
4. "Useless graphs and how to draw them" by Bugs Bunny, Ains Tain, published in Graph Drawing
All Papers by Bugs Bunny, Ains Tain & John Doe
Papers not available
All Papers by Bugs Bunny & Ains Tain
"Useless graphs and how to draw them" by Bugs Bunny, Ains Tain, published in Graph Drawing
All Papers by Robin Road & Peter Pan
"A Funny Paper" by Peter Pan, Robin Road, published in CALLDAM
4. Downloading Papers
In the Author class, add the following methods:
Add the following attribute to the PaperDB class:
Add the following method to the PaperDB class:
5. Bonus
Collections.sort(yourListOfPairs, new Comparator<Pair<Integer, Paper>>() {
public int compare(Pair<Integer, Paper> p1, Pair<Integer, Paper> p2) {
// PUT YOUR CODE IN HERE
}
});
Insert the missing code so that it returns an appropriate integer indicating whether pair p1 comes before or after pair p2 in the sort order (see notes, Chapter 8, Page 281).
Run the PaperDBTestProgram3.java test file to make sure it works with your code.
Here is the expected output:
Here are the downloaded papers:
"A Funny Paper" by Peter Pan, Robin Road, published in CALLDAM
"Art of selling your paper" by Peter Pan, published in XYZ conference
"A Nice Paper" by Ains Tain, published in SODA II
"A Nice Paper" by Ains Tain, published in SODA II
"Very Short paper" by Ains Tain, published in IEEE Mini
"How to survive first year comp sci" by Bugs Bunny, published in WALCOMP
"A Nice Paper" by Ains Tain, published in SODA II
"Very Short paper" by Ains Tain, published in IEEE Mini
Here are the unique downloaded papers alphabetically:
"A Funny Paper" by Peter Pan, Robin Road, published in CALLDAM
"A Nice Paper" by Ains Tain, published in SODA II
"Art of selling your paper" by Peter Pan, published in XYZ conference
"How to survive first year comp sci" by Bugs Bunny, published in WALCOMP
"Very Short paper" by Ains Tain, published in IEEE Mini
Here are the downloaded papers by populariry (BONUS):
(3 downloads) "A Nice Paper" by Ains Tain, published in SODA II
(2 downloads) "Very Short paper" by Ains Tain, published in IEEE Mini
(2 downloads) "A Funny Paper" by Peter Pan, Robin Road, published in CALLDAM
(1 downloads) "Art of selling your paper" by Peter Pan, published in XYZ conference
(1 downloads) "How to survive first year comp sci" by Bugs Bunny, published in WALCOMP
(0 downloads) "How to wrote a paper" by Ains Tain, John Doe, published in ABC conference
(0 downloads) "A boring paper" by Robin Road, published in ABC conference
(0 downloads) "Neural Nets: Say Hi to Our Future Machine Overlords" by Bugs Bunny, published in CYBORG
(0 downloads) "Useless graphs and how to draw them" by Bugs Bunny, published in Graph Drawing
import java.util.ArrayList;
public class Author {
private String authorName;
private boolean registered;
ArrayList<Paper> papers;
public Author() { this("");}
public Author(String aName) {
//todo: complete
authorName = aName;
registered = false;
papers=new ArrayList<Paper>();
}
public ArrayList<Paper> getPapers(){
return papers;
}
public String getAuthorName() { return authorName; }
public boolean isRegistered() { return registered; }
//TODO: complete code
public String toString() {
String s = "" + authorName + ": "+ "#" + " papers (";
if (!registered) s += "not ";
return s + "registered)";
}
public void addPaper(Paper p, PaperDB pdb){
//todo:complete
if(!papers.contains(p)){
papers.add(p);
}
}
}
import java.util.ArrayList;
public class Paper {
private String title;
private String conference;
ArrayList<Author>authors;
public Paper() {
this("","");
}
public Paper(String t, String c) {
title = t;
conference = c;
authors=new ArrayList<Author>();
}
public ArrayList<Author> getAuthors(){ return authors; }
public String getTitle() {
return title;
}
public String getConference() {
return conference;
}
//TODO: complete code
public String toString() {
String result = """ + title + "" by ";
result += "published in " + conference+"author is "+authors;
return result;
}
}
import java.util.ArrayList;
import java.util.HashMap;
public class PaperDB {
private String name;
ArrayList<Author>allRegisteredAuthors;
private HashMap<String, Paper> titleList;
private HashMap <Author, ArrayList<Paper>>authorList;
private HashMap<String, ArrayList<Paper>> conferenceList;
public PaperDB(){this("");}
public PaperDB(String aName) {
name = aName;
//TODO: instantiate all the member variables
allRegisteredAuthors=new ArrayList<>();
titleList=new HashMap<String, Paper>();
authorList=new HashMap<Author, ArrayList<Paper>>();
conferenceList=new HashMap<String,ArrayList<Paper>>();
}
public HashMap<String,Paper> getTitleList() { return titleList; }
public HashMap<Author,ArrayList<Paper>> getAuthorList() { return authorList; }
public HashMap<String,ArrayList<Paper>> getConferenceList() { return conferenceList; }
public String getName() {
return name;
}
// public String authorWithName(String s){
// if(!authorList.isEmpty()){
//
// }
// return null;
// }
// public Author registerAuthor(Author a){
// allRegisteredAuthors.add(a);
//
// }
//TODO: complete code
public String toString(){
return this.name + "Paper DB with # registered authors";
}
}
import java.util.ArrayList;
public class PaperDBTestProgram1 {
public static void main(String args[]) {
// Create a new paper database
PaperDB pdb = new PaperDB("DBLP");
ArrayList<String> list = new ArrayList<String>();
// Create some authors
Author ainsTain = new Author("Ains Tain");
Author johnDoe = new Author("John Doe");
Author peterPan = new Author("Peter Pan");
Author robinRoad = new Author("Robin Road");
Author bugsBunny = new Author("Bugs Bunny");
// Create some papers
Paper p1 = new Paper("How to write a paper", "ABC conference");
Paper p2 = new Paper("Triangles and Squares: A preschool analysis", "CCCG");
Paper p3 = new Paper("A Nice Paper", "SODA II");
Paper p4 = new Paper("Very Short paper", "IEEE Mini");
Paper p5 = new Paper("A boring paper", "ABC conference");
Paper p6 = new Paper("How to survive first year comp sci", "WALCOMP");
Paper p7 = new Paper("Neural Nets: Say Hi to Our Future Machine Overlords", "CYBORG");
Paper p8 = new Paper("Art of selling your paper", "XYZ conference");
Paper p9 = new Paper("A Funny Paper", "CALLDAM");
Paper p10 = new Paper("Useless graphs and how to draw them", "Graph Drawing");
//add papers to authors
ainsTain.addPaper(p1, pdb);
ainsTain.addPaper(p3, pdb);
ainsTain.addPaper(p4, pdb);
johnDoe.addPaper(p1, pdb);
johnDoe.addPaper(p2, pdb);
peterPan.addPaper(p8, pdb);
peterPan.addPaper(p9, pdb);
robinRoad.addPaper(p5, pdb);
robinRoad.addPaper(p9, pdb);
bugsBunny.addPaper(p6, pdb);
bugsBunny.addPaper(p7, pdb);
bugsBunny.addPaper(p10, pdb);
// Display the state of things before any author registers
System.out.println("Status: " + pdb);
System.out.println("All registerd Authors: " +
bugsBunny.requestAllRegisteredAuthors(pdb));
System.out.println("All downloadable Papers: ");
list = bugsBunny.requestAllDownloadablePapers(pdb);
if(list.size() == 0) System.out.println("None");
else {
for (String line : list) {
System.out.println(line);
}
}
System.out.println();
// Register the authors, except johnDoe
ainsTain.register(pdb);
bugsBunny.register(pdb);
robinRoad.register(pdb);
peterPan.register(pdb);
// Display the state of things after some authors register
System.out.println("Status: " + pdb);
System.out.println("All registerd Authors: " +
bugsBunny.requestAllRegisteredAuthors(pdb));
System.out.println("All downloadable Papers: ");
list = bugsBunny.requestAllDownloadablePapers(pdb);
if(list.size() == 0) System.out.println("None");
else {
for (String line : list) {
System.out.println(line);
}
}
System.out.println();
}
}
import java.util.ArrayList;
public class PaperDBTestProgram2 {
public static void main(String args[]) {
// Create a new paper database
PaperDB pdb = new PaperDB("DBLP");
ArrayList<String> list = new ArrayList<String>();
// Create some authors
Author ainsTain = new Author("Ains Tain");
Author johnDoe = new Author("John Doe");
Author peterPan = new Author("Peter Pan");
Author robinRoad = new Author("Robin Road");
Author bugsBunny = new Author("Bugs Bunny");
// Create some papers
Paper p1 = new Paper("How to write a paper", "ABC conference");
Paper p2 = new Paper("Triangles and Squares: A preschool analysis", "CCCG");
Paper p3 = new Paper("A Nice Paper", "SODA II");
Paper p4 = new Paper("Very Short paper", "IEEE Mini");
Paper p5 = new Paper("A boring paper", "ABC conference");
Paper p6 = new Paper("How to survive first year comp sci", "WALCOMP");
Paper p7 = new Paper("Neural Nets: Say Hi to Our Future Machine Overlords", "CYBORG");
Paper p8 = new Paper("Art of selling your paper", "XYZ conference");
Paper p9 = new Paper("A Funny Paper", "CALLDAM");
Paper p10 = new Paper("Useless graphs and how to draw them", "Graph Drawing");
//add papers to authors
ainsTain.addPaper(p1, pdb);
ainsTain.addPaper(p3, pdb);
ainsTain.addPaper(p4, pdb);
johnDoe.addPaper(p1, pdb);
johnDoe.addPaper(p2, pdb);
peterPan.addPaper(p8, pdb);
peterPan.addPaper(p9, pdb);
robinRoad.addPaper(p5, pdb);
robinRoad.addPaper(p9, pdb);
bugsBunny.addPaper(p6, pdb);
bugsBunny.addPaper(p7, pdb);
bugsBunny.addPaper(p10, pdb);
// Register the authors, except johnDoe
ainsTain.register(pdb);
bugsBunny.register(pdb);
robinRoad.register(pdb);
peterPan.register(pdb);
//search
System.out.println("Author with name Ains Tain: " + pdb.authorWithName("Ains Tain"));
System.out.println("All papers by Ains Tain");
list = bugsBunny.requestAllPapersByAuthor(pdb, "Ains Tain");
for(String line: list){
System.out.println(line);
}
System.out.println();
ainsTain.addPaper(p10,pdb);
System.out.println("Author with name Ains Tain: " + pdb.authorWithName("Ains Tain"));
System.out.println("All papers by Ains Tain after adding P10");
list = bugsBunny.requestAllPapersByAuthor(pdb, "Ains Tain");
for(String line: list){
System.out.println(line);
}
System.out.println();
ArrayList<Paper> paperList;
//No papers should be available - John Doe is not registered
paperList = bugsBunny.requestAllPapersByAuthors(pdb,"Bugs Bunny","Ains Tain","John Doe");
System.out.println("All Papers by Bugs Bunny, Ains Tain & John Doe");
if(paperList.isEmpty()) System.out.println("Papers not available");
else{for(Paper p: paperList)
System.out.println(p);}
System.out.println();
paperList = bugsBunny.requestAllPapersByAuthors(pdb,"Bugs Bunny","Ains Tain");
System.out.println("All Papers by Bugs Bunny & Ains Tain");
if(paperList.isEmpty()) System.out.println("Papers not available");
else{for(Paper p: paperList)
System.out.println(p);}
System.out.println();
paperList = bugsBunny.requestAllPapersByAuthors(pdb,"Robin Road","Peter Pan");
System.out.println("All Papers by Robin Road & Peter Pan");
if(paperList.isEmpty()) System.out.println("Papers not available");
else{for(Paper p: paperList)
System.out.println(p);}
}
}
import java.util.ArrayList;
public class PaperDBTestProgram3 {
public static void main(String args[]) {
// Create a new paper database
PaperDB pdb = new PaperDB("DBLP");
ArrayList<String> list = new ArrayList<String>();
// Create some authors
Author ainsTain = new Author("Ains Tain");
Author johnDoe = new Author("John Doe");
Author peterPan = new Author("Peter Pan");
Author robinRoad = new Author("Robin Road");
Author bugsBunny = new Author("Bugs Bunny");
// Create some papers
Paper p1 = new Paper("How to write a paper", "ABC conference");
Paper p2 = new Paper("Triangles and Squares: A preschool analysis", "CCCG");
Paper p3 = new Paper("A Nice Paper", "SODA II");
Paper p4 = new Paper("Very Short paper", "IEEE Mini");
Paper p5 = new Paper("A boring paper", "ABC conference");
Paper p6 = new Paper("How to survive first year comp sci", "WALCOMP");
Paper p7 = new Paper("Neural Nets: Say Hi to Our Future Machine Overlords", "CYBORG");
Paper p8 = new Paper("Art of selling your paper", "XYZ conference");
Paper p9 = new Paper("A Funny Paper", "CALLDAM");
Paper p10 = new Paper("Useless graphs and how to draw them", "Graph Drawing");
//add papers to authors
ainsTain.addPaper(p1, pdb);
ainsTain.addPaper(p3, pdb);
ainsTain.addPaper(p4, pdb);
johnDoe.addPaper(p1, pdb);
johnDoe.addPaper(p2, pdb);
peterPan.addPaper(p8, pdb);
peterPan.addPaper(p9, pdb);
robinRoad.addPaper(p5, pdb);
robinRoad.addPaper(p9, pdb);
bugsBunny.addPaper(p6, pdb);
bugsBunny.addPaper(p7, pdb);
bugsBunny.addPaper(p10, pdb);
// Register the authors, except johnDoe
ainsTain.register(pdb);
bugsBunny.register(pdb);
robinRoad.register(pdb);
peterPan.register(pdb);
//Download papers
bugsBunny.downloadPaper(pdb,"A Funny Paper");
bugsBunny.downloadPaper(pdb,"Art of selling your paper");
peterPan.downloadPaper(pdb,"A Nice Paper");
johnDoe.downloadPaper(pdb,"A Nice Paper"); //can't, johnDoe is not registered
ainsTain.downloadPaper(pdb,"A Nice Paper"); //can't, ainsTain is the author of this paper
robinRoad.downloadPaper(pdb,"A Nice Paper");
robinRoad.downloadPaper(pdb,"A Funny Paper");
peterPan.downloadPaper(pdb,"Very Short paper");
peterPan.downloadPaper(pdb,"How to survive first year comp sci");
peterPan.downloadPaper(pdb,"A Nice Paper");
peterPan.downloadPaper(pdb,"Very Short paper");
// Display the downloaded papers
System.out.println("nHere are the downloaded papers: ");
for (Paper p: pdb.getAllDownloadedPapers()){
System.out.println(p);
}
// Display the downloaded papers alphabetically
System.out.println("nHere are the unique downloaded papers alphabetically: ");
for (Paper p: pdb.uniqueDownloads()){
System.out.println(p);
}
// Display the downloaded papers in order of popularity
System.out.println("nHere are the downloaded papers by populariry (BONUS): ");
for (Pair<Integer,Paper> p: pdb.papersByPopularity()){
System.out.println("(" + p.getKey() + " downloads) " + p.getValue());
}
}
}
i am confused how to addPapaer into the PaperDB
