Question

In: Computer Science

Java Programming: In the program shown below, I want to print the result in the output...

Java Programming:

In the program shown below, I want to print the result in the output file. The main static method calls a method, displaySum which adds two integers and prints the result (sum). The problem is that I am not able to use "outputFile.print()" inside displaySum to print the content as the output file. I also want to display the content of two other methods, but I am facing this difficulty.

** DisplayOutput.java **

import java.io.*;
public class DisplayOutput
{
   public static void main(String[] args) throws IOException
   {
       FileWriter fw = new FileWriter("output.txt");
       PrintWriter outputFile = new PrintWriter(fw);
      
       outputFile.println("Suppose two inetegers are 2 and 3.");
       outputFile.println("Display the sum of two inetegers (2 and 3).");
       displaySum(2,3);
       outputFile.close();
   }
  
   private static void displaySum(int n1, int n2)
   {
       int sum = n1 + n2;
      
       outputFile.print("Sum of two integers (2 and 3) is: " + sum); // Error: outputFile cannot be resolved
   }  
}

Solutions

Expert Solution

This can be solved in 2 ways, I have provided both solutions:

Solution 1)

Pass the  PrintWriter outputFile, as an argument to method displaySum

---------DisplayOutput.java---------------------

import java.io.*;


public class DisplayOutput
{
public static void main(String[] args) throws IOException
{
FileWriter fw = new FileWriter("output.txt");
PrintWriter outputFile = new PrintWriter(fw);

outputFile.println("Suppose two inetegers are 2 and 3.");
outputFile.println("Display the sum of two inetegers (2 and 3).");
displaySum(2,3, outputFile); //Pass the outpuFile argument to displaySum Method
outputFile.close();
}


private static void displaySum(int n1, int n2, PrintWriter outputFile)
{
int sum = n1 + n2;
outputFile.print("Sum of two integers (2 and 3) is: " + sum);
}
}

---------------------------------------------------------------------------------

Solution 2)

Declare the FileWriter and PrintWriter objects outside main, so they can be accessed in all methods

---------DisplayOutput.java---------------------

import java.io.*;


public class DisplayOutput
{
   static FileWriter fw ;//Declare file and print objects outside, so it can be accessed in all methods
   static PrintWriter outputFile;
   public static void main(String[] args) throws IOException
   {
       fw = new FileWriter("output.txt");
       outputFile = new PrintWriter(fw);

       outputFile.println("Suppose two inetegers are 2 and 3.");
       outputFile.println("Display the sum of two inetegers (2 and 3).");
       displaySum(2,3); //Pass the outpuFile argument to displaySum Method
       outputFile.close();
   }


   private static void displaySum(int n1, int n2)
   {
       int sum = n1 + n2;
       outputFile.print("Sum of two integers (2 and 3) is: " + sum);
   }
}

-------------------Output---------------------

-----------------------------------------

I hope this helps you,

Please rate this answer if it helped you,

Thanks for the opportunity


Related Solutions

Write a Java program to print the pattern of asterisks shown below. For i=1 * For...
Write a Java program to print the pattern of asterisks shown below. For i=1 * For i=2 * * * For i=3 * ** * * * For i=n * * * * * * … … … * * * * * * ……… n
New to C programming and I am stuck. The program will print "no input" if no...
New to C programming and I am stuck. The program will print "no input" if no input is given. If a command line argument is given the program will print "input:" followed by the user input. Below is the code I put together to do as desired but I get errors. What do I need to fix to get my code to compile correctly? #include <stdio.h> #include <stdlib.h> int main () { char input; scanf("%c", &input); if (input == NULL)...
I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a Java program that uses nested for loops to print a multiplication table as shown...
Write a Java program that uses nested for loops to print a multiplication table as shown below.   Make sure to include the table headings and separators as shown.   The values in the body of the table should be computed using the values in the heading   e.g. row 1 column 3 is 1 times 3.
in java Write a program that will print the following output 1 1 2 1 1...
in java Write a program that will print the following output 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 18 8 4 2 1
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program...
Java - Text File to Arrays and output ------------------------------------------------------------------------------ I need to have a java program read an "input.txt" file (like below) and store the information in an respective arrays. This input.txt file will look like below and will be stored in the same directory as the java file. The top line of the text represents the number of people in the group for example. the lines below it each represent the respective persons preferences in regards to the other...
Java Program. Please read carefully. i need the exact same output as below. if you unable...
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks To the HighArray class in the highArray.java program (Listing 2.3), add the following methods: 1.      getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty. 2.      removeMax() that removes the item with the highest key from the...
Write a Java program that implements a queue in a hospital. I want your program to...
Write a Java program that implements a queue in a hospital. I want your program to ask the user to enter the number of patients then enter the patient number starting from 110 till the end of the queue then print number of patients waiting in the queue. Suppose you have a queue D containing the numbers (1,2,3,4,5,6,7,8), in this order. Suppose further that you have an initially empty Stack S. Give a code fragment that uses S, to store...
I need original java code that completes this program and gets it to print out results...
I need original java code that completes this program and gets it to print out results just like in the example. Also please upload answer in a word document format only. Implement both linear search and binary search, and see which one performs better given an array 1,000 randomly generated whole numbers (between 0-999), a number picked to search that array at random, and conducting these tests 20 times. Each time the search is conducted the number of checks (IE...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT