Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am getting an error saying \"Selection does not have a main type\" can someone

ID: 3819598 • Letter: I

Question

I am getting an error saying "Selection does not have a main type" can someone please help.

public class Int {

private int x;

public Int(int x) {

this.x = x;

}

@Override

public String toString() {

return this.toString();

}

public Int plus(int x) {

int result = this.x + x;

if (result < Integer.MIN_VALUE) {

throw new RuntimeException("Integer Underflow");

}

if (result > Integer.MAX_VALUE) {

throw new RuntimeException("Integer Overflow");

}

return new Int(result);

}

public Int minus(int x) {

int result = this.x - x;

if (result < Integer.MIN_VALUE) {

throw new RuntimeException("Integer Underflow");

}

if (result > Integer.MAX_VALUE) {

throw new RuntimeException("Integer Overflow");

}

return new Int(result);

}

public Int times(int x) {

int result = this.x * x;

if (result < Integer.MIN_VALUE) {

throw new RuntimeException("Integer Underflow");

}

if (result > Integer.MAX_VALUE) {

throw new RuntimeException("Integer Overflow");

}

return new Int(result);

}

public Int div(int x) {

Int a = null;

try {

a = new Int(this.x / x);

} catch (ArithmeticException e) {

System.out.println("Divide by Zero Error: " + e.toString());

}

return a;

}

public boolean isPrime() {

int num = this.x;

boolean flag = true;

for (int i = 2; i < num; i++) {

if (num % i == 0) {

flag = false;

break;

}

}

if (flag) {

System.out.println(num + " is a Prime Number");

}

return flag;

}

}

Explanation / Answer

There were actually two errors in the program.

1. You have not defined any main function to run. As every program must have a main function specified which will be run on top. I have added a sample main function in your code.

2. Your toString method is not right, as it will result in a loop. Because on calling of toString() method you are returning a call to same toString method resulting in StackOverflowException.

All the changes which I have done are in bold: -

package debug;

public class Int {

private int x;

public Int(int x) {
this.x = x;
}

@Override
public String toString() {
return new Integer(this.x).toString();

//parsing the primitive int to Integer object and then calling toString() on it
}

public Int plus(int x) {
int result = this.x + x;
if (result < Integer.MIN_VALUE) {
throw new RuntimeException("Integer Underflow");
}
if (result > Integer.MAX_VALUE) {
throw new RuntimeException("Integer Overflow");
}

return new Int(result);
}

public Int minus(int x) {
int result = this.x - x;
if (result < Integer.MIN_VALUE) {
throw new RuntimeException("Integer Underflow");
}
if (result > Integer.MAX_VALUE) {
throw new RuntimeException("Integer Overflow");
}

return new Int(result);
}

public Int times(int x) {
int result = this.x * x;
if (result < Integer.MIN_VALUE) {
throw new RuntimeException("Integer Underflow");
}
if (result > Integer.MAX_VALUE) {
throw new RuntimeException("Integer Overflow");
}

return new Int(result);
}

public Int div(int x) {
Int a = null;
try {
a = new Int(this.x / x);
} catch (ArithmeticException e) {
System.out.println("Divide by Zero Error: " + e.toString());
}
return a;
}

public boolean isPrime() {
int num = this.x;
boolean flag = true;
for (int i = 2; i < num; i++) {
if (num % i == 0) {
flag = false;
break;
}
}
if (flag) {
System.out.println(num + " is a Prime Number");
}
return flag;
}
  
public static void main(String[] args) {
       Int a = new Int(12); // a object defined with x = 12
      
       // calling of all functions and printing the results.
       System.out.println("Is Prime: "+a.isPrime());
       System.out.println("Divide by 3:"+ a.div(3));
       System.out.println("3 times x:"+ a.times(3));
       System.out.println("subtracting 4:"+a.minus(4));
       System.out.println("adding 4:"+a.plus(4));
   }

}

Output: -

Is Prime: false
Divide by 3:4
3 times x:36
subtracting 4:8
adding 4:16