Question

You will be given a list of conditions to test for. Write if-statements that display the...

You will be given a list of conditions to test for. Write if-statements that display the result if the condition is true. For example, if the condition was x is negative or over 5000, a possible answer would be:

if( x < 0 || x > 5000 )
      System.out.println("x is negative or > 5000.");

Conditions to test for:

1. x is closer in value to y than z is. Consider that all 3 numbers can be any combination of positive, negative and/or zero.

2. Suppose that answer is a String object. Write the code to display if the value of answer is some affirmative because the answer begins with "Y" or "y" as in one of the following: "Yes", "Yo", "YES", "YO", "YUP", "YEAH", , "yo", , "yEs", "yO", "yUp", etc.

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

Please find the answer below::

ConditionChecker.java

package classes5;

import java.util.Scanner;

public class ConditionChecker {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int x,y,z;
       System.out.print("Enter value of x :");
       x = sc.nextInt();
       System.out.print("Enter value of y :");
       y = sc.nextInt();
       System.out.print("Enter value of z :");
       z = sc.nextInt();
      
       int diff1 = Math.abs(x-y);
       int diff2 = Math.abs(x-z);
       if(diff1<diff2){
           System.out.println("X is closer to Y");
       }else{
           System.out.println("X is closer to Z");
       }
       sc.close();
   }

}

output:

AffirmativeChecker.java

package classes5;

import java.util.Scanner;

public class AffirmativeChecker {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       String ans;
       System.out.print("Enter your response : ");
       ans = sc.nextLine();
       ans = ans.toLowerCase();
       System.out.println("\nResponse : ");
       if(ans.length()>0 && ans.charAt(0)=='y'){
           System.out.println("It's YES");
       }else{
           System.out.println("Ohho!!!! It's denied");
       }
       sc.close();
   }

}

Add a comment
Know the answer?
Add Answer to:
You will be given a list of conditions to test for. Write if-statements that display the...
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
  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • Write an application that displays a series of at least five student ID numbers (that you...

    Write an application that displays a series of at least five student ID numbers (that you have stored in an array) and asks the user to enter a numeric test score for the student. Create a ScoreException class, and throw a ScoreException for the class if the user does not enter a valid score (less than or equal to 100). Catch the ScoreException, display the message Score over 100, and then store a 0 for the student’s score. At the...

  • thank you!! c++ 9. Write the code to prompt the User for website name and a...

    thank you!! c++ 9. Write the code to prompt the User for website name and a domain name. Display the website name, then add “www.” to the beginning of it and“. <domain name>” to the end of it, where <domain name> is the domain name they entered (e.g. “com”, “edu”, “net”, etc.). If the user included the “.” (dot) in the <domain name>, do not add an additional dot. If they did not include the dot, then add the dot...

  • Write and test a MIPS assembly language program to compute and display the first prime numbers...

    Write and test a MIPS assembly language program to compute and display the first prime numbers up to n where n is given. Set n to be 19 but the program should work of any value of n. For the program to identify primes, the easiest way is to use the algorithm: for (i = 2; i < x; i++)       if ((x % i) == 0) break;   //break out, not prime where x is the number you are checking...

  • JAVA 1.            Given the following class definition, what are the contents of the fields x and...

    JAVA 1.            Given the following class definition, what are the contents of the fields x and y of the object p ?    class MyPoint {      int x;      int y; public MyPoint (int x, int y){        x = x;        y = y;      } --------------------------------------- MyPoint p = new MyPoint( 100, 88 ); 2.            static and non-static members What would happen if you tried to compile and run the following code ? public class Driver {...

  • import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores....

    import java.util.*; /** Description: This program determines the average of a list of (nonnegative) exam scores. Repeats for more exams until the user says to stop. Input: Numbers, sum, answer Output: Displays program description Displays instruction for user Displays average Algorithm: 1. START 2. Declare variables sum, numberOf Students, nextNumber, answer 3. Display welcome message and program description 4. DOWHILE user wants to continue 5. Display instructions on how to use the program 6. Inititalize sum and number of student...

  • 29. (20 points) Write the complete C++ program that implements the following program, as if you...

    29. (20 points) Write the complete C++ program that implements the following program, as if you were turning this in for homework. Your program should have a commented header (your name, etc), comments within the program and a well-built display for the output. Don't forget the pre-processor commands Define an array called nums with a maximum of 20 integers, and fill the array with numbers, recei the keyboard (prompt for the numbers.) In this program, exactly 20 integers will be...

  • USE Python 1. Assign value 555 to the variable x, write code to square the variable...

    USE Python 1. Assign value 555 to the variable x, write code to square the variable x and print out the result. 2. Given a string x, write a program to check if its first character is the same as its last character. If yes, the code should output "True" . 3. We defined two variables, x = 'Python' and y = '3.0'. Write code to concatenate the two numbers (the result should be 'Python3.0').

  • 1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store...

    1. Write a new class, Cat, derived from PetRecord – Add an additional attribute to store the number of lives remaining: numLives – Write a constructor that initializes numLives to 9 and initializes name, age, & weight based on formal parameter values 2. Write a main method to test your Cat constructor & call the inherited writeOutput() method 3. Write a new writeOutput method in Cat that uses “super” to print the Cat’s name, age, weight & numLives – Does...

  • Write the? slope-intercept equation of the function f whose graph satisifies the given conditions. The graph...

    Write the? slope-intercept equation of the function f whose graph satisifies the given conditions. The graph of f passes through left parenthesis negative 6 comma 4 right parenthesis(?6,4) and is perpendicular to the line that has an? x-intercept of 11 and a? y-intercept of negative 3?3. The equation of the function is nothing. ?(Use integers or fractions for any numbers in the? equation.)

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
ADVERTISEMENT