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

  • java 1. Write a method header on line three with the following specs: Returns: a boolean...

    java 1. Write a method header on line three with the following specs: Returns: a boolean Name: beTrue Parameters: none public class Main {       {        return true;    } } 2. Write a method header on line two with the following specs: Returns: a String Name: makeCapital Parameters: a String named "name" You should not be writing code on any line other than #2 public class Main {       {    String ans = name.toUpperCase();...

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

  • Java Program 1. Write a class that reads in a group of test scores (positive integers...

    Java Program 1. Write a class that reads in a group of test scores (positive integers from 1 to 100) from the keyboard. The main method of the class calls a few other methods to calculate the average of all the scores as well as the highest and lowest score. The number of scores is undetermined but there will be no more than 50. A negative value is used to end the input loop. import java.util.Scanner; public class GradeStat {...

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

  • public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The...

    public class ArrayHeadTailList<T> implements HeadTailListInterface<T> You are given an interface for a type of list. The list works like this: entries can only be added to and removed from the beginning or end of the list entries can be accessed in any position entries begin at index 0 Write a class that implements this interface. The class uses arrays to implement the list. Your class header and instance data variables will be: public class ArrayHeadTailList<T> implements HeadTailListInterface<T> private T[] listArray;...

  • Java Write a Fraction class that implements these methods:  add ─ This method receives a...

    Java Write a Fraction class that implements these methods:  add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.  multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.  divide ─ This method receives a Fraction parameter and divides the parameter fraction by the calling object fraction.  print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.)...

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