Question

In: Computer Science

Java Implement a class MyInteger that contains: • An int data field/instance variable named value that...

Java

Implement a class MyInteger that contains:

• An int data field/instance variable named value that stores the int value represented by this object.

• A constructor that creates a MyInteger object for a default int value. What default value did you choose? What other values did you consider? Why did you make this choice?

• A constructor that creates a MyInteger object for a specified int value.

• A getter method, valueOf(), that returns value.

• A setter method, setValue(int), that replaces the current value with the specified one and returns the original value.

• The method equals(int) that return true if the value in this object is equal to the specified value. This method will not override Object’s equals() method.

• The method toString() that returns a text (String) version of value. toString() must include thousands grouping separators when value is greater than 999. This method will override Object’s toString() method. You will need to use String.format() to get the desired result.

• The static methods isZero(int), isEven(int) and isOdd(int) that return true if the specified value is zero, even or odd, respectively.

• The instance methods isZero(), isEven() and isOdd() that return true if the value in this object is zero, even or odd, respectively. Do not duplicate the code in the static methods – use them to do the work in the instance methods.

• The methods increment() and decrement() that increase or decrease value by 1, respectively. These methods should both modify value and return its new value.

• The instance methods add(int), subtract(int), multiplyBy(int), divideBy(int), and remainder(int), corresponding to the five arithmetic operators (+, -, *, /, %). These methods should both modify value and return its new value.

Continue by adding the following:

• The instance method equals(Object) that return true if the value in this object is equal to the value of the specified instance (the parameter – an instance of MyInteger). This method will override Object’s equals() method. You must check the parameter to make sure it’s not null and that it’s a MyInteger before you can cast it to a MyInteger and interrogate its value.

• The instance methods add(MyInteger), subtract(MyInteger), multiplyBy(MyInteger), divideBy(MyInteger), and remainder(MyInteger), corresponding to the five arithmetic operators (+, -, *, /, %). These methods should both modify value in this instance and return its new value. The specified instance (parameter) is unchanged.

• The methods isPrime() and isPrime(MyInteger) that return true if the value in this object or the specified instance (parameter), respectively, is a prime number. isPrime() is an instance method and isPrime(MyInteger) is a static method.

• The static methods parseInt(char[]) that converts an array of numeric characters to an int value and parseInt(String) that converts a String of numeric characters to an int value. You must do the conversions by hand (hint: subtract '0' from each digit character to get its numeric value).

• Javadoc comments for all methods. Make sure you generate the documentation for all public elements.

Create a MyIntegerStudentTests to test all methods. Your tests should include positive numbers, negative numbers, and zero as arguments and as the results of the operations. Make sure you test the static and instance methods.

