// Debug this method so it passes the unit test. ~A Guest
// Add comments beside any changes you make
// Add comments beside any changes you make ~A Guest
if(num1<num2)
if(num1>num2)// Reversed sign so query reads from (num1<num2) to now (num1>num2)
{
{
if(num3>num1)
if(num3>num1)// This and other uncommented lines remain the same
{
{
returnnum3;
returnnum3;
}
}
...
@@ -26,26 +29,72 @@ public class AssessmentPartOne {
...
@@ -26,26 +29,72 @@ public class AssessmentPartOne {
}
}
else
else
{
{
returnnum2
returnnum2;// Added semicolon to correctly end command
}
}
}
}
}
}
publicintsumNumbersBetween(intstart,intend)
publicintsumNumbersBetween(intstart,intend)
{
{
// 02 - Adding Across A Range
// 02 - Adding Across A Range ~A Guest
// Complete this method so that it adds together all
// Complete this method so that it adds together all ~A Guest
// the integers between start and end, but not including
// the integers between start and end, but not including ~A Guest
// start or end
// start or end ~A Guest
// This method should only deal with 0 and positive integers
// This method should only deal with 0 and positive integers ~A Guest
// This method should return -1 if it cannot calculate the result
// This method should return -1 if it cannot calculate the result ~A Guest
// You should comment your code explaining what each part does
// You should comment your code explaining what each part does ~A Guest
intsumOfSquares=0;
intsumOfSquares=0;// I'm unsure as to why the variable name is this when there's no squares involved?
// Either way name doesn't impact output so I'll leave it for convenience
// Conditions preventing calculations
// Either start or end is negative
if(start<0)// Tests for start being negative
{
sumOfSquares=-1;
returnsumOfSquares;// Ends the function with the output of -1, as value cannot be calculated
}
if(end<0)// Tests for end being negative
{
sumOfSquares=-1;
returnsumOfSquares;// Ends the function with the output of -1, as value cannot be calculated
}
// If no numbers between start and end
if(start==end-1)
{
sumOfSquares=-1;
returnsumOfSquares;
returnsumOfSquares;
}
}
// Start > End
if(start>end)
{
sumOfSquares=-1;
returnsumOfSquares;// Ends the function with the output of -1, as value cannot be calculated
}
elseif(start==end)// There are no numbers between the start and end if equal, meaning -1 output is required
{
sumOfSquares=-1;
returnsumOfSquares;// Ends the function with output of -1
}
else//Runs if start < end, all other conditions specified by previous statements
{
for(inti=start+1;i<end;i++)// Loops starting at start + 1 (the first value to be added) through until it reaches the end, where the loop ends before adding end to the total
{// This is what calculates the numbers between start and end: all else is deciding whether to run it
sumOfSquares=sumOfSquares+i;
}
}
returnsumOfSquares;// Required so there is a return line that can always be accessed
// However this will never be reached as at least one of (start>end),(start==end) or the else of that will be true