Tip of the day
News
Instructions
Objective
Write a java assignment program to implement recursion and stacks.
Requirements and Specifications
Source Code
A5 EXERCISE
public class A5Exercises {
// PART 1
/*
* Purpose: get a count of the number of elements in the array
* with a value that is a multiple of x
* Parameters: int[] arr
* Returns: int - the number multiples of x
* Pre-condition: x > 0
* Post-condition - the array contents are unchanged
*/
public static int countMultiples(int[] arr, int x) {
return countMultiplesStep(arr, x, arr.length);
}
private static int countMultiplesStep(int[] arr, int x, int len) {
if (len == 0) {
return 0;
}
return (arr[len-1] % x == 0 ? 1 : 0) + countMultiplesStep(arr, x, len-1);
}
/*
* Purpose: double all values in the given array
* Parameters: int[] array - the array to modify
* Returns: void - nothing
*/
public static void doubleAll(int[] array) {
doubleAllStep(array, array.length);
}
private static void doubleAllStep(int[] arr, int len) {
if (len == 0) {
return;
}
arr[len-1] *= 2;
doubleAllStep(arr, len-1);
}
/*
* Purpose: get the minimum value found in the array
* Parameters: int[] array - the array to search
* Returns: int - minimum value found in the array
* or -1 if the array is empty
* Post-condition - the array contents are unchanged
*/
public static int getMinimum(int[] array) {
return getMinimum(array, array.length);
}
private static int getMinimum(int[] arr, int len) {
if (len == 0) {
return -1;
}
if (len == 1) {
return arr[0];
}
return Math.min(arr[len-1], getMinimum(arr, len-1));
}
// PART II
/*
* Purpose: get the total number of books in s
* Parameters: Stack
s - the stack of books
* Returns: int - the total number of books
* Post-condition: s is not modified
*/
public static int totalBooks(Stack
s) {
return totalBooksStep(s);
}
private static int totalBooksStep(Stack
s) {
if (s.isEmpty()) {
return 0;
}
Book top = s.pop();
int result = 1 + totalBooksStep(s);
s.push(top);
return result;
}
/*
* Purpose: get the total number of pages of all
* books in the stack
* Parameters: Stack
s - the stack of books
* Returns: int - the total number of pages
* Post-condition: s is not modified
*/
public static int totalPages(Stack
s) {
return totalPagesStep(s);
}
private static int totalPagesStep(Stack
s) {
if (s.isEmpty()) {
return 0;
}
Book top = s.pop();
int result = top.getPages() + totalPagesStep(s);
s.push(top);
return result;
}
/*
* Purpose: get the average number of pages of books in s
* Parameters: Stack
s - the stack of books
* Returns: double - the average number of pages
* 0.0 if there are no books in s
* Post-condition: s is not modified
*/
public static double averagePages(Stack
s) {
// You don't need to change this, if you have
// completed the previous two exercises
// correctly, it should pass all the tests
if (s.isEmpty()) {
return 0.0;
} else {
double sum = totalPages(s);
int count = totalBooks(s);
return sum/count;
}
}
/*
* Purpose: determine whether toFind is contained in s
* Parameters: Stack
s - the stack of books
* Book toFind - the book to search for
* Returns: boolean - true if s contains toFind, false otherwise
* Post-condition: s is not modified
*/
public static boolean containsBook(Stack
s, Book toFind) {
return containsBookStep(s, toFind);
}
private static boolean containsBookStep(Stack
s, Book toFind) {
if (s.isEmpty()) {
return false;
}
if (toFind.equals(s.top())) {
return true;
}
Book top = s.pop();
boolean result = containsBookStep(s, toFind);
s.push(top);
return result;
}
/*
* Purpose: determine the books in s are stacked correctly
* (ie. there is never a book stacked on top of
* another book with fewer pages)
* Parameters: Stack
s - the stack of books
* Returns: boolean - true if books in s are stacked correctly
* Post-condition: s is not modified
*/
public static boolean stackedCorrectly(Stack
s) {
return stackedCorrectlyStep(s);
}
public static boolean stackedCorrectlyStep(Stack
s) {
if (s.isEmpty()) {
return true;
}
Book top = s.pop();
if (s.isEmpty()) {
s.push(top);
return true;
}
if (top.getPages() > s.top().getPages()) {
s.push(top);
return false;
}
boolean result = stackedCorrectlyStep(s);
s.push(top);
return result;
}
}
A5 NODE
apublic class A5Node
{
private T data;
protected A5Node
next;
public A5Node (T value) {
this.data = value;
this.next = null;
}
/*
* Purpose: get the data value of this Node
* Parameters: nothing
* Returns: T - the data value
*/
public T getData() {
return data;
}
/*
* Purpose: get the next node
* Parameters: nothing
* Returns: A5Node - the next node
*/
public A5Node
getNext() {
return next;
}
/*
* Purpose: update the next reference for this node
* Parameters: A5Node next - the new next
* Returns: void - nothing
*/
public void setNext(A5Node
next) {
this.next = next;
}
}
A5 STACK
public class A5Stack
implements Stack
{
private A5Node
head;
public A5Stack() {
head = null;
}
public void push(T value) {
A5Node
n = new A5Node
(value);
n.next = head;
head = n;
}
public T pop() {
if (isEmpty()) {
return null;
} else {
T toReturn = head.getData();
head = head.next;
return toReturn;
}
}
public T top() {
if (isEmpty()) {
return null;
} else {
return head.getData();
}
}
public void popAll() {
head = null;
}
public boolean isEmpty() {
return head == null;
}
/************
FOR TESTING
************/
// Create a stack from an array of books
public A5Stack (T[] books) {
head = null;
for (int i = books.length-1; i >= 0; i--) {
push(books[i]);
}
}
public String toString() {
if (isEmpty()) {
return "{}";
}
String s = "{";
A5Node
cur = head;
while (cur.next != null) {
s += cur.getData().toString() + ", ";
cur = cur.next;
}
s += cur.getData().toString() + "}";
return s;
}
}
BOOK
public class Book {
private String title;
private String author;
private int numPages;
public Book(String title, String author, int numPages) {
this.title = title;
this.author = author;
this.numPages = numPages;
}
public int getPages() {
return numPages;
}
public boolean isLonger(Book other) {
return this.numPages > other.numPages;
}
public boolean equals(Book other) {
return this.title.equals(other.title)
&& this.author.equals(other.author)
&& this.numPages == other.numPages;
}
public String toString() {
return "\"" + title + "\" (" + numPages + ")";
}
}
Related Samples
Explore our Java Assignments samples for a glimpse into efficient problem-solving. Master concepts with clear, annotated code snippets covering arrays, loops, OOP, and more. Gain insights and enhance your programming prowess with practical examples tailored for educational purposes. Dive into our curated collection and excel in Java programming today.
Java
Word Count
3961 Words
Writer Name:Chloe Wong
Total Orders:590
Satisfaction rate:
Java
Word Count
5749 Words
Writer Name:Tia Connor
Total Orders:964
Satisfaction rate:
Java
Word Count
4042 Words
Writer Name:Dr. Chloe jhones
Total Orders:750
Satisfaction rate:
Java
Word Count
6299 Words
Writer Name:Dr. Amanda C. Smallwood
Total Orders:356
Satisfaction rate:
Java
Word Count
4093 Words
Writer Name:Dr. Donald K. Grainger
Total Orders:689
Satisfaction rate:
Java
Word Count
3941 Words
Writer Name:Dr. Robert M. Sanchez
Total Orders:425
Satisfaction rate:
Java
Word Count
6086 Words
Writer Name:Prof. Liam Saunders
Total Orders:900
Satisfaction rate:
Java
Word Count
5597 Words
Writer Name:Dr. Candace J. Gentry
Total Orders:621
Satisfaction rate:
Java
Word Count
4942 Words
Writer Name:Dr. Julia Wong
Total Orders:426
Satisfaction rate:
Java
Word Count
6782 Words
Writer Name:Dr. Ophelia Whitethorn
Total Orders:759
Satisfaction rate:
Java
Word Count
5142 Words
Writer Name:Jessica Miller
Total Orders:652
Satisfaction rate:
Java
Word Count
8786 Words
Writer Name:Prof. Alexander Choice
Total Orders:900
Satisfaction rate:
Java
Word Count
6994 Words
Writer Name:Alex Thompson
Total Orders:1521
Satisfaction rate:
Java
Word Count
6718 Words
Writer Name:Chloe Wong
Total Orders:590
Satisfaction rate:
Java
Word Count
4871 Words
Writer Name:Dr. Marlowe Emberly
Total Orders:789
Satisfaction rate:
Java
Word Count
8521 Words
Writer Name:Dr. Zara Silvermist
Total Orders:654
Satisfaction rate:
Java
Word Count
3595 Words
Writer Name:Dr. Seraphina Stormcloud
Total Orders:852
Satisfaction rate:
Java
Word Count
3694 Words
Writer Name:Dr. George M. Long
Total Orders:507
Satisfaction rate:
Java
Word Count
4842 Words
Writer Name:Professor Harriet Sinclair
Total Orders:387
Satisfaction rate:
Java
Word Count
4329 Words
Writer Name:Dr. Seraphina Stormcloud
Total Orders:852
Satisfaction rate: