In: Computer Science
Linux Question: Describe how you created above file and its contents in VI.
a) Bash/VI commands: 20pts;
b) Non-brute-force solution: 10pts).
Note: Brute-force solution means that entering characters one by one
Install JDK 1.8 by the following command: $ sudo dnf –y install java-1.8.0-openjdk-devel.x86_64
Use VI to create a file “YourFirstNameHomework.java” (e.g., “MollyHomework.java”) and add the following contents into it.
(grading details: file name format:10 pts, a screenshot of the file content in the VI environment: 10pts, paragraphs: 10pts, empty lines and indentations: 10, text: 10pts)
import java.util.Scanner; // Import the Scanner class
class CynthiaHomework {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter your first name");
String fName = myObj.nextLine(); // Read user input of the first name
System.out.println("Enter your last name");
String lName = myObj.nextLine(); // Read user input of the last name
System.out.println("Hello " + fName + “ “ + lName); // Output user input
}
}
To compile the above java code, run the following command (you need to use your java file name): $ javac MollyHomework.java
To execute the java program, run the following command $ java MollyHomework
To do the stated commands follows the instructions:
1.Intially open the command prompt by using
then you are set to go.
2. Now type in
$ mkdir javaprograms
$ cd javaprograms
$ vi MollyHomework.java
The cmd will open the file for you and then paste the code in the file:
import java.util.Scanner; // Import the Scanner class
class CynthiaHomework {
public static void main(String[] args) {
Scanner myObj = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter your first name");
String fName = myObj.nextLine(); // Read user input of the first name
System.out.println("Enter your last name");
String lName = myObj.nextLine(); // Read user input of the last name
System.out.println("Hello " + fName + “ “ + lName); // Output user input
}
}
to move out of the vi editor:
Quit the vi editor by saving your changes
And back to the cmd and type in
$ javac MollyHomework.java
If there are any errors then it will show or else continue..
$ java MollyHomework.java
And then it will show the output.