Initial Commit
This commit is contained in:
8
Source Code/Assignment 2/.idea/.gitignore
generated
vendored
Normal file
8
Source Code/Assignment 2/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/../../../../../../../:\ProgrammingProjects\JavaProjects\JavaYear1\CS1083\Assignment 2\.idea/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
7
Source Code/Assignment 2/.idea/codeStyles/Project.xml
generated
Normal file
7
Source Code/Assignment 2/.idea/codeStyles/Project.xml
generated
Normal file
@ -0,0 +1,7 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<code_scheme name="Project" version="173">
|
||||
<ScalaCodeStyleSettings>
|
||||
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||
</ScalaCodeStyleSettings>
|
||||
</code_scheme>
|
||||
</component>
|
5
Source Code/Assignment 2/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
Source Code/Assignment 2/.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
@ -0,0 +1,5 @@
|
||||
<component name="ProjectCodeStyleConfiguration">
|
||||
<state>
|
||||
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||
</state>
|
||||
</component>
|
6
Source Code/Assignment 2/.idea/discord.xml
generated
Normal file
6
Source Code/Assignment 2/.idea/discord.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DiscordProjectSettings">
|
||||
<option name="show" value="PROJECT_FILES" />
|
||||
</component>
|
||||
</project>
|
6
Source Code/Assignment 2/.idea/misc.xml
generated
Normal file
6
Source Code/Assignment 2/.idea/misc.xml
generated
Normal file
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
8
Source Code/Assignment 2/.idea/modules.xml
generated
Normal file
8
Source Code/Assignment 2/.idea/modules.xml
generated
Normal file
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/Assignment 2.iml" filepath="$PROJECT_DIR$/Assignment 2.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
11
Source Code/Assignment 2/Assignment 2.iml
Normal file
11
Source Code/Assignment 2/Assignment 2.iml
Normal file
@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
11
Source Code/Assignment 2/input.txt
Normal file
11
Source Code/Assignment 2/input.txt
Normal file
@ -0,0 +1,11 @@
|
||||
5
|
||||
9780006716693,The Last Battle,Lewis,2
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9781400079179,The Da Vinci Code,Brown,2
|
||||
3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9780071079170,The Skin We're In,Cole,5
|
12
Source Code/Assignment 2/input2.txt
Normal file
12
Source Code/Assignment 2/input2.txt
Normal file
@ -0,0 +1,12 @@
|
||||
5
|
||||
9780006716693,The Last Battle,Lewis,2
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9781400079179,The Da Vinci Code,Brown,2
|
||||
4
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780071079170,The Skin We're In,Cole,5
|
13
Source Code/Assignment 2/input3.txt
Normal file
13
Source Code/Assignment 2/input3.txt
Normal file
@ -0,0 +1,13 @@
|
||||
6
|
||||
9780006716693,The Last Battle,Lewis,2
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9781400079179,The Da Vinci Code,Brown,2
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
4
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780071079170,The Skin We're In,Cole,5
|
15
Source Code/Assignment 2/input4.txt
Normal file
15
Source Code/Assignment 2/input4.txt
Normal file
@ -0,0 +1,15 @@
|
||||
6
|
||||
9780006716693,The Last Battle,Lewis,2
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9781400079179,The Da Vinci Code,Brown,2
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
6
|
||||
9780006716693,The Last Battle,Lewis,2
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9781400079179,The Da Vinci Code,Brown,2
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
9780071079170,The Skin We're In,Cole,5
|
9
Source Code/Assignment 2/input5.txt
Normal file
9
Source Code/Assignment 2/input5.txt
Normal file
@ -0,0 +1,9 @@
|
||||
5
|
||||
9780006716693,The Last Battle,Lewis,2
|
||||
9780007591855,The Silmarillion,Tolkien,3
|
||||
9780048232298,The Lord of the Rings,Tolkien,1
|
||||
9780385491037,The Robber Bride,Atwood,3
|
||||
9781400079179,The Da Vinci Code,Brown,2
|
||||
1
|
||||
9780062230621,The Confidence Code,Kay,0
|
||||
9780071079170,The Skin We're In,Cole,5
|
BIN
Source Code/Assignment 2/out/production/Assignment 2/Book.class
Normal file
BIN
Source Code/Assignment 2/out/production/Assignment 2/Book.class
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
87
Source Code/Assignment 2/src/Book.java
Normal file
87
Source Code/Assignment 2/src/Book.java
Normal file
@ -0,0 +1,87 @@
|
||||
/**
|
||||
Represents a Book in a personal library
|
||||
@author L Bidlake
|
||||
*/
|
||||
public class Book{
|
||||
|
||||
/**
|
||||
The ISBN of the book
|
||||
*/
|
||||
private final long ISBN;
|
||||
|
||||
/**
|
||||
The title of the book
|
||||
*/
|
||||
private final String TITLE;
|
||||
|
||||
/**
|
||||
The author of the book
|
||||
*/
|
||||
private final String AUTHOR;
|
||||
|
||||
/**
|
||||
The number of times the book has been read
|
||||
*/
|
||||
private int read;
|
||||
|
||||
/**
|
||||
Constructs a book with the specifice ISBN, title and author.
|
||||
@param isbn The ISBN of the book.
|
||||
@param title The title of the book.
|
||||
@param author The author of the book.
|
||||
@param read The number of times the book has been read.
|
||||
*/
|
||||
public Book(long isbn, String title, String author, int read){
|
||||
ISBN = isbn;
|
||||
TITLE = title;
|
||||
AUTHOR = author;
|
||||
this.read = read;
|
||||
}
|
||||
|
||||
/**
|
||||
This method returns the ISBN of the book.
|
||||
@return The ISBN of the book.
|
||||
*/
|
||||
public long getIsbn(){
|
||||
return ISBN;
|
||||
}
|
||||
|
||||
/**
|
||||
This method returns the title of the book.
|
||||
@return The title of the book.
|
||||
*/
|
||||
public String getTitle(){
|
||||
return TITLE;
|
||||
}
|
||||
|
||||
/**
|
||||
This method returns the author of the book.
|
||||
@return The author of the book.
|
||||
*/
|
||||
public String getAuthor(){
|
||||
return AUTHOR;
|
||||
}
|
||||
|
||||
/**
|
||||
This method returns the number of times the book has been read.
|
||||
@return The number of times the book has been read.
|
||||
*/
|
||||
public int getRead(){
|
||||
return read;
|
||||
}
|
||||
|
||||
/**
|
||||
This method changes the number of times the book has been read.
|
||||
@param readIn The number of times the book has been read.
|
||||
*/
|
||||
public void setRead(int readIn){
|
||||
read = readIn;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
149
Source Code/Assignment 2/src/Library.java
Normal file
149
Source Code/Assignment 2/src/Library.java
Normal file
@ -0,0 +1,149 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
Represents a personal library.
|
||||
@author L Bidlake, Isaac Shoebottom (3429069)
|
||||
*/
|
||||
public class Library{
|
||||
|
||||
/**
|
||||
A collection of books.
|
||||
*/
|
||||
private Book[] library;
|
||||
|
||||
/**
|
||||
Constructs a library with the specified collection of books.
|
||||
@param library A collection of books.
|
||||
*/
|
||||
public Library(Book[] library){
|
||||
this.library = library;
|
||||
}
|
||||
|
||||
/**
|
||||
Constructs a library by reading in the specified input.
|
||||
@param scanIn Input containing a list of books.
|
||||
*/
|
||||
public Library(Scanner scanIn){
|
||||
library = new Book[scanIn.nextInt()];
|
||||
scanIn.nextLine();
|
||||
|
||||
for(int i = 0; i < library.length; i++){
|
||||
Scanner scan = new Scanner(scanIn.nextLine());
|
||||
scan.useDelimiter(",");
|
||||
long isbn = scan.nextLong();
|
||||
String title = scan.next();
|
||||
String author = scan.next();
|
||||
int read = scan.nextInt();
|
||||
library[i] = new Book(isbn, title, author, read);
|
||||
}
|
||||
//makes sure the list is sorted after adding from entry.
|
||||
bubbleSort(library);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
This method determines how many books occur in one library and
|
||||
not the other
|
||||
@param other Other library to compare books in this library.
|
||||
@return The number of unique books.
|
||||
*/
|
||||
public int findUnique(Library other){
|
||||
int counter = 0;
|
||||
counter += countUniques(library, other.library);
|
||||
counter += countUniques(other.library, library);
|
||||
return counter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts the uniques on one side of an array
|
||||
* @param arr1 The first array, the one that the uniques are counted up against
|
||||
* @param arr2 The second array, the array that the first array is compared against
|
||||
* @return Integer of unique books
|
||||
*/
|
||||
public static int countUniques(Book[] arr1, Book[] arr2) {
|
||||
int counter = 0;
|
||||
boolean isUnique;
|
||||
for (Book book1 : arr1) {
|
||||
isUnique = true;
|
||||
for (Book book2 : arr2) {
|
||||
if (book1.getIsbn() < book2.getIsbn())
|
||||
break;
|
||||
if (book1.getIsbn() == book2.getIsbn()) {
|
||||
isUnique = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isUnique) {
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
This method merges two libraries (includes both if duplicates).
|
||||
@param other Library to merge with this library.
|
||||
@return The library containing all contents of both libraries.
|
||||
*/
|
||||
public Library merge(Library other){
|
||||
Book[] tempLibrary = new Book[library.length+other.library.length];
|
||||
System.arraycopy(library, 0, tempLibrary, 0, library.length);
|
||||
System.arraycopy(other.library, 0, tempLibrary, library.length, other.library.length);
|
||||
|
||||
bubbleSort(tempLibrary);
|
||||
return new Library(tempLibrary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorting algorithm of the bubble variety (in place sort)
|
||||
* @param arr Array to be sorted
|
||||
*/
|
||||
public static void bubbleSort(Book[] arr) {
|
||||
int length = arr.length;
|
||||
for (int i = 0; i < length-1; i++)
|
||||
for (int j = 0; j < length-i-1; j++)
|
||||
if (arr[j].getIsbn() > arr[j+1].getIsbn()) {
|
||||
Book temp = arr[j];
|
||||
arr[j] = arr[j+1];
|
||||
arr[j+1] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
This method add a book in sorted order by ISBN to this library.
|
||||
@param bookIn The book to be added to this library in sorted order.
|
||||
*/
|
||||
public void addBook(Book bookIn){
|
||||
int counter = 0;
|
||||
for (Book book: library) {
|
||||
if (book.getIsbn() <= bookIn.getIsbn()) {
|
||||
counter++;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Book[] tempLibrary = new Book[library.length+1];
|
||||
System.arraycopy(library, 0, tempLibrary, 0, counter);
|
||||
tempLibrary[counter] = bookIn;
|
||||
System.arraycopy(library, counter, tempLibrary, counter+1, (library.length-counter));
|
||||
library = tempLibrary;
|
||||
}
|
||||
|
||||
/**
|
||||
Creates a formated print out of the library.
|
||||
@return Formatted print out of the library.
|
||||
*/
|
||||
public String toString(){
|
||||
String result = "";
|
||||
for(int i = 0; i < library.length; i++){
|
||||
result += library[i].getIsbn() + "\t" +
|
||||
library[i].getTitle() + "\t" +
|
||||
library[i].getAuthor() + "\t" +
|
||||
library[i].getRead() + "\n";
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
39
Source Code/Assignment 2/src/TestLibrary.java
Normal file
39
Source Code/Assignment 2/src/TestLibrary.java
Normal file
@ -0,0 +1,39 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
Driver to test the Library class.
|
||||
@author L Bidlake
|
||||
*/
|
||||
public class TestLibrary{
|
||||
|
||||
public static void main(String[] args){
|
||||
Scanner scan = new Scanner(System.in);
|
||||
Library myLibrary = new Library(scan);
|
||||
Library yourLibrary = new Library(scan);
|
||||
|
||||
Scanner sc = new Scanner(scan.nextLine());
|
||||
sc.useDelimiter(",");
|
||||
long isbn = sc.nextLong();
|
||||
String title = sc.next();
|
||||
String author = sc.next();
|
||||
int read = sc.nextInt();
|
||||
Book bookToAdd = new Book(isbn, title, author, read);
|
||||
|
||||
System.out.println(myLibrary);
|
||||
System.out.println(yourLibrary);
|
||||
|
||||
System.out.println("Number of unique books: " +
|
||||
myLibrary.findUnique(yourLibrary));
|
||||
|
||||
myLibrary.addBook(bookToAdd);
|
||||
System.out.println("My Library after adding a book: \n" +
|
||||
myLibrary);
|
||||
|
||||
System.out.println("Merged Libraries: ");
|
||||
System.out.println(myLibrary.merge(yourLibrary));
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user