Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write java prog tasks ==========================================================

ID: 3910876 • Letter: W

Question

write java prog tasks

===========================================================

===============================================

==========================================

=======================================

Question 02: Write an interactive Java program that prompts or and reads a string string! and a word word·I? word is not the last wor ots rngl an error message is displayed and the program terminates; otherwise the program prompts for and reads another string string2 and another word word2. If word2 is not the first word of string2 an error message is displayed and the program terminates; otherwise the program writes, to the monitor: The contents of a new string in which each occurrence of wordl in stringl is replaced by word2. The contents of a new string in which each occurrence of word2 in string2 is replaced by word. Sample program runs: ease enter stringi: This is my brother and that is your brother Please enter wordl: brother Please enter string2: friend in need is a friend indeed Please enter word2: friend This is my friend and that is your friend brother in need is a brother indeed

Explanation / Answer

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// declaring variables

Scanner sc = new Scanner(System.in);

String one, first, two, last;

// taking input of string1 and word1

System.out.println("Please enter string1:");

one = sc.nextLine();

System.out.println("Please enter word1:");

last = sc.nextLine();

// if the last word matches

String lword = one.substring(one.lastIndexOf(" ")+1);

if(lword.equals(last))

{

// taking input of string2 and word2

System.out.println("Please enter string2:");

two = sc.nextLine();

System.out.println("Please enter word2:");

first = sc.nextLine();

// if first word matches, printing output

String fword = two.substring(0,two.indexOf(" "));

if(fword.equals(first))

{

System.out.println(one.replaceAll("\b"+last+"\b", first));

System.out.println(two.replaceAll("\b"+first+"\b", last));

}

else{

System.out.printf("Error: The word %s is not the first word of the string2", first);

}

}

else

{

System.out.printf("Error: The word %s is not the last word of the string1", last);

}

}

}

/*SAMPLE OUTPUT

Please enter string1:

He went to Jeddah after he read about Jeddah

Please enter word1:

Jeddah

Please enter string2:

Riyadh is a big city.

Please enter word2:

Riyadh

He went to Riyadh after he read about Riyadh

Jeddah is a big city.

Please enter string1:

Come here

Please enter word1:

please

Error: The word please is not the last word of the string1

Please enter string1:

I have a red car

Please enter word1:

car

Please enter string2:

He has a blue pen.

Please enter word2:

pencil

Error: The word pencil is not the first word of the string2

*/