Question

In: Computer Science

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 methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd, or prime, respectively.
  • Methods equals(int) and equals(MyInteger) that return true if the value in the object is equal to the specified value.
  • A static method parseInt(char[]) that converts an array of numeric characters to an int
  • A static method parseInt(String) that converts a string into an int

Testing: (Describe how you test this program, you should use your own input data to test not just the test cases given in sample runs.) (4 points)

Test your program using the class and the main method given below. There are two test objects, n1 and n2 in the following test program, you should add a third test object, n3 with your own integer, and invoke all methods in your MyInteger program using object n3.

public class ProgrammingAssignmentEight {

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));

// Write your third test code for object n3 here!

}

}

Solutions

Expert Solution

Code to copy along with screenshots of code and output are provided.
Please refer to screenshots to understand the indentation of code.
If you have any doubts or issues. Feel free to ask in comments
Please give this answer a like, or upvote. This will be very helpful for me.
================================================================

Screenshots of "ProgrammingAssignmentEight.java":

Screenshots of "MyInteger.java":

Screenshots of Output :

---------------------------------------------------------------------------------------------------------

Code to copy(" ProgrammingAssignmentEight.java " ):

---------------------------------------------------------------------------------------------------------

public class ProgrammingAssignmentEight {

public static void main(String[] args) {

// initializing MyInteger object with value = 5
   MyInteger n1 = new MyInteger(5);

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

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

// Checking isPrime(int) function
System.out.println("15 is prime? " + MyInteger.isPrime(15));

// declaring character array
char[] chars = {'3', '5', '3', '9'};

// checking parseInt(char[]) function
System.out.println(MyInteger.parseInt(chars));

// initializing string
String s = "3539";

// checking parseInt(String) function
System.out.println(MyInteger.parseInt(s));

// initializing MyInteger object with value = 24
MyInteger n2 = new MyInteger(24);

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

// checking us Odd(int) function
System.out.println("45 is odd? " + MyInteger.isOdd(45));

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

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

// Write your third test code for object n3 here!

// declaring MyInteger object named n3
MyInteger n3 = new MyInteger(5);

// print statement
System.out.println("<<<<<-------Third Test-------->>>>>>.");

// checking isEven() function
System.out.println("n3 is even? " + n3.isEven());

// checking isPrime() function
System.out.println("n3 is prime? " + n3.isPrime());

// checking static isPrime(int) function
System.out.println("29 is prime? " + MyInteger.isPrime(29));

// declaring array of characters
char[] chars2 = {'4', '3', '3', '2'};

// checking parseInt(char[]) function
System.out.println("chars2 = "+ MyInteger.parseInt(chars2));

// declaring string
String s2 = "4332";

// checking parseInst(String) function
System.out.println("s2 = "+ MyInteger.parseInt(s2));

// adding integers returned by parseInt function for checking
System.out.println("chars2 + s2 = " + (MyInteger.parseInt(chars2) + MyInteger.parseInt(s2)));

// checking equals(int) function
System.out.println("n3 is equal to n1? " + n3.equals(n1));

// checking equals(MyInteger) function
System.out.println("n3 is equal to 9? " + n1.equals(9));


}

}

------------------------------------------------------------------------------

Code to copy(" MyInteger.java " ):

------------------------------------------------------------------------------

public class MyInteger {

   // integer variable to store value
   int value;
  
   // constructor
   MyInteger(int val)
   {
       this.value = val;
   }
  
   // boolean function to return true if value is even
   boolean isEven()
   {
       if(this.value % 2 == 0)
       {
           // return true
           return true;
       }
       else
       {
           // otherwise return false
           return false;
       }
      
   }
  
   // boolean function to return false if value is Odd
   boolean isOdd()
   {
       if(this.value % 2 == 0)
       {
           // return false if remainder is 0
           return false;
       }
       else
       {
           // otherwise return true
           return true;
       }
      
   }
  
   // function to return true if value is prime
   boolean isPrime()
   {
       for(int i = 2; i <= this.value/2; ++i)
   {
  
   if(this.value % i == 0)
   {
   // return false if remainder is 0
       return false;
  
   }
   }
       // otherwise return true if divisible by none
       return true;

   }
  
   // static method to return true if n is even
   static boolean isEven(int n)
   {
       if(n%2 == 0)
       {
           // return true if divisible by 2
           return true;
       }
       else
       {
           // otherwise return false
           return false;
       }
   }
  
   // static method to return true if n is Odd
   static boolean isOdd(int n)
   {
       if(n%2 == 0)
       {
           // return false if divisible by 2
           return false;
       }
       else
       {
           // otherwise return false
           return true;
       }
   }
  
   // static method to return true if n is prime
   static boolean isPrime(int n)
   {
       for(int i = 2; i <= n/2; ++i)
{
  
if(n % i == 0)
{
   // return false if n is divisible by i
return false;
  
}
}
       // return true if divisible by none
   return true;
      
   }
  
   // method to return true if value is equal to n
   boolean equals(int n)
   {
      
       if(this.value == n)
       {
           // return true if value is equal to n
           return true;
       }
       else
       {
           // otherwise return false
           return false;
       }
   }
  
   // method to return true if MyInteger object's value is equalt to this value
   boolean equals(MyInteger n)
   {
       if(this.value == n.value)
       {
           // return true if equal
           return true;
       }
       else
       {
           // otherwise return false
           return false;
       }
   }
  
   // method to return the integer value of array of characters
   static int parseInt(char[] chars)
   {
         
       // store the value into integer num
       int num = Integer.parseInt(String.valueOf(chars));
     
       // return num
       return num;
   }
     
   // method to return the integer value of string str
   static int parseInt(String str)
   {
       // store integer value into num
       int num = Integer.parseInt(str);
         
       // return num
       return num;
   }
     
     
  
}

------------------------------------------------XXXXXXXXXX--------------------


Related Solutions

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,...
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...
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....
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...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor 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...
Design a class named Account that contains: A private String data field named accountNumber for the...
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. A constructor...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
Design a class named ArraySort and it contains the following method: Public int search(int target): if...
Design a class named ArraySort and it contains the following method: Public int search(int target): if the target is found in the array, return the number of its showing up. If the target is not found in the array, return -1. If the array is empty, return -1; Public int maximum():Return the maximum number in the array if the array is nonempty, otherwise return -1; Public int minimum(): Return the minimum number in the array if the array is nonempty,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT