In: Computer Science
in bluej java Write an application with two classes. Class NumberUtility has one instance variable n of type int. Constructor initializes instance variable n by using input parameter n.
Class also has the following methods:
// Implement method by invoking method isOdd.
// Parameter givenDigit should be single digit number with value
// between 0 and 9 including the limits.
// by using full sentence for each invoked method.
Class TestUtility has main method with variable obj of NumberUtility type. User should be repeatedly asked to provide (via keyboard) an input number or 0 to stop. A new object from NumberUtility class should be instantiated and used to reassign the variable obj for each nonzero number entered by user. Object obj should invoke method report(). Must use while loop to implement iteration (or repetition).
import java.util.*;
//Ceating Class
class NumberUtility{
//Creating Variable
public int n;
//Initialising Variable Through Constructor
public NumberUtility(int n){
this.n = n;
}
//Getting The Value Entered
public int getN(){
return n;
}
//Checking the number is Odd or not
public boolean isOdd(){
if( n % 2 != 0 ){
return true;
}
return false;
}
//Checking the number is Even or not
public boolean isEven(){
if( n % 2 == 0 ){
return true;
}
return false;
}
//Count The Digits of the number
public int countDigits(int givenDigit){
int count = 0;
while(givenDigit != 0)
{
givenDigit /= 10;
++count;
}
return count;
}
//Count the Even Digits in Number
public int countEven(){
int even_count = 0;
while(n > 0)
{
int rem = n % 10;
if (rem % 2 == 0)
even_count++;
n = n / 10;
}
return even_count;
}
//Printing All The Result
public void report(){
System.out.println("Number Entered: " + getN());
if( isEven() ) {
System.out.println("No is Even");
}
if( isOdd() ) {
System.out.println("No is Odd");
}
System.out.println("Number Of Digits In Entered Number: " + countDigits(n));
System.out.println("Number Of Even Digits In Entered Number: " + countEven());
}
}
public class Main
{
public static void main(String[] args) {
while(true){ //Infnite Loop Will Exit When User Will Enter 0
System.out.println("Enter Any No (0 to Exit): ");
Scanner sc=new Scanner(System.in);
int n = sc.nextInt(); //Getting The Input
if( n == 0 ){
break; //If No Entered is Zero Then Loop Will break
}
//Initiliaze The Object
NumberUtility a = new NumberUtility(n);
a.report(); //Calling The Report Function
}
}
}
Output: