Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program called Split in Java that reads a text file, in.txt, that contains a list of positive integers (duplicates are possible, zero is

Write a program called Split in Java that reads a text file, in.txt, that contains a list of positive integers (duplicates are possible, zero is not considered a positive integer) separated by spaces and/or line breaks. After reading the integers, the program prints out Yes if the set of integers can be split into two subsets with equal sums of elements and with equal numbers of elements. Otherwise (if the list if integers cannot be divided into two subsets satisfying the condition above), the program prints out No. Assume that in.txt contains at least 2 integers. (a) Source code (b) A report (not to exceed two pages) as an ASCI text document, MS Word document, or a PDF file that contains a description of the ALGORITHM implemented by your program and an analysis of its complexity using Big O notation. It is important that you write a description of the algorithm, not a description of your program! In other words, do not explain your classes and methods. Explain: 1) The sequence of operations that you use to solve the problem, and 2) Why this sequence of operations correctly solves the problem. Pseudo-code is a standard way of explaining algorithms. Examples: If in.txt contains 7 7, the program must print out Yes. In this case, the split is {7) and {7}. Both sets are of size of 1, and have the same sum of elements. If in.txt contains 5 3 2 4, the program must print out Yes. The split is {2, 5}, {3, 4}. Both sets have the same size, 2, and the same sum, 7. If in.txt contains 5 7 5 1 1 3, the program must print out Yes. The split is {1, 5, 5} and {1, 3, 7}.Both sets have three elements and the same sum, 11. If in.txt contains 6 5 8 3 4 4, the program must print out Yes. The split is {4, 5, 6} and {3, 4, 8}. Both sets have the same length, 3, and the same sum, 15. If in.txt contains 2 6 10 14 4 8 12 16. There are several splits satisfying the requirement: {2, 8, 10, 16} and {4, 6, 12, 14}; {4, 6, 10, 16} and {2, 8, 12, 14}; {4, 8, 10, 14} and {2, 6, 12, 16}; {6, 8, 10, 12} and {2, 4, 14, 16}. Your program does not need to find all of them. It must stop and print Yes after finding the first split satisfying the requirement. If in.txt contains 2 13 7 5 16 11, the program must print out No. The set of numbers cannot be divided into two subsets with equal sums and the same number of elements. If in.txt contains 7 8 2 4 5, the program must print out No. If in.txt contains 8 5 12 24 14 12 4 6, the program must print out No.Examples: If in.txt contains 7 7, the program must print out Yes. In this case, the split is {7) and {7}. Both sets are of size of 1, and have the same sum of elements. If in.txt contains 5 3 2 4, the program must print out Yes. The split is {2, 5}, {3, 4}. Both sets have the same size, 2, and the same sum, 7. If in.txt contains 5 7 5 1 1 3, the program must print out Yes. The split is {1, 5, 5} and {1, 3, 7}.Both sets have three elements and the same sum, 11. If in.txt contains 6 5 8 3 4 4, the program must print out Yes. The split is {4, 5, 6} and {3, 4, 8}. Both sets have the same length, 3, and the same sum, 15. If in.txt contains 2 6 10 14 4 8 12 16. There are several splits satisfying the requirement: {2, 8, 10, 16} and {4, 6, 12, 14}; {4, 6, 10, 16} and {2, 8, 12, 14}; {4, 8, 10, 14} and {2, 6, 12, 16}; {6, 8, 10, 12} and {2, 4, 14, 16}. Your program does not need to find all of them. It must stop and print Yes after finding the first split satisfying the requirement. If in.txt contains 2 13 7 5 16 11, the program must print out No. The set of numbers cannot be divided into two subsets with equal sums and the same number of elements. If in.txt contains 7 8 2 4 5, the program must print out No. If in.txt contains 8 5 12 24 14 12 4 6, the program must print out No. Program Specification The program must solve the problem using a recursive algorithm without modifying the linked list. Although the program requirements specified below might seem contrived an unnecessarily restrictive, their only purpose is to guide you in the right direction and to prevent you from taking a wrong step. The solution, we are looking for is very short and simple. Your program has to: (a) Implement a linked list. You are supposed to implement the linked list from scratch without using language libraries. For example, you are not allowed to use any built-in dynamic data structures, such as ArrayList or LinkedList. Each node of the linked list must include only two elements: an integer and a reference to the next element from the list. There is no need to provide a full-fledged implementation of linked list. Keep your linked list implementation minimal and implement as much as you need to solve the problem. (b) Read the integers from the file in.txt and store them into the linked list in the same order in which they appear in the input file. No calculations are allowed at this step; the program must only read the input numbers and store them into the linked list. (c) Work on the linked list in order to find if the list can be divided into two subsets with equal sums and equal number of elements. All computations must be done in place on the original linked list. In other words, your program can use only one data structure (and only one instance of it), the linked list, some auxiliary scalar variables and reference variables (used to point to different nodes of the list). No additional data structures are allowed, such as a second linked list, an array, string, etc. The only exception is I/O where you can use strings to read in.txt. You can use standard I/O libraries to perform I/O. For example, you can use StringTokenizer or Scanner. This is the only place you can use strings and libraries. No data structures are allowed (with the exception of string used to read in.txt). If you have any doubts whether you can use a particular construct, email me. (d) Print Yes or No. (e) The linked list is immutable and your program should not modify it, i.e., it should remain in its original form as it was read from the input file in.txt. Homework must be solved by working on the linked list. For example, bypassing the homework restrictions by saving the numbers in a string, array, or any other date structure (with the exception of the linked list) is not allowed and will be penalized. There must be only one instance of the list and it must not be changed, i.e., after you have read the numbers from in.txt and have stored them in the list (in their original order) no further changes of the list are allowed. Using an integer as a binary mask to store information about subset membership is not allowed. A binary mask is a binary coding of an integer to denote a possible subset configuration. For example, a zero bit in position K in the binary mask means that the K-th integer in the list belongs to a given subset, whereas a zero bit in position K means that the K-th integer in the list does not belong to the subset. By generating all possible binary representations of integers of length N (N is the length of the list) one can generate all possible subset configurations and check if one of them consists of two subsets with equal sums and equal number of elements. Binary masks of this type or variations of them are not allowed. Inner classes are allowed. Try to write simple and short programs. I/O, exception handling, and the linked list implementation could be kept to a minimum. You can also assume that the input is correct and that in.txt is in the same folder as your Java program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

C++ Database Development

Authors: Al Stevens

1st Edition

1558283579, 978-1558283572

Students also viewed these Databases questions