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...

  • Write a Java method that should take an ArrayList as a parameter, print its element in...

    Write a Java method that should take an ArrayList as a parameter, print its element in the reverse order. The main function that calls the method is the following: import java.util.*; public class ReverseGen {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);       System.out.println("How many numbers do you want to input?");       int num = input.nextInt();       ArrayList<Double> d = new ArrayList<Double>(num);       for(int i = 0 ; i < num; i++){           System.out.print("Enter...

  • 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

  • JAVA - Given two List objects (input and output), write a generic method that 1) clears...

    JAVA - Given two List objects (input and output), write a generic method that 1) clears output list if it contains any element, and 2) copies every element of input list to the output list in the same order and position. Note that the type of output list can be any super type of the input list (e.g. type(output)=Numeric & type(input)=Double). import java.util.List; import java.util.ArrayList; public class Collections { // Modify method signature if needed & implement the body of...

  • 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...

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