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...

  • 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....

  • Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following...

    Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...

  • Write a contacts database program that presents the user with a menu that allows the user...

    Write a contacts database program that presents the user with a menu that allows the user to select between the following options: (In Java) Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...

  • Java Switch Case Make From Pseudocode

    The following pseudo-code describes a menu-driven algorithm:loopask the user to input one of the characters a, b, c, d, e, q read in a character from the keyboardif the character is'a' output your name and your tutor's name (hard-coded) 'b' input 3 double numbers x, y, z and output the largestand the smallest of the three numbers'c' input 2 integer numbers m and n, and display all thenumbers between m and n (both inclusive) with five numbers per line (note...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • Write a Java Program that performs the following functionalities: and you can use if statements, cases,...

    Write a Java Program that performs the following functionalities: and you can use if statements, cases, and string methods Enter a new main sentence Find a String Find all incidents of a String Find and Replace a String Replace all the incidents of a String Count the number of words Count a letter’s occurrences Count the total number of letters Delete all the occurrences of a word Exit The program will display a menu with each option above, and then...

  • Please write it in Java language 2. (Myinterface.java) The program description below was found waaaaay back...

    Please write it in Java language 2. (Myinterface.java) The program description below was found waaaaay back in the archives of the Equinox history database. It is the specification for a simply "computer store storefront". Even though computer stores are now extinct, you find the idea charming and decide to use the specification as inspiration to write an interface for one of the Equinox systems. Read carefully: write your own GUI for one of the Equinox systems, drawing inspiration from the...

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