public static boolean isArithmetic(java.lang.String text)
Given a string of text containing numbers separated by commas, returns true if the numbers form an arithmetic sequence (a sequence in which each value differs from the previous one by a fixed amount). For example,
The method should return true for any string containing two or fewer numbers and false for any invalid string. Assume that the string does not contain whitespace before or after the comma.
Parameters:
text - a string of text containing numbers separated by commas
Returns:
true if each number differs from the previous one by the same amount
method to copy:
// definition of the method.
public static boolean isArithmetic(
java.lang.String text)
{
// Declare variables.
String[] words =
text.split(",");
int[] arr = new
int[words.length];
// Start the for loop
for (int i = 0; i <
words.length; i++)
{
// Start the try
block
try
{
arr[i] = Integer.parseInt(words[i]);
}
catch (Exception
e)
{
return false;
}
}
// check the condition.
if (arr.length > 1)
{
// declare
variable.
int a = arr[0],
d = arr[1] - a;
// start the for
loop.
for (int i = 2;
i < arr.length; i++)
{
// check the condition.
if (arr[i] != arr[i - 1] + d)
{
return false;
}
}
return
true;
}
// check the condition.
else if (arr.length == 0)
{
return
false;
}
return true;
}
Program Screenshot:
Sample Output:
true
true
false
false
Code to Copy:
public class IsArithmeticTest
{
// definition of the method.
public static boolean isArithmetic(
java.lang.String text)
{
// Declare variables.
String[] words =
text.split(",");
int[] arr = new
int[words.length];
// Start the for loop
for (int i = 0; i <
words.length; i++)
{
// Start the try
block
try
{
arr[i] = Integer.parseInt(words[i]);
}
catch (Exception
e)
{
return false;
}
}
// check the condition.
if (arr.length > 1)
{
// declare
variable.
int a = arr[0],
d = arr[1] - a;
// start the for
loop.
for (int i = 2;
i < arr.length; i++)
{
// check the condition.
if (arr[i] != arr[i - 1] + d)
{
return false;
}
}
return
true;
}
// check the condition.
else if (arr.length == 0)
{
return
false;
}
return true;
}
// start the main function.
public static void main(String[] args)
{
System.out.println(isArithmetic("2,4,6,8"));
System.out.println(isArithmetic("-2,5,12,19,26"));
System.out.println(isArithmetic("2,4,7"));
System.out.println(isArithmetic("1,2,23skidoo"));
}
}
public static boolean isArithmetic(java.lang.String text) Given a string of text containing numbers separated by commas, returns...
Write a recursive method, public static Boolean isPalindrome(String s) That returns true if s is a palindrome, else false.
Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please write the code according to functions assigned...
Please write a code in Java where it says your // your code here to run the code smoothly. Import statements are not allowed for this assignment, so don't use them. _ A Java method is a collection of statements that are grouped together to perform an operation. Some languages also call this operation a Function. When you call the System.out.println() method, for example, the system actually executes several statements in order to display a message on the console. Please...
import java.util.*; public class Movie { private String movieName; private int numMinutes; private boolean isKidFriendly; private int numCastMembers; private String[] castMembers; // default constructor public Movie() { movieName = "Flick"; numMinutes = 0; isKidFriendly = false; numCastMembers = 0; castMembers = new String[10]; } // overloaded parameterized constructor public Movie(String movieName, int numMinutes, boolean isKidFriendly, String[] castMembers) { this.movieName = movieName; this.numMinutes = numMinutes; this.isKidFriendly = isKidFriendly; numCastMembers = castMembers.length; this.castMembers = new String[numCastMembers]; for(int i=0;i<castMembers.length;i++)...
Given java code is below, please use it! import java.util.Scanner; public class LA2a { /** * Number of digits in a valid value sequence */ public static final int SEQ_DIGITS = 10; /** * Error for an invalid sequence * (not correct number of characters * or not made only of digits) */ public static final String ERR_SEQ = "Invalid sequence"; /** * Error for...
Please I need help. Java language Method name: getScores Return value is a String of numbers scaled so that the highest number in the parameter String becomes 100 and all the other numbers are moved up by the same amount. This String should also be numbers separated by spaces with no additional characters. The order of the numbers should stay the same, so that a number in the original string has the equivalent scaled number in the returned string in...
Please show public static void main (String args[]) {} method too! 9. Given a list containing two or more integers, this method returns a set containing the two integers in the list that are closest to each other; i.e. the absolute value of the difference between them is minimal. The returned set must be sorted. public static Set<Integer> closest(List<Integers list) Example, if list = [21, 4, 2, 12, 18, 24, 16, 7, 20, 39, -3, 26] then the return should...
Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March","April", "May", "June", "July", "August", "September", days_in_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day number...
Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day...