QUESTION:
Complete the exercise starting with the file ArrayListInt.java and Numbers.txt data file. The task is to create a class called InClass01 in file InClass01.java, with the main method that includes the code snippet in the file above, and additional methods which carry out the following tasks:
GIVEN ArrayListInt.java FILE
ArrayList<Integer> numbers = new
ArrayList<Integer>();
Scanner input = new Scanner(new File("Numbers.txt"));
while (input.hasNextInt()) {
int n = input.nextInt();
numbers.add(n);
}
System.out.println(numbers);
//insert additional methods and calls here ...
filterEvens(numbers);
System.out.println(numbers);
// Removes all elements with even values from the given
list.
public static void filterEvens(ArrayList<Integer> list)
{
for (int i = list.size() - 1; i >= 0; i--) {
int n = list.get(i);
if (n % 2 == 0) {
list.remove(i);
}
}
}
GIVEN Numbers.txt FILE
98
58
48
70
66
70
98
59
36
92
95
97
54
83
90
68
36
99
96
6
52
50
22
52
15
79
82
76
27
13
48
95
37
83
Please do comment if you have any doubt regarding this program
Program:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class InClass01 {
public static void main(String[] args) throws
FileNotFoundException {
ArrayList<Integer> numbers =
new ArrayList<Integer>();
Scanner input = new Scanner(new
File("Numbers.txt"));
while (input.hasNextInt()) {
int n =
input.nextInt();
numbers.add(n);
}
System.out.println(numbers);
//print the list of
numbers, with a count
listOfNumbersWithCount(numbers);
// compute the
average of the numbers
averageOfNumbers(numbers);
// compute the
maximum and minimum of the numbers
minAndmaxOfNumbers(numbers);
// filter out
the even numbers
filterEvens(numbers);
System.out.println(numbers);
}
// print the list of numbers,
with a count
private static void
listOfNumbersWithCount(ArrayList<Integer> numbers) {
System.out.println("print the list
of numbers, with a count");
int count = 0;
// Retrieve through the
list from 0 to size of the list and increase the count from each
element retrieval in the list
// (OR)count of the
list can also be calculated from the size of the arraylist
(numbers.size())
for(int i = 0 ; i <
numbers.size() ; i++) {
count++;
System.out.println(numbers.get(i));
}
System.out.println("count of the
list : " +count);
}
//compute the average of the numbers
private static void
averageOfNumbers(ArrayList<Integer> numbers) {
double sum = 0;
for(int i = 0 ; i <
numbers.size() ; i++ ) {
sum = sum +
numbers.get(i);
}
double average = sum /
numbers.size();
System.out.println("Average of
numbers : " + average);
}
//compute the minimum and maximum of the
numbers
private static void
minAndmaxOfNumbers(ArrayList<Integer> numbers) {
System.out.println("Min of numbers
" + Collections.min(numbers));
System.out.println("Max of numbers
" + Collections.max(numbers));
/*or sort the array using
Collections.sort(numbers) and get the first and last numbers from
the list.
Collections.sort(numbers);
System.out.println(numbers.get(0) +
" " + numbers.get(numbers.size()-1));*/
}
// Removes all elements with even values from
the given list
private static void
filterEvens(ArrayList<Integer> list) {
System.out.print("Filter out the
even numbers from the list : ");
for (int i = list.size() - 1; i
>= 0; i--) {
int n =
list.get(i);
if (n % 2 == 0)
{
list.remove(i);
}
}
}
}
QUESTION: Complete the exercise starting with the file ArrayListInt.java and Numbers.txt data file. The task is...
Write the output produced when the following method is passed each of the following lists and give the formal code for the problem: public static void mystery1(ArrayList<Integer> list) { for (int i = list.size() - 1; i > 0; i--) { if (list.get(i) < list.get(i - 1)) { int element = list.get(i); list.remove(i); list.add(0, element); } } System.out.println(list); } a. [2, 6, 1, 8] b. [30, 20, 10, 60, 50, 40]
Please answer the question in the picture below and explain your
answer. If it helps, the associated coursework is taught in
Java.
12. Given the following method, Write the output when the following method is passed each of the following lists: public static void mystery (ArrayList<Integer> list) { for (int i = 0; i < list.size()-1; i++) { int e = list.get(i); list.add (i+2, e + 3); list.remove(i); System.out.println(list); a. [80,60,50,40,30] b. (5,5,5,5,3]
1.The following statement gets an element from position 4 in an array named myArray: x = myArray[4]; What is the equivalent operation using an array list named list.? A x = list.get(); B x = list.get(4); C x = list.get[4]; D x = list[4]; 2.Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 &&...
How to write a Java file out that that reads from numbers.txt with these numbers 2 6 7 9 5 4 3 8 0 1 6 8 2 3 and write to a file called totalSum.txt that looks like: 2+6+7+9=24 and so on using this code import java.io.*; import java.util.Scanner; public class FileScanner { public static void main(String[] args) { /* For Homework! Tokens*/ Scanner file = null; PrintWriter fout= null; ...
You will implement the following method public static int[] linearSearchExtended(int[][] numbers, int key) { // Implement this method return new int[] {}; } There is a utility method provided for you with the following signature. You may use this method to convert a list of integers into an array. public static int[] convertIntegers(List<Integer> integers) Provided code import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; public class MatrixSearch { // This method converts a list of integers to an array...
This is the question:
These are in Java format.
Comments are required on these two Classes (Student.java
and StudentDemo.java) all over the coding:
Provide proper comments all over the
codings.
---------------------------------------------------------------------------------------------------------------
import java.io.*;
import java.util.*;
class Student {
private String name;
private double gradePointAverage;
public Student(String n , double a){
name = n;
gradePointAverage = a;
}
public String getName(){
return name;
}
public double getGradePointAverage(){
return gradePointAverage;
}
...
Develop a Generic String List (GSL). NOTE: I have done this lab but someting is wrong here is what i was told that was needed. Ill provide my code at the very end. Here is what is missing : Here is my code: public class GSL { private String arr[]; private int size; public GSL() { arr = new String[10]; size = 0; } public int size() { return size; } public void add(String value) { ...
please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following 1) print out the original data in the file without doing anything to it. 2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...
Hi All, Can someone please help me correct the selection sort method in my program. the output for the first and last sorted integers are wrong compared with the bubble and merge sort. and for the radix i have no idea. See program below import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Sort { static ArrayList<Integer> Data1 = new ArrayList<Integer>(); static ArrayList<Integer> Data2 = new ArrayList<Integer>(); static ArrayList<Integer> Data3 = new ArrayList<Integer>(); static ArrayList<Integer> Data4 = new ArrayList<Integer>(); static int...
Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...