Question

I need to write a method in java that returns void and takes a String element....

I need to write a method in java that returns void and takes a String element.

ex :public void addelemt(String element)

the method job is to add elements to an ArrayList as many times as the person wants.

please, no use of a switch or do.

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

import java.util.*;

class ArrayListDemo
{
ArrayList<String> a1;

ArrayListDemo()
{
a1=new ArrayList<String>();
}

public void addelemt(String element)
{
a1.add(element);
}

public void showElements()
{
System.out.println("\n\nElements Array : \n");
System.out.println(a1);
}
}

class TestClass
{
public static void main(String args[])
{
String element,yesNo;
Scanner sc=new Scanner(System.in);
ArrayListDemo ald=new ArrayListDemo();
System.out.println("\n");
while(true)
{
System.out.print("\nEnter Element : ");
element=sc.next();
ald.addelemt(element);
System.out.print("\nDo you want to add next element?(yes/no) : ");
yesNo=sc.next();
if(yesNo.equalsIgnoreCase("no"))
{
break;
}
}

ald.showElements();
System.out.println("\n");
}
}

PS C: \Usersluser\Desktop\Chegg Java ArrayList> javac TestClass.java PS C:\Usersluser\Desktop\Chegg Java\ArrayList> java Test

Note: Used showElements() method to display all the stored elements.

Add a comment
Know the answer?
Add Answer to:
I need to write a method in java that returns void and takes a String element....
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
  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait)...

    java Create the following classes: DatabaseType: an interface that contains one method 1. Comparator getComparatorByTrait(String trait) where Comparator is an interface in java.util. Database: a class that limits the types it can store to DatabaseTypes. The database will store the data in nodes, just like a linked list. The database will also let the user create an index for the database. An index is a sorted array (or in our case, a sorted ArrayList) of the data so that searches...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • java Write a generic method that takes an ArrayList of objects (of a valid concrete object...

    java Write a generic method that takes an ArrayList of objects (of a valid concrete object type) and returns a new ArrayList containing no duplicate elements from the original ArrayList. The method signature is as follows: public static ArrayList UniqueObjects(ArrayList list) Test the method with at least 2 array lists of different concrete object types. Label your outputs properly and show the array lists content before and after calling method UniqueObjects

  • JAVA Array 3. Write a method called noVowels that takes a String as input and returns...

    JAVA Array 3. Write a method called noVowels that takes a String as input and returns true if it doesn't contain any vowels (a, e, i, o, u)

  • please I need it urgent thank in java please 1. (20 pts) a) Write a method...

    please I need it urgent thank in java please 1. (20 pts) a) Write a method (function) that is passed an array of int and returms the largest element in the array b) Write a method (function) that is passed an array of int and returns the smallest element in the array. e) Write a method (function) that is passed an array of int and returns the average of the elements in the array

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • Working in java programming. I have a class (call it manage) that takes in an arraylist...

    Working in java programming. I have a class (call it manage) that takes in an arraylist (list) and a boolean (switch), which is created/preset in the parent main function above this class. The arraylist is modified and returned to the main function however, hence it's name 'manage.' I need help declaring the arraylist in the constructor? public class manage{ public manage(){ //Constructor // What is arraylist construction? this.switch = false; } public manage(ArrayList<Integer> list, boolean switch) { this.list = list;...

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