Question

In: Computer Science

JAVA PROGRAMMING 1)BuildLists: Write a program to create an ArrayList<Integer>. Fill it with numbers from 1...

JAVA PROGRAMMING

1)BuildLists:

Write a program to create an ArrayList<Integer>.

Fill it with numbers from 1 to 1000.
Then remove every even number.

Then remove every multiple of 3 remaining

Then remove every multiple of 5

Then remove every multiple of 7

Then sum the array, and print.

Solutions

Expert Solution

import java.util.*;
class Main{
public static void main(String args[])
{
List<Integer> numbers=new ArrayList<Integer>();
for(int i=1;i<=1000;i++)
{
numbers.add(i);
}
for(Iterator<Integer> iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%2==0)
{
iterator.remove();
}
}
for(Iterator<Integer> iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%3==0)
{
iterator.remove();
}
}
for(Iterator<Integer> iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%5==0)
{
iterator.remove();
}
}
for(Iterator<Integer> iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
if(number%7==0)
{
iterator.remove();
}
}
int sum=0;
for(Iterator<Integer> iterator=numbers.iterator();iterator.hasNext();)
{
Integer number=iterator.next();
sum=sum+number;
}
System.out.println(numbers);
System.out.println(sum);
}
}


Related Solutions

Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...
Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.   public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) { } Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.
Java Write a program that reads 4 integer numbers from the user combine it into a...
Java Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application...
Java Programming Create a program that prompts the user for an integer number and searches for...
Java Programming Create a program that prompts the user for an integer number and searches for it within an array of 10 elements. What is the average number of comparisons required to find an element in the array? Your program should print the number of comparisons required to find the number or determine that the number does not exist. Try finding the first and last numbers stored in the array. Run your program several times before computing the average.
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The...
Write a Java program to convert decimal (integer) numbers into their octal number (integer) equivalents. The input to the program will be a single non-negative integer number. If the number is less than or equal to 2097151, convert the number to its octal equivalent. If the number is larger than 2097151, output the phrase “UNABLE TO CONVERT” and quit the program. The output of your program will always be a 7-digit octal number with no spaces between any of the...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the lists with repeats (see example below). Write a generic method (removeDuplicates( ….. )) to remove those duplicate items and returns an ArrayList<E> list without duplicates. Original Integer List: [14, 24, 14, 42, 24, 25, 25, 23] No-Duplicate List: [14, 24, 42, 25, 23] Same generic method for the name list Original List: [Mike, Lara, Jenny, Lara, Jared, Jonny, Lindsey, Mike, Jared] No-Duplicate List: [Mike,...
Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT