Need help with this in Java 8 1 2 3 Task 1 There is a forum that
Fantastic news! We've Found the answer you've been seeking!
Question:
Need help with this in Java 8
Transcribed Image Text:
1 2 3 Task 1 There is a forum that has a limit of K characters per entry. In this task your job is to implement an algorithm for cropping messages that are too long. You are given a message, consisting of English alphabet letters and spaces, that might be longer than the limit. Your algorithm should crop a number of words from the end of the message, keeping in mind that: • it may not crop away part of a word; • the output message may not end with a space; • the output message may not exceed the K-character limit; • the output message should be as long as possible. Java 8 This means that, in some cases, the algorithm may need to crop away the entire message, outputting an empty string. For example, given the text: "Codility We test coders" With K = 14 the algorithm should output: "Codility We" Note that: • the output "Codility We te" would be incorrect, because the original message is cropped through the middle of a word; • the output "Codility We would be incorrect, because it ends with a space; • the output "Codility We test coders" would incorrect, because it exceeds the K-character limit; • the output "Codility" would be incorrect, because it is shorter than the correct output. Write a function class Solution { public String solution (String message, int K); } which, given a message and an integer K, returns the message cropped to no more than K characters, as described above. Examples: 1. Given message = "Codility We test coders" and K = 14, the function should return "Codility We". Autocomplete is connected | All changes saved Files task1 solution.java test-input.txt Test Output 1 error || Detected some errors. solution.java x // you can also use imports, for example: // import java.util.*; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public String solution (String message, int K) { K = 14; System.out.println ("Codility We test coders"); } } Compiler output: Solution.java:13: error: reached end of file while parsing To leave editor use Ctrl + M Any problems with the editor? Switch to basic editor > ▶ Run Code Give Feedback 6:14 PM 1 2 3 Task 1 There is a forum that has a limit of K characters per entry. In this task your job is to implement an algorithm for cropping messages that are too long. You are given a message, consisting of English alphabet letters and spaces, that might be longer than the limit. Your algorithm should crop a number of words from the end of the message, keeping in mind that: • it may not crop away part of a word; • the output message may not end with a space; • the output message may not exceed the K-character limit; • the output message should be as long as possible. Java 8 This means that, in some cases, the algorithm may need to crop away the entire message, outputting an empty string. For example, given the text: "Codility We test coders" With K = 14 the algorithm should output: "Codility We" Note that: • the output "Codility We te" would be incorrect, because the original message is cropped through the middle of a word; • the output "Codility We would be incorrect, because it ends with a space; • the output "Codility We test coders" would incorrect, because it exceeds the K-character limit; • the output "Codility" would be incorrect, because it is shorter than the correct output. Write a function class Solution { public String solution (String message, int K); } which, given a message and an integer K, returns the message cropped to no more than K characters, as described above. Examples: 1. Given message = "Codility We test coders" and K = 14, the function should return "Codility We". Autocomplete is connected | All changes saved Files task1 solution.java test-input.txt Test Output 1 error || Detected some errors. solution.java x // you can also use imports, for example: // import java.util.*; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 // you can write to stdout for debugging purposes, e.g. // System.out.println("this is a debug message"); class Solution { public String solution (String message, int K) { K = 14; System.out.println ("Codility We test coders"); } } Compiler output: Solution.java:13: error: reached end of file while parsing To leave editor use Ctrl + M Any problems with the editor? Switch to basic editor > ▶ Run Code Give Feedback 6:14 PM
Expert Answer:
Answer rating: 100% (QA)
The method solutionmessage k which takes the message and the k and then returns the substring from 0 ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
Below are batting averages of the New York Yankees players who were at bat ve times or more in 2006. (a) Construct a frequency distribution. Explain how you chose the number of bins and the bin...
-
Radioactive fallout from testing atomic bombs drifted across a region. There were 230 people in the region at the time and 42 of them eventually died of cancer. Cancer experts estimate that one would...
-
CircuitTown commenced a gift card program in January 2024 and sold $10,000 of gift cards in January, $15,000 in February, and $16,000 in March 2024 before discontinuing further gift card sales....
-
Consider the calculation of an external rate of return (ERR). The positive cash flows in the cash flow profile are moved forward to \(t=n\) using what value of \(i\) in the \((F \mid P i, n-t)\)...
-
Anya Clark opened Anyas Cleaning Service on July 1, 2017. During July, the following transactions were completed. July 1 Anya invested $20,000 cash in the business. 1 Purchased used truck for...
-
Assets Cash Accounts receivable Inventory Plant and equipment, net Joel de Paris, Incorporated Balance Sheet Beginning Balance $ 135,000 342,000 Ending Balance $ 135,000 485,000 Investment in...
-
Pacifico Company, a U.S.-based importer of beer and wine, purchased 1,500 cases of Oktoberfest-style beer from a German supplier for 390,000 euros. Relevant U.S. dollar exchange rates for the euro...
-
Assume the demand curve The 5-year breakeven inflation rate has fallen from 2.7% in March to 2.3% in September 2023. Everything else being equal, how would the demand curve and/or supply curve for...
-
Do you think companies have the experience in new channels or should they use agency specialists? Explain why or why not.
-
Suppose before in Seymour's Bay that both Jimmy and Bob devoted 50% of their time to each good. If Pizza and Burgers were valued equally, what would be the change in total output if specialization...
-
How do political biases and agendas affect decision-making processes in practice?
-
Use the rational zeros theorem to list all possible rational zeros of the following. h(x)=4x+2+3x + 5x- 8.x Be sure that no value in your list appears more than once.
-
Joe Levi bought a home in Arlington, Texas, for $127,000. He put down 25% and obtained a mortgage for 30 years at 6%. a. What is Joe's monthly payment? (Do not round intermediate calculations. Round...
-
Mauritius Eagle Mauritius Eagle Insurance Company Limited structure incorporated in 1973 and listed in the Stock Exchange of Mauritius in 1993. It is based on corporate governance has enjoyed an...
-
The population of Detroit, Michigan, decreased from 1,027,974 in 1990 to 688,701 in 2013 (Source: U.S. Census Bureau). Find the average rate of change in the population of Detroit, Michigan, over the...
-
Tarzlon is a liquid antibiotic that is taken orally to treat infections of the spleen. It is effective only if it can maintain a concentration in the bloodstream (based on volume of body fluid) above...
-
Titanium dioxide is a wide-bandgap semiconductor that is showing promise as an insulating dielectric in VLSI capacitors and for use in solar cells. Thin films of TiO2 are to be prepared by chemical...
-
The rate law for the hydrogenation (H) of ethylene (E) to form ethane (A) over a cobalt-molybdenum catalyst (Collection Czech. Chem. Commun., 51, 2760 (1988)) is rE=kPEPH1+KEPE a. Suggest a mechanism...
-
Provide an overview of the behavioral finance perspectives of risk.
-
Define the heuristic biases of representativeness, anchoring, and mental accounting.
-
List and explain some fundamental issues of behavioral finance.
Study smarter with the SolutionInn App