Programming Assignment 9
The purpose of this programming project is to demonstrate a
significant culmination of most constructs learned thus far in the
course. This includes Lists, Classes, accessors, mutators,
constructors, implementation of Comparable, Comparator, use of
Collections sort, iterators, properly accessing fields of complex
objects, and fundamental File I/O.
BACKGROUND
Look over Programming Project #4 at the end of the Searching &
Sorting Chapter (13), page 869. Programming Assignment 9 is similar
with some added features as described below.
You will submit a single file Programming9.zip through the
Programming Assignment 9 Submission link on Canvas. This zipped
file will contain a minimum of 5 files which make up the solution
to this assignment. DO NOT zip any folders.
The standard grading rules (documentation included) for all
previous assignments applies here as well.
DETAILS
To better prepare you for assignments in more advanced courses,
specific step-by-step, method headings, class names, and the like
have been omitted. What remains is a general description (you may
need to read this several times to fully understand the
requirements):
You will create a LinkedList of Word objects using all the words
found in the input file words.txt. A Word object contains 2 String
fields; 1 to store a word in its normal form and the other to store
a word in its canonical form. The canonical form stores a word with
its letters in alphabetical order, e.g. bob would be bbo, cat would
be act, program would be agmoprr, and so on. The class Word
constructor has the responsibility of storing the normal form of
the word in the normal form field and converting the normal form
into the canonical form which is stored in the canonical form field
(you should call a separate method for this conversion
purpose).
Once all the words from the input file have been properly stored in
a LinkedList of Word, you should use Collections to sort this list
ascending alphabetically based on the canonical words by making the
Word class Comparable.
Using an Iterator on the LinkedList of Word, create a 2nd list (new
LinkedList) consisting of objects of a new class named
AnagramFamily. AnagramFamily should contain at least 2 fields; 1 to
hold a list of “Word” words that are all anagrams of each other
(these should all be grouped together in the original canonical
sorted list), and the 2nd field to store an integer value of how
many items are in the current list. (Keep in mind, because the
original list contains both the normal and canonical forms, as the
AnagramFamily List will also have, a family of anagrams will all
have the same canonical form with different normal forms stored in
the normalForm field of the Word class). Each AnagramFamily List of
Word should be sorted Descending by normal form using a Comparator
of Word (if you insert Word(s) into a family one at a time, this
presents an issue on how to get this list sorted as each Word
insertion will require a new sort to be performed to guarantee the
list is always sorted. For this reason it is best to form a list,
sort it, and then create an AnagramFamily by passing the sorted
list to it).
Sort the AnagramFamily LinkedList in descending order based on
family size by use of a Comparator to be passed to the Collections
sort method.
Next, output the top five largest families then, all families of
length 8, and lastly, the very last family stored in the list to a
file named “out9.txt.” Be sure to format the output to be very
clear and meaningful.
Finally, the first 4 people to complete the assignment should post
their output results to the Canvas discussion forum for the
remaining students to see the correct answer. continued…
Be sure to instantiate new objects whenever transferring data from
one object to another to avoid a repeat of
the issues involved with Programming Assignment 6. Also, be sure to
include various methods for manipulation and access of fields as
well as helper methods to reduce code in main, such as the
input/output of file data (like all other assignments, you will be
graded on decomposition, i.e. main should not contain too many
lines of code).
Keep in mind such items as proper documentation (including
javadoc), meaningful variable names, proper indentation, reduction
of redundancy whenever possible, and so on.
Part of your grade will depend on time. If written correctly (use
of iterators and care taken when creating the anagram families),
the running time should be less than 3 seconds. Programs that take
longer will lose points based on the time. As encouragement to
consider all options for speed, programs taking 1 minute will
receive a 40 point deduction. Any longer than 3 minutes will
receive only minimal points (10) for effort.
Though the basic algorithms involved are straight forward enough,
there is a great deal of complexity involved with various levels of
access to specific data. As mentioned before and never so
importantly as with this assignment, start early and set a goal for
completion by this weekend. Trust me, this is sound advice.
As a reminder, you will create at least 5 files: the driver, Word
class, AnagramFamily class, and 2 comparators: 1 to compare Word
objects for sorting descending based on the normal form of Word
objects and 1 to compare AnagramFamily sizes for a descending
sort.
You will submit a single file Programming9.zip through the
Programming Assignment 9 Submission link on Canvas. This zipped
file will contain a minimum of 5 files which make up the solution
to this assignment. DO NOT zip any folders.
in JAVA ***
CODE:
here is your files : ----------------------
Word.java : --------------
import java.util.*;
public class Word implements Comparable<Word>{
private String normal;
private String canonical;
public Word(){
normal = "";
canonical = "";
}
public Word(String norm){
setNormal(norm);
}
public void setNormal(String norm){
normal = norm;
char[] arr = norm.toCharArray();
Arrays.sort(arr);
canonical = new String(arr);
}
public String getNormal(){
return normal;
}
public String getCanonical(){
return canonical;
}
public int compareTo(Word word){
return canonical.compareTo(word.getCanonical());
}
public String toString(){
return "("+normal+", "+canonical+")";
}
}
AnagramFamily.java : ------------------------------
import java.util.*;
public class AnagramFamily implements
Comparable<AnagramFamily>{
private LinkedList<Word> words;
private int size;
private class WordComp implements Comparator{
public int compare(Object o1,Object o2){
Word w1 = (Word)o1;
Word w2 = (Word)o2;
return w2.getNormal().compareTo(w1.getNormal());
}
}
public AnagramFamily(){
words = new LinkedList<>();
size = 0;
}
public LinkedList<Word> getAnagrams(){
return words;
}
public void addAnagram(Word word){
words.add(word);
size++;
}
public void sort(){
Collections.sort(words,new WordComp());
}
public int getSize(){
return size;
}
public int compareTo(AnagramFamily anag){
Integer i1 = new Integer(size);
Integer i2 = new Integer(anag.getSize());
return i2.compareTo(i1);
}
public String toString(){
return "{ Anagrams Family Size : "+size+"
"+words.toString()+"}";
}
}
WordMain.java : ------------------------------
import java.util.*;
import java.io.File;
import java.io.PrintWriter;
public class WordMain{
public static void readFile(LinkedList<Word> words){
try{
Scanner sc = new Scanner(new File("words.txt"));
while(sc.hasNext()){
words.add(new Word(sc.next()));
}
sc.close();
}catch(Exception e){
e.printStackTrace();
System.exit(-1);
}
}
public static void findAnagrams(LinkedList<AnagramFamily>
anagrams,LinkedList<Word> words){
Iterator<Word> itr = words.iterator();
while(itr.hasNext()){
Iterator<AnagramFamily> aitr = anagrams.iterator();
Word temp = itr.next();
System.out.println(temp);
boolean st = true;
while(aitr.hasNext()){
AnagramFamily anag = aitr.next();
Iterator<Word> anags = anag.getAnagrams().iterator();
while(anags.hasNext()){
Word t1 = anags.next();
if(t1.compareTo(temp) == 0 &&
!t1.getNormal().equals(temp.getNormal())){
anag.addAnagram(temp);
st = false;
break;
}
}
anag.sort();
}
if(st){
AnagramFamily anag = new AnagramFamily();
anag.addAnagram(temp);
anagrams.add(anag);
System.out.println(anag);
}
}
}
public static void writeOutput(LinkedList<AnagramFamily>
anagrams){
try{
PrintWriter pw = new PrintWriter(new File("out6.txt"));
Collections.sort(anagrams);
int i = 0;
Iterator<AnagramFamily> aitr = anagrams.iterator();
while(i < 5 && aitr.hasNext()){
AnagramFamily anag = aitr.next();
anag.sort();
pw.println(anag);
i++;
}
aitr = anagrams.iterator();
pw.println("\n\nAnagramsFamily size 8 datas : ");
while(aitr.hasNext()){
AnagramFamily anag = aitr.next();
anag.sort();
if(anag.getSize() == 8){
pw.println(anag);
}
}
pw.close();
}catch(Exception e){
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
LinkedList<Word> words = new LinkedList<>();
readFile(words);
Collections.sort(words);
//System.out.println(words.toString());
LinkedList<AnagramFamily> anagrams = new
LinkedList<>();
findAnagrams(anagrams,words);
//System.out.println(anagrams.toString());
writeOutput(anagrams);
}
}
Programming Assignment 9 The purpose of this programming project is to demonstrate a significant culmination of...
**********Using Java***************
Create 5 CLASSES: Driver(main), Word, AnagramFamily,
and 2 Comparators classes
words.txt is a very large file containing thousands of words. I
will post some words down below.
some of word.txt
aa
aah
aahed
aahing
aahs
aal
aalii
aaliis
aals
aardvark
aardvarks
aardwolf
aardwolves
aargh
aarrgh
aarrghh
aas
aasvogel
aasvogels
ab
aba
abaca
abacas
abaci
aback
abacterial
abacus
abacuses
abaft
abaka
abakas
abalone
abalones
abamp
abampere
abamperes
abamps
abandon
abandoned
abandoner
abandoners
abandoning
abandonment
abandonments
abandons
abapical
abas
abase...
java
Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...
Project Lists and Object-oriented Programming Note: using c++ This project is an individual assignment that focuses on object-oriented programming and lists. The list will be a doubly-linked list and it will be implemented using nodes and pointers. The data contained in the list will be objects from a class. Project data: You will choose one of the following classes to define: Boat, Earthquake, Game, Sport, State, Website, House, Phone, Activist, Plant, Fish, Parrot, and Course. Your class code will include...
PROGRAMMING ASSIGNMENT: Write a fully-commented program for the HC12 board, including appropriate directives and labels for memory operands and constants. The program should do the following: • The program should start execution at address $2000. • Use directives to create an array of N=20 unsigned 8-bit decimal values: 15,20,14,13,17,9,10,5,18,7,19,8,1,3,4,11,2,6,12,16 stored in contiguous memory at locations start at $1000. • Sort given numbers in a descending order. • Store sorted numbers at memory locations starting from $1000. *assembly programming language*
Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...
Programming Assignment #2 (Arrays) I. The Assignment This assignment is to take your Garage class from the previous assignment and modify it so that it uses an array of Car objects as the principal data structure instead of an ArrayList-of-Car. This is an example of the OOP principle of information hiding as your Car and test classes will not have to be modified at all. Unless you broke encapsulationon the previous assignment, that is II. Specifications Specifications for all 3...
Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...
Programming Assignment 1 Data structure Java Implement Shellsort for a linked list, based on a variant of bubble sort. The rather severe constraints imposed by the singly-linked list organization presents special problems for implementing Shellsort. Your task is to overcome these constraints and develop a simple, elegant implementation that does not sacrifice efficiency or waste space. Step 1. First, implement a routine to build a list: read integers from standard input, and build a list node for each item consisting...
Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student will demonstrate the ability to use inheritance in Java programs. Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following: Person Automobile Animal Based on your choice: If you choose Person, you will create a subclass of Person called Student. If you choose Automobile, you will create...
Programming Assignment 6: Object Oriented Programming Due date: Check Syllabus and Canvas Objectives: After successfully completing this assignment, students will practice Object Oriented design and programming by creating a Java program that will implement Object Oriented basic concepts. RetailItem Class: Part 1: Write a class named RetailItem that holds data about an item in a retail store. The class should have the following fields: • description. The description field references a String object that holds a brief description of the...