Have an array of integers with user input instead of given

Assignment Help JAVA Programming
Reference no: EM13162556

 change the current code to have an array of integers with user input intead of given input from the main where it says int[] a=....; And also from a text file but the same numbers as what is given in main.

public final class MaxSumTest

{

static private int seqStart = 0;

static private int seqEnd = -1;

 

/**

* Cubic maximum contiguous subsequence sum algorithm.

* seqStart and seqEnd represent the actual best sequence.

*/

public static int maxSubSum1( int [ ] a )

{

int maxSum = 0;

 

for( int i = 0; i < a.length; i++ )

for( int j = i; j < a.length; j++ )

{

int thisSum = 0;

 

for( int k = i; k <= j; k++ )

thisSum += a[ k ];

 

if( thisSum > maxSum )

{

maxSum = thisSum;

seqStart = i;

seqEnd = j;

}

}

 

return maxSum;

}

 

/**

* Quadratic maximum contiguous subsequence sum algorithm.

* seqStart and seqEnd represent the actual best sequence.

*/

public static int maxSubSum2( int [ ] a )

{

int maxSum = 0;

 

for( int i = 0; i < a.length; i++ )

{

int thisSum = 0;

for( int j = i; j < a.length; j++ )

{

thisSum += a[ j ];

 

if( thisSum > maxSum )

{

maxSum = thisSum;

seqStart = i;

seqEnd = j;

}

}

}

 

return maxSum;

}

 

/**

* Linear-time maximum contiguous subsequence sum algorithm.

* seqStart and seqEnd represent the actual best sequence.

*/

public static int maxSubSum3( int [ ] a )

{

int maxSum = 0;

int thisSum = 0;

 

for( int i = 0, j = 0; j < a.length; j++ )

{

thisSum += a[ j ];

 

if( thisSum > maxSum )

{

maxSum = thisSum;

seqStart = i;

seqEnd = j;

}

else if( thisSum < 0 )

{

i = j + 1;

thisSum = 0;

}

}

return maxSum; }

/**

* Recursive maximum contiguous subsequence sum algorithm.

* Finds maximum sum in subarray spanning a[left..right].

* Does not attempt to maintain actual best sequence.

*/

private static int maxSumRec( int [ ] a, int left, int right )

{

int maxLeftBorderSum = 0, maxRightBorderSum = 0;

int leftBorderSum = 0, rightBorderSum = 0;

int center = ( left + right ) / 2;

 

if( left == right ) // Base case

return a[ left ] > 0 ? a[ left ] : 0;

 

int maxLeftSum = maxSumRec( a, left, center );

int maxRightSum = maxSumRec( a, center + 1, right );

 

for( int i = center; i >= left; i-- )

{

leftBorderSum += a[ i ];

if( leftBorderSum > maxLeftBorderSum )

maxLeftBorderSum = leftBorderSum;

}

 

for( int i = center + 1; i <= right; i++ )

{

rightBorderSum += a[ i ];

if( rightBorderSum > maxRightBorderSum )

maxRightBorderSum = rightBorderSum;

}

 

return max3( maxLeftSum, maxRightSum,

maxLeftBorderSum + maxRightBorderSum );

}

 

/**

* Return maximum of three integers.

*/

private static int max3( int a, int b, int c )

{

return a > b ? a > c ? a : c : b > c ? b : c;

}

 

/**

* Driver for divide-and-conquer maximum contiguous

* subsequence sum algorithm.

*/

public static int maxSubSum4( int [ ] a )

{

return a.length > 0 ? maxSumRec( a, 0, a.length - 1 ) : 0;

}

 

/**

* Simple test program.

*/

public static void main( String [ ] args )

{

int a[ ] = { 4, -3, 5, -2, -1, 2, 6, -2 };

int maxSum;

 

maxSum = maxSubSum1( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum2( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum3( a );

System.out.println( "Max sum is " + maxSum + "; it goes"

+ " from " + seqStart + " to " + seqEnd );

maxSum = maxSubSum4( a );

System.out.println( "Max sum is " + maxSum );

}

}

Reference no: EM13162556

Questions Cloud

How many moles of oxygen were in the sample : If 5.50 of the unknown compound contained 0.183 of and 0.367 of , how many moles of oxygen, , were in the sample.
Derive an expression for the subsequent concentration : Derive an expression for the subsequent concentration of sodium chloride c in terms of c0, t, Q, and V. Make a sketch of c versus t and label the main features.
Which of your current costs are implicit : Which of your current costs are implicit, and which are explicit and suppose The Breakfast Club, Inc. offers to pay $800/month to use the building
State what is the initial ph of buffer : What is the initial pH of this buffer? 2. What is the pH of the buffer in question
Have an array of integers with user input instead of given : change the current code to have an array of integers with user input intead of given input from the main where it says int[] a=....; And also from a text file but the same numbers as what is given in main.
State sodium thiosulfate solution : A chemist must dilute 80.5mL of 126mM aqueous sodium thiosulfate solution until the concentration falls to 110.mM . He'll do this by adding distilled water to the solution until it reaches a certain final volume.
What are the percentages of co2 and ne in the sample by mole : If the sample contains a combined total of 1.90 mol and has a total mass of 60.3g, what are the percentages of CO2 and Ne in the sample by mole?
What was the concentration of the solution : A 515- sample of unknown solution reacts completely with to form 20.1 . What was the concentration of the solution?
What was the initial concentration : A zero-order reaction has a constant rate of 1.90×10-4 . If after 40.0 seconds the concentration has dropped to 6.50×10-2 , what was the initial concentration?

Reviews

Write a Review

JAVA Programming Questions & Answers

  Determine the type of moped

Write a driver class called MopedRental. This class should perform the following: asks the user to enter the size of the moped, the day of the week and the number of hours rented, creates the Moped object, based on the size, and displays the input..

  Write a java program in a netbeans project

Write a Java program in a NetBeans project

  Write java program which will permit user to input data

Write the Java Program which will permit the user to input data. The data will be validated using a loop that requires the user to input the data until it is correct or in the correct range. T

  Display random numbers to simulate rolling a die

1. Display random numbers to simulate rolling a die2. Create a java program that simulate a casino game. (rollin a die)

  Java program that prompts the user to input an integer

Write a Java program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.

  Create java applet to represent grade of gas

Write a java applet (not a java application program) for costco gas station. The applet will first ask you whether you are a costco customer, then the grade of gas you want to use.

  Create your own date class

You are to write a program that determines the day of the week for New Year's Day in the year 3000. To do this, you must create your own date class (MyDate) and use the following interface and main program:

  Calculate the maximum data rate in mb/second

b)How much cylinder skew is needed for an Oceangate disk operating at 20,000 rpm(new series for the Christmas promotions) which has a seek time of 0.5 milliseconds? This disk has 800 sectors of 512 bytes each on each track.

  Develop java code to compute monthly rent for housing units

Develop a java code that computes monthly rent for 3 housing units namely Bungalows,Apartments and hostels. All housing units have got size,color and monthly rental rate.

  When the user presses a button

When the user presses a button, get the text from text field. Now extract the words from the text one at a time and find the largest word of length 4 or more. Let's call this largest word X for now. In response, create a question based on the length ..

  Elliptic curve encryption

write a program to implement Elliptic Curve encryption/decryption and program will read parameters, plaintext and ciphertextfrom a file named "input.txt" (under the same directory).

  Write a recursive program

Write a recursive program to compute the number of ways in which an integer k can be written as sum

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