Question

In: Computer Science

Create a project with a Program class and write the following two methods (headers provided) as...

Create a project with a Program class and write the following two methods (headers provided) as described below:

  1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric value was entered.
  2. A Method, public static bool IsValid(string id), to check if an input string satisfies the following conditions: the string’s length is 5, the string starts with 2 uppercase characters and ends with 3 digits. For example, “AS122” is a valid string, “As123” or “AS1234” are not valid strings.

Solutions

Expert Solution

Java code given below :-

import java.util.*;

import java.io.*;

public class HelloWorld{
   
public static int InputValue(int min, int max)
{
    Scanner in = new Scanner(System.in);
     while (true) {
            System.out.println("Enter a number."); // get a number
            String input = in.next();
            int number = 0;
            try {
                number = Integer.parseInt(input); // convert string to integer
                if(number>=min && number<=max)   // if number between lower bound and upper bound(inclusive)
                     {
                            System.out.println("number is valid.");
                     }
                else
                  {
                     while (number< min || number >max) // if number not between lower bound and upper bound(inclusive)
                {
                         System.out.println("re-enter a number :"); // re-enter number
                            number = in.nextInt();
                 }
                    System.out.println("number is valid.");
                }
                  
                 break;
            }
            catch (NumberFormatException ne) {
                System.out.println("Input is not a number, continue"); // if user enter a non-number
            }
     }
     return 0;
}
public static boolean IsValid(String id)
{
   char[] sArr = id.toCharArray(); // convert given string to char array
   boolean x=false;
   if(sArr.length==5) // if length of array is 5 then
   {// check given string is valid or not
       if( ('A'<=sArr[0] && sArr[0]<='Z') && ('A'<=sArr[1] && sArr[1]<='Z') && ('0'<=sArr[2] && sArr[2]<='9') && ('0'<=sArr[3] && sArr[3]<='9') && ('0'<=sArr[4] && sArr[4]<='9'))
       {
           x=true;
       }
       else{
           x=false;
       }
   }
   else{ // if length of array is not 5
       x=false;
   }
   return x; // return boolean value
}
public static void main(String []args){
    Scanner in = new Scanner(System.in);
    Scanner sc = new Scanner(System.in);
        System.out.println("Enter a lower bound :"); // get lower bound
        int lower = in.nextInt();
        System.out.println("Enter a upper bound :"); // get upper bound
        int upper = in.nextInt();
        InputValue(lower,upper); // call InputValue() function
      
       System.out.println("\nEnter a String :"); // get String
        String str = sc.nextLine();
      
       boolean x =IsValid(str); // call IsValid() function
        if(x==true)
       {
               System.out.println("valid String.");   // if string is valid
       }
       else{
           System.out.println("In-valid String.");   // if string is in-valid
       }
     }
}

Screenshot of code and output :-


Related Solutions

Part A: Create a project with a Program class and write the following two methods (headers...
Part A: Create a project with a Program class and write the following two methods (headers provided) as described below: A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range or a non-numeric...
For this question you need to write some methods and class headers. (a) Assume that you...
For this question you need to write some methods and class headers. (a) Assume that you have written a Rectangle class with instance variables length and width. You have already written all set and get methods and area and perimeter methods. Write an equals() method that takes Object o as a parameter. The method should return true when the Object o is a rectangle with the same length and width. Answer: (b) A class named Fruit implements an interface called...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
(Palindrome integer) Write the methods with the following headers // Return the reversal of an integer,...
(Palindrome integer) Write the methods with the following headers // Return the reversal of an integer, i.e., reverse(456) returns 654 public static int reverse(int number) // Return true if number is a palindrome public static boolean isPalindrome(int number) Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome. Here is...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final grade for the course. Assume the following (you can hard-code these values in your program). Assignments are worth 30% Labs are worth 40% Mid-term is worth 15% Final is worth 15% Ask for the grades in each category. Store them in a double variable. Print out the final percentage grade.           For example, Final Grade Calculation Enter percentage grades in the following order. Assignments,...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
write program that develop a Java class Dictionary to support the following public methods of an...
write program that develop a Java class Dictionary to support the following public methods of an abstract data type: public class Dictionary { // insert a (key, value) pair into the Dictionary, key value must be unique, the value is associated with the key; only the last value inserted for a key will be kept public void insert(String key, String value); // return the value associated with the key value public String lookup(String key); // delete the (key, value) pair...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT