Hello, we are using Eclipse and would like help on this, please. 1) Design a cla
ID: 3881213 • Letter: H
Question
Hello, we are using Eclipse and would like help on this, please.
1) Design a class named MyInteger. The class contains An int data field named value that stores the int value represented by this object constructor that creates a MyInteger object for the specified int value A get method that returns the int value Methods isveOdd0, and isPriime0 that return true if the value is even, odd, or prime, respectively . A · . 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 MvInteger, isOdd MInteger), and isPrime MvInteger that return true if the specified value is even, odd, or prime, respectively Methods equals(int) and equalsMy nteger) that return true if the value in the object is equal to the specified value .A static method parselInt(charD that converts an array of numeric characters to an int value . A static method parselnt (String) that converts a string into an int value Draw the UML diagram for the class. Implement the class. Write a client program that tests all methods in the class Write a program that displays all the prime numbers less than 120 in decreasing order 2) 3) The class Ticket has been coded as follows public class Ticket private double price: private char service public Ticket double newBrice, char newService set Pricenewrice) setService( newService );Explanation / Answer
Hello, I have answers for your question. Please note that, you should not post so many questions together like this. Instead, you should divide questions into smaller units and post separately.
If you have any doubts, drop a comment. Thanks.
Answer for first question
Implemented all required methods and created a Test class to test the working of MyInteger class
//MyInteger.java
public class MyInteger {
private int value;
// constructor
public MyInteger(int value) {
this.value = value;
}
// getter method
public int getValue() {
return value;
}
/**
* method to check if value is even
*/
public boolean isEven() {
if (value % 2 == 0) {
return true;
}
return false;
}
/**
* method to check if value is odd
*/
public boolean isOdd() {
if (value % 2 != 0) {
return true;
}
return false;
}
/**
* method to check if value is prime
*/
public boolean isPrime() {
/**
* checking if value is less than 2
*/
if (value < 2) {
return false;
}
/**
* looping through each numbers from 2 to value, and checking if any
* number divides it without leaving any remainder. if yes, its not a
* prime number.
*/
for (int i = 2; i < value; i++) {
if (value % i == 0) {
return false;
}
}
return true;
}
/**
* static method to check if a given value is even
*/
public static boolean isEven(int n) {
if (n % 2 == 0) {
return true;
}
return false;
}
/**
* static method to check if a given value is odd
*/
public static boolean isOdd(int n) {
if (n % 2 != 0) {
return true;
}
return false;
}
/**
* static method to check if a given value is prime
*/
public static boolean isPrime(int n) {
/**
* checking if value is less than 2
*/
if (n < 2) {
return false;
}
/**
* looping through each numbers from 2 to value, and checking if any
* number divides it without leaving any remainder. if yes, its not a
* prime number.
*/
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* static method to check if a given MyInteger object is even
*/
public static boolean isEven(MyInteger m) {
if (m.getValue() % 2 == 0) {
return true;
}
return false;
}
/**
* static method to check if a given MyInteger object is odd
*/
public static boolean isOdd(MyInteger m) {
if (m.getValue() % 2 != 0) {
return true;
}
return false;
}
/**
* static method to check if a given MyInteger object is prime
*/
public static boolean isPrime(MyInteger m) {
/**
* checking if value is less than 2
*/
int n = m.getValue();
if (n < 2) {
return false;
}
/**
* looping through each numbers from 2 to value, and checking if any
* number divides it without leaving any remainder. if yes, its not a
* prime number.
*/
for (int i = 2; i < n; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
/**
* method to check if n is equal to value
*/
public boolean equals(int n) {
if (value == n) {
return true;
}
return false;
}
/**
* static method to check if a given MyInteger object is equal to current object
*/
public boolean equals(MyInteger m) {
if (value == m.getValue()) {
return true;
}
return false;
}
/**
* method to parse a char array to an integer and return it
*/
public static int parseInt(char[] array) {
int num = 0;// total
int x = 1;// unit
for (int i = array.length - 1; i >= 0; i--) {
/**
* getting the numeric value of character
*/
int n = Character.getNumericValue(array[i]);
/**
* Adding the value to the total, here x will increment by 10 times
* each time, i.e it represent the unit, tenth, hundredth..etc
* position
*/
num += n * x;
x = x * 10;
}
return num;
}
/**
* method to parse a String to an integer and return it
*/
public static int parseInt(String str) {
/**
* Converting String to character array and calls the parseInt(char[])
* method
*/
char[] char_array = str.toCharArray();
return parseInt(char_array);
}
}
//Test.java
public class Test {
public static void main(String[] args) {
/**
* Testing MyInteger class
*/
int a=MyInteger.parseInt(new char[]{'1','2','3','4'});
int b=MyInteger.parseInt("755253");
System.out.println("a= "+a);
System.out.println("b= "+b);
/**
* Creating MyInteger object from a
*/
MyInteger myInteger=new MyInteger(a);
System.out.println("a is even?: "+myInteger.isEven());
System.out.println("a is prime?: "+myInteger.isPrime());
System.out.println("5 is prime?: "+MyInteger.isPrime(5));
System.out.println("a equals 5?: "+myInteger.equals(5));
}
}
/*OUTPUT*/
a= 1234
b= 755253
a is even?: true
a is prime?: false
5 is prime?: true
a equals 5?: false
Answer for second question (displaying prime numbers under 120)
I have used the help of the above MyInteger class to print all the prime numbers under 120. Following is the PrintPrime class to fulfil the above requirements
//PrintPrime.java
public class PrintPrime {
public static void main(String[] args) {
System.out.println("Prime numbers under 120:");
for(int i=120;i>=2;i--){
/**
* Checking if i is prime using MyInteger class method isPrime()
*/
if(MyInteger.isPrime(i)){
System.out.println(i);
}
}
}
}
/*OUTPUT*/
Prime numbers under 120:
113
109
107
103
101
97
89
83
79
73
71
67
61
59
53
47
43
41
37
31
29
23
19
17
13
11
7
5
3
2
Answers for third question (Ticket class)
Everything is implemented according to the needs, and here is the completed Ticket.java class, and the testing class Client.java
//Modified Ticket.java class
public class Ticket {
public static char DEFAULT_SERVICE='B';
private double price;
private char service;
public Ticket(double newPrice, char newService) {
setPrice(newPrice);
setService(newService);
}
/**
* completed setter methods
*/
public void setPrice(double price) {
if (price < 0) {
this.price = 0;
} else {
this.price = price;
}
}
public void setService(char service) {
if (service != 'A' && service != 'B') {
/**
* if service is not A or B , setting default
*/
service = DEFAULT_SERVICE;
} else {
this.service = service;
}
}
/**
* completed getter methods
*/
public double getPrice() {
return price;
}
public char getService() {
return service;
}
/**
* method to switch services
*/
public void switchService() {
if (service == 'A') {
service = 'B';
} else {
service = 'A';
}
}
@Override
public String toString() {
return String.format("%c:%.2f", service, price);
}
/**
* method to calculate tax and returns it. datatype of parameter is float
* and return type of the method is double
*/
public double tax(float rate) {
return price*rate;
}
}
//Client.java class
public class Client {
public static void main(String[] args) {
/**
* creating a Ticket instance
*/
Ticket myTicket=new Ticket(34.99, 'A');
/**
* Displaying it
*/
System.out.println(myTicket);
float taxRate=0.06f;
/**
* Calculating the tax, answer for question 'f'
*/
double myTax=myTicket.tax(taxRate);
System.out.println("Tax: "+myTax);
/**
* displaying the default ticket service, answer for the last question
*/
System.out.println("Default service: "+Ticket.DEFAULT_SERVICE);
}
}
/*OUTPUT*/
A:34.99
Tax: 2.0993999530747534
Default service: B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.