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
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();
}
}
Write the following Java code for a file called input.txt that has the below text in...
Java : Please help me correct my code: create a single class (Program11.java) with a main method and some auxiliary methods to input a 2-D array from a disk file, input some “transactions” to change the 2-D array and output the changed 2-D array to another file. Your main method will be minimal (see below). Most of the work will go on in your methods. In main, declare (but do not instantiate) 2-D array. Then call the three methods. The...
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:...
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 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...
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 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...
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...
Using the diagram below and the starter code, write Document Processor that will read a file and let the user determine how many words of a certain length there are and get a count of all of the word lengths in the file. Your program should do the following: The constructor should accept the filename to read (word.txt) and then fill the words ArrayList up with the words from the file. The countWords method should accept an integer that represents...
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...
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...