Implement a recursive function static boolean

Assignment Help JAVA Programming
Reference no: EM132109175

Programming language is JAVA:

We have a set of coins of arbitrary positive values and want to see whether a given nonnegative total can be made using a some subset of the coins.

For example if we have two pennies, a nickel, two dimes and one quarter then the total 27 could be made using a quarter and two pennies (or alternatively two dimes, one nickel and two pennies).

On the other hand, given the same set of coins, we couldn't make the total 28 since we would need three pennies to add to the quarter and we only have two.

Implement a recursive function static boolean coinsMake(int [] coins, int startIndex, int total) that returns true if and only if the total can be made by adding a subset of the values in the coins array , from indexes startIndex to the last index (i.e. coins.length-1).

You function will need two base cases: When the total is zero and when startIndex reaches coins.length but the total is positive.

If the total is zero then we will always return true since 0 by convention can be made without needing any coins at all.

If the total is positive but startIndex is equal to coins.length, we will return false since in this case there are no values available to add.

The recursive case is when the total is greater than zero and startIndex is still less than coins.length.

In this case we will need to combine two recursive calls and return true if either of those calls returns true .

To figure out what the the two recursive calls are, think about two possibilities: either we use the coin at index startIndex or we don't.

As a precondition you can assume that both total and startIndex are nonnegative.

We would usually call coinsMake with startIndex equal to 0, since we want to know whether the total can be make using any subset of the coins, not just subsets that exclude the elements at indexes 0 through startIndex-1.

As an extra challenge, see if you can implement the method using a single line of code in the body, i.e. just a return statement which uses the logical operators && and ||.

For testing purposes, some coins will have values other than 1, 5, 10 etc. There is a coin worth 38 cents, for example.

Reference no: EM132109175

Questions Cloud

Create an algorithm for solving the following version : Create an algorithm for solving the following version of the m Smallest Numbers problem. Instead of just returning the m smallest values.
Compute the january balance for hulse in accounts payable : Hulse's sales journal for January shows a total of $111,109 in the selling price column, Compute the January balance for Hulse in Accounts Payable
Write a program in java to replace the word : If a word ends in 'a', append an 'r'. For example "tuna" becomes "tunar", "Cuba" becomes "Cubar", and "idea" becomes "idear".
List the phrases and simple phrases and the handle : Show the parse tree for the expression: ((id + id) * id) + id . List the phrases, simple phrases, and the handle.
Implement a recursive function static boolean : We have a set of coins of arbitrary positive values and want to see whether a given nonnegative total can be made using a some subset of the coins.
What are the current international trade patterns : BSBMKG605 Evaluate international marketing opportunities - Briefly describe how a recession might negatively affect global demand and the economy
Conduct of monetary policy : What can you say about their respective growth rates? What implications do these rates have on the conduct of monetary policy?
Make all necessary journal entries to record transactions : ACC 322 Case Study Assignment, Southern New Hampshire University, USA. Make all necessary journal entries to record these transactions
Estimate the profit on the project : Estimate the profit on the project if the bid is successful and all costs are incurred as planned. Estimate the tender bid for the project

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd