please provide the answers for questions about java. 1. The question is about us
ID: 3567594 • Letter: P
Question
please provide the answers for questions about java.
1. The question is about using File class from the Java library. Create a File object fot the file data.txt in the working directory.
_________________________________
2. This question is about using the File class from the Java library. A File object named dataFile has been created in a program for a file. Write the code that will print out the size of the file along with a suitable message.
_________________________________
3. This question is about using the File class form the Java library. A File object named commerce has been created in a program for either a file or directory. Whatever it is, it does exist. Write the code that will pritn out a suitable message that states whether commerce is a regular file or a directory.
_________________________________
4. This question is about using the FIle class from the Java library. A File object named scores.odt has been created in a program for a file that might exist. Write the code that will print out a suitable message that staes whether scores.odt exists or not.
_________________________________
5. You are to write some code that is part of a larger program. Write the code that will write the message " the radius of the earth is 1" to a file named message.txt in the current working directory of the program.
_________________________________
6.You are to write some code that is part of a larger program. In the current working directory of the program is a file named data.txt with the following content:
Write the code that will read the two numbers from the file, add them together and display the sum with a suitable message.
_________________________________
7. Write a method with the following header:
public int slowAdd ( int x, int y)
This method works as follows:
1.If y is 0, then return x
2.Otherwise, call slowAdd with the parameters x and y-1, add one to the result and return the sum.
_________________________________
8. Write a method with the following header:
public int gcd(int x, int y)
This method works as follows:
1. If y is 0, then return x
2. Otherwise, call slowAdd with the first parameter equal to y and the second parameter equal to x%y. Return the result.
Note: this is actually a reasonable implementation of the greatest common divisor function.
35 46Explanation / Answer
1. The question is about using File class from the Java library. Create a File object fot the file data.txt in the working directory.
File dataFile = new File("data.txt");
2. This question is about using the File class from the Java library. A File object named dataFile has been created in a program for a file. Write the code that will print out the size of the file along with a suitable message.
System.out.println("Size of data file is "+dataFile.length());
3. This question is about using the File class form the Java library. A File object named commerce has been created in a program for either a file or directory. Whatever it is, it does exist. Write the code that will pritn out a suitable message that states whether commerce is a regular file or a directory.
if(commerce.isDirectory())
System.out.println("commerce is Directory");
else if(commerce.isFile())
System.out.println("commerce is File");
4. This question is about using the FIle class from the Java library. A File object named scores.odt has been created in a program for a file that might exist. Write the code that will print out a suitable message that staes whether scores.odt exists or not.
if(scores.odt.exists())
System.out.println("scores.odt exists");
else
System.out.println("scores.odt does not exist");
5. You are to write some code that is part of a larger program. Write the code that will write the message " the radius of the earth is 1" to a file named message.txt in the current working directory of the program.
PrintWriter pr = new PrintWriter(new File("message.txt"));
pr.println(" the radius of the earth is 1");
pr.close();
6.You are to write some code that is part of a larger program. In the current working directory of the program is a file named data.txt with the following content:
35 46 Write the code that will read the two numbers from the file, add them together and display the sum with a suitable message.
Scanner in = new Scanner(new File("data.txt"));
int a = in.nextInt();
int b = in.nextInt();
System.out.println("Integers read from file is " + a + " and " + b);
7. Write a method with the following header:
public int slowAdd ( int x, int y)
This method works as follows:
1.If y is 0, then return x
2.Otherwise, call slowAdd with the parameters x and y-1, add one to the result and return the sum.
public int slowAdd ( int x, int y){
if(y==0) return x;
else return 1+slowAdd(x,y-1);
}
8. Write a method with the following header:
public int gcd(int x, int y)
This method works as follows:
1. If y is 0, then return x
2. Otherwise, call slowAdd with the first parameter equal to y and the second parameter equal to x%y. Return the result.
Note: this is actually a reasonable implementation of the greatest common divisor function.
public int gcd(int x, int y){
if(y==0) return x;
else slowAdd (y,x%y);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.