Computer Science HW 2 hours.
Part 2 - GCD/Euclid.java
Part 2 - GCD/Euclid.java
public
class
Euclid
{
}
// end class
Part 2 - GCD/output.txt
The gcd of 2 and 30 is 2 The gcd of 3 and 27 is 3 The gcd of 4 and 24 is 4 The gcd of 5 and 21 is 1 The gcd of 6 and 18 is 6 The gcd of 7 and 15 is 1 The gcd of 8 and 12 is 4 The gcd of 9 and 9 is 9 The gcd of 10 and 6 is 2 The gcd of 11 and 3 is 1
Part 2 - GCD/Tester.java
Part 2 - GCD/Tester.java
/*
The Euclidean algorithm is used to efficiently calculate the greatest
common divisor (Gcd) of two numbers, both greater than 0.
This algorithm has both an iterative and recursive solution.
The recursive solution says that for two non-negative numbers a and b,
the gcd is a, when b is equal to 0;
otherwise the gcd is recursively calculated with b and the
result of a mod b (the integer remainder of a divided by b).
Some examples: the Gcd of 6 and 9 is 3, the Gcd of 9 and 10 is 1, the
Gcd of 20 and 10 is 10. You should look at the output from this problem
for further examples (recursion.txt).
Write the recursive solution to the gcd method based on the
above algorithm and the code given below.
NOTE: If you do not utilize recursion in this problem, you will earn
0 points.
*/
public
class
Tester
{
public
static
void
main
(
String
[]
args
)
{
int
a
,
b
,
theGcd
;
for
(
a
=
2
,
b
=
30
;
b
>
0
;
b
-=
3
,
a
++
)
{
theGcd
=
Euclid
.
gcd
(
a
,
b
);
System
.
out
.
println
(
"The gcd of "
+
a
+
" and "
+
b
+
" is "
+
theGcd
+
"\n"
);
}
// end for
}
// end main
}
// end class
Part 1 - LinkedList/LinkedList$Node.class
public synchronized class LinkedList$Node { protected Comparable data; protected LinkedList$Node next; protected void LinkedList$Node(Comparable); protected void LinkedList$Node(Comparable, LinkedList$Node); public void setData(Comparable); public void setNext(LinkedList$Node); public Comparable getData(); public LinkedList$Node getNext(); }
Part 1 - LinkedList/LinkedList.class
public synchronized class LinkedList { protected LinkedList$Node head; protected int size; public void LinkedList(); public int size(); public void clear(); public void addFirst(Comparable); public String toString(); }
Part 1 - LinkedList/LinkedListA.class
public synchronized class LinkedListA extends LinkedList { public void LinkedListA(); public void addLast(Comparable); }
Part 1 - LinkedList/LinkedListB.class
public synchronized class LinkedListB extends LinkedListA { public void LinkedListB(); public void sort(); }
Part 1 - LinkedList/LinkedListC.class
public synchronized class LinkedListC extends LinkedListB { public void LinkedListC(); public Comparable remove(int); }
Part 1 - LinkedList/LinkedListD.java
Part 1 - LinkedList/LinkedListD.java
public
class
LinkedListD
extends
LinkedListC
{
// If the list is empty, print the message "The list is empty."
// Otherwise list all the data fields, one to a line --- IN REVERSE.
}
// end class
Part 1 - LinkedList/output.txt
After adding 26 items the list size is 26 Printing List in order 0. Apple 1. Banana 2. Carrot 3. Drum stick 4. Eclair 5. Flan 6. Grape 7. Hash browns 8. Ice cream 9. Jicama 10. Kiwi fruit 11. Lettuce 12. Mango 13. New potatoes 14. Olives 15. Papaya 16. Quiche 17. Romaine lettuce 18. Steak 19. Tomatoes 20. Umble pie 21. V-8 22. Wheat bread 23. Xmas cookies 24. Yellowtail 25. Zwieback Printing List in Reverse on Filled List Zwieback Yellowtail Xmas cookies Wheat bread V-8 Umble pie Tomatoes Steak Romaine lettuce Quiche Papaya Olives New potatoes Mango Lettuce Kiwi fruit Jicama Ice cream Hash browns Grape Flan Eclair Drum stick Carrot Banana Apple Clear the List Printing List in Reverse on EMPTY List The list is empty Finished
Part 1 - LinkedList/Tester.java
Part 1 - LinkedList/Tester.java
public
class
Tester
{
public
static
void
main
(
String
[]
args
)
{
String
[]
ordered
=
{
"Apple"
,
"Banana"
,
"Carrot"
,
"Drum stick"
,
"Eclair"
,
"Flan"
,
"Grape"
,
"Hash browns"
,
"Ice cream"
,
"Jicama"
,
"Kiwi fruit"
,
"Lettuce"
,
"Mango"
,
"New potatoes"
,
"Olives"
,
"Papaya"
,
"Quiche"
,
"Romaine lettuce"
,
"Steak"
,
"Tomatoes"
,
"Umble pie"
,
"V-8"
,
"Wheat bread"
,
"Xmas cookies"
,
"Yellowtail"
,
"Zwieback"
};
LinkedListD
list
=
new
LinkedListD
();
for
(
int
x
=
0
;
x
<
ordered
.
length
;
x
++
)
list
.
addLast
(
ordered
[
x
]);
System
.
out
.
println
(
"After adding "
+
ordered
.
length
+
" items "
+
"the list size is "
+
list
.
size
()
);
System
.
out
.
println
();
System
.
out
.
println
(
"Printing List in order"
);
System
.
out
.
println
(
list
);
System
.
out
.
println
();
System
.
out
.
println
(
"Printing List in Reverse on Filled List"
);
list
.
listReverse
();
System
.
out
.
println
();
System
.
out
.
println
(
"Clear the List"
);
list
.
clear
();
System
.
out
.
println
();
System
.
out
.
println
(
"Printing List in Reverse on EMPTY List"
);
list
.
listReverse
();
System
.
out
.
println
();
System
.
out
.
println
(
"Finished"
);
}
// end main
}
// end class