1) Design a class named MyInteger. The class contains: - An int data field named
ID: 3664424 • Letter: 1
Question
1) 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.
Draw the UML diagram for the class. Implement the class. Write a client program that tests
all methods in the class.
2) Write a program that displays all the prime numbers less than 120 in decreasing order.
3) The class Ticket has been coded as follows:
public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
}
a) Write the mutators (setPrice and setService methods) for the Ticket class; the price must be greater than or equal to 0; the service must be either A or B, the default service is B.
b) Write the accessor (getPrice and getService methods) for the Ticket class.
c) Code a method that switches the value of service: if it is A, it changes to B; if it is B, it changes to A.
d) Code the toString method: it returns the service and price separated by a : (colon) as in the following examples:
Example 1: B:34.99
Example 2: A:94.99
Example 3: B:44.99
e) What is the data type of the parameter and the return type of the tax method? public double tax( float rate )
f) In a client class and inside the main method, myTicket is an object reference of type Ticket; call the method tax with myTicket, assuming a tax rate of 0.06, and assign the resulting tax value to a variable named myTax.
float taxRate = 0.06f;
g) Consider the following constant of class Ticket
public static char DEFAULT_SERVICE = ‘B’;
h) In a client class and inside main, write a statement to output the value of the above constant.
Explanation / Answer
Solution of 1)
import java.util.Iterator;
public class myinteger {
int value;
public myinteger(int value)
{
this.value=value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
boolean Even(int value)
{
if(value%2==0)
{
return true;
}
else
{
return false;
}
}
boolean isOdd()
{
if(value%2==1)
{
return true;
}
else
{
return false;
}
}
boolean isPrime(int value)
{
int flag=0;
for (int i = 2; i < value/2; i++) {
if(value%i==0)
{
flag=1;
}
}
if(flag==1)
{
return false;
}
else if(flag==0)
{
return true;
}
return false;
}
public static int parseInt(char[] str)
{
StringBuilder strs=new StringBuilder();
for (int i = 0; i < str.length; i++) {
strs.append(str[i]);
}
return Integer.parseInt(str.toString());
}
public static int parseInt(String str)
{
return Integer.parseInt(str);
}
public static void main(String[] args) {
}
}
2)Solution of 2
To find prime numbers less than 120
3)public class Ticket
{
private double price;
private char service;
public Ticket( double newPrice, char newService )
{
setPrice( newPrice );
setService( newService );
}
public double getPrice() {
return price;
}
public char getService() {
return service;
}
private void setService(char newService) {
// TODO Auto-generated method stub
}
private void setPrice(double newPrice) {
// TODO Auto-generated method stub
}
private void getService(char newService) {
// TODO Auto-generated method stub
}
private void getPrice(double newPrice) {
// TODO Auto-generated method stub
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "B:34.99";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.