Question

Write a Java part code to do the following   : ** Suppose a file called infile stored...

Write a Java part code to do the following   :

** Suppose a file called infile stored in drive D: ,filled with five integers(1000,200,3030,40 and 500)

  • Read  the integers from infile then store them in array called ar[] and print it elements.
  • Method called search( ) to print the location of   value 500.
  • Method called sort() to sort array elements.
  • Build another file called outfile in drive D: to save the sorted array elements .
    Note : Use ArrayIndexOutOfBoundsException when you use array in one method.

Homework Answers

Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.*;

public class file
{
   //method to search 500 and return its position in the array
   public static int search(int arr[],int size)
   {
       int i=-1;
       try
       {
       for(i=0;i<size;i++)
           if(arr[i]==500) //compare for 500
               break;
       }
       catch(ArrayIndexOutOfBoundsException e)
       {
               System.out.println("Array out of Bound");
           }
       if(i==size) //if not found then return -1
           return -1;
       else
           return (i+1); //return the position
   }
     
   //method to sort the array
   public static void sort(int arr[],int num)
   {
       int i,j,t;
       //selection sort logic
       try
       {
       for(i=0;i<num;i++)
       {
           for(j=i+1;j<num;j++)
           {
               if(arr[i]>arr[j])
               { //swap
                   t=arr[i];
                   arr[i]=arr[j];
                   arr[j]=t;
               }
           }
       }
       }
       catch(ArrayIndexOutOfBoundsException e)
       {
               System.out.println("Array out of Bound");
           }
   }
  
   public static void main(String[] args) throws Exception
   {
       //declare scanner object to read the file contents
       Scanner scanner = new Scanner(new File("D:\\infile.txt"));
       int n,i;
//declare filewriter object to open the file and write the array contents
FileWriter writer = new FileWriter("D:/outfile.txt");
int arr[] = new int[50]; //declare array with size 50
       i=0;

  
//loop to read the data from file
  
try
{
           while (scanner.hasNext())
           {
               n=scanner.nextInt();//read the numbers
               arr[i]=n; //assign the number into array
               i++;
           }
}
catch(ArrayIndexOutOfBoundsException e)
       {
               System.out.println("Array out of Bound");
           }
          
       int len = i; //assign number of elements read from file
       System.out.print("\nThe array elements : ");
       for(i=0;i<len;i++) //loop to print the array elements
           System.out.print(arr[i]+" ");
       int pos = search(arr,len); //call search() to know the postion of 500
       if(pos==-1)
           System.out.print("\n500 is not in the array");
       else
       System.out.print("\nThe number 500 is at "+ pos + " Position");
       //call top sort() method
       sort(arr,len);
       //loop to write the array elements into file
for (i = 0; i < len; i++) {
writer.write( arr[i] + "\t"+ "");
}
writer.close();//close the files
   scanner.close();
}

}

output

Know the answer?
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
Similar Questions
Write an insertion sort algorithm in C language by performing following steps .make sure code is...
Write an insertion sort algorithm in C language by performing following steps .make sure code is work. thanks Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Implement the insertion sort algorithm (note that insertion sort is an in-place sort that modifies the original array). Print the sorted array.
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which...
Java programming. Write a public Java class called WriteToFile that opens a file called words.dat which is empty. Your program should read a String array called words and write each word onto a new line in the file. Your method should include an appropriate throws clause and should be defined within a class called TextFileEditor. The string should contain the following words: {“the”, “quick”, “brown”, “fox”}
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods...
Write a Java class called CityDistances in a class file called CityDistances.java.    2. Your methods will make use of two text files. a. The first text file contains the names of cities. However, the first line of the file is a number specifying how many city names are contained within the file. For example, 5 Dallas Houston Austin Nacogdoches El Paso b. The second text file contains the distances between the cities in the file described above. This file...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two...
IN JAVA PLEASE write code to MERGESORT an array of user inputted OBJECTS you have two different classes in the program being made as objects in the main (Bunny class and Dog Class). ask the user "what kind of animal would you like to sort?" user chooses the type of animal (bunny or dog), then you ask the user how big would you like the array of animals to be (maximum array size = 20)? and then you ask what...
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice)....
Download the ProductUpTo3.java file, and open it in jGrasp (or a text editor of your choice). This program will read in three integers with Scanner, put the values in an array of int, and then print the product of the three values. Example output of the program is shown below, with user input shown in bold: Enter first integer: 3 Enter second integer: 4 Enter third integer: 5 Product: 60 More details about the method you need to write are...
Write a Java program using the OOP paradigm to do the following: 1. Main Function: create...
Write a Java program using the OOP paradigm to do the following: 1. Main Function: create a list with a million items, and you can use the generate function to add a million random values to the list. 2. Create a method to sort the list created in the main function using bubble sort, then use the sorted list to search for the (k) value using binary search. 3. Create a method to create a new list of 1000 items...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the...
IN JAVA Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort: <-- (I need the code to be written with these) I need Class river, Class CTRiver and Class Driver with comments so I can learn and better understand the code I also need a UML Diagram HELP Please! Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong()...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT