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 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) 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...
Write a Java program such that it consists of 2 classes: 1. a class that serves...
Write a Java program such that it consists of 2 classes: 1. a class that serves as the driver (contains main()) 2. a class that contains multiple private methods that compute and display a. area of a triangle (need base and height) b area of a circle (use named constant for PI) (need radius) c. area of rectangle (width and length) d. area of a square (side) e. surface area of a solid cylinder (height and radius of base) N.B....
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the...
Write these java classes: 1) DynArray.java: a class that models some of the functionality of the Java ArrayList. This class is not complete and must be modified as such: Write the method body for the default constructor Write the method body for the methods: arraySize(), elements(), grow(), shrink(). The incomplete code is provided here: public class DynArray { private double[] array; private int size; private int nextIndex;    public int arraySize() { }    public int elements() { } public...
Write a program in Java and run it in BlueJ according to the following specifications: The...
Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade on each line) and determines their type (excellent or ok). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "all" - prints all student records (first name, last name, grade, type). "excellent" - prints students with grade > 89. "ok"...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public...
Use BlueJ java to finish this PolkaDots class, class circle is included below as well. public class PolkaDots { private ArrayList<Circle> dots;// an arrayList field to hold an ArrayList. /** * Create a new list of Circles, starts with 5 default circles. */ public PolkaDots() { dots = new ArrayList<Circle>(); // an arrayList to hold a bunch of circles Circle circle1 = new Circle(30, 10, 10, "blue");//create the 1st circle dots.add(circle1);// add the 1st circle to the arrayList    Circle...
This is in Java 1. Create an application called registrar that has the following classes: a....
This is in Java 1. Create an application called registrar that has the following classes: a. A student class that minimally stores the following data fields for a student:  Name  Student id number  Number of credits  Total grade points earned             And this class should also be provides the following methods:  A constructor that initializes the name and id fields  A method that returns the student name field  A method that returns the student...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member...
In JAVA please... Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT