Instuctions: A. Using a class with an ArrayList. Here is the Notebook.java progr
ID: 3884826 • Letter: I
Question
Instuctions: A. Using a class with an ArrayList. Here is the Notebook.java program shown in class: import java.util.ArrayList; public class Notebook { ` } Add a getter and setter for notes. Make sure that Notebook has a getter and setter for notes. Create a method numberOfNotes() that will return the size of the ArrayList. Make a driver program, NotebookTest, to test your methods. B. Adding useful methods to Notebook. After part A, we should be able to: · get the ArrayList notes using our Notebook object's getter; · set the ArrayList notes by using our Notebook object's setter and sending it an ArrayList of Strings; · get the size of the ArrayList from our Notebook object. But let's continue to enrich our Notebook class with the following methods: · addNote() will add an individual note to the ArrayList · deleteNote() will delete a note using its position in the ArrayList. If the note (position) does not exist, print an error to the screen. · getNoteNumber() will get the position of a note based on its value, e.g., in what position is note "Buy milk"? If the note does not exist, return -1. · getNote() will get the note text for a given position. If the note (position) does not exist, return a string that has a message that indicates this. · setNote() will set (update) the note text for a given position . If the note (position) does not exist, print an error to the screen. OOPDA: Notebook App Assignment C. Preventing duplicate notes. Modify your "add note" method above so that it will not add a duplicate value to the ArrayList.. The following sections deal with methods that can reposition notes in the notebook. The user should specify which note to reposition by its text, and not its index position in the ArrayList. D. Moving notes up and down. Imagine if your notes were in priority order. It might be nice to select a note and then move it up (prioritize) or down (deprioritize). Create a moveNoteUp() method that will move a note up one spot, and a corresponding moveNoteDown() method that will move a note down one spot. E. Moving notes multiple times In your NotebookTest driver program, use a For loop to move a note up for a specified number of moves (which you will store in a variable). Set the value of this variable to 2 and test your For loop. Use the method you created in step D. Make a corresponding For loop to move a note down a specified number of times. F. Moving notes to the top or bottom of the list. Finally, make two last methods to move a list item to the top of the list, or the bottom of the list.
Explanation / Answer
package Online;
import java.util.ArrayList;
import java.util.Iterator;
//Class Notebook definition
class Notebook
{
//Creating arraylist of typd string
ArrayList<String> note = new ArrayList<String>();
//Set the node to array list
void setNoteData(String ss)
{
note.add(ss);
}//End of method
//Method returns the string at specified position
String getNoteData(int pos)
{
//Initializes the counter and result string to null
int c = 0;
String res = "";
//Iterates through array list using enhanced for loop
for(String b : note)
{
//Checks if the counter is equal to position specified
if(c == pos)
//Set the searched string to res
res = b;
//Increase the counter by one
c++;
}//End of for loop
//Returns the string
return res;
}//End of method
//Method to return number of notes in array list
int numberOfNotes()
{
//Creates a Iterator class object
Iterator itr = note.iterator();
//Initializes the count to zero
int c = 0;
//Loops till data is available in array list
while(itr.hasNext())
{
//Move next
itr.next();
//Increase the counter by one
c++;
}//End of while loop
//Returns the counter
return c;
}//End of method
//Method to add a note if it does not exists
void addNote(String s)
{
//Set the flag to zero
int f = 0;
//Loops through the array list
for(String d : note)
{
//Checks the string with the array list contents whether it exists or not
if(d.equals(s))
{
//If exits then display error message and set the flag to one
System.out.println("Note already exists. Duplicates not allowed ");
f = 1;
//Come out of the loop
break;
}//End of if
}//End of loop
//Checks if the flag value is zero then string not found in the array list
if(f == 0)
//Add the note to the array list
note.add(s);
}//End of method
//Method to delete a note at specified position
void deleteNote(int pos)
{
//Checks is the position is greater than the maximum number of notes present display error message
if(pos > numberOfNotes())
System.out.println("Invalid Index position");
//Otherwise delete the note from the array list
else
System.out.println("Removed note = " + note.remove(pos));
}//End of method
//Method to return the index position of a string in the array list
int getNoteNumber(String no)
{
//Initialize the counter to zero and index position to -1
int c = 0;
int pos = -1;
//Loops through the array list
for(String b : note)
{
//Checks for the equality ignoring case
if(no.equalsIgnoreCase(b))
//If found store the counter position
pos = c;
//Increase the counter position by one
c++;
}//End of for loop
//Returns the position
return pos;
}//End of method
//Method to return the string at specified index position
String getNote(int pos)
{
//Set the result string and counter to null
String res = "";
int c = 0;
//Checks if the index position is greater than the maximum note present in the array list display the error message
if(pos > numberOfNotes())
System.out.println("Invalid Position");
//Otherwise
else
{
//Loops through the array list
for(String b : note)
{
//Checks if the counter value is equal to specified position
if(c == pos)
//Store the string in the result string
res = b;
//Increase the counter by one
c++;
} //End of for loop
}//End of else
//Return the result string
return res;
}//End of method
//Method to set a string note at a specified position
void setNote(String data, int pos)
{
//Checks if the index position is greater than the maximum note present in the array list display the error message
if(pos > numberOfNotes())
System.out.println("Invalid Position");
//Otherwise
else
{
//Add the note
note.add(pos, data);
}//End of else
}//End of method
//Method to move a note one up
void moveNoteUp(String s)
{
//Initialize the position to zero
int pos = 0;
//Loops through the array list
for(String d : note)
{
//Checks the equality ignoring case
if(d.equalsIgnoreCase(s))
{
//Checks if the position is zero then display error message cannot move up
if(pos == 0)
{
System.out.println("Zero index position cannot move up");
//Come out of the loop
break;
}//End of if
//Otherwise
else
{
//Store the string at found index position
String temp = getNote(pos);
//Store the string of previous index position of the found index position
String temp1 = getNote(pos-1);
//Set the string
note.set(pos, temp1);
note.set((pos-1), temp);
break;
}//End of else
}//End of if
//Increase the counter by one
pos++;
}//End of for loop
}//End of method
//Method to move a note one up
void moveNoteDown(String s)
{
//Initialize the position to zero
int pos = 0;
//Loops through the array list
for(String d : note)
{
//Checks the equality ignoring case
if(d.equalsIgnoreCase(s))
{
//Checks if the position is the last index position then display error message cannot move down
if(pos == numberOfNotes()-1)
{
System.out.println("Last index position cannot move down");
//Come out of the loop
break;
}//End of if
//Otherwise
else
{
//Store the string at found index position
String temp = getNote(pos);
//Store the string of next index position of the found index position
String temp1 = getNote(pos+1);
//Set the string
note.set(pos, temp1);
note.set((pos+1), temp);
break;
}//End of else
}//End of if
//Increase the counter by one
pos++;
}//End of for loop
}//End of method
}//End of class
//Driver class
public class NotebookTest
{
//Main method definition
public static void main(String st[])throws Exception
{
//Creates class object
Notebook nt = new Notebook();
int pos;
//Test the methods and display result
nt.setNoteData("age");
nt.setNoteData("do it");
for(String d : nt.note)
System.out.println(d);
System.out.println("Note = " + nt.getNote(0));
System.out.println("Number of Note = " + nt.numberOfNotes());
nt.addNote("for you");
System.out.println("Note = " + nt.getNote(2));
nt.deleteNote(5);
System.out.println("Number of Note = " + nt.numberOfNotes());
nt.deleteNote(0);
System.out.println("Number of Note = " + nt.numberOfNotes());
pos = nt.getNoteNumber("How are u");
if(pos == -1)
System.out.println("No such note available");
else
System.out.println("Note Position: " + pos);
pos = nt.getNoteNumber("For you");
if(pos == -1)
System.out.println("No such note available");
else
System.out.println("Note Position: " + pos);
System.out.println(nt.getNote(9));
System.out.println("Note: " + nt.getNote(1));
nt.setNote("I like U", 2);
for(String d : nt.note)
System.out.println(d);
nt.addNote("do it");
nt.addNote("Searching");
for(String d : nt.note)
System.out.println(d);
nt.moveNoteUp("I like U");
System.out.println(" After Move up");
for(String d : nt.note)
System.out.println(d);
nt.moveNoteDown("do it");
System.out.println(" After Move down");
for(String d : nt.note)
System.out.println(d);
}//End of main
}//End of class
Sample Run:
age
do it
Note = age
Number of Note = 2
Note = for you
Invalid Index position
Number of Note = 3
Removed note = age
Number of Note = 2
No such note available
Note Position: 1
Invalid Position
Note: for you
do it
for you
I like U
Note already exists. Duplicates not allowed
do it
for you
I like U
Searching
After Move up
do it
I like U
for you
Searching
After Move down
I like U
do it
for you
Searching
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.