Question

In: Computer Science

QUESTION: Add code so that if no parameter values are specified for method “main”, then the...

QUESTION:

Add code so that if no parameter values are specified for method “main”, then the error message “file name expected” is printed out instead of the "Opening file " message. Hint: If no parameter values are specified for method “main”, then the length of the array “args” will be zero. If that error message “file name expected” is printed out, the program should stop. You can make any Java program stop using this line …

  • System.exit(0);

skeleton code:

/**
* This is a simple program for the first lab, to familiarise students
* with the lab and with plate.
*
* @author Raymond Lister
* @version March 2015
*/
public class Assignment
{
public static void main(String[] args)
{
  
// DO NOT CHANGE THIS LINE OR ANYTHING ABOVE THIS LINE
String[] list = {"Hello.txt"};
System.out.println("Opening file "+ list[0]);

// DO NOT CHANGE THIS LINE OR ANYTHING BELOW THIS LINE
}
}

Solutions

Expert Solution

Source Code :

//assignment class
public class Assignment
{


//main function
public static void main(String[] args)
{

// DO NOT CHANGE THIS LINE OR ANYTHING ABOVE THIS LINE
  
String[] list = {"Hello.txt"};
//check main function has a parameter or not
//args array hold the arguments passed to main function
//if no arguments are passed the size of args array is 0
if(args.length==0){


//if arguments are not passed
//print the error message
// "file name expected"
System.out.println("file name expected ");
//after priting the error message
//stop the program
System.exit(0);


}
//if arguments are passed to main function
//print opening file

System.out.println("Opening file "+ list[0]);

// DO NOT CHANGE THIS LINE OR ANYTHING BELOW THIS LINE
}


}

Output and screenshots :

  • javac Assignment.java , is used to compile the java code.
  • java Assignment , is used to run the code created after compiling.
  • java Assignment "fileName" , here "fileName" is the argument passed to main function.


Related Solutions

HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT...
HOW DO I ADD ON TO THIS CODE SO THAT IT DISPLAYS ALL THE VALUES INPUT BY THE USER AS SPECIFIED IN THEH FIRST PART OF THE QUESTION? Ask the user to enter a number and display the number, followed by ***, followed by the number squared, followed by &&&, and followed by the number cubed. Allow the user to enter as many numbers as he/she wishes. So, use a reasonable sentinel value to end the loop (for example, -999)....
matlab code that performs overlap add method.
matlab code that performs overlap add method.
Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
Add a new method called insert_in_order() which accepts a number as its parameter and inserts that...
Add a new method called insert_in_order() which accepts a number as its parameter and inserts that number in the linked list in an ascending (sorted) order. Note that this method should be called for all the numbers (1, 5, 19, 7, 23, 17, 2) and the resulting linked list should come out sorted. (1, 2, 5, 7, 17, 19, 23) JAVA CODE BELOW class aNode { char data; aNode next; aNode(char mydata) { // Constructor data = mydata; next =...
In the following hypothetical scenarios, classify each of the specified numbers as a parameter or a...
In the following hypothetical scenarios, classify each of the specified numbers as a parameter or a statistic. There are 100 senators in the 114th Congress, and 54% of them are Republicans. The 54% here is a In 1936, Literary Digest polled 2.3 million adults in the United States, and 57% of them said they would vote for Alf Landon for the Presidency. The 57% here is a The survey results from 75 local elementary schools show the mean weight of...
Add two lines of code in main() function to create a variable, goodStudent, whose type is...
Add two lines of code in main() function to create a variable, goodStudent, whose type is Student with name, "John" and age, 21. public class Student { private String name; private int age; public Student(){ name = ""; age= 0; } public Student(String initName){ name = initName; age = 0; } public String getName() { return name; } public void setAge(int anAge) { age = anAge; } public static void main (String[] args) { // !!!! Your two lines of...
code the following methods: one returning the average weekly hours and a method to add a...
code the following methods: one returning the average weekly hours and a method to add a weeks hours to the array of hours (this means creating a larger array).
pseudocode The main( ) program passes these integer values as arguments to the findMax( ) method...
pseudocode The main( ) program passes these integer values as arguments to the findMax( ) method for further processing. The main program receives one result back from findMax( ) method and displays the maximum of these three integer values. The findMax( ) method simply receives three integer values from the main program and finds the maximum of three integers and returns one value back to the caller (in this case the main application).
Add to the code below so that when complied, it also shows the stalls which made...
Add to the code below so that when complied, it also shows the stalls which made a profit in increasing order of profit (lowest profit to highest profit) in output.txt #include <iostream> #include <fstream>// for file streaming using namespace std; int main() { struct Stall { string name; double income; double expenses; double net; }; double tprofit_loss = 0, most_profit; Stall tmp; int n = 0; Stall Stalls[100]; bool loop = true; ifstream f; //this is a input file object...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an...
**Add comments to existing ARM code to explain steps** Question that my code answers: Write an ARM assembly program to calculate the value of the following function: f(y) = 3y^2 – 2y + 10 when y = 3. My Code below, that needs comments added: FunSolve.s LDR R6,=y LDR R1,[R6] MOV R2,#5 MOV R3,#6 MUL R4,R1,R1 MUL R4,R4,R2 MUL R5,R1,R3 SUB R4,R4,R5 ADD R4,R4,#8 st B st y DCD 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT