Question

In: Computer Science

Posting 3 times , still not get right answer (The MyInteger class) Design a class named...

Posting 3 times , still not get right answer

(The MyInteger class) 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 value. A getter method that returns the int value.

* The methods isEven(), isOdd(), and isPrime() that return true if the value in this object is even, odd, or prime, respectively.

* The static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively.

* The static methods isEven(MyInteger), isOdd(MyInteger), and isPrime(MyInteger) that return true if the specified value is even, odd,or prime, respectively.

* The methods equals(int) and equals(MyInteger) that return true if the value in this object is equal to the specified value.

* A static method parseInt(char[]) that converts an array of numeric characters to an int value.

* A static method parseInt(String) that converts a string into an int value.

Write a client program that tests all methods in the class. Given that the definition of a prime number is a positive integer be sure to instruct the use to only enter positive integers.

SAMPLE RUN #1

--- Prompts For Keyboard/Console/Standard Input ---

Enter a positive integer to create a MyInteger object or to move on to next part of program:
Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:
Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:
Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or to move on to next part of program:
Enter a the second of two positive integers to test obj2.equals(int):
Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or to move on to next part of program:
Enter a the second of two positive integers to create obj3 and test obj2.equals(MyInteger obj3):
Enter a positive integer that will be placed into a char[] array to demonstrate the MyInteger.parseInt(char[]):
Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):

Inputs

--- Keyboard/Console/Standard Input stdin ---

1
2
3
7
8

1
2
19
23
24

1
2
5
22
19

22
22
33
33
33
34

11
11
1
1
1
2

125
256

333
444
987

Outputs

--- Monitor/Console/Standard Output ---

Enter a positive integer to create a MyInteger object or to move on to next part of program:MyInteger obj0 = new MyInteger(1);
obj0.getValue() = 1
obj0.isEven() = false
obj0.isOdd() = true
obj0.isPrime() = false
Enter a positive integer to create a MyInteger object or to move on to next part of program:MyInteger obj0 = new MyInteger(2);
obj0.getValue() = 2
obj0.isEven() = true
obj0.isOdd() = false
obj0.isPrime() = true
Enter a positive integer to create a MyInteger object or to move on to next part of program:MyInteger obj0 = new MyInteger(3);
obj0.getValue() = 3
obj0.isEven() = false
obj0.isOdd() = true
obj0.isPrime() = true
Enter a positive integer to create a MyInteger object or to move on to next part of program:MyInteger obj0 = new MyInteger(7);
obj0.getValue() = 7
obj0.isEven() = false
obj0.isOdd() = true
obj0.isPrime() = true
Enter a positive integer to create a MyInteger object or to move on to next part of program:MyInteger obj0 = new MyInteger(8);
obj0.getValue() = 8
obj0.isEven() = true
obj0.isOdd() = false
obj0.isPrime() = false
Enter a positive integer to create a MyInteger object or to move on to next part of program:Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:MyInteger.isEven(1) = false
MyInteger.isOdd(1) = true
MyInteger.isPrime(1) = false
Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:MyInteger.isEven(2) = true
MyInteger.isOdd(2) = false
MyInteger.isPrime(2) = true
Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:MyInteger.isEven(19) = false
MyInteger.isOdd(19) = true
MyInteger.isPrime(19) = true
Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:MyInteger.isEven(23) = false
MyInteger.isOdd(23) = true
MyInteger.isPrime(23) = true
Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:MyInteger.isEven(24) = true
MyInteger.isOdd(24) = false
MyInteger.isPrime(24) = false
Enter a positive integer to test static isXXX(int) methods or to move on to next part of program:Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:MyInteger obj1 = new MyInteger(1);
obj1.getValue() = 1
MyInteger.isEven(obj1) = false
MyInteger.isOdd(obj1) = true
MyInteger.isPrime(obj1) = false
Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:MyInteger obj1 = new MyInteger(2);
obj1.getValue() = 2
MyInteger.isEven(obj1) = true
MyInteger.isOdd(obj1) = false
MyInteger.isPrime(obj1) = true
Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:MyInteger obj1 = new MyInteger(5);
obj1.getValue() = 5
MyInteger.isEven(obj1) = false
MyInteger.isOdd(obj1) = true
MyInteger.isPrime(obj1) = true
Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:MyInteger obj1 = new MyInteger(22);
obj1.getValue() = 22
MyInteger.isEven(obj1) = true
MyInteger.isOdd(obj1) = false
MyInteger.isPrime(obj1) = false
Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:MyInteger obj1 = new MyInteger(19);
obj1.getValue() = 19
MyInteger.isEven(obj1) = false
MyInteger.isOdd(obj1) = true
MyInteger.isPrime(obj1) = true
Enter a positive integer to test static isXXX(MyInteger) methods or to move on to next part of program:Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or to move on to next part of program:MyInteger obj2 = new MyInteger(22);
obj2.getValue() = 22
Enter a the second of two positive integers to test obj2.equals(int):obj2.equals(22) = true
Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or to move on to next part of program:MyInteger obj2 = new MyInteger(33);
obj2.getValue() = 33
Enter a the second of two positive integers to test obj2.equals(int):obj2.equals(33) = true
Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or to move on to next part of program:MyInteger obj2 = new MyInteger(33);
obj2.getValue() = 33
Enter a the second of two positive integers to test obj2.equals(int):obj2.equals(34) = false
Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or to move on to next part of program:Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or to move on to next part of program:MyInteger obj2 = new MyInteger(11);
obj2.getValue() = 11
Enter a the second of two positive integers to create obj3 and test obj2.equals(MyInteger obj3):MyInteger obj3 = new MyInteger(11);
obj3.getValue() = 11
obj2.equals(obj3) = true
Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or to move on to next part of program:MyInteger obj2 = new MyInteger(1);
obj2.getValue() = 1
Enter a the second of two positive integers to create obj3 and test obj2.equals(MyInteger obj3):MyInteger obj3 = new MyInteger(1);
obj3.getValue() = 1
obj2.equals(obj3) = true
Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or to move on to next part of program:MyInteger obj2 = new MyInteger(1);
obj2.getValue() = 1
Enter a the second of two positive integers to create obj3 and test obj2.equals(MyInteger obj3):MyInteger obj3 = new MyInteger(2);
obj3.getValue() = 2
obj2.equals(obj3) = false
Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or to move on to next part of program:Enter a positive integer that will be placed into a char[] array to demonstrate the MyInteger.parseInt(char[]):MyInteger obj4 = new MyInteger(MyInteger.parseInt(char []);
obj4.getValue() = 125
obj4.isEven() = false
obj4.isOdd() = true
obj4.isPrime() = false
Enter a positive integer that will be placed into a char[] array to demonstrate the MyInteger.parseInt(char[]):MyInteger obj4 = new MyInteger(MyInteger.parseInt(char []);
obj4.getValue() = 256
obj4.isEven() = true
obj4.isOdd() = false
obj4.isPrime() = false
Enter a positive integer that will be placed into a char[] array to demonstrate the MyInteger.parseInt(char[]):Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):MyInteger obj5 = new MyInteger(MyInteger.parseInt(String);
obj5.getValue() = 333
obj5.isEven() = false
obj5.isOdd() = true
obj5.isPrime() = false
Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):MyInteger obj5 = new MyInteger(MyInteger.parseInt(String);
obj5.getValue() = 444
obj5.isEven() = true
obj5.isOdd() = false
obj5.isPrime() = false
Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):MyInteger obj5 = new MyInteger(MyInteger.parseInt(String);
obj5.getValue() = 987
obj5.isEven() = false
obj5.isOdd() = true
obj5.isPrime() = false
Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):

Solutions

Expert Solution

Code:

import java.util.Scanner;
public class MyInteger {

    public static void main(String[]args){
        Scanner input = new Scanner (System.in);
        // Print the menu
        System.out.print("Enter a positive integer to create a MyInteger object or <enter> to move on to next part of program:");
        String userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger obj0 = new MyInteger("+intInput+");");
            MyInteger obj0 = new MyInteger(intInput);
            System.out.println("obj0.getValue() = " + obj0.getValue());
            System.out.println("obj0.isEven() = " + obj0.isEven());
            System.out.println("obj0.isOdd() = " + obj0.isOdd());
            System.out.println("obj0.isPrime() = " + obj0.isPrime());
            System.out.print("Enter a positive integer to create a MyInteger object or <enter> to move on to next part of program:");
            userInput=input.nextLine();
        }

        System.out.print("Enter a positive integer to test static isXXX(int) methods or <enter> to move on to next part of program:");
        userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger.isEven("+intInput+") = " + isEven(intInput));
            System.out.println("MyInteger.isOdd("+intInput+") = " + isOdd(intInput));
            System.out.println("MyInteger.isPrime("+intInput+") = " + isPrime(intInput));
            System.out.print("Enter a positive integer to test static isXXX(int) methods or <enter> to move on to next part of program:");
            userInput=input.nextLine();
        }

        System.out.print("Enter a positive integer to test static isXXX(MyInteger) methods or <enter> to move on to next part of program:");
        userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger obj1 = new MyInteger("+intInput+");");
            System.out.println("obj1.getValue() = " + intInput);
            System.out.println("MyInteger.isEven(obj1) = " + isEven(intInput));
            System.out.println("MyInteger.isOdd(obj1) = " + isOdd(intInput));
            System.out.println("MyInteger.isPrime(obj1) = " + isPrime(intInput));
            System.out.print("Enter a positive integer to test static isXXX(MyInteger) methods or <enter> to move on to next part of program:");
            userInput=input.nextLine();
        }

        System.out.print("Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or <enter> to move on to next part of program:");
        userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger obj2 = new MyInteger("+intInput+");");
            System.out.println("obj2.getValue() = " + intInput);
            System.out.print("Enter a the second of two positive integers to test obj2.equals(int):");
            int intInput2=parseInt(input.nextLine());
            System.out.println("obj2.equals(" + intInput2 + ") = " + (intInput2==intInput));
            System.out.print("Enter a the first of two positive integers to create obj2 and test obj2.equals(int) or <enter> to move on to next part of program:");
            userInput=input.nextLine();
        }

        System.out.print("Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or <enter> to move on to next part of program:");
        userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger obj2 = new MyInteger("+intInput+");");
            System.out.println("obj2.getValue() = " + intInput);
            System.out.print("Enter a the second of two positive integers to create obj3 and test obj2.equals(MyInteger obj3):");
            int intInput2=parseInt(input.nextLine());
            System.out.println("MyInteger obj3 = new MyInteger("+intInput2+");");
            System.out.println("obj3.getValue() = " + intInput2);
            System.out.println("obj2.equals(obj3) = " + (intInput2==intInput));
            System.out.print("Enter a the first of two positive integers to create obj2 and test obj2.equals(MyInteger obj3) or <enter> to move on to next part of program:");
            userInput=input.nextLine();
        }

        System.out.print("Enter a positive integer that will be placed into a char[] array to demonstrate the MyInteger.parseInt(char[]):");
        userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger obj4 = new MyInteger(MyInteger.parseInt(char []);");
            System.out.println("obj4.getValue() = " + intInput);
            System.out.println("obj4.isEven() = " + isEven(intInput));
            System.out.println("obj4.isOdd() = " + isOdd(intInput));
            System.out.println("obj4.isPrime() = " + isPrime(intInput));
            System.out.print("Enter a positive integer that will be placed into a char[] array to demonstrate the MyInteger.parseInt(char[]):");
            userInput=input.nextLine();
        }

        System.out.print("Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):");
        userInput=input.nextLine();
        while(userInput.compareTo("")!=0) {
            int intInput = parseInt(userInput);
            System.out.println("MyInteger obj5 = new MyInteger(MyInteger.parseInt(String);");
            System.out.println("obj5.getValue() = " + intInput);
            System.out.println("obj5.isEven() = " + isEven(intInput));
            System.out.println("obj5.isOdd() = " + isOdd(intInput));
            System.out.println("obj5.isPrime() = " + isPrime(intInput));
            System.out.print("Enter a positive integer that will be placed into a String to demonstrate the MyInteger.parseInt(String):");
            userInput=input.nextLine();
        }
    }

    private int value;

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

    public int getValue() {
        return value;
    }

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

    public boolean isEven() {
        return value % 2 == 0;
    }

    public boolean isOdd() {
        return !isEven();
    }
    
    
    public boolean isPrime() {
        if (value<2) return false;
        if (value == 2) return true;
        if (value % 2 == 0) return false;
//        Iterate from 3 upto sqrt(value) and check if there is a common factor.
        for (int i = 3; i * i <= value; i += 2)
            if (value % i == 0) return false;
        return true;
    }

    public static boolean isEven(int value) {
        return value % 2 == 0;
    }

    public static boolean isOdd(int value) {
        return !isEven(value);
    }

    public static boolean isPrime(int value) {
        if (value<2) return false;
        if (value == 2) return true;
        if (value % 2 == 0) return false;
//        Iterate from 3 upto sqrt(value) and check if there is a common factor.
        for (int i = 3; i * i <= value; i += 2)
            if (value % i == 0) return false;
        return true;
    }

    public static boolean isEven(MyInteger myInteger) {
        return myInteger.isEven();
    }

    public static boolean isOdd(MyInteger myInteger) {
        return myInteger.isOdd();
    }

    public static boolean isPrime(MyInteger myInteger) {
        return myInteger.isPrime();
    }

    public boolean equals(int value) {
        return this.value == value;
    }

    public boolean equals(MyInteger myInteger) {
        return this.value == myInteger.value;
    }
//Parse char array to integer
    public static int parseInt(char[] value) {
    
        double valueDouble = 0;
        int toPower = value.length - 1;
        for (char aValue : value) {

            valueDouble += Math.pow(10, toPower--) * (aValue - '0');
        }

        return (int)valueDouble;
    }

    public static int parseInt(String value) {

        return MyInteger.parseInt(value.toCharArray());
    }
}

Screenshots:

Output:

-------------------------END---------------------

Please give a thumbs up(upvote) if you liked the answer.


Related Solutions

I have tried this multiple times and I can't get the right answer. The 2014 balance...
I have tried this multiple times and I can't get the right answer. The 2014 balance sheet of Jordan’s Golf Shop, Inc., showed long-term debt of $5.2 million, and the 2015 balance sheet showed long-term debt of $5.45 million. The 2015 income statement showed an interest expense of $170,000. The 2014 balance sheet showed $520,000 in the common stock account and $5.5 million in the additional paid-in surplus account. The 2015 balance sheet showed $560,000 and $5.7 million in the...
please answer each next to its question Problem Description: (The Account class) Design a class named...
please answer each next to its question Problem Description: (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...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
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 Message to represent a sentence or phrase. The class will contain: •...
Design a class named Message to represent a sentence or phrase. The class will contain: • a private string data field to hold the sentence or phrase. • A no-arg constructor with an empty string message. • A constructor that create a message object with the specified string sentence or phrase. • Accessor and mutator (getter/setter) for string data field. • A method named getVowels ( ) that returns the number of vowels in a sentence or phrase. • A...
Design a class named Account (put it in a package named accountspackages) with the following UML...
Design a class named Account (put it in a package named accountspackages) with the following UML diagram: Account -customerID: int -customerName: String -balance: double +setCustomerID(int): void +setCustomerName(String): void +setBalance(double):void +getCustomerID(): int +getCustomerName(): String +getBalance(): double +deposit(double): void +withdraw(double): void +printInformation():void The method withdraw(double) withdraws a specified amount from the account if the amount is less than or equal the balance, otherwise the method prints the message: Sorry! The account does not have sufficient funds. The method printInformation() prints:     the...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
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 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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT