Hello again, I have a question. The solution must be written in Java. Thank you
ID: 3828802 • Letter: H
Question
Hello again, I have a question. The solution must be written in Java.
Thank you very much.....
Chapter 7: Single-Dimensional Arrays
Assignment 7: MyInteger
Problem Description:
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 get method that returns the int value.
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 value.
A static method parseInt(String) that converts a string into an int value.
Implement the class. Write a client program that tests all methods in the class.
public class Exercise10_03 {
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));
}
}
class MyInteger {
// Implement your class here
}
Explanation / Answer
JAVA PROGRAM :
import java.io.*;
import java.util.*;
class MyInteger {
int val;
MyInteger(int val) {
this.val = val;
}
int getVal() {
return val;
}
boolean isEven() {
return val % 2 == 0;
}
boolean isOdd() {
return !isEven();
}
boolean isPrime() {
if (val % 2 == 0) {
return false;
}
int x = (int) Math.sqrt(val);
for (int i = 3; i <= x; i += 2) {
if (val % i == 0) {
return false;
}
}
return true;
}
static boolean isEven(int val) {
return val % 2 == 0;
}
static boolean isOdd(int val) {
return !isEven(val);
}
static boolean isPrime(int val) {
if (val % 2 == 0) {
return false;
}
int x = (int) Math.sqrt(val);
for (int i = 3; i <= x; i += 2) {
if (val % i == 0) {
return false;
}
}
return true;
}
boolean equals(int x) {
return x == this.val;
}
boolean equals(MyInteger m) {
return m.val == this.val;
}
static int parseInt(char ch[]) {
int ans = 0;
for (int i = 0; i < ch.length; i++) {
ans = 10 * ans + ch[i] - 48;
}
return ans;
}
static int parseInt(String ch) {
int ans = 0;
for (int i = 0; i < ch.length(); i++) {
ans = 10 * ans + ch.charAt(i) - 48;
}
return ans;
}
}
public class Exercise10_03 {
public static void main(String args[]) throws IOException {
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));
}
}
OUTPUT :
n1 is even? false
n1 is prime? true
15 is prime? false
3539
3539
n2 is odd? false
45 is odd? true
n1 is equal to n2? false
n1 is equal to 5? true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.