SlideShare a Scribd company logo
I need help with this program for java.
The program you are given to start with: Lab4.java
The input file of ints:10,000ints.txt
The input file of words:172,822words.txt
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 arrays are loaded, each array will be
tested for the presence of duplicates via calls to indexOfFirstDuplicate(). Do not do any trim
operation.
Here are the rules:
In your methods that look for the first occurrence of a duplicate in each array, you must sort the
arrays before looking for the dupe. Do not search the array for the dupe until it is sorted. We
require this because an unsorted array would require a quadratic algorithm (N squared via nested
loops). If you sort you array first you will only incur an O(n*Log2n) cost for the sort with an
additional O(n) for the search. This is the best that can be done without using a technology such
as hashing which you will use for your next lab.
Inside your method to find the dupe you must sort the array first.
Do not define/use any new type or data structure that we have not covered yet.
Your traversal of the array looking for the dupe should require no more than one pass.
Be as efficient as you can without breaking rule #2.
Assuming the array below, the 1st occurrence of a duplicate is at index 4, not 3, since the
element at [4] is the first index position where that value was seen for a second time. Be sure you
understand this. Do not report the index of the first occurrence of a value as being the index
where the duplicate occurred.
Your job will be to fill in the definitions of these methods below main
static int[] upSizeArr( int[] fullArray ); // incoming array is FULL. It needs it's capacity doubled
static String[] upSizeArr( String[] fullArray ); // incoming array is FULL. It needs it's capacity
doubled
static int indexOfFirstDupe( int[] arr, int count ); // returns ind of 1st occurrence of duplicate
value
static int indexOfFirstDupe( String[] arr, int count ); // returns ind of 1st occurrence of duplicate
value
Here is the starting file!
Solution
HI, Please find my implementation of all required methods.
Please let me know in case of any issue.
/* Lab4.java Reads two files into two arrays then checks both arrays for dupes */
import java.io.*;
import java.util.*;
public class Lab4
{
static final int INITIAL_CAPACITY = 10;
static final int NOT_FOUND = -1; // indexOfFirstDupe returns this value if no dupes found
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 )
{
System.out.println(" usage: C:> java Lab4   "); // i.e. C:> java Lab4 10000ints.txt
172822words.txt
System.exit(0);
}
String[] wordList = new String[INITIAL_CAPACITY];
int[] intList = new int[INITIAL_CAPACITY];
int wordCount = 0, intCount=0;
Scanner intFile = new Scanner( new File(args[0]) );
BufferedReader wordFile = new BufferedReader( new FileReader(args[1]) );
// P R O C E S S I N T F I L E
while ( intFile.hasNextInt() ) // i.e. while there are more ints in the file
{ if ( intCount == intList.length )
intList = upSizeArr( intList );
intList[intCount++] = intFile.nextInt();
} //END WHILE intFile
intFile.close();
System.out.format( "%s loaded into intList array. size=%d, count=%d
",args[0],intList.length,intCount );
int dupeIndex = indexOfFirstDupe( intList, intCount );
if ( dupeIndex == NOT_FOUND )
System.out.format("No duplicate values found in intList ");
else
System.out.format("First duplicate value in intList found at index %d ",dupeIndex);
// P R O C E S S S T R I N G F I L E
while ( wordFile.ready() ) // i.e. while there is another line (word) in the file
{ if ( wordCount == wordList.length )
wordList = upSizeArr( wordList );
wordList[wordCount++] = wordFile.readLine();
} //END WHILE wordFile
wordFile.close();
System.out.format( "%s loaded into word array. size=%d, count=%d
",args[1],wordList.length,wordCount );
dupeIndex = indexOfFirstDupe( wordList, wordCount );
if ( dupeIndex == NOT_FOUND )
System.out.format("No duplicate values found in wordList ");
else
System.out.format("First duplicate value in wordList found at index %d ",dupeIndex);
} // END MAIN
//############################################################################
######################
// FYI. Methods that don't say private are by default, private.
// copy/adapt your working code from lab3 || project3
static String[] upSizeArr( String[] fullArr )
{
/* Y O U R C O D E H E R E */
int currentSize = fullArr.length;
int newSize = currentSize*2;
// creating an array
String[] newArr = new String[newSize];
// copying valus from fullArr to newArr
for(int i=0; i

More Related Content

PDF
An Introduction to Programming in Java: Arrays
PDF
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
PPT
9781439035665 ppt ch09
PPTX
Chapter 7.1
DOCX
Array assignment
PPT
Data Structure In C#
PPTX
Arrays in Java with example and types of array.pptx
PPTX
Computer programming 2 Lesson 13
An Introduction to Programming in Java: Arrays
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
9781439035665 ppt ch09
Chapter 7.1
Array assignment
Data Structure In C#
Arrays in Java with example and types of array.pptx
Computer programming 2 Lesson 13

Similar to I need help with this program for java.The program you are given t.pdf (20)

PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPT
Chap09
PPT
Week6.ppt
PPTX
PPT
ARRAYS.ppt
PDF
Python Interview Questions And Answers
DOCX
Write a program that will test a name) method no sorting routine from.docx
PDF
1-Intoduction ------------- Array in C++
PPTX
Arrays in programming
PDF
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
PDF
java I am trying to run my code but it is not letting me .pdf
PPTX
Intro to C# - part 2.pptx emerging technology
PPTX
Arrays in C++
DOCX
Provide copy constructor- destructor- and assignment operator for the.docx
PDF
Ds lab handouts
PPTX
Programming in Python
PPTX
In Python, a list is a built-in dynamic sized array. We can store all types o...
PPTX
object oriented programing in python and pip
PPTX
set.pptx
PDF
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
Chap09
Week6.ppt
ARRAYS.ppt
Python Interview Questions And Answers
Write a program that will test a name) method no sorting routine from.docx
1-Intoduction ------------- Array in C++
Arrays in programming
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
java I am trying to run my code but it is not letting me .pdf
Intro to C# - part 2.pptx emerging technology
Arrays in C++
Provide copy constructor- destructor- and assignment operator for the.docx
Ds lab handouts
Programming in Python
In Python, a list is a built-in dynamic sized array. We can store all types o...
object oriented programing in python and pip
set.pptx
I am stuck on parts E and FExercise 1      NumberListTester.java.pdf
Ad

More from fonecomp (20)

PDF
write a program that prompts the user to enter the center of a recta.pdf
PDF
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
PDF
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
PDF
What was the court-packing plan, and why is it sig- nificant to a.pdf
PDF
Who are the major stakeholders that Sony must consider when developi.pdf
PDF
What sort of prevention techniques would be useful when dealing with.pdf
PDF
What are the main three types of organizational buyers How are they.pdf
PDF
The following code, is a one player battleship game in JAVA. Im tryi.pdf
PDF
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
PDF
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
PDF
Neeb Corporation manufactures and sells a single product. The com.pdf
PDF
Describe the primary, secondary, and tertiary structure of DNASo.pdf
PDF
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
PDF
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
PDF
I need help creating a parametized JUnit test case for the following.pdf
PDF
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
PDF
General question How would the technology affect who lives and who .pdf
PDF
Describe how partial diploids can be produced in E. coli.Solutio.pdf
PDF
A single layer of gold atoms lies on a table. The radius of each gol.pdf
PDF
An erect object is 25 cm from a concave mirror of radius 30 cm. The .pdf
write a program that prompts the user to enter the center of a recta.pdf
write 3 3 slide on China and Germany. Individual work (1) Choose a c.pdf
Why do negotiations fail O Conflicts are boring O Conflicts are co.pdf
What was the court-packing plan, and why is it sig- nificant to a.pdf
Who are the major stakeholders that Sony must consider when developi.pdf
What sort of prevention techniques would be useful when dealing with.pdf
What are the main three types of organizational buyers How are they.pdf
The following code, is a one player battleship game in JAVA. Im tryi.pdf
Sharks are able to maintain their fluids hypertonic to the ocean env.pdf
Quantum Bank Inc. is a regional bank with branches throughout the so.pdf
Neeb Corporation manufactures and sells a single product. The com.pdf
Describe the primary, secondary, and tertiary structure of DNASo.pdf
Harrison works in a cubicle at a window next to Karen Ravenwoods cu.pdf
Hi there I am having difficulty in finalizing my Tetris game , below.pdf
I need help creating a parametized JUnit test case for the following.pdf
How do the genomes of Archaea and Bacteria compare Drag and drop th.pdf
General question How would the technology affect who lives and who .pdf
Describe how partial diploids can be produced in E. coli.Solutio.pdf
A single layer of gold atoms lies on a table. The radius of each gol.pdf
An erect object is 25 cm from a concave mirror of radius 30 cm. The .pdf
Ad

Recently uploaded (20)

PPTX
master seminar digital applications in india
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Structure & Organelles in detailed.
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
A systematic review of self-coping strategies used by university students to ...
PPTX
Lesson notes of climatology university.
PDF
RMMM.pdf make it easy to upload and study
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
master seminar digital applications in india
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Types and Its function , kingdom of life
GDM (1) (1).pptx small presentation for students
Cell Structure & Organelles in detailed.
Abdominal Access Techniques with Prof. Dr. R K Mishra
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
A systematic review of self-coping strategies used by university students to ...
Lesson notes of climatology university.
RMMM.pdf make it easy to upload and study
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Microbial disease of the cardiovascular and lymphatic systems
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Supply Chain Operations Speaking Notes -ICLT Program
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

I need help with this program for java.The program you are given t.pdf

  • 1. I need help with this program for java. The program you are given to start with: Lab4.java The input file of ints:10,000ints.txt The input file of words:172,822words.txt 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 arrays are loaded, each array will be tested for the presence of duplicates via calls to indexOfFirstDuplicate(). Do not do any trim operation. Here are the rules: In your methods that look for the first occurrence of a duplicate in each array, you must sort the arrays before looking for the dupe. Do not search the array for the dupe until it is sorted. We require this because an unsorted array would require a quadratic algorithm (N squared via nested loops). If you sort you array first you will only incur an O(n*Log2n) cost for the sort with an additional O(n) for the search. This is the best that can be done without using a technology such as hashing which you will use for your next lab. Inside your method to find the dupe you must sort the array first. Do not define/use any new type or data structure that we have not covered yet. Your traversal of the array looking for the dupe should require no more than one pass. Be as efficient as you can without breaking rule #2. Assuming the array below, the 1st occurrence of a duplicate is at index 4, not 3, since the element at [4] is the first index position where that value was seen for a second time. Be sure you understand this. Do not report the index of the first occurrence of a value as being the index where the duplicate occurred. Your job will be to fill in the definitions of these methods below main static int[] upSizeArr( int[] fullArray ); // incoming array is FULL. It needs it's capacity doubled static String[] upSizeArr( String[] fullArray ); // incoming array is FULL. It needs it's capacity doubled static int indexOfFirstDupe( int[] arr, int count ); // returns ind of 1st occurrence of duplicate value static int indexOfFirstDupe( String[] arr, int count ); // returns ind of 1st occurrence of duplicate
  • 2. value Here is the starting file! Solution HI, Please find my implementation of all required methods. Please let me know in case of any issue. /* Lab4.java Reads two files into two arrays then checks both arrays for dupes */ import java.io.*; import java.util.*; public class Lab4 { static final int INITIAL_CAPACITY = 10; static final int NOT_FOUND = -1; // indexOfFirstDupe returns this value if no dupes found 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 ) { System.out.println(" usage: C:> java Lab4 "); // i.e. C:> java Lab4 10000ints.txt 172822words.txt System.exit(0); } String[] wordList = new String[INITIAL_CAPACITY]; int[] intList = new int[INITIAL_CAPACITY]; int wordCount = 0, intCount=0; Scanner intFile = new Scanner( new File(args[0]) ); BufferedReader wordFile = new BufferedReader( new FileReader(args[1]) ); // P R O C E S S I N T F I L E while ( intFile.hasNextInt() ) // i.e. while there are more ints in the file { if ( intCount == intList.length ) intList = upSizeArr( intList ); intList[intCount++] = intFile.nextInt(); } //END WHILE intFile intFile.close();
  • 3. System.out.format( "%s loaded into intList array. size=%d, count=%d ",args[0],intList.length,intCount ); int dupeIndex = indexOfFirstDupe( intList, intCount ); if ( dupeIndex == NOT_FOUND ) System.out.format("No duplicate values found in intList "); else System.out.format("First duplicate value in intList found at index %d ",dupeIndex); // P R O C E S S S T R I N G F I L E while ( wordFile.ready() ) // i.e. while there is another line (word) in the file { if ( wordCount == wordList.length ) wordList = upSizeArr( wordList ); wordList[wordCount++] = wordFile.readLine(); } //END WHILE wordFile wordFile.close(); System.out.format( "%s loaded into word array. size=%d, count=%d ",args[1],wordList.length,wordCount ); dupeIndex = indexOfFirstDupe( wordList, wordCount ); if ( dupeIndex == NOT_FOUND ) System.out.format("No duplicate values found in wordList "); else System.out.format("First duplicate value in wordList found at index %d ",dupeIndex); } // END MAIN //############################################################################ ###################### // FYI. Methods that don't say private are by default, private. // copy/adapt your working code from lab3 || project3 static String[] upSizeArr( String[] fullArr ) { /* Y O U R C O D E H E R E */ int currentSize = fullArr.length; int newSize = currentSize*2; // creating an array String[] newArr = new String[newSize]; // copying valus from fullArr to newArr for(int i=0; i