Question

In: Computer Science

A. Write a program called OnesZerosA.java to do all of the following. Please note, for this...

A. Write a program called OnesZerosA.java to do all of the following. Please note, for this problem you are not allowed to use ArrayList.

Read a file containing ones and zeros (such as the example below, also available at http://www2.hawaii.edu/~esb/2020fall.ics111/oneszeros.txt) into a two-dimensional int array. Your program must create the array with the same number of rows as the number of lines in the file, and the same number of columns as the number of ones and zeros in each line.

Your program must (a) read the name of the file from the command-line arguments, (b) open the file, (c) check that each line has the same number of characters, and (d) check that the only characters in a line are ones and zeros. If there is no such command-line argument, or no such file, or if any of the the checks fail, your program should exit -- it is OK if your program exits by returning from main, by throwing an exception, or in any other way.

Once you have completed all your checks, allocate the int array and read the file again to fill it with the ones and zeros.

Next, print each of the elements of the array by row and column, giving a result such as:

0000000000001000000000000
0000000000010100000000000
0000000000100010000000000
0000000001000001000000000
0000000010000000100000000
0000000100000000010000000
0000001000000000001000000
0000010000000000000100000
0000100000000000000010000
0001000000000000000001000
0010000000000000000000100
0100000000000000000000010
1000000000000000000000001
0100000000000000000000010
0010000000000000000000100
0001000000000000000001000
0000100000000000000010000
0000010000000000000100000
0000001000000000001000000
0000000100000000010000000
0000000010000000100000000
0000000001000001000000000
0000000000111110000000000
0000000000000000000000000
0000000000000000000000000

Solutions

Expert Solution

Complete code in java:-

import java.util.*;
import java.io.*;

class OnesZerosA {
   public static void main(String ... args) {
       // Taking file name in 'filename' from command line argument.
       String filename = args[0];
       try {
           // Creating file object.
           File file = new File(filename);
           // Creating Scanner object to from 'file'.
           Scanner sc = new Scanner(file);
           // 'rows' and 'cols' will define number of rows and number of columns for the array
           int rows, cols;
           rows = 0;
           cols = 0;
           // Check variable will tell that file is valid or invalid for the desired conditions.
           boolean check = true;
           // Reading from file one row at a time.
           while(sc.hasNextLine()) {
               String line = sc.nextLine();
               rows++;
               // Checking if length of all rows are same.
               if(cols != 0 && line.length() != cols) {
                   check = false;
               }
               cols = line.length();
               // Checking if each character of row is either '1' or '0'
               for(int i = 0; i < cols; i++) {
                   if(line.charAt(i) != '0' && line.charAt(i) != '1') {
                       check = false;
                       break;
                   }
               }
               if(!check) {
                   break;
               }
           }
           // If file is valid, make integer matrix.
           if(check) {
               // Again creating scanner object to read from file.
               sc = new Scanner(file);
               // Creating of matrix of required size.
               int a[][] = new int[rows][cols];
               int i = 0;
               // Reading file, one row at a time.
               while(sc.hasNextLine()) {
                   String line = sc.nextLine();
                  
                   // Storing value in matrix.
                   for(int j = 0; j < line.length(); j++) {
                       a[i][j] = Character.getNumericValue(line.charAt(j));
                   }
                   i++;
               }
               // Printing matrix.
               for(i = 0; i < rows; i++) {
                   for(int j = 0; j < cols; j++) {
                       System.out.print(a[i][j]);
                   }
                   System.out.println();
               }
           }
           else {
               System.out.println("File is invalid");
           }
       } catch(FileNotFoundException e) {
           System.out.println("File not found! Try again with another file.\n");
       }catch(Exception e) {
           System.out.println("An exception occurred! Try again\n"+e);
       }
   }
}

Content of input file:-

01110101100011111
00110010011001011
10111100101011000
00000001000000011
00010011100011100
01011011010010110
10000101111111111
10111100100101001
11010011000100101
11011111110001111
01100010001100000
00111111011101110
11100111111011010
10011111000000110
01101011101101110
01101011011111001
00011000100100101
00011001101010001
10100001011100010
00010001000000111

Screenshot of output:-


Related Solutions

Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a...
Specifications: Write a Java program called LifeRaft.java that will do all of the following: Display a title Ask the user to enter the following values: The type of plane(Boeing 747 or Airbus A300) The number of passengers on board the Plane The number of crew aboard the plane The maximum number of people that can be carried by one liferaft assuming all the liferafts on the plane are the same size The actual number of liferafts that are available on...
Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".
 c++Write a program to complete the following tasks. Record all results to a data file called "GradeSheet.dat".(1) Read the grade sheet from attached data file, "Assignment4.dat". Print the original grade sheet.(3) Write a module to sort array GRADE[] and print the sorted grade sheet.(4) Write a module to sort array NAME[] and print the sorted grade sheet.(5) Write a module to sort array ID[] and print the sorted grade sheet.(6) Write a module to print the student's id, name, and...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all...
Module 1 Program Write a complete Java program in a file called Module1Program.java that reads all the lyrics from a file named lyrics.txt that is to be found in the same directory as the running program. The program should read the lyrics for each line and treat each word as a token. If the line contains a double (an integer is also treated as a double) it should use the first double it finds in line as the timestamp for...
(Note: Do not attempt to solve if you cannot answer all) Please answer the following discussion...
(Note: Do not attempt to solve if you cannot answer all) Please answer the following discussion questions in detail on company "Vitamin Shoppe": 1) Review the organizational structure for "Vitamin Shoppe" and determine if and how it needs to be adjusted in order to be aligned with the new strategy or strategies.
Complete all parts of this java program. Use Homework2Driver.java below for testing. /* Note: Do not...
Complete all parts of this java program. Use Homework2Driver.java below for testing. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class Node<T> { private T info; private Node nextLink; public Node(T info) { } public void setInfo(T info) { } public void setNextLink(Node nextNode) { } public T getInfo() { } public Node getNextLink() { } } /* Note:...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives...
C++ PLEASE Write a new program called Lab15A Write a void function named fillArray that receives an array of 10 integers as a parameter and fills the array by reading the values from a text file (Lab15A.txt). It should also print the values of the array all on the same line, separated by spaces. Write an int function named findLast that receives an array of 10 integers as a parameter and returns the last negative number in the array. Write...
Question 4 (Please note that you do NOT need to write a scientific answer to the...
Question 4 (Please note that you do NOT need to write a scientific answer to the question in bold below. Simply use the question to demonstrate your ability to break down a question into its key parts. Write your answers in the spaces indicated below) For the question below, identify the a) key topic word, b) the influencer words and c) the instruction words. Describe the major structural differences between Arachnids and Crustaceans and explain how their mouthparts work.
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
Write a complete program in java that will do the following:
Write a complete program in java that will do the following:Sports:             Baseball, Basketball, Football, Hockey, Volleyball, WaterpoloPlayers:           9, 5, 11, 6, 6, 7Store the data in appropriate arraysProvide an output of sports and player numbers. See below:Baseball          9 players.Basketball       5 players.Football           11 players.Hockey            6 players.Volleyball        6 players.Waterpolo       7 players.Use Scanner to provide the number of friends you have for a team sport.Provide an output of suggested sports for your group of friends. If your...
( PLEASE SHOW ALL YOUR WORK). I MPORTANT NOTE: Make sure you do the following: -State...
( PLEASE SHOW ALL YOUR WORK). I MPORTANT NOTE: Make sure you do the following: -State Ho and Ha using notation for each hypothesis test conducted. -Use α= 0.05 for all hypothesis tests conducted. -Explain all results obtained for both hypothesis tests and confidence intervals. You will need your ticker code (company abbreviation) for stock prices for this question. Use your ticker code to obtain the closing prices for the following two time periods to obtain two data sets: March...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT