answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
blondinia [14]
2 years ago
7

Create the class named SortFile. The class will have a single attribute, which is an ArrayList of integers. The class will have

the following methods: 1. SortFile() : the null constructor will ask the user for a file name. It will then open that file, reading each and every integer value in the file. Each integer will be placed into the ArrayList attribute. Note that the file may contain multiple integers on each line, and the file may contain many lines. 2. SortFile(String file) : this constructor will receive a file name as a parameter. It will operate in the same fashion as the null constructor described above. 3. int[ ] sort() : the sort() function will take the values in the ArrayList attribute and return a sorted array containing all of the values, from highest to lowest (descending order). Duplicate values will not be included.Use the Selection sort algorithm to perform this effort. Note that the order of items in the ArrayList attribute will be UNCHANGED during the execution of this method.
Computers and Technology
1 answer:
S_A_V [24]2 years ago
6 0

Answer:

See explaination

Explanation:

//SortFile.java

import java.io.File;

import java.io.FileNotFoundException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

public class SortFile {

// array list of integers to store the numbers

private ArrayList<Integer> list;

// default constructor

public SortFile() {

// initializing list

list = new ArrayList<Integer>();

// reading file name using a scanner

Scanner sc = new Scanner(System.in);

System.out.print("Enter file name: ");

String filename = sc.nextLine();

// opening file inside a try catch block. if you want to throw exception

// instead, just remove try-catch block and add throws

// FileNotFoundException to the method signature.

try {

// opening file

sc = new Scanner(new File(filename));

// looping and reading each integer from file to list

while (sc.hasNextInt()) {

list.add(sc.nextInt());

}

// closing file

sc.close();

} catch (FileNotFoundException e) {

// exception occurred.

System.err.println(e);

}

}

// constructor taking a file name

public SortFile(String filename) {

// initializing list

list = new ArrayList<Integer>();

// opening file inside a try catch block. if you want to throw exception

// instead, just remove try-catch block and add throws

// FileNotFoundException to the method signature.

try {

Scanner sc = new Scanner(new File(filename));

// looping and reading each integer from file to list

while (sc.hasNextInt()) {

list.add(sc.nextInt());

}

sc.close();

} catch (FileNotFoundException e) {

System.err.println(e);

}

}

// method to return a sorted array of integers containing unique elements

// from list

public int[] sort() {

// creating an array of list.size() size

int arr[] = new int[list.size()];

// count of unique (non duplicate) numbers

int count = 0;

// looping through the list

for (int i = 0; i < list.size(); i++) {

// fetching element

int element = list.get(i);

// if this element does not appear before on list, adding to arr at

// index=count and incrementing count

if (!list.subList(0, i).contains(element)) {

arr[count++] = element;

}

}

// now reducing the size of arr to count, so that there are no blank

// locations

arr = Arrays.copyOf(arr, count);

// using selection sort algorithm to sort the array

int index = 0;

// loops until index is equal to the array length. i.e the whole array

// is sorted

while (index < arr.length) {

// finding the index of biggest element in unsorted array

// initially assuming index as the index of biggest element

int index_max = index;

// looping through the unsorted part of array

for (int i = index + 1; i < arr.length; i++) {

// if current element is bigger than element at index_max,

// updating index_max

if (arr[i] > arr[index_max]) {

index_max = i;

}

}

// now we simply swap elements at index_max and index

int temp = arr[index_max];

arr[index_max] = arr[index];

arr[index] = temp;

// updating index

index++;

// now elements from 0 to index-1 are sorted. this will continue

// until whole array is sorted

}

//returning sorted array

return arr;

}

//code for test-run

public static void main(String[] args) {

//initializing SortFile using default constructor

SortFile sortFile = new SortFile();

//displaying sorted array

System.out.println(Arrays.toString(sortFile.sort()));

}

You might be interested in
What advantage do ExpressCard modules and USB adapters offer over expansion cards?
ss7ja [257]
<span>ExpressCard modules and USB adapters are faster and smaller. They are easier to plug into a computer and allow a person to add memory, wired and wireless communications, multimedia, and security features by inserting the small card or adapter into a small slot in the computer.</span>
3 0
2 years ago
1. Accessing calendars, contact information, emails, files and folders, instant messages, presentation, and task lists over the
Veseljchak [2.6K]
1. Cloud Computing
2. Web Conferencing
3. Ribbon
4. PowerPoint
5. Sadie should share the document in One Drive and give her sister permission to edit the document
6. The design and arrangement of items for efficiency and safety
6 0
2 years ago
To use the Web, each client computer requires a data link layer software package called a Web browser. True False
Luden [163]

Answer:

The answer is "False"

Explanation:

The data link layer is the protocol layer within a program, which controls data movement into a physical wireless connection. This layer is a second layer, that is also known as a collection of communications applications.

  • The server network, in which many clients request, and receive service from a centralized server.
  • This system provides an interface, that enables a user to request server services and view the server returns results, that's why it is wrong.
6 0
2 years ago
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the elemen
Xelga [282]

Answer:

The JAVA program to implement for loop for given scenario is shown below.

public class MyProgram {

   public static void main(String args[]) {    

     int len = 5;

     int[] lowerScores = new int[len];      

     lowerScores[0] = 5;

     lowerScores[1] = 0;

     lowerScores[2] = 2;

     lowerScores[3] = -3;      

     System.out.println("The original elements of array are ");

     for(int i=0; i<len; i++)

     {

         System.out.print(lowerScores[i]+" ");

     }      

     System.out.println();

     System.out.println("The modified elements of array are ");

     for(int i=0; i<len; i++)

     {

         // if element is 0 or negative, 0 is assigned to this index

         if(lowerScores[i] <= 0)

           lowerScores[i] = 0;

       // 1 is subtracted from every non zero and positive element

         else

           lowerScores[i] = lowerScores[i] - 1;

           

       System.out.print(lowerScores[i]+" ");

     }

   }

}

OUTPUT

The original elements of array are  

5 0 2 -3 0  

The modified elements of array are  

4 0 1 0 0

Explanation:

The integer array is declared as shown.

int[] lowerScores = new int[len];

The length of the array is given by integer variable len. As per the question, we have taken four elements and size of 5 for the given array.

int len = 5;

The elements of the array are initialized inside the program with the elements given in the question.

lowerScores[0] = 5;

      lowerScores[1] = 0;

The values of the array can be changed to test the program. Also, the size of the array can be changed for testing.

As per the given scenario, if the element is greater than zero, 1 is subtracted from this element. For elements less than or equal to zero, that particular element is assigned the value of 0.

The given program implements if else statement inside for loop for this purpose.

for(int i=0; i<len; i++)

     {

         // if element is 0 or negative, 0 is assigned to this index

         if(lowerScores[i] <= 0)

           lowerScores[i] = 0;

       // 1 is subtracted from every non zero and positive element

         else

           lowerScores[i] = lowerScores[i] - 1;

     }  

5 0
2 years ago
Read 2 more answers
assume that name is a variable of type stirng that has been assigned a value write an expression whose value is the last charact
Snezhnost [94]

Answer:

The most straight forward way to do it: in general string are zero index based array of characters, so you need to get the length of the string, subtract one and that will be the last character, some expressions in concrete languages would be:

In Python:

name = "blair"

name[len(name) - 1]

In JavaScript:

name = "blair"

name[name.length - 1]

In C++:

#include <string>

string name = "blair";

name[name.length() - 1];

7 0
2 years ago
Other questions:
  • Prompt: Which references and reference formats are you most likely to use? Why?<br><br><br> ED2020
    13·2 answers
  • Mobile computing has two major characteristics that differentiate it from other forms of computing. What are these two character
    8·1 answer
  • Consider a single-platter disk with the following parameters: rotation speed: 7200 rpm; number of tracks on one side ofplatter:
    10·1 answer
  • For any element in keysList with a value greater than 60, print the corresponding value in itemsList, followed by a semicolon (n
    14·2 answers
  • 3. Megan and her brother Marco have a side business where they shop at flea markets, garage sales, and estate
    9·1 answer
  • g 18.6 [Contest 6 - 07/10] Reverse an array Reversing an array is a common task. One approach copies to a second array in revers
    8·1 answer
  • How to code 2.9.5: Four colored triangles {Code HS}
    10·1 answer
  • Write code which takes a sentence as an input from the user and then prints the length of the first word in that sentence.
    6·1 answer
  • A large number of genetic codes are stored as binary values in a list. Which one of the following conditions must be true in ord
    5·2 answers
  • A small e-commerce company uses a series of Robotic Process Automation solutions to manage the backup of data from individual wo
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!