IDNLearn.com provides a user-friendly platform for finding answers to your questions. Ask any question and get a thorough, accurate answer from our community of experienced professionals.
Sagot :
Program Explanation:
- Import package.
- Defining a class ToDoList.
- Inside the class create an "ArrayList" class object to add, remove the value in the list.
- For this "addItem, deleteItem, and listAll" method is declared, in which first two method uses a variable to "add and remove" value and last method "listAll" uses loop to print its value.
- In the next step, a Main class with the main method is declared, which creates the "ToDoList" class object.
- After creating its object a loop is defined that prints message and uses multiple conditional statements to add, remove and show list values.
Program:
import java.util.*;//import package
class ToDoList//defining a class ToDoList
{
ArrayList<String> To = new ArrayList<String>(); //creating an ArrayList class object
public void addItem(String i) //defining a method addItem that takes a sting parameter
{
this.To.add(i);//use this to hold or add value into ArrayList }
public void deleteItem(int n)//defining a method deleteItem that takes an integer parameter
{
this.To.remove(n);//use this to remove value into ArrayList
}
public void listAll()//defining a method listAll
{
for (int x = 0; x < this.To.size(); x++) //defining loop to print List value
{
System.out.println((x+1)+"."+this.To.get(x));//print value
}
}
}
public class Main //defining a class Main
{
public static void main(String[] ar)//defining main method
{
ToDoList to=new ToDoList(); //creating ToDoList class object
int f=0,o;//defining integer variable
while(f==0)//defining while loop to print message and calculate value
{
System.out.println("\n----------To do list----------\n");//print message
System.out.println("1. Add item ");//print message
System.out.println("2. Delete item ");//print message
System.out.println("3. List of todo Item ");//print message
System.out.println("4. Exit ");//print message
System.out.println("Enter your choice:");//print message
Scanner i=new Scanner(System.in); //creating Scanner class object
o=i.nextInt(); i.nextLine(); //input value
if(o==1)//defining if block to check input value equal to 1
{
System.out.println("Enter the item:");//print message
String it=i.nextLine();//defining String variable that input value
to.addItem(it);//add value into the ArrayList
System.out.println("1 item added!");//print message
}
else if(o = =2)//defining else if block to check input value equal to 2
{
to.listAll();//calling method listAll
System.out.println("Enter item number to delete");//print message
int n=i.nextInt();//defining integer variable that input value
to. deleteItem (n - 1);//calling method delete Item that remove value from ArrayList
System.out.println("1 item deleted!");//print message
}
else if(o==3)//defining else if block to check input value equal to 3
{
to.listAll();//calling method listAll that prints ArrayList
}
else
{
f=1;//use f variable that hold value 1 for LOOP beaking
}
}
}
}
Output:
Please find the attached file.
Learn more:
brainly.com/question/13437184
We are happy to have you as part of our community. Keep asking, answering, and sharing your insights. Together, we can create a valuable knowledge resource. Thank you for visiting IDNLearn.com. For reliable answers to all your questions, please visit us again soon.