Question

Refactor the following program to use ArrayList instead of Arrays. You can google "Java ArrayList" or...

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');

}
}

Homework Answers

Answer #1
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');

    }
}
Know the answer?
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
Similar Questions
Fix the program: what if the input is three? import java.util.Scanner public class Test { public...
Fix the program: what if the input is three? import java.util.Scanner public class Test { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter an integer: "); int m = in.nextInt(); System.out.print("Enter another integer: "); int n = in.nextInt(); System.out.println(m + " " + n); } }
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the...
Covert the following Java program to a Python program: import java.util.Scanner; /* Calculates and displays the area of a rectangle * based on the width and length entered by the user. */ public class RectangleArea2 {             public static void main(String[] args) { int length; //longer side of rectangle             int width; //shorter side of rectangle int area; //calculated area of rectangle Scanner input = new Scanner(System.in);                               System.out.print("Enter the length: ");            length = input.nextInt(); System.out.print("Enter...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65...
//please debug program // need Scanner output import java.util.Scanner; // Display every character between Unicode 65 and 122 // Start new line after 20 characters public class DebugSix2 { public static void main(String args[]) { Scanner sc = new Scanner(System.in); char letter; int a; final int MIN = 65; final int MAX = 122; final int NUMPERLINE = 200; final int STOPLINE1 = 0; final int STOPLINE2 = STOPLINE1 + NUMPERLINE; for(a = MIN; a <= MAX; a++) { letter...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average...
Covert the following Java program to a Python program: import java.util.Scanner; /** * Displays the average of a set of numbers */ public class AverageValue { public static void main(String[] args) { final int SENTINEL = 0; int newValue; int numValues = 0;                         int sumOfValues = 0; double avg; Scanner input = new Scanner(System.in); /* Get a set of numbers from user */ System.out.println("Calculate Average Program"); System.out.print("Enter a value (" + SENTINEL + " to quit): "); newValue =...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is...
/* Program Name: BadDate.java Function: This program determines if a date entered by the user is valid. Input: Interactive Output: Valid date is printed or user is alerted that an invalid date was entered. */ import java.util.Scanner; public class BadDate { public static void main(String args[]) { // Declare variables Scanner userInput = new Scanner (System.in); String yearString; String monthString; String dayString; int year; int month; int day; boolean validDate = true; final int MIN_YEAR = 0, MIN_MONTH = 1,...
I wrote the following java code with the eclipse ide, which is supposed to report the...
I wrote the following java code with the eclipse ide, which is supposed to report the number of guesses it took to guess the right number between one and five. Can some repost the corrected code where the variable "guess" is incremented such that the correct number of guesses is displayed? please test run the code, not just look at it in case there are other bugs that exist. import java.util.*; public class GuessingGame {    public static final int...
Using Java in this task, you will use the switch statement to provide your activity of...
Using Java in this task, you will use the switch statement to provide your activity of whole week. First, you will ask to provide the number of the day, where number of a day means: 1 = Monday 2 = Tuesday 3 = Wednesday 4 = Thursday 5 = Friday 6 = Saturday 7 = Sunday When the user provides a day number then you will print the statement that shows your lectures on that day and your other activities...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a java program that calculates the number of duplicate words in a sentence. You may...
Write a java program that calculates the number of duplicate words in a sentence. You may assume that the input consists only 52 English characters with a single white-space as the separator between words (not punctuations in the inputted sentence). You need consider uppercase and lowercase letters the same. You may get the input from the keyboard. (You need to use Set and/or Hashset). What I have so far import java.util.HashSet; import java.util.Scanner; public class Duplicate { int counter; int...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...