I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file.
import java.util.*;
import java.io.*;
public class Lab3
{
static final int INITIAL_CAPACITY = 5;
public static void main( String args[] ) throws Exception
{
// ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE
if (args.length < 1 )
{
System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n");
System.exit(0);
}
// LOAD FILE INTO ARR USING INSERINORDER
Scanner infile = new Scanner( new File( args[0] ) );
int[] arr = new int[INITIAL_CAPACITY];
int count= 0;
while (infile.hasNextInt())
{
if ( count == arr.length ) arr = upSize( arr );
insertInOrder( arr, count, infile.nextInt() );
++count; // WE JUST ADDED A VALUE - UP THE COUNT
}
infile.close();
printArray( "SORTED ARRAY: ", arr, count );
} // END MAIN
// ################################################################
// USE AS IS - DO NOT MODIFY
static void printArray( String caption, int[] arr, int count )
{
System.out.print( caption );
for( int i=0 ; i<count ;++i )
System.out.print(arr[i] + " " );
System.out.println();
}
// YOU WRITE THIS METHOD - DO NOT MODIFY THIS FILE ANYWHERE ELSE
// ################################################################
static void insertInOrder( int[] arr, int count, int key )
{
}
static int[] upSize( int[] fullArr )
{
int l = fullArr.length * 2;
// CHANGE TO YOUR RETURN STATEMENT
}
} // END LAB3
import java.util.*;
import java.io.*;
public class Lab3
{
static final int INITIAL_CAPACITY = 5;
public static void main( String args[] ) throws Exception
{
// ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND
LINE
if (args.length < 1 )
{
System.out.println("\nusage: C:\\> java Lab3
L3input.txt\n");
System.exit(0);
}
// LOAD FILE INTO ARR USING INSERINORDER
Scanner infile = new Scanner( new File( args[0] ) );
int[] arr = new int[INITIAL_CAPACITY];
int count= 0;
while (infile.hasNextInt())
{
if ( count == arr.length ) arr = upSize( arr );
insertInOrder( arr, count, infile.nextInt() );
++count; // WE JUST ADDED A VALUE - UP THE COUNT
}
infile.close();
printArray( "SORTED ARRAY: ", arr, count++ );
} // END MAIN
//
################################################################
// USE AS IS - DO NOT MODIFY
static void printArray( String caption, int[] arr, int count
)
{
System.out.print( caption );
for( int i=0 ; i<count ;++i )
System.out.print(arr[i] + " " );
System.out.println();
}
// YOU WRITE THIS METHOD - DO NOT MODIFY THIS FILE ANYWHERE
ELSE
//
################################################################
static void insertInOrder( int[] arr, int count, int key )
{
int flag=0;
for(int i=0;i<count;i++){
if(arr[i]>key){
int temp1=arr[i];
int temp2;
for(int j=i;j<count;j++){
temp2=arr[j+1];
arr[j+1]=temp1;
temp1=temp2;
}
arr[i]=key;
flag=1;
break;
}
}
if(flag == 0){
arr[count] = key;
}
}
static int[] upSize( int[] fullArr )
{
int l = fullArr.length * 2;
int[] modArr =new int[l];
for(int i=0;i<fullArr.length;i++){
modArr[i]=fullArr[i];
}
// CHANGE TO YOUR RETURN STATEMENT
return modArr;
}
} // END LAB3
OUTPUT:
I need to write a program in java that reads a text file with a list...
JAVA getting the following errors: Project4.java:93: error: ']' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: ';' expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:93: error: <identifier> expected arr[index] = newVal; // LEAVE THIS HERE. DO NOT REMOVE ^ Project4.java:94: error: illegal start of type return true; ^ Project4.java:98: error: class, interface, or enum expected static int bSearch(int[] a, int count, int key) ^ Project4.java:101: error: class, interface, or...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
Hi I need some help on this lab. The world depends on its successfull compilation. /* Lab5.java Arrays, File input and methods Read the comments and insert your code where indicated. Do not add/modify any output statements */ import java.io.*; import java.util.*; public class Lab5 { public static void main (String[] args) throws Exception { final int ARRAY_MAX = 30; // "args" is the list of tokens you put after "java Project3" on command line if (args.length == 0 )...
I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 { static final int INITIAL_CAPACITY = 10; public static void main (String[] args) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) ...
Our 1st new array operation/method is remove. Implement as follows: public static boolean remove( int[] arr, int count, int key ) { 1: find the index of the first occurance of key. By first occurance we mean lowest index that contains this value. hint: copy the indexOf() method from Lab#3 into the bottom of this project file and call it from inside this remove method. The you will have the index of the value to remove from the array 2:...
I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...
Execute your program like this: C:\> java Lab4 10000ints.txt 172822words.txt Be sure to put the ints filename before the words filename. The starter file will be expecting them in that order. Lab#4's main method is completely written. Do not modify main. Just fill in the methods. Main will load a large arrays of int, and then load a large array of Strings. As usual the read loops for each file will be calling a resize method as needed. Once the...
Need // descriptions for each line of code that is relevant (Example, a description of "arr[i]=r.nextInt(100)+1;".Will give positive rate import java.util.Random; public class TestApp { public static void main(String[] args) { int arr[]=new int[11]; Random r = new Random(); //Random function for generation of random numbers for(int i=0;i<arr.length;i++) // arr[i]=r.nextInt(100)+1; printArray(arr); System.out.println(); bubbleSort(arr); printArray(arr); System.out.println(); System.out.println("Median : "+(arr[arr.length/2])); } private static void printArray(int[] aArray) { for (int i...
I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...
Here is the finsihed java program, it just needs to be converted to MIPs assembly language: import java.util.*; public class LoveCS { public static void main(String[] args) { int timesMessagePrinted; int count = 0; int sum = 0; Scanner input = new Scanner(System.in); System.out.print("How many times should the message be printed?: "); timesMessagePrinted = input.nextInt(); for(int x = 1; x <= timesMessagePrinted; x++) { System.out.println(x +" I love Computer Science!!"); count++; sum += x; } System.out.print("Printed this message " +count...