Question

In: Computer Science

in bluej java Write an application with two classes. Class NumberUtility has one instance variable n...

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.

  • public NumberUtility(int n)

Class also has the following methods:  

  • public int getN()                              // Returns instance variable n
  • public boolean isOdd()                  // Returns true if number n is odd and returns false otherwise.
  • public boolean isEven()               // Returns true if number n is even and returns false if it is odd.

     // Implement method by invoking method isOdd.

  • public int countDigits(int givenDigit)    // Returns count how many digits from  n are equal to givenDigit.

                   // Parameter givenDigit should be single digit number with value

                   //  between 0 and 9 including the limits.                                                     

  • public int countEven()                   // Returns count how many digits are even in number n.
  • public void report()                        // Method report invokes the above  methods and reports results

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

Solutions

Expert Solution

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:


Related Solutions

Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a...
Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used to hold the rectangle’s width....
Code in Java Write a Student class which has two instance variables, ID and name. This...
Code in Java Write a Student class which has two instance variables, ID and name. This class should have a two-parameter constructor that will set the value of ID and name variables. Write setters and getters for both instance variables. The setter for ID should check if the length of ID lies between 6 to 8 and setter for name should check that the length of name should lie between 0 to 20. If the value could not be set,...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number)...
JAVA Write a Temperature class that has two instance variables: a temperature value (a floating-point number) and a character for the scale, either C for Celsius or F for Fahrenheit. The class should have four constructor methods: one for each instance variable (assume zero degrees if no value is specified and Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument constructor (set to zero degrees Celsius). Include the following: (1) two...
write Java program has two classes ,( using Inheritance ) first class set ( id ,...
write Java program has two classes ,( using Inheritance ) first class set ( id , name ) and method output second class ( id , name , Mark 1 , Mark 2 , Mark 3 ) method total , average , grade , results ( if the student pass or not ) , and output method
(java) Write a class called CoinFlip. The class should have two instance variables: an int named...
(java) Write a class called CoinFlip. The class should have two instance variables: an int named coin and an object of the class Random called r. Coin will have a value of 0 or 1 (corresponding to heads or tails respectively). The constructor should take a single parameter, an int that indicates whether the coin is currently heads (0) or tails (1). There is no need to error check the input. The constructor should initialize the coin instance variable to...
Java language Exercise #2: Design a Lotto class with one array instance variable to hold three...
Java language Exercise #2: Design a Lotto class with one array instance variable to hold three random integer values (from 1 to 9). Include a constructor that randomly populates the array for a lotto object. Also, include a method in the class to return the array. Use this class in the driver class (LottoTest.java) to simulate a simple lotto game in which the user chooses a number between 3 and 27. The user runs the lotto up to 5 times...
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel....
Writing Classes I Write a Java program containing two classes: Dog and a driver class Kennel. A dog consists of the following information: • An integer age. • A string name. If the given name contains non-alphabetic characters, initialize to Wolfy. • A string bark representing the vocalization the dog makes when they ‘speak’. • A boolean representing hair length; true indicates short hair. • A float weight representing the dog’s weight (in pounds). • An enumeration representing the type...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
(Java Problem) Create a Produce class that have an instance variable of type String for the...
(Java Problem) Create a Produce class that have an instance variable of type String for the name, appropriate constructors, appropriate accessor and mutator methods, and a public toString() method. Then create a Fruit and a Vegetable class that are derived from Produce. These classes should have constructors that take the name as a String, the price (this is the price per box) as a double, the quantity as an integer, and invoke the appropriate constructor from the base class to...
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp....
(JAVA) Referencing the class Oven, complete the Constructor and set method for the instance variable temp. Constructor checks if the temperature is between 0 and 500. If the temperature does not fit the requirement, then set the temperature to be zero. Otherwise, set it to be the value passed to the constructor. Set the power to be off. Set the String to be the color passed to the constructor. Use the same requirement for the constructor for the set temperature...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT