Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 11, Problem 4PP
Program Plan Intro
Possible cuts in a bar
Program plan:
- Import required packages
- Define the class “Sequence”.
- Define the class “main”
- Create an object for scanner class
- Get the value from the user
- Call the method “c(number)”.
- Call the method “d(number)”.
- Create an object for scanner class
- Define the method “c()”.
- Declare required variables.
- Check whether “k” to “0”.
- Assign “1” to “res”.
- Otherwise, call the method “c(k-1)”.
- Return the value “res”.
- Define the method “d()”.
- Declare required variables.
- Check whether “k” to “0”.
- Assign “1” to “res”.
- Otherwise, assign “0” to “res”.
- Iterate till “i” is less than “k”.
- Call the method “d(i-1)*d(k-1)”.
- Return the value “res”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
There are a set of courses, each of them requiring a set of disjoint time intervals. For example, a course could require the time from 9am to 11am and 2pm to 3pm and 4pm to 5pm. You want to know, given a number K, if it’s possible to take at least K courses. You can only take one course at any single point in time (i.e. any two courses you choose can’t overlap). Show that the problem is NP-complete, which means that choosing courses is indeed a difficult thing in our life. Use a reduction from the Independent set problem.
We have n glasses which can hold a varying amount of liquid. We will say that glass1 is size q1 quarts and glass2 is size q2 quarts, etc. We now give you a size, say x quarts. You must give me a glass of exactly x quarts. You are allowed to fill any glass to exactly its size and to pour any liquid from one glass into another. When you pour, you either have to empty the glass you are pouring from or fill the glass you are pouring into. Can you do it?More concrete example: G1 has size 10 quarts. G2 has size 4 quarts. G3 has size 1 quart. You can get a 5 quart drink by filling G1, pouring 4 quarts from it into G2 and 1 quart of it into G1. After that, G1 has exactly 5 quarts.
In the same problem, we could get 8 quarts by filling G2 twice and emptying it into G1 twice.
Given This:
Write an algorithm to do this and determine its complexity.
Suppose you have a set of n lectures that need to be scheduled in classrooms. Each lecture
has fixed starting and ending times. You would like to use as few classrooms as possible
to schedule all lectures.
For example:
Lectures
1
2
3
4
7
8
9.
10
3:00
Starts:
9:00
9:00
9:00
11:00 11:00
1:00
1:00
2:00
3:00
Ends:
10:30
12:30
10:30 12:30
2:00
2:30
2:30
4:30
4:30
4:30
These lectures can be scheduled in 3 classrooms.
Explain how a brute force algorithm would solve this problem and
analyze its complexity.
Design a more efficient algorithm to solve this problem, and
analyze its complexity [Important instruction: Create an arbitrary
list of 10 lectures starting 4 times and their corresponding ending
times to provide full explanation of how your proposed algorithm
should work step by step]
• Implement your algorithm using Python
Chapter 11 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 11.1 - What output will be produced by the following...Ch. 11.1 - What is the output produced by the following code?Ch. 11.1 - Write a recursive definition for the following...Ch. 11.1 - What is the output of the following code? public...Ch. 11.1 - Prob. 5STQCh. 11.1 - Complete the definition of the following method....Ch. 11.2 - Revise the method getCount in Listing 11.5 so that...Ch. 11.2 - Prob. 8STQCh. 11.2 - Prob. 9STQCh. 11.2 - Suppose you want me class ArraySearcher to work...
Ch. 11.2 - What Java statement will sort the following array,...Ch. 11.2 - How would you change the class MergeSort so that...Ch. 11.2 - How would you change the class MergeSort so that...Ch. 11.2 - If a value in an array of base type int occurs...Ch. 11.3 - Convert the following event handler to use the...Ch. 11 - What output will be produced by the following...Ch. 11 - What output will be produced by the following...Ch. 11 - Write a recursive method that will compute the...Ch. 11 - Write a recursive method that will compute the sum...Ch. 11 - Complete a recursive definition of the following...Ch. 11 - Write a recursive method that will compute the sum...Ch. 11 - Write a recursive method that will find and return...Ch. 11 - Prob. 8ECh. 11 - Write a recursive method that will compute...Ch. 11 - Suppose we want to compute the amount of money in...Ch. 11 - Prob. 11ECh. 11 - Write a recursive method that will count the...Ch. 11 - Write a recursive method that will remove all the...Ch. 11 - Write a recursive method that will duplicate each...Ch. 11 - Write a recursive method that will reverse the...Ch. 11 - Write a static recursive method that returns the...Ch. 11 - Write a static recursive method that returns the...Ch. 11 - One of the most common examples of recursion is an...Ch. 11 - A common example of a recursive formula is one to...Ch. 11 - A palindrome is a string that reads the same...Ch. 11 - A geometric progression is defined as the product...Ch. 11 - The Fibonacci sequence occurs frequently in nature...Ch. 11 - Prob. 4PPCh. 11 - Once upon a time in a kingdom far away, the king...Ch. 11 - There are n people in a room, where n is an...Ch. 11 - Prob. 7PPCh. 11 - Prob. 10PPCh. 11 - Prob. 12PP
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Given a deck of 52 playing cards, we place all cards in random order face up next to each other. Then we put a chip on each card that has at least one neighbour with the same face value (e.g., on that has another queen next to it), we put a chip. Finally, we collect all chips that were each queen placed on the cards. For example, for the sequence of cards A♡, 54, A4, 10O, 10♡, 104, 94, 30, 3♡, Q4, 34 we receive 5 chips: One chip gets placed on each of the cards 100, 10♡, 104, 30, 3♡. (a) Let p5 be the probability that we receive a chip for the 5th card (i.e., the face value of the 5th card matches the face value of one of its two neighbours). Determine p5 (rounded to 2 decimal places). (b) Determine the expected number of chips we receive in total (rounded to 2 decimal places). (c) For the purpose of this question, you can assume that the expectation of part (b) is 6 or smaller. Assume that each chip is worth v dollars. Further, assume that as a result of this game we receive at least…arrow_forwardA salesman would like to visit a set of n cities. The cities are connected pairwise by a direct trains. The cost of a train ticket varies depending on the cities it connects. This salesman would like to fly into one city, then use trains to visit each city exactly one, and then fly out of the last city on his trip. The cost of his trip is the total cost of all the train tickets (the plane tickets are not included). The salesman may chose any city to fly into and any city to fly out of. Let the problem of determining if there is a trip that costs at most k dollars be called T rainSalesman. Show that this problem is NP-complete.arrow_forwardUse a computer programming language of your choice to implement the dynamic programming that allows the change of edge weights and compute the shortest path from 1 to 14, and also shows the shortest route. For example, what is the shortest path and its route from node 1 to node 14 if the distance from node 4 to node 7 is 9?arrow_forward
- A hardware store is selling steel rods of various lengths. However, customers sometimes want to buy rods that the store doesn't carry. For example, if the store only carries 10cm and 30cm rods and a customer wants to buy a 25cm rod, then the order cannot be fulfilled. To boost sales, the store has come up with an idea to weld two rods together to make a longer rod. For example, if the store only carries 3cm and 5cm rods and a customer wants to buy a 8cm rod, then the order can be fulfilled by welding together 3-cm and 5-cm rods. Since rod welding can compromise the strength, the store will only weld up to TWO rods together. More precisely, this means, an order for x cm can be fulfilled if there is a single rod of length x or there are two rods (potentially of the same length) whose combined length is x. Your Task: Write a function process Orders (orders: List[int], sizes:List[int]) -> List[bool] to determine whether or not each of the given orders can be fulfilled. Here, orders is a…arrow_forwardAn n × n square consists of black and white cells arranged in a certain way. The problem is to determine the number of white areas and the number of white cells in each area. For example, a regular 8 × 8 chessboard has 32 one-cell white areas; the square in Figure 5.22a consists of 10 areas, 2 of them of 10 cells, and 8 of 2 cells; the square in Figure 5.22b has 5 white areas of 1, 3, 21, 10, and 2 cells. Write a program that, for a given n × n square, outputs the number of white areas and their sizes. Use an (n + 2) × (n + 2) array with properly marked cells. Two ad- ditional rows and columns constitute a frame of black cells surrounding the entered square to simplify your implementation. For instance, the square in Figure 5.22b is stored as the square in Figure 5.22c. (a–b) Two n 3 n squares of black and white cells and (c) an (n + 2) 3 (n + 2) array implementing square (b). bbbbbbbbbb bwbbwwbwwb bbbbbwbwwb bwwwbbwwwb bwbwbwwbbb bwbwwwbwbb bwbbbbwwwb bwbwbbwwwb bwbwbbwwwb…arrow_forwardWrite a function find_equivalent_matchings that takes a matching and returns all matchings with the same pattern (including the original one). Using this function, draw all matchings equivalent to {(0, 7), (1, 4), (2, 3), (5, 6)}. You might find it helpful to generate all permutations of a list using itertools.permutation (imported above) as in the following code template. perms=permutations (list_of_items) for p in perms:arrow_forward
- Let l be a line in the x-yplane. If l is a vertical line, its equation is x = a for some real number a. Suppose l is not a vertical line and its slope is m. Then the equation of l is y = mx + b, where b is the y-intercept. If l passes through the point (x₀, y₀), the equation of l can be written as y - y₀ = m(x - x₀). If (x₁, y₁) and (x₂, y₂) are two points in the x-y plane and x₁ ≠ x₂, the slope of line passing through these points is m = (y₂ - y₁)/(x₂ - x₁). Instructions Write a program that prompts the user for two points in the x-y plane. Input should be entered in the following order: Input x₁ Input y₁ Input x₂arrow_forwardSuppose you have a set of n lectures that need to be scheduled in classrooms. Each lecture has fixed starting and ending times. You would like to use as few classrooms as possible to schedule all lectures. For example: Lectures 1 2 3 5 7 8 10 11:00 11:00 10:30 12:30 10:30 12:30 2:00 Starts: 9:00 9:00 9:00 1:00 1:00 2:00 3:00 3:00 Ends: 2:30 2:30 4:30 4:30 4:30 These lectures çan be scheduled in 3 classrooms. a) Explain how a brute force algorithm would solve this problem and analyze its complexity. b) Design a more efficient algorithm to solve this problem, and analyze its complexity| [Important instruction: Create an arbitrary list of 10 lectures starting times and their corresponding ending times to provide full explanation of how your proposed algorithm should work step by step] Implement your efficient algorithm using Pythonarrow_forwardSuppose we can buy a chocolate bar from the vending machine for $1 each.Inside every chocolate bar is a coupon. We can redeem six coupons for onechocolate bar from the machine. This means that once you have startedbuying chocolate bars from the machine, you always have some coupons.We would like to know how many chocolate bars can be eaten if we startwith N dollars and always redeem coupons if we have enough for an additional chocolate bar.For example, with 6 dollars we could consume 7 chocolate bars afterpurchasing 6 bars giving us 6 coupons and then redeeming the 6 couponsfor one bar. This would leave us with one extra coupon. For 11 dollars, wecould have consumed 13 chocolate bars and still have one coupon left.For 12 dollars, we could have consumed 14 chocolate bars and have twocoupons left.arrow_forward
- You have a mixed pile of N nuts and N bolts and need to quickly find the corresponding pairs of nuts and bolts. Each nut matches exactly one bolt, and each bolt matches exactly one nut. By fitting a nut and bolt together, you can see which is bigger, but it is not possible to directly compare two nuts or two bolts. Give an efficient method for solving the problem.arrow_forwardX-Kingdom has trapped n number of soldiers of their opponent. They want to execute them. They created a strategy so that the prisoners will kill each other and at the end one prisoner will be alive and eventually released. As part of the process, they assigned each trapped soldier a sequence number starting from 1 and ending at n. If n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So, the person at position 3 survives. If n = 7 and k = 3, then the safe position is 4. The people at positions 3, 6, 2, 7, 5, 1 are killed in order, and the person at position 4 survives. Input:n and k Output:Print the list of prisoners in reverse order from n to 1Then reverse the list to print it in correct order from 1 to nDisplay the position number who will survive. You must have to use circular doubly linked list for your solution.arrow_forwardX-Kingdom has trapped n number of soldiers of their opponent. They want to execute them. They created a strategy so that the prisoners will kill each other and at the end one prisoner will be alive and eventually released. As part of the process, they assigned each trapped soldier a sequence number starting from 1 and ending at n. If n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So, the person at position 3 survives. If n = 7 and k = 3, then the safe position is 4. The people at positions 3, 6, 2, 7, 5, 1 are killed in order, and the person at position 4 survives. Input:n and k Output:Print the list of prisoners in reverse order from n to 1Then reverse the list to print it in correct order from 1 to nDisplay the position number who will survive. You must have to use circular doubly linked list for your solution. You…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks Cole
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole
Binary Numbers and Base Systems as Fast as Possible; Author: Techquikie;https://www.youtube.com/watch?v=LpuPe81bc2w;License: Standard YouTube License, CC-BY
Binary Number System; Author: Neso Academy;https://www.youtube.com/watch?v=w7ZLvYAi6pY;License: Standard Youtube License