In: Computer Science
irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface Mathematical (in the Mathematical.java file). These should have the following definitions:
You cannot change the signature for the method. This method is similar to the ones we've discussed in the lab and will return true or false depending on if the passed in integer values is prime or not, respectively. Return false if the invoker passes in a number less than 1. A prime number is one that is not evenly divisible by any other number. For example, if we have number 2, it should return True, and if we have number 55, it will return False. Some sample assertions (Note that for these assertions we've not added a. message, which is optional):
assert isPrime(2); assert isPrime(53); assert !isPrime(55); assert !isPrime(24); assert !isPrime(-37337);
Prime numbers form the basis of cryptography! Read into why this is a little bit. Really cool stuff.
assert sumOfSums(1) == 1; assert sumOfSums(3) == 10; assert sumOfSums(6) == 56; assert sumOfSums(25) == 2925; assert sumOfSums(-5) == 0;
This is first program:
public class HazMath implements Mathematical {
// Fill-in methods to implement the Mathematical interface
public boolean isPrime(int n)
{
return false;
}
public int sumOfSums(int n) {
return 0;
}
public static void main(String[] args)
{
HazMath HM=new HazMath();
assert HM.isPrime(3);
assert !HM.isPrime(-37337);
assert HM.sumOfSums(1) == 1;
assert HM.sumOfSums(-5) == 0;
System.out.println("All tests passed. VICTORY!");
}
}
This is the second program:
import java.lang.*;
public interface Mathematical
{
// Validate each Char
public boolean isPrime(int n);
// Validate each Char in row with size 3
public int sumOfSums(int n);
}
Dear Student,
Thanks for posting an interesting question of Java Programming language to implement two methods of Mathematical interface to find prime and to find sum of sums.
1. For the given query I have complete both the programs with proper comments. Please find below program screenshot of HazMath.java class with proper comments.
2. Please find below complete Mathematical.java interface file screenshot:-
3. Please find below HazMath.java class file contents in text format with proper comments:-
public class HazMath implements Mathematical {
// Fill-in methods to implement the
Mathematical interface
public boolean isPrime(int n){
//If the number is less than equal
to 1 then it is not prime and return false
if(n<=1)
return
false;
//First set flagIsPrime
to true
boolean
flagIsPrime=true;
/*
* Start this loop from 2 as all the
numbers are divisible by 1 but we need to
* check further And check till the
half of the n i.e. till (n/2)
* as more than n/2 will never
divide the n
*/
for(int i = 2; i <= n/2; i++)
{
//Is
n is divisible by i then set flagIsPrime=false i.e. not a prime
number and come out to the loop
if(n % i == 0)
{
flagIsPrime=false;
break;
}
}
return flagIsPrime;
}
public int sumOfSums(int n) {
//Initialize sum with 0
int sum=0;
//Iterate loop to find
sum upto n
for(int i=1;i<=n;i++) {
//Iterate loop
to find sum upto i i.e. sum of sums
for(int
j=1;j<=1;j++) {
//Add the value of j with sum
sum=sum+j;
}
}
//returning the value of sum
return sum;
}
public static void main(String[] args){
HazMath HM=new HazMath();
assert HM.isPrime(3);
assert !HM.isPrime(-37337);
assert HM.sumOfSums(1) == 1;
assert HM.sumOfSums(-5) == 0;
assert HM.sumOfSums(3) == 10;
System.out.println("All tests
passed. VICTORY!");
}
}
4. Please find below Mathematical.java interface contents in text format for ready copying:-
import java.lang.*;
public interface Mathematical{
// Validate each Char
public boolean isPrime(int n);
// Validate each Char in row with size 3
public int sumOfSums(int n);
}
5. Please find below output screenshot of the program after placing both the files at D:/. :-
6. Please note that I have added one extra test case in main method i.e. assert HM.sumOfSums(3) == 10;.
7. Main part of the code is the implementation logic of both the methods, i.e. isPrime and sumOfSums. So, I made that part of the code in bold.
8. Though, I put comments to each of the line of the both the methods. Still if you have any query please feel free to reach me through comments section. I will be more than happy to help you further.
9. Please upvote by pressing thumb's up if you like my answer.
Thanks!!
Happy Learning!!