1answer.
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]
4 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]4 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
Understanding Sequential Statements Summary
Kisachek [45]

Answer:

// Payroll.java

public class Payroll

{

int numOfDependents;

double salary;

double salaryToHome;

double Federal_Tax,TAX_RATE,Depen_tax;

 

Payroll(double salry,int nod)

{

numOfDependents=nod;

salary=salry;

Federal_Tax=6.5;

TAX_RATE=28.0;

Depen_tax=2.5;

}

 

double getTAX_RATE()

{

return TAX_RATE*salary/100;

}

double Federal_Tax()

{

return Federal_Tax*salary/100;

}

double getDepenAmount()

{

return numOfDependents*(Depen_tax*salary/100);

}

double getTakeHomeSalary()

{

return salary+getDepenAmount()-(getTAX_RATE()+Federal_Tax());

}

void printOutput()

{

System.out.print("\nState Tax: $"+getTAX_RATE());

System.out.print("\nFederal Tax: $"+Federal_Tax());

System.out.print("\nDependents: $"+getDepenAmount());

System.out.print("\nSalary: $"+salary);

System.out.println("\nTake Home Pay: $"+getTakeHomeSalary());

 

}

public static void main(String args[])

{

Payroll obj=new Payroll(1250,2);

obj.printOutput();

}

}

// ModifiedPayroll.java

import java.util.Scanner;

public class ModifiedPayroll

{

 

int numOfDependents;

double salary;

double salaryToHome;

double Federal_Tax,TAX_RATE,Depen_tax;

 

ModifiedPayroll(double salry,int nod)

{

//initialise variables

numOfDependents=nod;

salary=salry;

Federal_Tax=6.5;

TAX_RATE=28.0;

Depen_tax=2.5;

}

 

double getTAX_RATE()

{

return TAX_RATE*salary/100;

}

double Federal_Tax()

{

return Federal_Tax*salary/100;

}

double getDepenAmount()

{

return numOfDependents*(Depen_tax*salary/100);

}

double getTakeHomeSalary()

{

return salary+getDepenAmount()-(getTAX_RATE()+Federal_Tax());

}

void printOutput()

{

System.out.print("\nState Tax: $"+getTAX_RATE());

System.out.print("\nFederal Tax: $"+Federal_Tax());

System.out.print("\nDependents: $"+getDepenAmount());

System.out.print("\nSalary: $"+salary);

System.out.println("\nTake Home Pay: $"+getTakeHomeSalary());

 

}

public static void main(String args[])

{

Scanner sc=new Scanner(System.in);

System.out.print("\nEnter Salary: $");

double salary=sc.nextDouble();

System.out.print("\nEnter number Of Dependents:$");

int nob=sc.nextInt();

ModifiedPayroll obj=new ModifiedPayroll(salary,nob);

obj.printOutput();

}

}

Explanation/Output:

// For Payroll.java

State Tax: $350.0

Federal Tax: $81.25

Dependents: $62.5

Salary: $1250.0

Take Home Pay: $881.25

// For ModifiedPayroll.java

Enter Salary:$1550.0

Enter number of Dependents:$3

State Tax: $434.0

Federal Tax: $100.75

Dependents: $116.25

Salary: $1550.0

Take Home Pay: $1131.5

8 0
3 years ago
You must lower your high beams when within how many feet of an approaching vehicle?
drek231 [11]

2000 feet of any vehicle

7 0
3 years ago
An automated search feature used by search engines to find that match your search terms is called a spider or
9966 [12]

Answer

Crawler

Explanation

This is a program that go to various website to read their pages and content provided in order to form entries for a search engine index.All search engines have this program called spider or a bot. It acts as an automated script that browses through the internet to scan the web pages and find words contained in the pages and where the words are used.


5 0
4 years ago
Read 2 more answers
Devices that allow for the retention of data when your computer has been shutdown
zepelin [54]
That would be persistent storage devices (non-volatile memory)
- hard disk drives (mechanical)
- solid state drives (electronic / NAND flash)
- CD (optical)

If u need more I can give u a few more
4 0
3 years ago
A column to be used in a database is called the
Temka [501]

it is called an attribute

8 0
3 years ago
Other questions:
  • Which interest bearing account is best for people who won’t need access to their money for several months or longer?
    6·1 answer
  • A program written in a(n) procedural language consists of sequences of statements that manipulate data items. __________________
    9·1 answer
  • What is encyclopedia. Com considered to be?
    14·1 answer
  • Where is line-of-sight Internet common?<br> In space<br> Outdoors<br> Inside<br> In businesses
    5·1 answer
  • The CUSTOMERS and SALES tables contain these columns:
    6·1 answer
  • Create a recursive method, a method that calls itself, that returns the number of vowels that appear in any string given. Keep i
    11·1 answer
  • The two major types of system software programs are utility programs and the ________. Select one: A. user interface B. supervis
    13·1 answer
  • What will you see on the next line?
    6·2 answers
  • 2.13 LAB: Branches: Leap Year
    11·1 answer
  • Why do we use the internet so much?​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!