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
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 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...
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...
In JAVA Find the code for sorts in your language some where online. You will copy...
In JAVA Find the code for sorts in your language some where online. You will copy code from the internet (with citation!) and modify it to test. Make a random array of integers, then perform each search on them. LINEAR SEARCH Write a linear search method to find a number in the list. Call this before each of your sort methods. Include a comment explaining how Linear Search works, when to use it, when you cannot. BINARY SEARCH Write a...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
Write the following program in MIPS: a) declare an array A of the following numbers: 3,...
Write the following program in MIPS: a) declare an array A of the following numbers: 3, 5, 8, 10, 12, 2, 76, 43, 90, 44 b) declare a variable called size which stores the number of element in array A, that is 10. c) write a subroutine to search for a number stored in an array and return true or false. In C++ the subroutine is as follows: search(array, size, number_To_Search) e.g. search(A, 10, 12) The subroutine should return 0...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept...
Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them. Deliverables A listing of the fully commented, working source code of the Java program Test data for the code A screen shot of the application in execution Step 1 Create a new project. Name it "Assignment_2_1". Step 2 Build a solution. Write the Java source code necessary to build a solution for the problem below:You have just...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that...
1. Given the following multi-way if statement, provide a switch statement, using proper java syntax, that will provide the same function. Char grade; String tstmsg; if (grade == ‘A’) {   tstmsg = “Excellent”; } else if (grade == ‘B’) {   tstmsg = “Good”; } else if (grade == ‘C’) {   tstmsg = “OK”; } else {   tstmsg = “Study More”; } 2.Write the following for statement as a while statement. for (k = 0; k < 3; k++) {   System.out.println...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...