Question

In: Computer Science

[JAVA] How to do this problem. we have to tell if given list of three number...

[JAVA] How to do this problem. we have to tell if given list of three number is in ascending, descending or no order.

Instructions

You will be given three integers: ?, ? and ? (read in for you). Then you will be given two boolean values that tell you which way the numbers are going - if the numbers are in increasing order or in decreasing order, respectively.

 

Details

Input

  • The program reads in:

    • a sequence of three integers: ?, ? and ?

    • boolean isIncreasing: set to True if the sequence of integers is increasing, i.e. ?<?<? (note, we are reading in either 0 for False or 1 for True)

    • boolean isDecreasing: set to true if the sequence of integers is decreasing, i.e. ?>?>?

Processing

  • Based on the boolean values read in determine if the sequence of integers is increasing, decreasing or neither.

Output

  • The original order is output for you.
  • If the sequence of integers is increasing, output “The numbers are increasing”

      "The numbers are strictly increasing."
  • If they are decreasing, output “The numbers are decreasing”

      "The numbers are strictly decreasing."
  • Otherwise, output “No obvious order”  

      "There is no strict order."

Given code:

import java.util.Scanner;

public class PoD {
   public static void main (String [] args) {
      
  
       Scanner in = new Scanner (System.in);
      
       //Input
       int i = in.nextInt();
       int j = in.nextInt();
       int k = in.nextInt();
      
       boolean isIncreasing = in.nextInt() == 1 ? true : false;
       boolean isDecreasing = in.nextInt() == 1 ? true : false;
      
       System.out.println("Original order: " + i + " " + j + " " + k);


  
if ( boolean isIncreasing = 1) {
   System.out.println("The numbers are strictly increasing.");
}
else if ( boolean isDecreasing = 1) {
   System.out.println("The numbers are strictly decreasing.");
}
       else {
           System.out.println("There is no strict order.");
       }


      
   }
}

Solutions

Expert Solution

import java.util.Scanner;

public class PoD {
   public static void main (String [] args) {
      
  
       Scanner in = new Scanner (System.in);
      
       //Input
       int i = in.nextInt();
       int j = in.nextInt();
       int k = in.nextInt();
      
       boolean isIncreasing = in.nextInt() == 1 ? true : false;
       boolean isDecreasing = in.nextInt() == 1 ? true : false;
      
       System.out.println("Original order: " + i + " " + j + " " + k);


  
if (isIncreasing) { 
   System.out.println("The numbers are strictly increasing.");
}
else if (isDecreasing) { 
   System.out.println("The numbers are strictly decreasing.");
}
       else {
           System.out.println("There is no strict order.");
       }


      
   }
}

You just made a small mistake in "if" and "else if" conditions .

You already declared "isIncreasing" and "isDecreasing" variables so you don't need to say "boolean" again and you can't give "isIncreasing = 1" in conditions

I will submit my output screenshot below

Hope this will be helpful..


Related Solutions

Java, no imports: Given a list of integers, round each number to the nearest multiple of...
Java, no imports: Given a list of integers, round each number to the nearest multiple of 5.        2.5-7.49 round to 5. 7.5-12.49 round to 10. 12.5-17.49 round to 15. etc.        return the sum of all the rounded elements.        Implement this way for credit:        Fill in the method directly below this one called helperRound() to round each number. Call that method and use the sum of the returned values.        helperSum({4.3, 16.7})...
in java we need to order a list , if we create a program in java...
in java we need to order a list , if we create a program in java what  are the possible ways of telling your program how to move the numbers in the list to make it sorted, where each way provides the required result. list the name of sorting with short explanation
We have a list of runner and their running time, write a program in Java to...
We have a list of runner and their running time, write a program in Java to show the fastest runner and their time.
Although we do not have for most diseases, we do have vaccines for several. List 3...
Although we do not have for most diseases, we do have vaccines for several. List 3 each bacteria, DNA viruses, and RNA viruses that we have approved vaccines for use in humans in the United States. For each you must say which it is, bacteria, DNA virus, or RNA virus.
LIST the cranial nerves by name and number. Tell me their origin; tell me their function,...
LIST the cranial nerves by name and number. Tell me their origin; tell me their function, and tell me how to test the nerve(s).
In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00); Given the class...
In java, I have problem how to declare BankAccount Account = new BasicAccount(100.00); Given the class BankAccount , implement a subclass of BankAccount called BasicAccount whose withdraw method will only withdraw money if there is enough money currently in the account (no overdrafts allowed). Assume all inputs to withdraw and the constructor will be valid non-negative doubles. public class BankAccount {     private double balance;     public BankAccount() {         balance = 0;     }     public BankAccount(double initialBalance) {         balance = initialBalance;     }     public...
Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How...
Java basic sorting problem Supposed I have a simple array list storing the telephone numbers. How can I sort the numbers in descending order with different ways? Give ArrayList<String> tel = ["11223344", "55442211", "99881122", "99002211", "34446666", "12342353"] I come up a solution using Collections.sort(tel), but it requires a compare method and I have no idea its contents and also the position of the method. Would you suggest 2 or 3 ways and write the code to achieve my purpose?
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
Given the following program(Java); we are asked to do the following 1. Add a loop in...
Given the following program(Java); we are asked to do the following 1. Add a loop in the main to enqueue 12 items of your choice. 2. Be sure to implement some form of error checking that lets you know if the loop tries to add too many items to the queue. Error message: "Unexpected overflow" 3. Add a loop to dequeue items and print them on their own line with their location. Location = ? item = ? package Khatrijavaarrayqueue;...
How do we change source code on java programming? Thanks
How do we change source code on java programming? Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT