You are on page 1of 1

  Textbook Solutions Expert Q&A Study Pack Practice 

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / solve in java starter code import java.io.*; import java.mat…

Question: Solve in java


 
 
Starter code 
import java.io.*;
import java.math.*… Post a question
Answers from our experts for your tough
homework questions

Solve in java Enter question

Continue to post
0 questions remaining

Snap a photo from your


phone to post a question
We'll send you a one-time download
link

888-888-8888 Text me

By providing your phone number, you agree to receive a one-time


automated text message with a link to get the app. Standard
messaging rates may apply.

My Textbook Solutions

Managerial Fluid Design of


Accounting Mechanics... Concrete...
14th Edition 2nd Edition 14th Edition

View all solutions

Starter code
import java.io.*;

import java.math.*;
import java.security.*;

import java.text.*;

import java.util.*;

import java.util.concurrent.*;
import java.util.regex.*;
class Result {
/*

* Complete the 'countHighlyProfitableMonths' function below.

* The function is expected to return an INTEGER.

* The function accepts following parameters:

* 1. INTEGER_ARRAY stockPrices

* 2. INTEGER k

*/
public static int
countHighlyProfitableMonths(List<Integer> stockPrices, int k)
{

}
}
public class Solution {

public static void main(String[] args) throws IOException {

BufferedReader bufferedReader = new BufferedReader(new


InputStreamReader(System.in));

BufferedWriter bufferedWriter = new BufferedWriter(new


OutputStreamWriter(System.out));
int stockPricesCount =
Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> stockPrices = new ArrayList<>();
for (int i = 0; i < stockPricesCount; i++) {

int stockPricesItem =
Integer.parseInt(bufferedReader.readLine().trim());

stockPrices.add(stockPricesItem);

}
int k = Integer.parseInt(bufferedReader.readLine().trim());
int result = Result.countHighlyProfitableMonths(stockPrices,
k);
bufferedWriter.write(String.valueOf(result));

bufferedWriter.newLine();
bufferedReader.close();

bufferedWriter.close();

}
Starter input:
EXAMPLE 1:
8

12

2
EXAMPLE 2:
4

4
EXAMPLE 3:
6

3
Show transcribed image text

Expert Answer

Shradha Patel answered this


Was this answer helpful? 2 0
1,042 answers

CODE
SCREENSHOTS:

SOLUTION 1:

SOLUTION 2:

CODE:

SOLUTION 1:

import java.io.*;

import java.util.*;

class Result {

  /*

  * Complete the 'countHighlyProfitableMonths' function


below.

  *

  * The function is expected to return an INTEGER. The


function accepts

  * following parameters: 1. INTEGER_ARRAY stockPrices


2. INTEGER k

  */

  public static int


countHighlyProfitableMonths(List<Integer> stockPrices, int k)
{

      // Variable to hold number of


groups of k consecutive months which were

      // highly profitable

      int grpCount = 0;

      // Total number of k consecutive


months in stockPrices

      int kGrps = stockPrices.size() - k


+ 1;

      // Find groups k months which


were highly profitable

      for (int i = 0; i < kGrps; i++)


{

          // Get price


at index i

          Integer


priceToCompare = stockPrices.get(i);

          // Boolean to


check whether a group of k profitabe months is found

          boolean


kProfitableMnthsFound = true;

          // Find group


of k profitable months

          for (int j = i +


1; kProfitableMnthsFound && (j < (i + k)); j++) {

         
    // Check if price at j is greater than
priceToCompare

         
    if (stockPrices.get(j) > priceToCompare)
{

         
        // Set priceToCompare to
price at j

         
        priceToCompare =
stockPrices.get(j);

         
    } else

         
        kProfitableMnthsFound =
false;

          }

          // Check if


kProfitableMnthsFound is true

          if
(kProfitableMnthsFound)

         
    grpCount += 1; // Increment grpCount by 1

      }

      // Return grpCount

      return grpCount;

  }

public class Solution {

  public static void main(String[] args) throws


IOException {

      // Reader to get user input

      BufferedReader bufferedReader = new


BufferedReader(new InputStreamReader(System.in));

      // Writer to print to the


console

      BufferedWriter bufferedWriter = new


BufferedWriter(new OutputStreamWriter(System.out));

      // Number of months

      int stockPricesCount =


Integer.parseInt(bufferedReader.readLine().trim());

      // List to hold stock


prices

      List<Integer> stockPrices =


new ArrayList<>();

      // Get 'stockPricesCount' stock


prices from the console

      for (int i = 0; i <


stockPricesCount; i++) {

          int


stockPricesItem =
Integer.parseInt(bufferedReader.readLine().trim());

         
stockPrices.add(stockPricesItem);

      }

      // Get analysis parameter

      int k =
Integer.parseInt(bufferedReader.readLine().trim());

      // Get number of groups of k


consecutive months which were highly

      // profitable

      int result =


Result.countHighlyProfitableMonths(stockPrices, k);

      // Print the result

     
bufferedWriter.write(String.valueOf(result));

      bufferedWriter.newLine();

      // Close reader

      bufferedReader.close();

      // Close writer

      bufferedWriter.close();

  }

SOLUTION 2:

import java.io.*;

import java.util.*;

class Result {

  /*

  * Complete the 'countHighlyProfitableMonths' function


below.

  *

  * The function is expected to return an INTEGER. The


function accepts

  * following parameters: 1. INTEGER_ARRAY stockPrices


2. INTEGER k

  */

  public static int


countHighlyProfitableMonths(List<Integer> stockPrices, int k)
{

      // Variable to hold number of


groups of k consecutive months which were

      // highly profitable

      int profitableGrpCount = 0;

      // Variable to indicate the


index where the stock price was not

      // profitable compared to the


previous price

      int mismatchIdx = 0;

      // Find groups k months which


were highly profitable

      for (int i = 1; i <


stockPrices.size(); i++) {

          // Check if


price at i is less than or equal to that at (i - 1)

          if
(stockPrices.get(i) <= stockPrices.get(i - 1)) {

         
    // Set mismatchIdx to i

         
    mismatchIdx = i;

          }

          // Check if


(mismatchIdx to i) makes a group of k prices

          if (((i -


mismatchIdx + 1) == k) || ((i - mismatchIdx) >= k))

         
    profitableGrpCount += 1; // Increment
profitableGrpCount by 1

      }

      // Return
profitableGrpCount

      return profitableGrpCount;

  }

public class Solution {

  public static void main(String[] args) throws


IOException {

      // Reader to get user input

      BufferedReader bufferedReader = new


BufferedReader(new InputStreamReader(System.in));

      // Writer to print to the


console

      BufferedWriter bufferedWriter = new


BufferedWriter(new OutputStreamWriter(System.out));

      // Number of months

      int stockPricesCount =


Integer.parseInt(bufferedReader.readLine().trim());

      // List to hold stock


prices

      List<Integer> stockPrices =


new ArrayList<>();

      // Get 'stockPricesCount' stock


prices from the console

      for (int i = 0; i <


stockPricesCount; i++) {

          int


stockPricesItem =
Integer.parseInt(bufferedReader.readLine().trim());

         
stockPrices.add(stockPricesItem);

      }

      // Get analysis parameter

      int k =
Integer.parseInt(bufferedReader.readLine().trim());

      // Get number of groups of k


consecutive months which were highly

      // profitable

      int result =


Result.countHighlyProfitableMonths(stockPrices, k);

      // Print the result

     
bufferedWriter.write(String.valueOf(result));

      bufferedWriter.newLine();

      // Close reader

      bufferedReader.close();

      // Close writer

      bufferedWriter.close();

  }

SAMPLE
OUTPUT:

Ouput is same for both the


solutions

Sample Output 1

Sample Output 2

Sample Output 3

Sample Output 4

LOGIC

In the stockPrices array, for each price (starting with the


price at the zeroth index), compare with the
adjacent element.

For example,

if n = 6

stockPrices = {1, 2, 3, 3, 4, 5}

k=3

There are 4 possible groups of 3 consecutive months (done in the


first for loop (loop with i))

Group 1 - 1, 2, 3

Group 2 - 2, 3, 3

Group 3 - 3, 3, 4

Group 4 - 3, 4, 5

Iterate through each group to find the k highly profitable months


(done in the first for loop (loop with j))

In group 1, 1 < 2 < 3, therefore this group has k highly


profitable months

In group 2, 2 < 3 = 3, this is not a group with k highly


profitable months

In group 3, 3 = 3 < 4, this is not a group with k highly


profitable months

In group 4, 3 < 4 < 5, therefore this group has k highly


profitable months

Thus there are 2 groups of 3 highly profitable months

View comments (2)




Up next for you in Computer Science

The Company has hired you


This week's assignment deals

as a Network Engineer. You


with creating our own

need to implement the given


method and overloading that

Routing Protocols on the to… method. As a review metho…


See more questions for
subjects you study

See answer See answer

Questions viewed by other students

Q: Solve in Java
Starter code:
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
class Result {
/*
* Complete the 'getUnallottedUsers' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1.
2D_INTEGER_ARRAY...

A: See answer

Q: IPO Coding question - Solve in JAVA 7 please


class Result {
    /*
     * Complete the 'getUnallottedUsers'
function below.
     *
     * The function is expected to return an
INTEGER_ARRAY.
     * The function accepts following
parameters:
     *  1.
2D_INTEGER...

A: See answer

Show more 

COMPANY LEGAL & POLICIES CHEGG PRODUCTS AND SERVICES CHEGG NETWORK CUSTOMER SERVICE
About Chegg Advertising Choices Cheap Textbooks Mobile Apps EasyBib Customer Service
Chegg For Good Cookie Notice Chegg Coupon Sell Textbooks Internships.com Give Us Feedback
College Marketing General Policies Chegg Play Solutions Manual Thinkful Help with eTextbooks
Corporate Development Intellectual Property Rights Chegg Study Help Study 101 Help to use EasyBib Plus
Investor Relations Terms of Use College Textbooks Textbook Rental Manage Chegg Study
Jobs Global Privacy Policy eTextbooks Used Textbooks Subscription

Join Our Affiliate Program DO NOT SELL MY INFO Flashcards Digital Access Codes Return Your Books

Media Center Honor Code Learn Chegg Money Textbook Return Policy

Site Map Honor Shield Chegg Math Solver

© 2003-2021 Chegg Inc. All rights reserved.

You might also like