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

  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