Answer to Question #274971 in Java | JSP | JSF for Muhammad Ahsan

Question #274971

Show the following in your testing section (supported by screen shots):


·        2 x successful execution of Add_money between one client and the server (two shots – one for the client, one for the server) with different clients (e.g., one example with A and one with B)

·        2 x successful execution of Subtract_money between one client and the server (two shots – one for the client, one for the server) with different clients (e.g., one example with A and one with C)

·        2x successful execution of Transfer_money between one client and the server (two shots – one for the client, one for the server) with different clients (e.g., one example with A transferring between A and B, one with C transferring between B and C)

Remember that these should show the connection, the protocol, correct locking and updates of relevant values.



1
Expert's answer
2021-12-03T18:53:09-0500
import java.util.Scanner;

public class Main {
    public static void displayArray(char[] array, int start, int end) {
        for (int i = start; i <= end; i++) {
            System.out.print(array[i]);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String data;
        while (true) {
            System.out.println("Enter a string(leave empty to exit)");
            data = in.nextLine();
            if (data.length() == 0) {
                break;
            }
            char[] source = data.toCharArray();
            char[] swapped = new char[source.length];

            int firstStart = 0;
            int firstEnd = 0;
            boolean firstFound = false;
            int lastStart = 0;
            int lastEnd = 0;
            boolean lastFound = false;

            for (int i = 0; i < source.length; i++) {
                if (source[i] >= 'a' && source[i] <= 'z' || source[i] >= 'A' && source[i] <= 'Z') {
                    if (!firstFound) {
                        firstStart = i;
                        firstEnd = i;
                        firstFound = true;
                    } else {
                        firstEnd = i;
                    }
                } else if (firstFound) {
                    break;
                }
            }

            for (int i = source.length - 1; i >= 0; i--) {
                if (source[i] >= 'a' && source[i] <= 'z' || source[i] >= 'A' && source[i] <= 'Z') {
                    if (!lastFound) {
                        lastEnd = i;
                        lastStart = i;
                        lastFound = true;
                    } else {
                        lastStart = i;
                    }
                } else if (lastFound) {
                    break;
                }
            }
            if (firstStart != lastStart) {
                int j = 0;
                for (int i = 0; i < firstStart; i++, j++) {
                    swapped[j] = source[i];
                }
                for (int i = lastStart; i <= lastEnd; i++, j++) {
                    swapped[j] = source[i];
                }
                for (int i = firstEnd + 1; i < lastStart; i++, j++) {
                    swapped[j] = source[i];
                }
                for (int i = firstStart; i <= firstEnd; i++, j++) {
                    swapped[j] = source[i];
                }
                for (int i = lastEnd + 1; i < source.length; i++, j++) {
                    swapped[j] = source[i];
                }
            } else {
                for (int i = 0; i < source.length; i++) {
                    swapped[i] = source[i];
                }
            }
            System.out.println("Source:");
            displayArray(source, 0, source.length - 1);
            if (firstFound) {
                System.out.println("First word:");
                displayArray(source, firstStart, firstEnd);
                System.out.println("Last word:");
                displayArray(source, lastStart, lastEnd);
            }
            System.out.println("After swap:");
            displayArray(swapped, 0, swapped.length - 1);
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS