Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or start with the link below:
https://www.thoughtco.com/using-the-arraylist-2034204
import java.util.Scanner;
public class DaysOfWeeks {
public static void main(String[] args) {
String DAY_OF_WEEKS[] =
{"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
char ch;
int n;
Scanner scanner = new Scanner(System.in);
do {
System.out.print("Enter the day of the Week: ");
n = scanner.nextInt() - 1;
if (n >= 0 && n <= 6)
System.out.println("The day of the week is " + DAY_OF_WEEKS[n] +
".");
else
System.out.println("Invalid Entry");
System.out.print("Try again (Y/N): ");
ch = scanner.next().charAt(0);
}while(ch=='Y');
}
}
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { ArrayList<String> DAY_OF_WEEKS = new ArrayList<String>(Arrays.asList("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")); char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS.get(n) + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); } while (ch == 'Y'); } }
Get Answers For Free
Most questions answered within 1 hours.