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