Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in java 1) Given a String variable response that has already been declared , wri

ID: 3804752 • Letter: I

Question

in java

1)

Given a String variable response that has already been declared , write some code that repeatedly reads a value from standard input into response until at last a Y or y or N or n has been entered.

ASSUME the availability of a variable , stdin, that references a Scanner object associated with standard input.

2)

Clunker Motors Inc. is recalling all vehicles from model years 2001-2006. A boolean variable named norecall has been declared . Given a variable modelYear write a statement that assigns true to norecall if the value of modelYear does NOT within the recall range and assigns false otherwise.

Do not use an if statement in this exercise!

3)Assume that x is a char variable has been declared and already given a value . Write an expression whose value is true if and only if x is a decimal digit (0-9).

4) Assume that x is a char variable has been declared and already given a value . Write an expression whose value is true if and only if x is NOT a letter.

5)Assume that x is a char variable has been declared and already given a value . Write an expression whose value is true if and only if x is alphanumeric , that is either a letter or a decimal digit.

Explanation / Answer

1)

ReadVals.java

import java.util.Scanner;

public class ReadVals {
   public static void main(String[] args) {

       String str;
int n;
       // Scanner object is used to get the inputs entered by the user
       Scanner sc = new Scanner(System.in);

       while (true) {
           System.out.print("Enter a value :");
           str = sc.next();
           if (str.equals("Y") || str.equals("y") || str.equals("N")
                   || str.equals("n")) {
               System.out.println(":: Program Exit ::");
               break;
           } else {
               n=Integer.parseInt(str);
               System.out.println("You entered :"+n);
               continue;
           }
       }

   }
}

__________________

Output:

Enter a value :5
You entered :5
Enter a value :6
You entered :6
Enter a value :8
You entered :8
Enter a value :Y
:: Program Exit ::

_________________

2)

ModelYear.java

import java.util.Scanner;

public class ModelYear {

   public static void main(String[] args) {
       //Declaring variables
       int modelYear = 0;
       boolean norecall;
      
       //Scanner object is used to get the inputs entered by the user
       Scanner sc=new Scanner(System.in);
      
       //getting the model year entered by the user
       System.out.print("Enter the model year :");
       modelYear=sc.nextInt();
      
       //Checking the model year is in the range or not
       norecall=(modelYear>=2001 && modelYear<=2006)?true:false;
  
System.out.println(norecall);
   }

}

________________

Output#1:

Enter the model year :2010
false

________________

output#2:

Enter the model year :2005
true

________________

3)

Digit.java

public class Digit {

   public static void main(String[] args) {
       //Declaring a character type variables and assigned a value
       char x = 9;

       //Expression whether the char value is within range or not
       boolean bool = (x >= 0 && x <= 9) ? true : false;
       System.out.println(bool);
   }

}

___________________

output:

true

_________________

4)

NotLetter.java

public class NotLetter {

   public static void main(String[] args) {
       char x=9;
      
       boolean bool=(!Character.isAlphabetic(x))?true:false;
       System.out.println(bool);
      

   }

}

__________________

Output:

true

_________________

5)

LetterDigitOrNot.java

public class LetterDigitOrNot {

   public static void main(String[] args) {
       char x='9';
      
       boolean bool=(Character.isAlphabetic(x) ||Character.isDigit(x))?true:false;
       System.out.println(bool);
      

   }

}

__________________

Output:

true

________________