Question

Write the following Java code for a file called input.txt that has the below text in...

Write the following Java code for a file called input.txt that has the below text in it.


5 6

1 1 0 0 1 1

0 1 0 0 1 1

0 1 0 0 2 1

0 3 1 1 1 1

1 2 3 4 5 1

2

0 1 hello

2 3 bye


Create the Entry class.

· Entry should have 3 data members

o int x.

o int y.

o String name.

Create a class called Level.

· Level should have a constructor, which takes in a file name.

o It then uses the filename to open the file and read the data.

o It reads in the data into the level class.

· You will probably want methods for toString and maybe accessors.

· The level class has the following:

· A y size and x size for the amount of data.

· A 2d array of map data (integers) (you can use arraylist if you want to instead).

· The number of Entries in a list.

· A list of Entry.

Create the client.

· Create a level object by passing in the filename to the string object.

o You can hardcode the string.

· Print the level to the screen.

· Print the level to a file using the same format (you can hardcode the output file name).

5 6 //this is the y and x size. Y = [number of rows] X = [number of columns per row]

1 1 0 0 1 1 //array

0 1 0 0 1 1

0 1 0 0 2 1

0 3 1 1 1 1

1 2 3 4 5 1

2 //the number of entries in the list below

0 1 hello //first entry in the list

2 3 bye //second entry in the list; there are 2 Entry because “2” was the number of entries

Example run of output to java text window:


5 6

1 1 0 0 1 1

0 1 0 0 1 1

0 1 0 0 2 1

0 3 1 1 1 1

1 2 3 4 5 1

2

0 1 hello

2 3 bye


Example run of code:

6 5

1 1 0 0 1 1

0 1 0 0 1 1

0 1 0 0 2 1

0 3 1 1 1 1

1 2 3 4 5 1

2

0 1 hello

2 3 bye

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here are the complete code

___________________________________________________________________________________________

public class Entry {

    private int x;
    private int y;
    private String name;

    public Entry(int x, int y, String name) {
        this.x = x;
        this.y = y;
        this.name = name;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return x + " " + y + " " + name;
    }
}

____________________________________________________________________________________________

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.ArrayList;

public class Level {

    private int sizeY;
    private int sizeX;
    private int[][] mapData;
    private ArrayList<Entry> entries;

    public Level(String fileName) {
        entries = new ArrayList<Entry>();
        loadData(fileName);
    }

    private void loadData(String fileName) {
        try {
            Scanner scanner = new Scanner(new File(fileName));
            String[] sizes = scanner.nextLine().split("\\s+");

            sizeY = Integer.parseInt(sizes[0]);
            sizeX = Integer.parseInt(sizes[1]);

            mapData = new int[sizeY][sizeX];
            for (int index = 1; index <= sizeY; index++) {
                String[] rowNums = scanner.nextLine().split("\\s+");
                for (int col = 0; col < sizeX; col++) {
                    mapData[index - 1][col] = Integer.parseInt(rowNums[col]);
                }
            }

            int entryCount = Integer.parseInt(scanner.nextLine());

            for (int index = 1; index <= entryCount; index++) {

                String[] entryData = scanner.nextLine().split("\\s+");
                entries.add(new Entry(Integer.parseInt(entryData[0]),
                        Integer.parseInt(entryData[1]), entryData[2]));
            }

            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }


    public void printLevel() {
        for (int i=0;i<sizeY;i++) {
            for (int j=0;j<sizeX;j++) {
                System.out.print(String.format("%-3d", mapData[i][j]));
            }
            System.out.println();
        }

        for (Entry entry : entries) {
            System.out.println(entry);
        }
    }

    public static void main(String[] args) {
        String fileName = "D:\\abcdef.txt";
        Level level = new Level(fileName);
        level.printLevel();

    }
}
Add a comment
Know the answer?
Add Answer to:
Write the following Java code for a file called input.txt that has the below text in...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file...

    Java: Create the skeleton. Create a new file called ‘BasicJava4.java’ Create a class in the file with the appropriate name. public class BasicJava4 { Add four methods to the class that return a default value. public static boolean isAlphabetic(char aChar) public static int round(double num) public static boolean useSameChars(String str1, String str2) public static int reverse(int num) Implement the methods. public static boolean isAlphabetic(char aChar): Returns true if the argument is an alphabetic character, return false otherwise. Do NOT use...

  • The following code uses a Scanner object to read a text file called dogYears.txt. Notice that...

    The following code uses a Scanner object to read a text file called dogYears.txt. Notice that each line of this file contains a dog's name followed by an age. The program then outputs this data to the console. The output looks like this: Tippy 2 Rex 7 Desdemona 5 1. Your task is to use the Scanner methods that will initialize the variables name1, name2, name3, age1, age2, age3 so that the execution of the three println statements below will...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • composed the following java code to read a string from a text file but receiving compiling...

    composed the following java code to read a string from a text file but receiving compiling errors. The text file is MyNumData.txt. Included the original java script that generated the output file. Shown also in the required output results after running the java program. I can't seem to search for the string and output the results. Any assistance will be greatly appreciated. import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; public class Main {   public static void main(String[] args) {     System.out.print("Enter the...

  • using java File Details – Build a class called FileDetails.java. When you instantiate this class and...

    using java File Details – Build a class called FileDetails.java. When you instantiate this class and give it a filename, it will report back the size of the file, whether the file is Readable and whether the file is Writeable; plus any other file information that you might deem important. This cd goes in main FileDetails fd=newFileDetails(“anyfile.doc”); All other code goes in the constructor. Write a String to a File using PrintStream – This time build a class WriteString. This...

  • C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank...

    C Language program. Please follow the instructions given. Must use functions, pointers, and File I/O. Thank you. Use the given function to create the program shown in the sample run. Your program should find the name of the file that is always given after the word filename (see the command line in the sample run), open the file and print to screen the contents. The type of info held in the file is noted by the word after the filename:...

  • Your assignment is to write a grade book for a teacher. The teacher has a text file, which includ...

    Your assignment is to write a grade book for a teacher. The teacher has a text file, which includes student's names, and students test grades. There are four test scores for each student. Here is an example of such a file: Count: 5 Sally 78.0 84.0 79.0 86.0 Rachel 68.0 76.0 87.0 76.0 Melba 87.0 78.0 98.0 88.0 Grace 76.0 67.0 89.0 0.0 Lisa 68.0 76.0 65.0 87.0 The first line of the file will indicate the number of students...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

  • C++ Demonstrate an understanding of array processing. Write a function that accepts the name of a...

    C++ Demonstrate an understanding of array processing. Write a function that accepts the name of a file (by asking the user), an array of strings, and the size of the array. Display each character of each string vertically to the file. For example, if the array is called with an array of 3 strings “hello”, “abc”, “bye”, the output will be: h e l l o …. y e Test the function with a string declaration of: string name[] =...

  • I am trying to read from a file with with text as follows and I am...

    I am trying to read from a file with with text as follows and I am not sure why my code is not working. DHH-2180 110.25 TOB-6851 -258.45 JNO-1438 375.95 CS 145 Computer Science II IPO-5410 834.15 PWV-5792 793.00 Here is a description of what the class must do: AccountFileIO 1. Create a class called AccountFileIO in the uwstout.cs145.labs.lab02 package. (This is a capital i then a capital o for input/output.) a. This class will read data from a file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT