Question

debug the program in java /** This program demonstrates an array of String objects. It should...

debug the program in java

/**
This program demonstrates an array of String objects.
It should print out the months and each day of the month.
It should print out the month name and days for the month with the least days
It should print out the average number of days in each month
Here is what it should print when executed:
January has 31 days.
February has 28 days.
March has 31 days.
April has 30 days.
May has 31 days.
June has 30 days.
July has 31 days.
August has 31 days.
September has 30 days.
October has 31 days.
November has 30 days.
December has 31 days.
Lowest Month is February with 28 days
Average days in each month is 30.42 days
*/

public class MonthDays
{
public static void main(String[] args)
{
String[] months = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };

int[] days = { 31, 28, 31, 30, 31, 30, 31,
31, 30, 31, 30, 31 };

double average = 0.0;
int lowest = 0;
String lowestMonth = " ";
int sum = 0;

for (int index = 0; index <= months.length; index++)
{
System.out.println(months[index] + " has " + days[index] + " days.");
}

lowest = days[0];

for (int index = 0; index < months.length; index++)
{
if (days[index] >= lowest)
{
    lowest = days[index];
    lowestMonth = months[index];
sum = sum + days[index];

}
average = sum / 12;
}
System.out.println("Lowest Month is " + lowestMonth + " with " + lowest + " days");
System.out.printf("Average days in each month is %4d days", average);

}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You Did small mistakes...

in for loop, if condition

Program :

public class MonthDays
{
public static void main(String[] args)
{
String[] months = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };

int[] days = { 31, 28, 31, 30, 31, 30, 31,31, 30, 31, 30, 31 };
double average = 0.0;
int lowest = 0;
String lowestMonth = " ";
double sum = 0;

for (int index = 0; index < months.length; index++)
{
System.out.println(months[index] + " has " + days[index] + " days.");
}

lowest = days[0];

for (int index = 0; index <months.length; index++)
{
if (days[index] <= lowest)
{
lowest = days[index];
lowestMonth = months[index];
}
sum = sum + days[index];
}
average = sum / 12;
System.out.println("Lowest Month is " + lowestMonth + " with " + lowest + " days");
System.out.printf("Average days in each month is %4f days", average);

}
}

Screen shot of the program

Output:


Add a comment
Know the answer?
Add Answer to:
debug the program in java /** This program demonstrates an array of String objects. It should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on

    # JAVA In the following program, declare an integer array daysInMonth that stores the number of days in January, February, March, April, and so on. Fill it with the appropriate values (31, 28, 31, 30, ...).The program reads a month and year from the user.When the year is a leap year, change the entry for February to 29.Print out how many days the given month has.CODE:import java.util.Scanner;public class NumberOfDays{   public static void main(String[] args)   {      // Declare and initialize daysOfMonth      ....

  • (InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user...

    (InputMismatchException) and (ArrayIndexOutOfBoundsException): Using the two arrays shown below, write a program that prompts the user to enter an integer between 1 and 12 and then displays the months and its number of days corresponding to the integer entered. 1. Your program should display “Wrong number - Out of Bounds” if the user enters a wrong number by catching ArrayIndexOutOfBoundsException. 2. Also the program should display “Mismatch – not a number” if the user enters anything other than an integer...

  • Write a Java console application that reads a string from the keyboard and tests whether it...

    Write a Java console application that reads a string from the keyboard and tests whether it contains a valid date. Display the date and a message that indicates whether it is valid. If it is not valid, also display a message explaining why it is NOT valid. The input date will have the format mm/dd/yyyy. A valid month value mm must be from 1 to 12 (January is 1). You can use: String monthPart = inputDate.substring(0, 2); int month =...

  • Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the...

    Java Programming: Hi, I need help Modifying my code that will throw an IllegalArgumentException. for the following data entry error conditions: A number less than 1 or greater than 12 has been entered for the month A negative integer has been entered for the year utilizes a try and catch clause to display an appropriate message when either of these data entry errors exceptions occur. Thank you let me know if you need more info import java.util.*; //Class definition public...

  • How to add exceptions to this code for each month so that it won't exceed the...

    How to add exceptions to this code for each month so that it won't exceed the amount of days that in each one for them. Example there are only 28 days in feb and 31 days in march. import java.sql.Timestamp; public class RandomDate { public static void main(String[] args) { long begin=Timestamp.valueOf("1900-01-01 00:00:00").getTime(); long end=Timestamp.valueOf("2014-12-31 00:00:00").getTime(); long diff=end-begin+1; Timestamp rand=new Timestamp(begin+(long)(Math.random()*diff));    int month=rand.getMonth()+1,day=rand.getDate(),year=rand.getYear()+1900;    String[] months={"Janunary","February","March","April","May","June","July","August","September","October","November","December"}; String suffix="th"; if(day==2)suffix="nd"; else if(day==3)suffix="rd"; else if(day==1)suffix="st";    System.out.println("Random Date is: "+month+"/"+day+"/"+year); System.out.println("Formatted...

  • Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then...

    Copy all the classes given in classes Calendar, Day, Month, & Year into BlueJ and then generate their documentation. Examine the documentation to see the logic used in creating each class. (Code given below) import java.util.ArrayList; import java.util.Iterator; class Calendar { private Year year; public Calendar() { year = new Year(); } public void printCalendar() { year.printCalendar(); } } class Year { private ArrayList<Month> months; private int number; public Year() { this(2013); } public Year(int number) { this.number = number;...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March","April", "May", "June", "July", "August", "September", days_in_month = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31) where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day number...

  • Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a...

    Define the is_a_valid_date() function which is passed a string as a parameter. The function returns a boolean indicating whether the parameter string is a valid date or not. The first two lines of the function are: month_names = ["January", "February", "March", "April", "May", "June", "July", "August", "September", days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] where month_names is a list of valid month names, and days_in_month is a list which contains the maximum day...

  • IN PYTHON, Write a program that reads a string from the user containing a date in...

    IN PYTHON, Write a program that reads a string from the user containing a date in the form mm/dd/ yyyy. It should print the date in the form April 12, 2017. Hint: Get the "mm" from the user entered date "mm/dd/yyyy", then convert the "mm" into a month number. You may use an list month_list = ['January', 'February','March','April', 'May','June', 'July','August', 'September', 'October', 'November', 'December']. Use the month number you got from "mm" to get the month name.

  • C++ Write a program that illustrates enumeration types. Define an enumeration type for the months of...

    C++ Write a program that illustrates enumeration types. Define an enumeration type for the months of the year. This should be global. Put it above the main program. Declare a variable of the enumerated type called month; Declare an array of unsigned integers called year[12] and initialize the array to the days in each month. (Just assume 28 for February.) 1. Write a loop that will progress from January through December printing out the days in each month. The index...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT