Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Java Code below needs to have comments added. Once you have correctly run an

ID: 3752385 • Letter: T

Question

The Java Code below needs to have comments added. Once you have correctly run and tested the program, comment in the source code where there is a “Comment Here” statement. Also respond to the 3 challenge questions below. Be sure your response includes proper rationale to support your reasoning. Copy and paste completed code and type answers to challenge questions.

Code:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class CopyFileContent {

public static void main(String[] args) {

/* Comment Here */

File sourceFile = new File("file1.txt");

/* Comment Here */

File destFile = new File("file2.txt");

/* Comment Here */

if (!destFile.exists()) {

try {

destFile.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

InputStream input = null;

OutputStream output = null;

try {

/* Comment Here */

input = new FileInputStream(sourceFile);

/* Comment Here */

output = new FileOutputStream(destFile);

byte[] buf = new byte[1024];

int bytesRead;

while ((bytesRead = input.read(buf)) > 0) {

output.write(buf, 0, bytesRead);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

finally {

try {

if (null != input) {

input.close();

}

if (null != output) {

output.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

Challenge Questions:

1. The services and functions provided by an operating system can be divided into two main categories. Briefly describe the two categories and discuss how they differ.

2. What are the five major activities of an operating system with regard to file management?

3.What is the main advantage for an operating-system designer of using a virtual-machine architecture? What is the main advantage for a user?

Explanation / Answer

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class file {

public static void main(String[] args) {

/*It will create new file instance in the memory and searches for

* file1.txt in the directory. If not found it will not create a new one

* and gives fileNotFound Exception

*/

File sourceFile = new File("file1.txt");

/*It will create new file instance in the memory and searches for

* file2.txt in the directory. If not found it will not create a new one

* and gives fileNotFound Exception

*/

File destFile = new File("file2.txt");

/*It will look for file2.txt and if file2.txt does'nt exist, it will create new one.*/

if (!destFile.exists()) {

try {

destFile.createNewFile();

} catch (IOException e) {

e.printStackTrace();

}

}

InputStream input = null;

OutputStream output = null;

try {

/*It will read the source file i.e file1.txt*/

input = new FileInputStream(sourceFile);

/* It will write the result in the destination file i.e file2,txt*/

output = new FileOutputStream(destFile);

byte[] buf = new byte[1024];

int bytesRead;

while ((bytesRead = input.read(buf)) > 0) {

output.write(buf, 0, bytesRead);

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

finally {

try {

if (null != input) {

input.close();

}

if (null != output) {

output.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

Challenge Questions:

1. The two main categories are

a. To provide protection to the processes which are running concurerntly accross the system. These processes are allowed only to access those memory locations which are asssociated with their address spaces.The processes are not allowed to access the devices directly without the permission of operating system.

b. To provide two new services in terms of virtual memory and file system in order to overcome the problem which is not supported directly by current hardware configuration.

2. Five major activities of an opeating system with regards to file management are:

a. Creating and deleting Files : file creation and deletion are very basic to computer operation. Previously data can't be stored in efficient manner unless stored in some file structure. Now, if files are not deleted it will occupy memory space and it will be a memory issue.

b . Creating and deleting directories : Data needs to be stored in files and files themselves need to be stored in directories. So efficient creation and deletion of directory is needed to perfrm the opetrtions.

c. File level instructor : Operating system allow softwares to do file manipulation with the help of system level instructor. operating system needs machine level instruction set to connect with the hardware directly.

d. Mapping to permanent storage : Operating system needs to store the files into their physical location in order to store and retreive the files in an efficient manner.

e. Backing up files: Files are the things which require lots of time to be stored. Loss of file is a serious concerns. So operating system provides a way to store files in additional secure media in order to prevent loss of files.

3. The main advantages of an operating-system designer of using a virtual-machine(VM) architecture are:

a. Security : Before VM runs for suspicious activity, it verifies all the programms that it comes accross. Any minor or major erros that ot encountors will be stored in specific area of VM.

b. Cross Platform: Apart from security, VM comes with cross platform architecture i.e the program needs to be compiled once and can be used in wide variety of system without again need to recomiple it. This feature leads to the application to be platform independent.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote