Question

In: Computer Science

irst, you will complete a class called HazMath (in the HazMath.java file) that implements the interface...

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:

  • public boolean isPrime(int n)

    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.

  • public int sumOfSums(int n)
    You cannot change the signature for the method. This method takes an integer, n, and computes the sum of each of the sums of each integer between 1 and n. For example, the sum of the sums of 3 is (1) + (1 + 2) + (1 + 2 + 3) = 10. Some sample assertions:
    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);

}

Solutions

Expert Solution

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!!


Related Solutions

If a class A implements interface I1 and class C and D are derived from class...
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct 1. A, C, and D 2. A and D
4) Define an abstract class Name Java class that implements interface Comparable   
4) Define an abstract class Name Java class that implements interface Comparable   
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The...
3.2. Unfortunately, you cannot modify the Rectangle class so that it implements the Comparable interface. The Rectangle class is part of the standard library, and you cannot modify library classes. Fortunately, there is a second sort method that you can use to sort a list of objects of any class, even if the class doesn't implement the Comparable interface. Comparator<T> comp = . . .; // for example, Comparator<Rectangle> comp = new RectangleComparator(); Collections.sort(list, comp); Comparator is an interface. Therefore,...
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a...
Develop a JavaFX GUI application called Registration that implements a user interface for registering for a web site. The application should have labeled text fields for the Full Name, User Name, Password, Student Id, and a TextAreafor About Me. Include a button labeled Send. When the Send button is clicked, your program should print the contents of all fields (with labels) to standard output using println()statements.
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in...
Define a class of queues that implements the interface QueueInterface, as defined in Listing 7-1 in Chapter 7. Use an instance of the class ArrayList to contain a queues entries. Then write a driver/test program adequately demonstrates your new class. Note that you might have to handle exceptions thrown by methods of ArrayList. Deliverables: EmptyQueueException.java (given in Lab_7_StartUp folder) QueueInterface.java (given in Lab_7_StartUp folder) ArrayListQueue.java (need to create) Lab7.java (need to create) QueInterface: @author Frank M. Carrano @author Timothy M....
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with...
In Java: Job class The Job implements the Comparable interface A Job object is instantiated with three int variables, indicating the arrivalTime, the timeForTheJob, and the priority. When the Job is created it is given the next sequential ID starting from 1. (You should use a static variable to keep track of where you are in ID assignment.) There are also int variables for startTime, waitTime (in queue) and endTime for the Job. The following methods are required: getID, set...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode...
Write a class "LinkedBag" which implements this interface. The LinkedBag class has two instance variables firstNode and numberOfEntries. It has an inner class Node. BagInterface.java /** An interface that describes the operations of a bag of objects. @author Frank M. Carrano @author Timothy M. Henry @version 4.1*/public interface BagInterface<T>{ /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ public int getCurrentSize(); /** Sees whether this bag is empty....
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class...
Directions: Create a Bluej project called Exam1 that implements the Headphone class and the HeadphoneInventory class described below. Each class is worth 50 points. When you are finished, zip the project folder and submit it. The Headphone class represents a headphone. It stores the headphone’s manufacturer: manufacturer, name: name, quantity: quantity, number of times restocked: timesRestocked. Write the Headphone class according to the following requirements. Add the four fields to the Headphone class. Create a constructor that takes two parameters....
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative...
Complete the // TODO sections in the EasyRental class. Create a method that implements an iterative sort that sorts the vehicle rentals by color in ascending order (smallest to largest) Create a method to binary search for a vehicle based on a color, that should return the index where the vehicle was found or -1 You are comparing Strings in an object not integers. Ex. If the input is: brown red white blue black -1 the output is: Enter the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT