please rewrite this code as Pseudo-Code,.. basically rewrite the code but in eng
ID: 3822642 • Letter: P
Question
please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank you so much!
Example : if(5 > x) should be like if(5 is greater than x)
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
class WriteToFile{
/*Static method to write data to file from Array */
static void writeArray(String filename,int[] array) {
FileWriter fw = null;
BufferedWriter bw = null;
try {
fw = new FileWriter(filename);
bw = new BufferedWriter(fw);
bw.write(Arrays.toString(array));
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
/*Static method to read data from file.
if want to add data to array, import JSON library and uncomment the commented code . */
static void readArray(String filename,int[] array) {
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String Line;
while ((Line = br.readLine()) != null) {
System.out.println(Line);
}
/* JSONArray jsonArray = (JSONArray) new JSONObject(new JSONTokener("{data:"+Line+"}")).get("data");
for(int i=0; i<jsonArray.length(); i++) {
array[i] = jsonArray.getInt(i);
}
System.out.println(Arrays.toString(array));
*/
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String file = new String("try.txt");
int[] array = new int[] {11,22,33,44,55};
int[] array2 = new int[6];
writeArray(file,array);
readArray(file,array2);
}
}
Explanation / Answer
Hi below is the pseudo code without actual code:
Import Arrays class from java.util package
Import BufferedWriter class from java.io package
Import BufferedReader class from java.io package
Import FileWriter class from java.io package
Import FileReader class from java.io package
Import IOException class from java.io package
create class with name WriteToFile
create static method with name writeArray which takes String and int[]-Array of integer values as parameters to write data to file from int aray passed
Initialize FileWriter fw to null
Initialize BufferedWriter bw to null;
start of try block
set fw to new FileWriter(filename);
set bw to new BufferedWriter(fw);
set the array to bw using method write(Arrays.toString(array));
print "Done"
catch exception
print exception through e using printStackTrace method of IOException
finally block start
try block inside finally
if (bw not equals to null)
call close method of bw
if (fw not equals to null)
call close method of fw
catch exception inside finally block
print exception through e using printStackTrace method of IOException
create method readyArray with String and int array as parameters
start of try block
set BufferedReader br to new BufferedReader(new FileReader(filename));
create String variable Line;
while br has another line to read and is not null
print Line.
catch exception block
print exception through e using printStackTrace method of IOException
main method to execute the program
create String file variable and set new Sting object with value "try.txt"
create Array of integers variable array and assign values {11,22,33,44,55}
create Array of integers variable array2 and with capacity as 6
call writeArray method with file and array as parameters
call readArray method with file and array2 as parameters
Pseudo code with code
import java.util.Arrays; //Import Arrays class from java.util package
import java.io.BufferedWriter;//Import BufferedWriter class from java.io package
import java.io.BufferedReader;//Import BufferedReader class from java.io package
import java.io.FileWriter;//Import FileWriter class from java.io package
import java.io.FileReader;//Import FileReader class from java.io package
import java.io.IOException;//Import IOException class from java.io package
class WriteToFile{// create class with name WriteToFile
/*Static method to write data to file from Array */
static void writeArray(String filename,int[] array) { //create static method with name writeArray which takes String and int[]-Array of integer values as parameters to write data to file from int aray passed
FileWriter fw = null;//Initialize FileWriter fw to null
BufferedWriter bw = null;//Initialize BufferedWriter bw to null;
try {//start of try block
fw = new FileWriter(filename);// set fw to new FileWriter(filename);
bw = new BufferedWriter(fw);// set bw to new BufferedWriter(fw);
bw.write(Arrays.toString(array));//set the array to bw using method write(Arrays.toString(array));
System.out.println("Done");// print "Done"
} catch (IOException e) {//catch exception
e.printStackTrace();// print exception through e using printStackTrace method of IOException
} finally {//finally block start
try {//try block inside finally
if (bw != null)// if (bw not equals to null)
bw.close();//call close method of bw
if (fw != null)// if (fw not equals to null)
fw.close();//call close method of fw
} catch (IOException ex) {//catch exception inside finally block
ex.printStackTrace();//print exception through e using printStackTrace method of IOException
}
}
}
/*Static method to read data from file.
if want to add data to array, import JSON library and uncomment the commented code . */
static void readArray(String filename,int[] array) {//create method readyArray with String and int array as parameters
try {//start of try block
BufferedReader br = new BufferedReader(new FileReader(filename));//set BufferedReader br to new BufferedReader(new FileReader(filename));
String Line;//create String variable Line;
while ((Line = br.readLine()) != null) {// while br has another line to read and is not null
System.out.println(Line);//print Line.
}
} catch (IOException e) {//catch exception block
e.printStackTrace();//print exception through e using printStackTrace method of IOException
}
}
public static void main(String[] args) {//main method to execute the program
String file = new String("try.txt");// create String file variable and set new Sting object with value "try.txt"
int[] array = new int[] {11,22,33,44,55};// create Array of integers variable array and assign values {11,22,33,44,55}
int[] array2 = new int[6];//create Array of integers variable array2 and with capacity as 6
writeArray(file,array);//call writeArray method with file and array as parameters
readArray(file,array2);//call readArray method with file and array2 as parameters
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.