JAVA Language Create a class named Module2. You should submit your source code f
ID: 3755209 • Letter: J
Question
JAVA Language
Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) 1) Data fields a. A Scanner object named reader (your class will need to import the Scanner class) b. Three String objects named inputA, inputB, and command a. A Module2 constructor method that initializes all three Strings (inputA, inputB, and command) to b. A main method that, in order: 2) Methods " (an empty String) . Creates a new Module2 object using the constructor from a) . Prints the text "starting application" to the console . Calls method c) and assigns the returned String to inputA Calls method c) again and assigns the returned String to note . Calls method d) with inputA and inputB as arguments, and prints the String returned by d) . Prints the text "ending application" to the console c. A method named getlnputString that accepts no parameters, prints a prompt to the console (i.e "please enter a word"), accepts a user-entered String via the console (use Scanner.nextLine)) prints a confirmation message to the console (i.e., "Thank you."), and returns the entered String A method named getCommand that accepts two Strings as parameters, prompts the user to enter a command (i.e., "please enter a command"), and proceeds as follows . If the command is "concat", the method returns the first parameter String concatenated with d. the second parameter String If the command is "firsts", the method returns (as a String) the first characters of the two parameter Strings If the command is "lasts", the method returns (as a String) the last characters of the two parameter Strings . .Explanation / Answer
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
Module2.java
--------
import java.util.Scanner;
public class Module2 {
private Scanner reader;
private String inputA,inputB, command;
public Module2(){
inputA = "";
inputB = "";
command = "";
reader = new Scanner(System.in);
}
public static void main(String[] args) {
Module2 m2 = new Module2();
System.out.println("starting application");
m2.inputA = m2.getInputString();
m2.inputB = m2.getInputString();
String result = m2.getCommand(m2.inputA, m2.inputB);
System.out.println(result);
System.out.println("ending application");
}
public String getInputString(){
System.out.print("please enter a word ");
String word = reader.nextLine();
System.out.println("Thank you.");
return word;
}
public String getCommand(String a, String b){
System.out.print("please enter a command ");
command = reader.nextLine();
if(command.equals("concat"))
return a + b;
else if(command.equals("firsts"))
return a.charAt(0) + "" + b.charAt(0);
else if(command.equals("lasts"))
{
int lastIndex1 = a.length() - 1;
int lastIndex2 = b.length() - 1;
return a.charAt(lastIndex1) + "" + b.charAt(lastIndex2);
}
else
return "unknown command";
}
}
output
------
starting application
please enter a word hello
Thank you.
please enter a word world
Thank you.
please enter a command concat
helloworld
ending application
-----
starting application
please enter a word hello
Thank you.
please enter a word world
Thank you.
please enter a command lasts
od
ending application
----
starting application
please enter a word hello
Thank you.
please enter a word world
Thank you.
please enter a command firsts
hw
ending application
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.