0% found this document useful (0 votes)
2 views2 pages

Import Java - util.ArrayList

The document contains multiple Java classes that demonstrate different functionalities. The 'SeparatePositiveNegative' class separates positive and negative integers from an array, the 'LongestWord' class finds the longest word in a given sentence, and the 'PangramChecker' class checks if a sentence is a pangram using two different implementations. Each class includes a main method that executes the respective functionality and prints the results.

Uploaded by

hitechbrain7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Import Java - util.ArrayList

The document contains multiple Java classes that demonstrate different functionalities. The 'SeparatePositiveNegative' class separates positive and negative integers from an array, the 'LongestWord' class finds the longest word in a given sentence, and the 'PangramChecker' class checks if a sentence is a pangram using two different implementations. Each class includes a main method that executes the respective functionality and prints the results.

Uploaded by

hitechbrain7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

ArrayList;

public class SeparatePositiveNegative {


public static void main(String[] args) {
int[] arr = {-12, 11, -5, -7, -9, 6, -1, 5};
ArrayList<Integer> negative = new ArrayList<>();
ArrayList<Integer> positive = new ArrayList<>();

// Separate negative and positive numbers


for (int num : arr) {
if (num < 0) {
negative.add(num);
} else {
positive.add(num);
}
}

// Combine both lists into a single output array


negative.addAll(positive);
System.out.println(negative);
}
}

public class LongestWord {


public static void main(String[] args) {
String sentence = "The quick brown fox jumps over the lazy dog";
String[] words = sentence.split(" ");
String longestWord = "";

for (String word : words) {


if (word.length() > longestWord.length()) {
longestWord = word;
}
}

System.out.println("The longest word is: " + longestWord);


}
}

package predrive9;

import java.util.HashSet;
import java.util.Set;

public class PangramChecker {


public static void main(String[] args) {
String sentence = "The quick brown fox jumps over the lazy dog";

if (isPangram(sentence)) {
System.out.println("The sentence is a pangram.");
} else {
System.out.println("The sentence is not a pangram.");
}
}
public static boolean isPangram(String sentence) {
Set<Character> letters = new HashSet<>();
sentence = sentence.toLowerCase();

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


char c = sentence.charAt(i);
if (c >= 'a' && c <= 'z') {
letters.add(c);
}
}

return letters.size() == 26;


}
}

package predrive9;

public class PangramChecker {


public static void main(String[] args) {
String sentence = "The quick brown fox jumps over the lazy dog";

if (isPangram(sentence)) {
System.out.println("The sentence is a pangram.");
} else {
System.out.println("The sentence is not a pangram.");
}
}

public static boolean isPangram(String sentence) {


sentence = sentence.toLowerCase();
boolean[] letters = new boolean[26]; // Array to track each letter in the
alphabet

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


char c = sentence.charAt(i);
if (c >= 'a' && c <= 'z') {
letters[c - 'a'] = true; // Mark the corresponding letter as found
}
}

for (boolean found : letters) {


if (!found) {
return false; // If any letter is missing, it's not a pangram
}
}

return true; // All letters were found, so it's a pangram


}
}

You might also like