Question

Java program please

Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints the string in reverse order.Write a Java program that has 3 buttons. Clicking the first button shows the message See no evil, clicking the second butto

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

Java Program for Exercise 7.2.8:

import java.util.Scanner;

public class CharTest {
   public static void main(String[]r){
      
       //Scanner instance to read the input from keyboard
       Scanner scanner = new Scanner(System.in);
      
       //asking the user input
       System.out.print("Entery any string:");
       String inputString = scanner.nextLine();
      
       //using for loop and charAt() method,displaying the string in reverse order
       System.out.print("Reverse of "+inputString+" is:");
       for(int i=inputString.length()-1;i>=0;i--){
           System.out.print(inputString.charAt(i));
       }
   }
}

Java Program and Output Screenshot:

public class Char Test f 3 4public static void main (String[]r)t 6 //Scanner instance to read the input from keyboard Scanner

Java Program:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonDemo extends JFrame
{
/**
   *
   */
   private static final long serialVersionUID = 6216899821916327889L;
  
   /*Buttons declarations*/
   JButton firstButton;
   JButton secondButton;
   JButton thirdButton;
JLabel lbl;
  
/*Constructor to initialize buttons and labels*/
ButtonDemo()
{
firstButton = new JButton("Click on See");
secondButton = new JButton ("Click on Hear");
thirdButton = new JButton ("Click on Speak");
lbl = new JLabel ("");
setLayout (new GridLayout(4,1));
setSize (300,250);
add(firstButton);
add(secondButton);
add(thirdButton);
add(lbl);
setVisible(true);
  
ButtonHandler bh = new ButtonHandler();
firstButton.addActionListener(bh);
secondButton.addActionListener(bh);
thirdButton.addActionListener(bh);
}
  
//class to handle button actions
class ButtonHandler implements ActionListener
{
   //method to perform different actions base on the different buttons
public void actionPerformed(ActionEvent ae)
{
if (ae.getSource()==firstButton)
{
lbl.setText("See no evil");
}
if (ae.getSource()==secondButton)
{
lbl.setText("Hear no evil");
}
if (ae.getSource()==thirdButton)
{
   lbl.setText("Speak no evil");
}
}
}

//main() method - starting point of the program
public static void main(String[] args)
{
   //calling ButtonDemo() constructor
new ButtonDemo();
}
}

Output:

Click on See Click on Hear Click on Speak Hear no evil

Add a comment
Know the answer?
Add Answer to:
Java program please Exercise 7.2.8: Using the charAt) method, write and run a Java program that rea tring and prints th...
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
  • Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write...

    Using Java, please design the GUI in the following prompt. PLEASE TEST YOUR PROGRAM. Thanks! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate of his/her choice. Display the current number of votes above each button. Include a Finished button that erases the images of the losers and displays only the winner's image with a message of...

  • Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME,...

    Using Java, please create the program for the following prompt. MUST CREATE BUTTONS IN A JFRAME, as specified by the prompt! DO NOT use user input of 1, 2, 3 etc. User input must come from clicking the buttons. Please be sure to test your program. Thank you! Write a program that displays three buttons with the names or images of three candidates for public of office. Imagine that a person votes by clicking the button that shows the candidate...

  • Write a Java program that reads a word and prints its bigrams substrings. A character bigram...

    Write a Java program that reads a word and prints its bigrams substrings. A character bigram is defined as a continuous sequence of two characters in a word. For example, if the user provides the input "rum", the program prints ru um Hint: 1. set two pointers, one always moves from the beginning of the string and the other moves from i+2, in which i is the current position of the first pointer. 2. print an error message if the...

  • Write a program in java that asks a user for a file name and prints the...

    Write a program in java that asks a user for a file name and prints the number of characters, words (separated by whitespace), and lines in that file. Then, it replaces each line of the file with its reverse. For example, if the file contains the following lines (Test1.txt): This is a test Hi there Output on the console: Number of characters: 22 Number of words: 6 Number of lines: 2 Data written to the file (Test1.txt): tset a si...

  • Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to...

    Problem 1: Dynamic Grocery Array Using Java to write a program that prompts the user to enter an item’s name for each position in the array. The program then prints uses a loop to output the array. Example 1: Banana 2: Orange 3: Milk 4: Carrot 5: Chips Banana, Orange, Milk, Carrot, Chips Problem 2: Print Characters in String Array    Declare a string array of size 5. Prompt the user enters five strings that are stored in the array....

  • Write a JAVA program that has a method which accepts a string and prints it back...

    Write a JAVA program that has a method which accepts a string and prints it back to the user with all vowels replaced with *'s (multiple asterisks not '*s') with a new line after the string. The method signature should look like this: public static void replacePrint(String input)

  • JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming...

    JAVA CODING Must be compilable, thanks The purpose is to provide an exercise in event-driven programming and image handling using JavaFX. Create an application that responds to the user clicking command buttons. Initially, it will display one of two graphic images and, based upon the button clicked by the user, will switch the image displayed. If the user clicks the mouse button to display the same image as is currently displayed, the image is not changed, but a message at...

  • QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce...

    QUESTION 3 [49 MARKS 3.1) Write the Java statements for a class called Calculator to produce the GUI below. Use 3 panels to arrange the components. You must use an array when defining the numbered buttons (o-9. Do not declare 10 buttons individually. The textfield must be initialized with the text as shown below, and be able to accommodate up to 25 characters. The spacing between the numbered buttons is 10. (28) for caloula Add Equals 3.2 Rewrite any existing...

  • JAVA: Write a program that inputs a string that represents a binary number. The string can...

    JAVA: Write a program that inputs a string that represents a binary number. The string can contain only 0s and 1s and no other characters, not even spaces. Validate that the entered number meets these requirements. If it does not, display an error message. If it is a valid binary number, determine the number of 1s that it contains. If it has exactly two 1s, display "Accepted". Otherwise, display "Rejected". All input and output should be from the console.

  • USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...

    USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s = new StringBuilder(); s.append("abc"); The text in the StringBuilder is now "abc" Character has static methods toUpperCase() and to LowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'. Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false. You will also need String's charAt()...

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