What to submit: your answers to exercises 1, 3, and 4 and separate the codes to each question.
==============================================================================================
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");
}
}
}
==================================================================================================================================
public SimpleApp(String windowTitle) { ... }
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.
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:



***********************************************************************************************************************************
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);
}
}
What to submit: your answers to exercises 1, 3, and 4 and separate the codes to...
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 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 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 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 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 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 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 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 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 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...