Your program should not do any input/prompting. The output is mostly up to you, but it should inform the user of the operations under test and the expected and actual results.

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// MyInteger.java

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

   // Zero Argumented constructor
   public MyInteger() {
       this.value = 1;
   }

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

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

   public void setValue(int val) {
       this.value = val;
   }

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

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

   // This method will check whether the number is prime or not
   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;
   }


   public void increment() {
       this.value = value + 1;
   }

   public void decrement() {
       this.value = value - 1;
   }

   public int add(int val) {
       this.value += val;
       return value;
   }

   public int subtract(int val) {
       this.value -= val;
       return value;
   }

   public int multiplyBy(int val) {
       this.value *= val;
       return value;
   }

   public int divideBy(int val) {
       this.value /= val;
       return value;
   }

   public int remainder(int val) {
       this.value %= val;
       return value;
   }

   @Override
   public boolean equals(Object o) {
       if (o != null) {
           MyInteger mi = (MyInteger) o;
           if (value == mi.valueOf()) {
               return true;
           } else {
               return false;
           }
       }
       return false;

   }

   // This method will check whether the number is even or not
   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
   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
   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;
   }

   public boolean isZero() {
       if (value == 0)
           return true;
       else
           return false;
   }

   public static boolean isZero(int val) {
       if (val == 0)
           return true;
       else
           return false;
   }

   // This method will check whether the number is prime or not
   public static boolean isPrime(MyInteger mi) {
       // If the user entered number is '2' return true
       if (mi.valueOf() == 2)
           return true;
       for (int i = 2; i * i <= mi.valueOf(); i++) {
           if (mi.valueOf() % 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.valueOf())
           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 String toString() {
       return "MyInteger " + value;
   }

   public int add(MyInteger mi) {
       this.value += mi.valueOf();
       return value;
   }

   public int subtract(MyInteger mi) {
       this.value -= mi.valueOf();
       return value;
   }

   public int multiplyBy(MyInteger mi) {
       this.value *= mi.valueOf();
       return value;
   }

   public int divideBy(MyInteger mi) {
       this.value /= mi.valueOf();
       return value;
   }

   public int remainder(MyInteger mi) {
       this.value %= mi.valueOf();
       return value;
   }

  
}
___________________________

// MyIntegerStudentTests.java

public class MyIntegerStudentTests {

   public static void main(String[] args) {
       int num = 567;
       MyInteger mi1 = new MyInteger(num);
       if (mi1.isEven()) {
       System.out.println(num + " is Even.");
       } else {
       System.out.println(num + " is not Even.");
       }
       if (mi1.isOdd()) {
       System.out.println(num + " is Odd.");
       } else {
       System.out.println(num + " is not Odd.");
       }
       if (mi1.isPrime()) {
       System.out.println(num + " is Prime.");
       } else {
       System.out.println(num + " is not Prime.");
       }
       num = 555;
       if (MyInteger.isEven(num)) {
       System.out.println(num + " is Even.");
       } else {
       System.out.println(num + " is not Even.");
       }
       if (MyInteger.isOdd(num)) {
       System.out.println(num + " is Odd.");
       } else {
       System.out.println(num + " is not Odd.");
       }
       if (MyInteger.isPrime(num)) {
       System.out.println(num + " is Prime.");
       } else {
       System.out.println(num + " is not Prime.");
       }
       num = 987;
       MyInteger mi2 = new MyInteger(num);
       if (MyInteger.isEven(num)) {
       System.out.println(num + " is Even.");
       } else {
       System.out.println(num + " is not Even.");
       }
       if (MyInteger.isOdd(num)) {
       System.out.println(num + " is Odd.");
       } else {
       System.out.println(num + " is not Odd.");
       }
       if (MyInteger.isPrime(mi2)) {
       System.out.println(num + " is Prime.");
       } else {
       System.out.println(num + " is not Prime.");
       }
       num = 567;
       if (mi1.equals(num)) {
       System.out.println(mi1.valueOf() + " is equal to " + num);
       } else {
       System.out.println(mi1.valueOf() + " is not equal to " + num);
       }
       if (mi1.equals(mi2)) {
       System.out.println(mi1.valueOf() + " is equal to " + mi2.valueOf());
       } else {
       System.out.println(mi1.valueOf() + " is not equal to " + mi2.valueOf());
       }
       char arr[] = {
       '9',
       '9',
       '9'
       };
       int number1 = MyInteger.parseInt(arr);
       System.out.println("After converting char array to number : " + number1);
       int number2 = MyInteger.parseInt("786");
       System.out.println("After converting String to number : " + number2);
       }


   }

_____________________________

Output:

567 is not Even.
567 is Odd.
567 is not Prime.
555 is not Even.
555 is Odd.
555 is not Prime.
987 is not Even.
987 is Odd.
987 is not Prime.
567 is equal to 567
567 is not equal to 987
After converting char array to number : 999
After converting String to number : 786


_______________Could you plz rate me well.Thank You


Related Solutions

In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named BankAccount that contains: A private int data field named id for the...
Design a class named BankAccount that contains: A private int data field named id for the account. A private double data field named balance for the account. A constructor that creates an account with the specified id and initial balance. A getBalance() method that shows the balance. A method named withdraw that withdraws a specified amount from the account. Create a subclass of the BankAccount class named ChequingAccount. An overdraftlimit to be 1000 for ChequingAccount . Test your ChequingAccount class...
1. Define a class named Book that contains:  An int data field named pages that...
1. Define a class named Book that contains:  An int data field named pages that stores the number of pages in the book.  A String data field named title that represents the title of the book.  A constructor with parameters for initializing pages and title.  The getter and setter methods for all data fields.  A toString method that returns book information, including the book title and pages.  The equals method that returns true if...
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate 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 account....
Design a class named Student that contains the followingprivate instance variables: A string data field name...
Design a class named Student that contains the followingprivate instance variables: A string data field name for the student name. An integer data field id for the student id. A double data field GPA for the student GPA. An integer data field registeredCredits. It contains the following methods: An empty default constructor. A constructor that creates a student record with a specified id and name. The get and set methods for id, name, GPA, and registeredCredits. A method called registerCredit...
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...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
(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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT