Question

What to submit: your answers to exercises 1, 3, and 4 and separate the codes to...

What to submit: your answers to exercises 1, 3, and 4 and separate the codes to each question.

  1. Using your solution to question 3 of Lab Practice 2, modify it to create a class that contains a static method; the method takes a string as a parameter and returns a boolean value indicating whether the parameter string has repeated characters in it or not. That is, return true if there is at least one character which appears more than once in the string. The string should be input by the user.

==============================================================================================

codes using in question 3 of Lab Practice 2

package week02;

import java.util.Scanner;

public class Q3 {

   

                static public void main (String[] args)

    {

       Scanner scanner = new Scanner(System.in);

       System.out.println("Enter a string : ");

       String word = scanner.next();

       boolean repeatChar= false;

       for(int i =0; i<word.length();i++)

       {

                   char letter = word.charAt(i);

                                   

       for (int j=i+1; j<word.length(); j++)

       {        

                   char otherletter = word.charAt(j);

                   if(otherletter==letter)

           {

                                   repeatChar=true;

                                   break;  

                   }             

       }

       }

      

       if (repeatChar)

       {

                   System.out.println("There are repeated characters in it");

        }

      

                else

       {

                System.out.println("There are not repeated characters in it");

                }

    }

}

==================================================================================================================================

  1. The GUI program SimplApp.java is available from Topic 7 lecture slides. Copy the code and make a project; get the program to work. Without needing to understand everything the code does, you should at least understand the working of the constructor:

public SimpleApp(String windowTitle) { ... }

  1. Copy and rename SimplApp.java; then modify it to create a GUI program that accepts a String from the user in a TextField and reports whether or not there are repeated characters in it. Thus your program is a client of the class which you created in question 1 above.

N.B. most of the modification needs to occur in the constructor and the actionPerformed() methods. So you should spend time working out exactly what these two methods are doing, so that you can make the necessary modifications.

  1. This exercise extends exercise 1 from Lab Practice 5. Write a method that takes an integer array as its parameter and sorts the contents of the array in ascending order using the Insertion Sort algorithm (see Topic 6 lecture notes for the algorithm). Call this method (created in exercise 1 of Lab Practice 5) after the original array and other stats have been displayed. Once the array has been sorted by your method, display its contents to the screen in the same manner as the original array was displayed.
  1. The GUI program BinarySearch.java is available from Topic 7 lecture slides. Copy the code and make a project; get the program to work. You should attempt to understand the operations of the Binary Search algorithm.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

I have written the program based on your requirements.

The below code works perfectly and it has no-errror.

You just simply Compile and Run the below program.

I have also attached the Output Screen-shot that I got by running the below program.

Output:

C:\Users\Public>javac Q3.java C:\Users\Public>java 23

X Find Repeat Char il Enter a String : aa CHECKх Message i There are repeated characters in it OK

***********************************************************************************************************************************

Code:

Q3.java:

import java.io.*;
import java.util.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Q3{

static public boolean repeatChar(String word)
{

boolean repeatChar= false;

for(int i =0; i<word.length();i++)
{
char letter = word.charAt(i);

for (int j=i+1; j<word.length(); j++)
{
char otherletter = word.charAt(j);

if(otherletter==letter)
{
repeatChar=true;
return true;
           }   
   }
}
return false;
}

static public void main (String[] args)

{
                  
                   JFrame frame=new JFrame("Find Repeat Char");
                   frame.setSize(300,300);
                  
                   JLabel l1=new JLabel("Enter a String : ");
                   l1.setBounds(80,50,100,30);
                  
                   JTextField f1=new JTextField();
                   f1.setBounds(75,100,100,30);
                  
                  
                   JButton button=new JButton("CHECK");
                   button.setBounds(75,150,100,30);
                  
                   frame.add(l1);
                   frame.add(f1);
                   frame.add(button);
                  
                   button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
  
           String temp=f1.getText();
              
               if(temp.equals(""))
               {
                   JOptionPane.showMessageDialog(frame,"TextField Should not be Empty !!!","Warning",JOptionPane.WARNING_MESSAGE);
               }
               else
               {
                   if(repeatChar(temp))
                   {
                       JOptionPane.showMessageDialog(frame,"There are repeated characters in it");
                   }
                   else
                   {
                       JOptionPane.showMessageDialog(frame,"There are not repeated characters in it");
                   }
                      
               }
                  
              
}
});
  
  

               frame.setLayout(null);
frame.setVisible(true);


}

}

Add a comment
Know the answer?
Add Answer to:
What to submit: your answers to exercises 1, 3, and 4 and separate the codes to...
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
  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    In the code shown above are two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM ON THE SAME CLASS BUT SO WE CAN RECALL them threw different methods. Thank you. 1-First exercise import java.util.Scanner; public class first { public static int average(int[] array){    int total = 0;    for(int x: array)        total += x;    return total / array.length;    } public static double average(double[] array){    double total = 0;    for(double x: array)        total += x;    return total /...

  • What to submit: your answers to exercises 1, and 2 and separate the codes to each...

    What to submit: your answers to exercises 1, and 2 and separate the codes to each question. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick...

    Sorting algorithm: quick sort Exercise One (20 marks) Given the following program body for implementing Quick sort, complete the program by writing code where required import java.util.Random; public class QuickSort public void quickSectlinti] A) QuickSort/A, O, A.length-1); private void guickSortlin Aiat low.int high) //Complete the code for the quicksort method (5 marks] private void swaplint[] a, int indexl, int index2) //Complete the code for the swap method [3 marks] private int setPivotlint low, int high) Random rand = new Random();...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • JAVA HELP: Directions Write a program that will create an array of random numbers and output...

    JAVA HELP: Directions Write a program that will create an array of random numbers and output the values. Then output the values in the array backwards. Here is my code, I am having a problem with the second method. import java.util.Scanner; import java.util.Random; public class ArrayBackwards { public static void main(String[] args) { genrate(); print(); } public static void generate() { Scanner scanner = new Scanner(System.in);    System.out.println("Seed:"); int seed = scanner.nextInt();    System.out.println("Length"); int length = scanner.nextInt(); Random random...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • - Part 1 – 4 Debugging Exercises – there are things wrong – either logic or...

    - Part 1 – 4 Debugging Exercises – there are things wrong – either logic or syntax errors.  Correct the pseudocode and one Java program. Add the corrected code in BOLD and change the corrected color to RED.   Debugging 4:  Add the missing code in bold and red. import java.util.Scanner; public class Airline {             public static void main(String args[])             {                         Scanner s = new Scanner(System.in);                         String passengerName = ""                              String ageString = ""                           int passengerAge = 0                         System.out.println("Enter passenger's name: ");                         passengerName  =...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

  • Please help. I need a very simple code. For a CS 1 class. Write a program...

    Please help. I need a very simple code. For a CS 1 class. Write a program in Java and run it in BlueJ according to the following specifications: The program reads a text file with student records (first name, last name and grade). Then it prompts the user to enter a command, executes the command and loops. The commands are the following: "printall" - prints all student records (first name, last name, grade). "firstname name" - prints all students with...

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