Question

public static boolean isArithmetic(java.lang.String text) Given a string of text containing numbers separated by commas, returns...

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,

  • given "2,4,6,8", the method returns true
  • given "-2,5,12,19,26", returns true
  • given "2,4,7", returns false
  • given "1,2,23skidoo", returns false

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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

public class IsArithmeticTest // definition of the method public static boolean isArithmetic ( java.lang. String text) Declar

start the main function. public static void main (String[i args) System.out.println(isArithmetic(2,4, 6,8)) System.out.prin

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"));

   }
}

Add a comment
Know the answer?
Add Answer to:
public static boolean isArithmetic(java.lang.String text) Given a string of text containing numbers separated by commas, returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is...

    Write a recursive method,     public static Boolean isPalindrome(String s) That returns true if s is a palindrome, else false.


  • 1.Method name: findSmallestPositiveNumber Parameter(s): A String containing integer numbers separated by spaces. There must be at...

    1.Method name: findSmallestPositiveNumber Parameter(s): A String containing integer numbers separated by spaces. There must be at least one positive number in the String. Return value: An int value that is the smallest number greater than 0 in the input string. Example: findSmallestPositiveNumber("2 -4 5") should return 2. Note: Even though I put this first, I would consider working on this later. The other methods are basically just applying the patterns we looked at in class. This one is similar but...

  • Please write a code in Java where it says your // your code here to run...

    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...

    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;...

    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++)...

  • in c++ You must implement the function: string get_ap_terms(int a, int d, size_t n); which returns...

    in c++ You must implement the function: string get_ap_terms(int a, int d, size_t n); which returns a string containing the first n terms of the arithmetic progression (AP) as a sequence of comma-separated values. Recall that an AP is specified by two terms a and d. The first term is a, and the d is how much you add to each term to get the next term. So the first N terms of the above AP will be: a, a+d,...

  • (IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String...

    (IN JAVA) Write a program named Review.java and implement the following methods: public static boolean isValidTicTacToeBoardString(String str) This method takes as its parameter a String that contains 9 characters representing the status of the Tic Tact Toe game. The String is mixed with X, O, x, o, and other letters. The first three letters represent the first row, letter 4 through letter 6 represent the second row, and the rest for the last row. To be considered as a valid...

  • Given java code is below, please use it! import java.util.Scanner; public class LA2a {      ...

    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...

    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...

  • Create a complete LinkedList class which implements all of the methods listed below using dynamic memory...

    Create a complete LinkedList class which implements all of the methods listed below using dynamic memory allocation. You will need a Node class to represent each node in the list. The list should store String values. Include a main method which tests all the methods of your list. Also answer these questions: What is the time complexity (using big-O notation) of these operations in your linked list? add(String val) add(int index, String val) get(int index) remove(String val) Submit three files:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT