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

  • I need help with my code when I run my code running the wrong thing like...

    I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 {    static final int INITIAL_CAPACITY = 10;    public static void main (String[] args) throws Exception    {        // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE        if (args.length < 1 )       ...

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

  • Write a program that stores a phrase as an array of words, and then prints it...

    Write a program that stores a phrase as an array of words, and then prints it backwards. The main method calls method getInput , which asks the user how many words there are, stores them in an array, and returns this array. printBackwards then takes this array of words, and prints it in reverse. Code Example: import java.util.Scanner; public class L17Num1{    public static void main(String[] args) {    String[] sArray=getInput(); printBackwards(sArray); } public static String[] getInput() { System.out.println("How many...

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

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