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

I need help revising this code so that the if(true) are gone. I need to express

ID: 662321 • Letter: I

Question

I need help revising this code so that the if(true) are gone. I need to express this solution using negatioin, logic, if/else statements and boolean. Nothing fancy. This is a simply problem with a more simpler answer. Java language only and no arrays.

problem:

method monthApart accepts four integer parameters representing two calendar dates. Each date consists of a month (1 through 12) and a day (1 through the number of days in that month [28-31]). The method returns whether the dates are at least a month apart. Assume that all dates in this problem occur during the same year.

Here's the the desired output:

public static boolean anglePairs(int a, int b, int c)
   {
       boolean angleSup=false;
       boolean angleComp=false;
      
       if ((a+b)==180||(b+c==180)||(a+c)==180)
       {
           angleSup=true;
       }

       if((a+b)==90||(b+c==90)||(a+c)==90)
       {
           angleComp=true;
       }
     
       return(angleSup && angleComp);
   }

monthApart( 6, 14, 9, 21) true June 14th is at least a month before September 21st monthApart( 4, 5, 5, 15) true April 5th is at least a month before May 15th monthApart( 4, 15, 5, 15) true April 15th is at least a month before May 15th monthApart( 4, 16, 5, 15) false April 16th is NOT at least a month before May 15th monthApart( 6, 14, 6, 8) false June 14th is NOT at least a month apart from June 8th monthApart( 7, 7, 6, 8) false July 7th is NOT at least a month apart from June 8th monthApart( 7, 8, 6, 8) true July 8th is at least a month after June 8th monthApart( 10, 14, 7, 15) true October 14th is at least a month after July 15th

Explanation / Answer

/**
* The java program that tests the method monthApart that takes
* two months number of days, and day of the first month and day of the
* second month as input arguments and returns true if the difference between
* two months is a month apart otherwise returns false.
* From main method, call the method with given test cases, print
* if months difference is one month apart then print true
* otherwise print false.
* */
//MonthApartDriver.java
public class MonthApartDriver
{
   public static void main(String[] args)
   {
       //June 14th is at least a month before September 21st is true
       if(monthApart( 6, 14, 9, 21))
       {
           System.out.print("June 14th is at least a month before September 21st ");
           System.out.println("true");
       }
       else
       {
           System.out.print("June 14th is at least a month before September 21st ");
           System.out.println("false");
       }
      
       //April 5th is at least a month before May 15th is true
       if(monthApart( 4, 5, 5, 15))   
       {
           System.out.print("April 5th is at least a month before May 15th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("April 5th is at least a month before May 15th ");
           System.out.println("false");
       }
      
       //April 15th is at least a month before May 15th is true
       if(monthApart( 4, 15, 5, 15))
       {
           System.out.print("April 15th is at least a month before May 15th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("April 15th is at least a month before May 15th ");
           System.out.println("false");
       }
      
       //April 16th is NOT at least a month before May 15th is false
       if(monthApart( 4, 16, 5, 15))   
       {
           System.out.print("April 16th is at least a month before May 15th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("April 16th is at least a month before May 15th ");
           System.out.println("false");
       }
      
       //June 14th is NOT at least a month apart from June 8th is false
       if(monthApart( 6, 14, 6, 8))   
       {
           System.out.print("June 14th is at least a month apart from June 8th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("June 14th is at least a month apart from June 8th ");
           System.out.println("false");
       }
      
       //July 7th is NOT at least a month apart from June 8th is false
       if(monthApart( 7, 7, 6, 8))   
       {
           System.out.print("July 7th is at least a month apart from June 8th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("July 7th is at least a month apart from June 8th ");
           System.out.println("false");
       }
      
       //July 8th is at least a month after June 8th is true
       if(monthApart( 7, 8, 6, 8))   
       {
           System.out.print("July 8th is at least a month after June 8th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("July 8th is at least a month after June 8th ");
           System.out.println("false");
       }
      
       //October 14th is at least a month after July 15th is true
       if(monthApart( 10, 14, 7, 15))   
       {
           System.out.print("October 14th is at least a month after July 15th ");
           System.out.println("true");
       }
       else
       {
           System.out.print("October 14th is at least a month after July 15th ");
           System.out.println("false");
       }
   }
  
  
   /*The method monthApart that takes four input argumets of firstMonth number,
   *day of the firstmonth , otherMonth number and day of the other month
   *and checks if the number of days in both the months are a month apart
   *then returns true otherwise returns false
   * */
  
   public static boolean monthApart(int firstMonth, int startDay,
           int otherMonth, int endDay)
   {
        int monthDays = -1;
        int monthMin ;
        //find the minimum month of two given months
        if(firstMonth<otherMonth)
           monthMin=firstMonth;
        else
           monthMin=otherMonth;
        

        //check if the given number of month is in the range
        if ((firstMonth<1 || firstMonth >12) || (otherMonth>12 || firstMonth<1))
        {
            System.out.println("Out of range");
            //close the program
            System.exit(0);
        }

        //Note :Switch case is removed using if else case.Code reduced
        //set number of days in monthDays =31 if monthMin is 1 or 3 or 5 or 7 or 8 or 10 or 12
        if(monthMin==1 ||monthMin==3 ||monthMin==5 ||monthMin==7||
               monthMin==8 ||monthMin==10 ||monthMin==12)
           monthDays=31;
       //set number of days in monthDays =30 if monthMin is 4 or 6 or 9 or 11
        else if(monthMin==4 ||monthMin==6 ||monthMin==9 ||monthMin==11)
           monthDays=30;
        else
           //otherwise set monthDays =28
           monthDays=28;

          
        int dayFirst = -1;
      
        int daySecond = -1;
        //checking if the firstMonth is lesser month than the otherMonth
        //set startDay to dayFirst
        //set endDay to daySecond
        if (firstMonth<otherMonth)
        {
            dayFirst = startDay;
            daySecond = endDay;
        }
        else if (firstMonth>otherMonth)
        {
            dayFirst = endDay;
            daySecond = startDay;
        }

      
        // checking if the difference between two months is greater than 1
        //is that the two months are one month apart from each
        if (Math.abs(firstMonth-otherMonth)>1)
        {
           //return true
            return true;
        }
       // checking if the two months are same
        //is that no month apart between them
        else if (firstMonth==otherMonth)
        {
           // return false
            return false;
        }
       //checking if the difference between two months is one
        //is that the two months are one month apart from each
        else if (Math.abs(firstMonth-otherMonth)==1)
        {
           // Get the remainging days in lesser month month and add number of days
           //in othermonth and check if the sum is less than the monthDays of the
           //same month or firstMonth then there is no month apart
            if (((monthDays-dayFirst) + daySecond) < monthDays)
            {
               // returns false
                return false;
            }
            else
            {
               //returns true
                return true;
            }
        }
        //If no above case then returns false
        return false;
   }
}//end of class

---------------------------------------------------------------------------------------------------------------------------------------

sample output:

June 14th is at least a month before September 21st true
April 5th is at least a month before May 15th true
April 15th is at least a month before May 15th true
April 16th is at least a month before May 15th false
June 14th is at least a month apart from June 8th false
July 7th is at least a month apart from June 8th false
July 8th is at least a month after June 8th true
October 14th is at least a month after July 15th true

Hope this helps you

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote