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

Here is my code which does not work. Please help me with this problem. Write the

ID: 3812638 • Letter: H

Question

Here is my code which does not work. Please help me with this problem.

Write the function readable-interval that takes a string consisting of a human-readable time interval Homework #3 CSE 101 Spring 2017 Page 3 and converts it into a list of six integers, given in this order: of years, of months, t of days, #of hours, of minutes, of seconds] The input values stored within the string can be quite scrambled and consist of multiple, repeated time components (days, months, etc.), as shown in the examples below. If a time component appears more than once, the values are be added. The time units might be given in singular or plural form (e.g., "month" and "months" are both valid). There will always be at least (but not necessarily exactly) one space between the tokens (i.e., components) of the string. Examples: Function Call: readable-interval 1 year 2 months 3 seconds') Return Value: 1, 2, 0, 0, 0, 31 Function Call: readable-interval 1 second 2 years 3 seconds 7 months 6 days' Return Value: [2, 7, 6, 0, 0, 4 Function Call 4 hours 17 minutes') readable interval 1 days 4 days 3 years Return Value: 13, 0, 5, 4, 17, 01 Function Call readable interval 6 hours 20 minutes 421 seconds Return Value: [0, 0, 0, 6, 20 421] Function Call readable interval 0 days 0 months 7 months 18 years') Return Value: 18 7, 0, 0, 0, 01

Explanation / Answer

package myProject;
import java.util.*;
//Class StringOperations definition
public class StringOperations
{
   //Calculates and returns an array having year, month, day, hour, minute and second
   int [] redable_interval(String interval)
   {
       //Creates an array of size 6
       int intervals[] = new int[6];
       //Split data on space
       String value[] = interval.split(" ");
       //Loops till end of string
       for(int i = 0; i < value.length; i++)
       {
           //Check for year or years
           if(value[i].compareTo("year") == 0 || value[i].compareTo("years") == 0)
               //Add years data
               intervals[0] += Integer.parseInt(value[i-1]);
           //Check for month or months
           else if(value[i].compareTo("month") == 0 || value[i].compareTo("months") == 0)
               //Add months data
               intervals[1] += Integer.parseInt(value[i-1]);
           //Check for day or days
           else if(value[i].compareTo("day") == 0 || value[i].compareTo("days") == 0)
               //Add days data
               intervals[2] += Integer.parseInt(value[i-1]);
           //Check for hour or hours
           else if(value[i].compareTo("hour") == 0 || value[i].compareTo("hours") == 0)
               //Add hours data
               intervals[3] += Integer.parseInt(value[i-1]);
           //Check for minute or minutes
           else if(value[i].compareTo("minute") == 0 || value[i].compareTo("minutes") == 0)
               //Add minutes data
               intervals[4] += Integer.parseInt(value[i-1]);
           //Check for second or seconds
           else if(value[i].compareTo("second") == 0 || value[i].compareTo("seconds") == 0)
               //Add seconds data
               intervals[5] += Integer.parseInt(value[i-1]);          
       }//End of loop
       //Returns array
       return intervals;
   }//End of method
  
   //Main method
   public static void main(String ss[])
   {
       //Creates scanner class object to accept data
       Scanner sc = new Scanner(System.in);
       //Creates an integer array of size 6 to store the result
       int res [] = new int[6];
       String interval;

//Creates an object of the class StringOperations
       StringOperations so = new StringOperations();
       //Accepts data
       System.out.println("Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]" );
       interval = sc.nextLine();
       res = so.redable_interval(interval);
       //Display the result
       System.out.println("[ " + res[0] + ", " + res[1] + ", " + res[2] + ", " + res[3] + ", " + res[4] + ", " + res[5] + " ]");
   }//End of main
}//End of class

Sample Run 1:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
1 year 2 months 3 seconds
[ 1, 2, 0, 0, 0, 3 ]

Sample Run 2:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
1 second 2 years 3 seconds 7 months 6 days
[ 2, 7, 6, 0, 0, 4 ]

Sample Run 3:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
1 days 4 days 3 years 4 hours 17 minutes
[ 3, 0, 5, 4, 17, 0 ]

Sample Run 4:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
6 hours 20 minutes 421 seconds
[ 0, 0, 0, 6, 20, 421 ]

Sample Run 5:

Enter the data in given order [# of years # of months # of days # of hours # of minutes # of seconds]
0 days 0 months 7 months 18 years
[ 18, 7, 0, 0, 0, 0 ]

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