Question

JAVA Write a public class call FunnyWord. class FunnyWord must have a main method. what it...

JAVA

Write a public class call FunnyWord.

class FunnyWord must have a main method.

what it does:

0. prompt user for a String
1. read a String from the user
2. print whether the String is funny or not
3. continue until user enters the word "end"

The String is funny if it looks the same read from left to right or
right to left.

In order to pass all tests you must find funny phrases when spaces,
punctuation and case are ignored (look at the examples).

HINT: you could use the method replaceAll from String in order to
delete all the punctuation marks.

For example:

str.replaceAll(";", "");

returns a string where all semicolons have been replaced by nothing.

or you could use
str.replaceAll("\\W","")
which replaces all punctuation marks

you may also need other methods of String:

toLowerCase(): returns another String in lower case
length(): gives you the length of the String

you can build a String character by character this way:

char c;
String rr;

rr = rr + c; //adds the c at the end of rr

here is a piece of code that uses some of these methods:

public class Traverse{
    
    public static void main(String [] aaa){
        
        String ss = "x y z a b.?/;:'[{]} c  defgh";
        ss = ss.replaceAll("\\W","");
        String new1 = "";
        String new2 = "";
        
        for (int i = 0; i < ss.length(); i++){
            new1 = new1 + ss.charAt(i);
            new2 = ss.charAt(i) + new2;
        }
        System.out.println(new1);
        System.out.println(new2);
        
    }
}

Notice: you cannot use any method called "reverse".  That is
considered forbidden knowledge.

Examples

% java FunnyWord Enter a String: Anna

funny!

Enter a String: AnnA AnnA

funny!

Enter a String: A man, a plan, a canal: Panama.

funny!

Enter a String: foo

that is not funny

Enter a String: Annax

that is not funny

Enter a String: end

that is not funny

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

Code:

/*
*Program to check whether an input string is funny.
*String is funny if it reads same from both left and right side
*after ignoring spaces,punctualtion and case
*/

import java.util.Scanner;
public class FunnyWord {

  
    public static void main(String[] args) {
        String inputString;
      
        int len;
        Scanner input=new Scanner(System.in);
        System.out.println("Java FunnyWord ");
      
        //run the while loop till it is true
        while(true){
         
            System.out.print("\nEnter a string: "); //prompt user for a String
            inputString=input.nextLine(); //read a String from the user
          
          
            //remove everything other than word
            inputString=inputString.replaceAll("\\W","");
          
            //convert string to lowercase
            inputString=inputString.toLowerCase();
         
            //find the length of string
            len=inputString.length();
            String new1=""; // string from left side variable
            String new2=""; // string from right side variable
          
            //run for loop till it traverse the whole inputString
            for(int i=0;i<len;i++){
                new1=new1+inputString.charAt(i);
                new2=inputString.charAt(i)+new2;
            }
          
            //if left side string and right side string is equal
            if(new1.equals(new2))
                System.out.println("Funny!"); //print string is funny
            else{
                System.out.println("This is not Funny!"); //print string is not funny
                if(inputString.equals("end"))
                    break; //end if user enters end
            }
          
          
     
        }
      
      
      
      
    }
  
}

Output:

Add a comment
Know the answer?
Add Answer to:
JAVA Write a public class call FunnyWord. class FunnyWord must have a main method. what it...
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
  • Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** *...

    Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...

  • Complete the following Java class. In this class, inside the main() method, user first enters the...

    Complete the following Java class. In this class, inside the main() method, user first enters the name of the students in a while loop. Then, in an enhanced for loop, and by using the method getScores(), user enters the grades of each student. Finally, the list of the students and their final scores will be printed out using a for loop. Using the comments provided, complete the code in methods finalScore(), minimum(), and sum(). import java.util.Scanner; import java.util.ArrayList; public class...

  • Given code: /** * Provides some methods to manipulate text * */ public class LoopyText {...

    Given code: /** * Provides some methods to manipulate text * */ public class LoopyText { private String text;    /** * Creates a LoopyText object with the given text * @param theText the text for this LoopyText */ public LoopyText(String theText) { text = theText; }    //Your methods here } Given tester code: /** * Tests the methods of LoopyText. * @author Kathleen O'Brien */ public class LoopyTextTester { public static void main(String[] args) { LoopyText loopy =...

  • In Java This is the method we did in class. How do I reverse the string...

    In Java This is the method we did in class. How do I reverse the string "monday"? If I could get the string "onday" reversed (to produce "yadno" then all I have to do is append the first character to make "yadnom". import java.util.Scanner; public class Lab12Num2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); System.out.println("The string entered was " + s); System.out.println("The string printed backwards is "+ printBackwards(s)); }    public static String printBackwards(String one)...

  • Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public...

    Please write in dr java Here is the driver: import java.util.*; public class SquareDriver { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String input =""; System.out.println("Welcome to the easy square program"); while(true) { System.out.println("Enter the length of the side of a square or enter QUIT to quit"); try { input = keyboard.nextLine(); if(input.equalsIgnoreCase("quit")) break; int length = Integer.parseInt(input); Square s = new Square(); s.setLength(length); s.draw(); System.out.println("The area is "+s.getArea()); System.out.println("The perimeter is "+s.getPerimeter()); } catch(DimensionException e)...

  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • Complete the following Java program by writing the missing methods. public class 02 { public static...

    Complete the following Java program by writing the missing methods. public class 02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter a number: int n = scan.nextInt(); if (isOdd (n)) { //Print n to 1 System.out.println("Print all numbers from "+n+" to 1"); printNumToOne (n); } else { System.out.println(n + " is //End of main()

  • Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as...

    Question 1[JAVA] Add a method in the Main class named getLines() that takes a filename as a String and returns each line in the file in an array. Have each element of the array be a String. Do not include the newline at the end of each line. Use a BufferedReader object to read the file contents. Note, the contents of input.txt will be different (including the number of lines) for each test case. Example: getLines( "input.txt" ) returns (an...

  • I was presented with this problem: Create a Java class MonthNames with a single static method...

    I was presented with this problem: Create a Java class MonthNames with a single static method getMonthName which Takes a single integer corresponding to a month of the year, 1 representing January through 12 representing December, and Returns a string for the name of the month. This is what I have so far public class MonthNames {    public static String getMonthName (int numMonth) {        return "month"; }        public static void main (String [] args) {...

  • Assume that you have a String "name" attribute in a Java class definition.  Write a public method...

    Assume that you have a String "name" attribute in a Java class definition.  Write a public method to return this "name" attribute. public String getName() { return name;         } 1.) Write a statement that throws an IllegalArgumentException with the error message “Argument cannot be negative”.​ 2.) The method getValueFromFile is public and returns an int. It accepts no arguments. The method is capable of throwing an IOException and a FileNotFoundException. Write the header for this method.

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
ADVERTISEMENT