Question

In: Computer Science

In.java In this program write a method called upDown that takes three integers as arguments and...

In.java

In this program write a method called upDown that takes three integers as arguments and returns one of these 3 strings:

  • "increasing" if they are in strictly increasing order (note that 3,3,4 - are not strictly increasing),
  • "decreasing" if they are in strictly decreasing order.
  • "none" otherwise.
    I recommend you use a complex condition to check for this (that is, have a single if statement with one big question).

In the main method do the following:

  • read three integers from the user and call upDown for those integers, save the result in a variable, res1, and then print it.
  • read again three integers from the user and call upDown for those integers, save the result in another variable, res2, and then print it.
  • compare res1 and res2 and print "same" or "different"

read three integers from the user and call upDown for those integers, save the result in a variable, res1, and then print it.

---------- Sample run 1:
Enter three integers separated by spaces: 2 0 9
These numbers are in order: none
Enter three integers separated by spaces: 17 90 567
These numbers are in order: increasing
different

---------- Sample run 2:
Enter three integers separated by spaces: 90 9 1
These numbers are in order: decreasing
Enter three integers separated by spaces: 7 3 1
These numbers are in order: decreasing
same

---------- Sample run 3:
Enter three integers separated by spaces: 3 3 4
These numbers are in order: none
Enter three integers separated by spaces: 8 1 16
These numbers are in order: none
same

Solutions

Expert Solution

SOURCE CODE:

import java.util.*;
public class Main
{
    //method upDown to check sequence increasing,decreasing or none
    public static String upDown(int a,int b,int c)
    {
        String s=" ";
        if(a>b && b>c)
        {
            s="decreasing";
          
        }
        else if(a<b && b<c)
        {
            s="increasing";
        }
        else
        {
            s="none";
        }
        return s;
    }
   public static void main(String[] args) {
       int a,b,c,d,e,f;
       String res1,res2;
       Scanner sc=new Scanner(System.in);
       System.out.print("Enter three integers separated by spaces: ");
       //reading integers from the user
       a=sc.nextInt();
       b=sc.nextInt();
       c=sc.nextInt();
       res1=upDown(a,b,c);
       System.out.println("These numbers are in order: "+res1);
      
       System.out.print("Enter three integers separated by spaces: ");
       //reading integers from the user
       d=sc.nextInt();
       e=sc.nextInt();
       f=sc.nextInt();
       res2=upDown(d,e,f);
       System.out.println("These numbers are in order: "+res2);
       //compare if two results are equal or not
       if(res1.equals(res2))
       {
            //if two strings are equal
            System.out.println("same");
       }
       else
       {
             System.out.println("different");
       }
   }
}


CODE SCREENSHOT:


OUTPUT:

RUN-1

RUN-2

RUN-3

RUN-4


Related Solutions

(Triangle Inequality) Write a program triangle.py that takes three integers as command-line arguments and writes True...
(Triangle Inequality) Write a program triangle.py that takes three integers as command-line arguments and writes True if each one of them is less than or equal to the sum of the other two and False otherwise. This computation tests whether the three numbers could be the lengths of the sides of some triangle. $ python3 triangle.py 3 4 5 True $ python3 triangle.py 2 4 7 False
Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem.
Java programming:Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer called removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (Note that after deleting the element, the array size is reduced by 1.) You may assume that the array is unsorted.Now re-do this exercise and name it ExInsertionSort....
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a function called alternate that takes two positive integers, n and m, as input arguments...
Write a function called alternate that takes two positive integers, n and m, as input arguments (the function does not have to check the format of the input) and returns one matrix as an output argument. Each element of the n-by-m output matrix for which the sum of its indices is even is 1. All other elements are zero. For example, here is an example run: >> alternate(4,5) ans = 1 0 1 0 1 0 1 0 1 0...
Write the definition of a Point class method called clone that takes NO arguments and returns...
Write the definition of a Point class method called clone that takes NO arguments and returns a new Point whose x and y coordinates are the same as the Point’s coordinates.
use java for : 1. Write a method called indexOfMax that takes an array of integers...
use java for : 1. Write a method called indexOfMax that takes an array of integers and returns the index of the largest element. 2. The Sieve of Eratosthenes is “a simple, ancient algorithm for finding all prime numbers up to any given limit” (https://en.wikipedia. org/wiki/Sieve_of_Eratosthenes).Write a method called sieve that takes an integer parameter, n, and returns a boolean array that indicates, for each number from 0 to n -1, whether the number is prime.
.. Write a method called findNums that takes a two-dimension array of integers as a parameter...
.. Write a method called findNums that takes a two-dimension array of integers as a parameter and returns the number of times a two-digit number appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 The value returned would be 5 (there are 5 two-digit numbers in the array) public class Question2 {    public static void main(String args[]){      int arr[][] = {{10, 45,...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an...
FOR JAVA Write a method called findNum that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class HomeworkA { public static...
in java language Write a method called findNums that takes a two-dimension array of integers and...
in java language Write a method called findNums that takes a two-dimension array of integers and an int as parameters and returns the number of times the integer parameter appears in the array. For example, if the array (as created by the program below) is 10 45 3 8 2 42 3 21 44 And the integer parameter is 3, the value returned would be 2 (the number 3 appears two times in the array) public class Question2 {   ...
Using Java, write a program that takes in two integers from the keyboard called m and...
Using Java, write a program that takes in two integers from the keyboard called m and n, where m > n. Your program should print the first m natural numbers (m..1) downwards in n rows.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT