Question

. Design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by

0 0
Add a comment Improve this question Transcribed image text
Answer #1

======== MyInteger.java ========

public class MyInteger {
   // Declaring instance variables
   private int value;

   // Parameterized constructor
   public MyInteger(int value) {
       this.value = value;
   }

   // Getter method
   public int getValue() {
       return value;
   }

   /*
   * This method will check whether the number is even or not if even returns
   * true else return false
   */
   public boolean isEven() {
       if (value % 2 == 0)
           return true;
       else
           return false;
   }

   /*
   * This method will check whether the number is odd or not if odd returns
   * true else return false
   */
   public boolean isOdd() {
       if (value % 2 != 0)
           return true;
       else
           return false;
   }

   /*
   * This method will check whether the number is prime or not if prime
   * returns true else return false
   */
   public boolean isPrime() {
       // If the user entered number is '2' return true
       if (value == 2)
           return true;
       for (int i = 2; i * i <= value; i++) {
           if (value % i == 0)
               return false;
       }
       return true;
   }

   /*
   * This method will check whether the number is even or not if even returns
   * true else return false
   */
   public static boolean isEven(int val) {
       if (val % 2 == 0)
           return true;
       else
           return false;
   }

   /*
   * This method will check whether the number is odd or not if odd returns
   * true else return false
   */
   public static boolean isOdd(int val) {
       if (val % 2 != 0)
           return true;
       else
           return false;
   }

   /*
   * This method will check whether the number is prime or not if prime
   * returns true else return false
   */
   public static boolean isPrime(int val) {
       // If the user entered number is '2' return true
       if (val == 2)
           return true;
       for (int i = 2; i * i <= val; i++) {
           if (val % i == 0)
               return false;
       }
       return true;
   }

   /*
   * This method will check whether the number is even or not if even returns
   * true else return false
   */
   public static boolean isEven(MyInteger mi) {
       if (mi.getValue() % 2 == 0)
           return true;
       else
           return false;
   }

   /*
   * This method will check whether the number is odd or not if odd returns
   * true else return false
   */
   public static boolean isOdd(MyInteger mi) {
       if (mi.getValue() % 2 != 0)
           return true;
       else
           return false;
   }

   /*
   * This method will check whether the number is prime or not if prime
   * returns true else return false
   */
   public static boolean isPrime(MyInteger mi) {
       // If the user entered number is '2' return true
       if (mi.getValue() == 2)
           return true;
       for (int i = 2; i * i <= mi.getValue(); i++) {
           if (mi.getValue() % i == 0)
               return false;
       }
       return true;
   }

   // This method will whether the two numbers are equal are not
   public boolean equals(int val) {
       if (this.value == val)
           return true;
       else
           return false;
   }

   // This method will whether the two numbers are equal are not
   public boolean equals(MyInteger mi) {
       if (this.value == mi.getValue())
           return true;
       else
           return false;
   }

   public static int parseInt(char[] ch) {
       return Integer.parseInt(String.valueOf(ch));
   }

   public static int parseInt(String str) {
       return Integer.parseInt(str);
   }
}

==========================================

public class MyInteger { // Declaring instance variables private int value; // Parameterized constructor public MyInteger (in

* * This method will check whether the number is odd or not if odd returns true else return false */ public boolean isOdd ()

* This method will check whether the number is odd or not if odd returns * true else return false */ public static boolean is

D /* * This method will check whether the number is odd or not if odd returns * true else return false */ public static boole

// This method will whether the two numbers are equal are not public boolean equals (MyInteger mi) { if (this.value == mi.get

======== Exercise.java ========

public class Exercise

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

       MyInteger n1 = new MyInteger(5);

       System.out.println("n1 is even? " + n1.isEven());

       System.out.println("n1 is prime? " + n1.isPrime());

       System.out.println("15 is prime? " + MyInteger.isPrime(15));

       char[] chars = { '3', '5', '3', '9' };

       System.out.println(MyInteger.parseInt(chars));
       String s = "3539";

       System.out.println(MyInteger.parseInt(s));

       MyInteger n2 = new MyInteger(24);

       System.out.println("n2 is odd? " + n2.isOdd());

       System.out.println("45 is odd? " + MyInteger.isOdd(45));

       System.out.println("n1 is equal to n2? " + n1.equals(n2));

       System.out.println("n1 is equal to 5? " + n1.equals(5));

   }

}

==========================================

public class Exercise { public static void main(String[] args) { MyInteger nl = new MyInteger (5); System.out.println(nl is

==========================================

Output:

<terminated > Exercise [Java Application] C:\Program Files\ava\jre 1.8.0_181\bin\javaw.exe (Sep 10, 2020 4:40:11 AM) nl is ev

=====================Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
. Design a class named MyInteger. The class contains: An int data field named value that...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another...

    Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another Java file, a program named MyIntegerTester.java that tests all methods in the class. One technique is to write both classes at the same time. As much as possible you implement and test one method at a time. This minimized the amount of new code you have to look at when debugging a method. Only the Version 4.0 “tester” file will have main() method in...

  • (The Account class) Design a class named Account that contains: A private int data field named...

    (The Account class) Design a class named Account that contains: A private int data field named id for the account (default 0). A private double data field named balance for the account (default 0). A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default...

  • PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field...

    PYTHON PROGRAMMING LANGUAGE Design a class named Savings Account that contains: A private int data field named id for the savings account. A private float data field named balance for the savings account. A private float data field named annuallnterestRate that stores the current interest rate. A_init_ that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). . The accessor and mutator methods for id, balance, and annuallnterestRate. A method...

  • 9.7 (The Account class) Design a class named Account that contains: • A private int data...

    9.7 (The Account class) Design a class named Account that contains: • A private int data field named id for the account (default o). • A private double data field named balance for the account (default o). • A private double data field named annualInterestRate that stores the current interest rate (default o). Assume that all accounts have the same interest rate. • A private Date data field named dateCreated that stores the date when the account was created. •...

  • Design a class named Account that contains: A private int datafield named id(default 0) A private...

    Design a class named Account that contains: A private int datafield named id(default 0) A private double datafield named balance(default 0) A no-arg constructor that creates default account A constructor that creates an account with specified id & balance The getter and setter methods for id and balance. Create an object of account class using default constructor and update the balance to 2000$ .

  • Design a class named BankAccount that contains: 1. A private int data field named accountId for...

    Design a class named BankAccount that contains: 1. A private int data field named accountId for the account. 2. A private double data field named accountBalance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A private int data field named numberOfDeposits...

  • Write a class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • In Python - Design a class named Time. The class contains: -Data fields hour, minute, and...

    In Python - Design a class named Time. The class contains: -Data fields hour, minute, and second that represent a time. -A no-arg constructor that creates a Time object for the current time.(the values of the data fields will respresent the current time.) -A constructor that constructs a Time object with a specified hour, minute, and second, respectively. -A constructor that constructs a Time object with a specified elapsed time since midnight, Jan 1, 1970, in milliseconds. (The values of...

  • Part I: (The Myate class) Design a class named MyDate. The class contains: • The data...

    Part I: (The Myate class) Design a class named MyDate. The class contains: • The data fields year, month, and day that represent a date. Month is 0-based, i.e., 0 is for January. • A no-arg constructor that creates a MyDate object for the current date. • A constructor that constructs a MyDate object with a specified elapsed time since midnight, January 1, 1970, in milliseconds. • A constructor hat constructs a MyDate object with the specified year, month, and...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT