1) Create a folder called pgm1 2) Using a LINUX EDITOR, in your pgm1 folder, cre
ID: 3879175 • Letter: 1
Question
1) Create a folder called pgm1
2) Using a LINUX EDITOR, in your pgm1 folder, create a Java program named:
Your lastName, firstLetterOfYourFirstName, pgm1U.java that will do the following:
Start here
3) From your program's main method, passing your name, call a new method named: processUbuntu
4) At the processUbuntu method, call a new method named printMyUbuntuName, passing the data
received from the main method
5) At the printMyUbuntuName method display the data received from the processUbuntu method
using the "System.out.printf" java method.
Explanation / Answer
Open the terminal on a Linux machine and do the following.
First, make a directory pgm1 as mentioned using the following command in terminal.
aroha@Rainbow-1:~$ mkdir pgm1
Change the current directory to the pgm1.
aroha@Rainbow-1:~$ cd pgm1
Create a new java file. The below command will create and open it in the editor .
Note: I am using the name James bond so the file name is jamesbpgm1U.java as mentioned in the question.
aroha@Rainbow-1:~/pgm1$ gedit jamesbpgm1U.java
Type the following program in the text editor.
public class jamesbpgm1U {
// Main Function from which program execution starts.
public static void main(String[] args) {
ProcessUbuntu("James Bond"); // calling processUbuntu method using parameter as james bond.
}
// ProcessUbuntu fnction which takes a string as parameter.
private static void ProcessUbuntu(String name) {
printMyUbuntuName(name); // calling the printMyUbuntuName method and passing the name(James bod) // recived from main method.
}
private static void printMyUbuntuName(String name1){
System.out.printf ("%s ", name1); // printing the name recived from processUbuntu method. %s is used to //indicate its a string.
}
Note : the name of file and class is required to be same some times.
Run the program as shown below
aroha@Rainbow-1:~/pgm1$ javac jamesbpgm1U.java
A new file will be created named as jamesbpgm1U.class run it as shown below.
aroha@Rainbow-1:~/pgm1$ java jamesbpgm1U
james bond
Explanation:
1. Main method: The main method is where the program execution starts.It will call the processUbuntu method by passing your name as a parameter. you can replace "James bond" with your actual name. Now the control goes to processUbuntu method.
2.processUbuntu Method: It simply takes "James bond" and holds it in its local variable( name). so here name is equal to James bond.It will call the printMyUbuntuName method by passing the name as a parameter. Now the control goes to processUbuntu method.
3.printMyUbuntuName method: It simply takes your name from processUbuntu method and holds it in its local variable( name1). so here name1 is equal to name which is equal to James bond.
4.System.out.printf(format, args): it takes two arguments one is a format which describes what we want to print in this case a string so %s. Next is the actual argument to print. which is name1 which is nothing but James bond .
Hope it clarifies.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.