題目和要求:
In?this?question,?you?will?write?a?linked?list?class,?and?use?it?to?store?all?of?the?words?contained?in?a?text?file.
You?will?write?your?linked?list?class?in?a?file?called?LinkedList.java,?which?will?contain?BOTH?the
LinkedList?class?and?the?Node?class?(only?the?LinkedList?class?is?public).
You?have?been?provided?with?a?main?program?file?called?A4Test.java?and?a?text?file?called
darwin.txt.
First,?create?a?Node?class.?It?should?contain,?as?usual,?a?pointer?to?another?node,?and?a?String?reference.
Write?a?constructor?that?sets?the?data?being?stored?in?the?Node,?and?set?its?link?to?the?next?Node?to?null.
Write?appropriate?getters?and?setters?as?well.
Next,?create?your?LinkedList?class.?As?usual,?it?should?contain?a?first?(top)?pointer?to?the?first?node?in
the?list.?Additionally,?create?a?last?pointer?to?the?last?Node?in?the?linked?list?(we’ll?see?why?later).?Add?an
integer?instance?variable?to?keep?track?of?the?size?(i.e.?the?number?of?words)?in?the?linked?list.
Create?the?following?public?methods:
??A?null?constructor?that?initializes?the?two?Node?pointers?to?null?and?the?size?to?0
??int?size()?that?returns?the?size?of?the?list.
??void?append(String),?which?takes?the?given?word?and?places?it?at?the?end?of?the?list,?not?at
the?beginning.?Instead?of?performing?a?costly?traversal?of?the?linked?list?from?the?beginning
until?the?end?is?reached,?use?the?last?pointer?instead,?when?appending?a?new?item.?Be?mindful
of?the?case?when?the?list?is?empty.
??A?second?constructor?that?takes?a?String?parameter,?which?is?the?name?of?a?file.?The
constructor?will?attempt?to?read?the?file?and?add?all?the?words?in?the?file?to?the?linked?list?(using
append).?Use?whitespace?to?separate?the?lines?into?words.?In?Java,?the?String?literal
"\\p{Space}+"?specifies?one?or?more?whitespace?characters.
??String?format(int?width)?that?returns?a?String?containing?all?the?words?in?their?original
order,?formatted?in?lines?that?are?width?characters?long,?fully?justified?(i.e.?the?lines?are?aligned
on?both?the?left?and?right?sides).?This?is?achieved?by?adding?extra?spaces?between?the?words?so
that?the?right?side?is?aligned?at?width?characters.?In?order?to?make?the?justification?look
balanced,?distribute?the?extra?spaces?throughout?the?line.?Add?the?extra?spaces?left-to-right?on
each?second?line,?and?right-to-left?on?the?alternating?lines.?You?may?find?it?helpful?to?use?two
private?methods?to?perform?these?justification?steps?(though?you?are?not?required?to?do?so).?The
last?line?is?not?justified,?since?it?may?contain?only?a?few?words.
A4test?code:
public?class?A4Test{
??public?static?void?main(String?[]?args){
????String?fileName?=?"darwin.txt";
????System.out.println("This?is?A4Test.\n");
????LinkedList?myList?=?new?LinkedList(fileName);
????System.out.println(myList.format(80));
????System.out.println(fileName?+?"?contains?"?+?myList.size()?+?"?words.");
??}//?main
}//?class?A4Test
drawin.txt內(nèi)容:
qwertyuiopasdfghjklzxcvbnm
JAVA Linked Lists問題
0C13602793
2016-07-05 07:30:58