Question

Add the package hw5p3 to your FirstName-LastName-HW5 project. public static void main(String[] args) { decideAnagrams("Mother In...

Add the package hw5p3 to your FirstName-LastName-HW5 project.

public static void main(String[] args) {

decideAnagrams("Mother In Law", "Hitler Woman");

decideAnagrams("keEp", "peeK");

decideAnagrams("SiLeNt CAT", "LisTen AcT");

decideAnagrams("Debit Card", "Bad Credit");

decideAnagrams("School MASTER", "The ClassROOM");

decideAnagrams("DORMITORY", "Dirty Room");

decideAnagrams("ASTRONOMERS", "NO MORE STARS");

decideAnagrams("Toss", "Shot");

decideAnagrams("joy", "enjoy");

}

static void decideAnagrams(String s1, String s2) {

// Removing all white spaces from s1 and s2

String copyOfs1 = s1.replaceAll("\\s", "");

String copyOfs2 = s2.replaceAll("\\s", "");

// Initially setting status as true

boolean areAnagrams = true;

if (copyOfs1.length() != copyOfs2.length()) {

// Setting status as false if copyOfs1 and copyOfs2 don't have

same length

areAnagrams = false;

} else {

// Changing the case of characters of both copyOfs1 and copyOfs2

and converting

// them to char array

char[] s1Array = copyOfs1.toLowerCase().toCharArray();

char[] s2Array = copyOfs2.toLowerCase().toCharArray();

// Sorting both s1Array and s2Array

Arrays.sort(s1Array);

Arrays.sort(s2Array);

// Checking whether s1Array and s2Array are equal

areAnagrams = Arrays.equals(s1Array, s2Array);

}

// Output

if (areAnagrams) {

System.out.println("\"" + s1 + "\"" + " and " + "\"" + s2 + "\"" +

" are anagrams.");

} else {

System.out.println("\"" + s1 + "\"" + " and " + "\"" + s2 + "\"" +

" are not anagrams.");

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//to add a package, mention package with the name
package hw5p3;

import java.util.Arrays;

public class Anagram {

   public static void main(String[] args) {

       decideAnagrams("Mother In Law", "Hitler Woman");

       decideAnagrams("keEp", "peeK");

       decideAnagrams("SiLeNt CAT", "LisTen AcT");

       decideAnagrams("Debit Card", "Bad Credit");

       decideAnagrams("School MASTER", "The ClassROOM");

       decideAnagrams("DORMITORY", "Dirty Room");

       decideAnagrams("ASTRONOMERS", "NO MORE STARS");

       decideAnagrams("Toss", "Shot");

       decideAnagrams("joy", "enjoy");

       }

       static void decideAnagrams(String s1, String s2) {

       // Removing all white spaces from s1 and s2

       String copyOfs1 = s1.replaceAll("\\s", "");

       String copyOfs2 = s2.replaceAll("\\s", "");

       // Initially setting status as true

       boolean areAnagrams = true;

       if (copyOfs1.length() != copyOfs2.length()) {

       // Setting status as false if copyOfs1 and copyOfs2 don't have

       System.out.println("same length");

       areAnagrams = false;

       }
       else {

       // Changing the case of characters of both copyOfs1 and copyOfs2 and converting them to char array

       char[] s1Array = copyOfs1.toLowerCase().toCharArray();

       char[] s2Array = copyOfs2.toLowerCase().toCharArray();

       // Sorting both s1Array and s2Array

       Arrays.sort(s1Array);

       Arrays.sort(s2Array);

       // Checking whether s1Array and s2Array are equal

       areAnagrams = Arrays.equals(s1Array, s2Array);

       }

       // Output

       if (areAnagrams) {

       System.out.println("\"" + s1 + "\"" + " and " + "\"" + s2 + "\"" +

       " are anagrams.");

       } else {

       System.out.println("\"" + s1 + "\"" + " and " + "\"" + s2 + "\"" +

       " are not anagrams.");

       }

       }
}

output

Add a comment
Know the answer?
Add Answer to:
Add the package hw5p3 to your FirstName-LastName-HW5 project. public static void main(String[] args) { decideAnagrams("Mother In...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args)...

    Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp {    public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...

  • import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {   ...

    import java.util.Scanner; public class StudentClient {       public static void main(String[] args)    {        Student s1 = new Student();         Student s2 = new Student("Smith", "123-45-6789", 3.2);         Student s3 = new Student("Jones", "987-65-4321", 3.7);         System.out.println("The name of student #1 is ");         System.out.println("The social security number of student #1 is " + s1.toString());         System.out.println("Student #2 is " + s2);         System.out.println("the name of student #3 is " + s3.getName());         System.out.println("The social security number...

  • Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args)...

    Explain this java code, please. import java.util.Scanner; public class Program11 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); final int maxSize = 128; String[] titles = new String[maxSize]; int[] lengths = new int[maxSize]; int numDVDs = 0; String op; op = menu(stdIn); System.out.println(); while (!op.equalsIgnoreCase("q")) { if (op.equalsIgnoreCase("a")) { if (numDVDs < maxSize) numDVDs = addDVD(titles, lengths, numDVDs, stdIn); } else if (op.equalsIgnoreCase("t")) searchByTitle(titles, lengths, numDVDs, stdIn);    else if (op.equalsIgnoreCase("l")) searchByLength(titles, lengths, numDVDs, stdIn); System.out.println('\n');...

  • Show the output of the following piece of code: public static void main(String[] args) { char...

    Show the output of the following piece of code: public static void main(String[] args) { char x = 'a'; char y = 'c'; System.out.println(++x); System.out.println(y++); System.out.println(x - y); }

  • public class SquareTest {    public static void main(String[] args) {               System.out.println("Number...

    public class SquareTest {    public static void main(String[] args) {               System.out.println("Number of sides is " + Square.NUM_OF_SIDES);                      Square s1 = new Square(-5.7f);               System.out.println(s1);               s1.setLength(0.001f);        System.out.println(s1.getLength());               s1.setLength(-9999);        System.out.println(s1.getLength());               System.out.println("Number of sides is " + s1.NUM_OF_SIDES);               s1.calculateArea();                            } }...

  • Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[]...

    Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[] args) { // sort doubleArray into ascending order char [] A = {'g', ', 'y', 'a', 'e','d' }; Arrays.sort( A); for (char value: A) System.out.printf("%2C", value); System.out.printf("\n"); char [] A Copy = new char[ 3 ]; System.arraycopy( A, 2, A Copy, 0, A Copy.length); for(char value: A Copy) System.out.printf("%2C", value); System.out.printf("\n"); int location = Arrays.binary Search(A Copy, 'y'); if(location >=0) System.out.printf("y Found at element...

  • import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new...

    import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...

  • import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); }    if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...

  • package Lab09; public class Lab09Driver { public static void main(String[] args) { new Lab09Driver(); } public...

    package Lab09; public class Lab09Driver { public static void main(String[] args) { new Lab09Driver(); } public Lab09Driver() { Scanner input = new Scanner(System.in); System.out.println("Would you like to load a previous file? (Y/N)"); String.choice = input.nextLine().trim().toUpperCase(); if (choice.charAt(0) == 'Y') { System.out.println("Enter the name of the file that you want to load:"); String.fileName = input.nextLine(); try { Scanner fileInput = new Scanner(new File(fileName)); while(fileInput.hasNextLine()){ String answer = fileInput.nextLine(); System.out.println(answer); }fileInput.close(); } catch (FileNotFoundException e) { System.out.println("Could not find the file, and...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT