Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
package chapter10.creditcardnumber;

public class Main
{
public static void main(String[] args)
{
public class CreditCardNumber {
public static void main(String[] args) {
String creditCardNum = "4111 1111 1111 1111";
String last4 = creditCardNum.substring(creditCardNum.length() - 4);
String last5 = creditCardNum.substring(creditCardNum.length() - 6);
Expand Down
102 changes: 54 additions & 48 deletions src/main/java/chapter10/palindrome1/Palindrome.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,72 @@

import static java.lang.Character.isLetterOrDigit;

public class Palindrome
{
public static void main(String[] args)
{
System.out.println(isPalindromeU("madd am"));
}

public static boolean isPalindromeU(String word)
{
StringBuilder theWord = new StringBuilder();
/**
* A class containing utility functions for determining palindromes.
*/
public class Palindrome {
/**
* Determines if the given string is a palindrome. This method considers only
* letter or digit characters, so whitespace is ignored.
* <p>
* This method implements palindrome-checking by {@link #isPalindrome(String) isPalindrome()},
* so it uses the {@link String#substring(int, int) substring()} method, which is discouraged.
* @see #isPalindrome(String) isPalindrome()
* @param input Any string.
* @return {@code true} if all letter or digit characters are palindromic, {@code false} otherwise.
*/
public static boolean isPalindromeIgnoreWhitespace(String input) {
StringBuilder sb = new StringBuilder();
int length = input.length();

int length = word.length();
for(int counter = 0; counter < length; counter++)
{
//Gets char for the specific position in the string from the counter
char character = theWord.charAt(counter);
//If the character is a letter/digit
if(!isLetterOrDigit(character))
{
theWord.deleteCharAt(counter);
for (int counter = 0; counter < length; counter++) {
char character = sb.charAt(counter); // Gets char at the index of current counter value
if (!isLetterOrDigit(character)) {
sb.deleteCharAt(counter);
}
word = String.valueOf(theWord);
}

length = word.length(); //Length = 6
String backLetter = "";

for (int backCounter = length - 1; backCounter >= 0; backCounter--)
{
//Gets first character and last character
backLetter += word.substring(backCounter, backCounter + 1);
}
System.out.println(word);
return backLetter.compareTo(word) == 0;
return isPalindrome(sb.toString());
}

public static boolean isPalindrome(String word)
{
int length = word.length(); //Length = 6
String backLetter = "";
/**
* Determines if the given string is a palindrome using {@link String#substring(int, int) substring()}
* method. This method considers characters that are not letters or digits, so whitespace is
* significant.
* <p>
* This method is much less readable than {@link #isPalindromeCharAt(String) isPalindromeCharAt()}
* with negligible improvements in performance. This implementation is discouraged.
* @param input Any string.
* @return {@code true} if all characters are palindromic, {@code false} otherwise.
*/
public static boolean isPalindrome(String input) {
StringBuilder sb = new StringBuilder();
int length = input.length();

for (int backCounter = length - 1; backCounter >= 0; backCounter--)
{
//Gets first character and last character
backLetter += word.substring(backCounter, backCounter + 1);
for (int backCounter = length - 1; backCounter >= 0; backCounter--) {
sb.append(input.substring(backCounter, backCounter + 1));
}
return backLetter.compareTo(word) == 0;
return sb.toString().compareTo(input) == 0;
}

//Alternative method of finding palindromes using charAt()
public static boolean isPalindrome2(String word)
{
int length = word.length();
for (int counter = 0; counter < (length / 2); ++counter)
{
if (word.charAt(counter) != word.charAt(length - counter - 1))
{
/**
* Determines if the given string is a palindrome using the {@link String#charAt(int) charAt()}
* method. This method considers characters that are not letters or digits, so whitespace
* is significant.
* @param input Any string.
* @return {@code true} if all characters are palindromic, {@code false} otherwise.
*/
public static boolean isPalindromeCharAt(String input) {
int length = input.length();

for (int counter = 0; counter < (length / 2); counter++) {
if (input.charAt(counter) != input.charAt(length - counter - 1)) {
return false;
}
}
return true;
}

public static void main(String[] args) {
System.out.println(isPalindromeIgnoreWhitespace("madd am"));
}
}
16 changes: 6 additions & 10 deletions src/main/java/chapter10/palindrome1/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,12 @@

import static chapter10.palindrome1.Palindrome.isPalindrome;

public class StringTest extends JFrame
implements ActionListener
{
public class StringTest extends JFrame implements ActionListener {
private final JTextField input;
private final JTextField result;

public StringTest()
{
super("String test");
public StringTest() {
super("StringTest");
Box box1 = Box.createVerticalBox();
box1.add(new JLabel("Input:"));
box1.add(Box.createVerticalStrut(10));
Expand All @@ -42,8 +39,8 @@ public StringTest()
input.requestFocus();
}

public void actionPerformed(ActionEvent e)
{
@Override
public void actionPerformed(ActionEvent e) {
String str = input.getText();

str = String.valueOf(isPalindrome(str));
Expand All @@ -52,8 +49,7 @@ public void actionPerformed(ActionEvent e)
input.selectAll();
}

public static void main(String[] args)
{
public static void main(String[] args) {
StringTest window = new StringTest();
window.setBounds(100, 100, 300, 100);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@
import static java.lang.Character.isLetterOrDigit;
import static java.lang.Character.toUpperCase;

public class Palindrome
{
public static void main(String[] args)
{
System.out.println(isPalindrome("mad' dam"));
}

public static boolean isPalindrome(String word)
{
//Gets length of word
public class PalindromeExtended {
public static boolean isPalindrome(String word) {
int length = word.length();
//Initialize new char array
char[] wordChar;
//Converts String word to char array
wordChar = word.toCharArray();

//Gets length of char array without the special characters included
Expand All @@ -28,11 +18,9 @@ public static boolean isPalindrome(String word)

int counter2 = 0;
//Removes all the special characters from the char array
for (int counter = 0; counter < length; counter++)
{
for (int counter = 0; counter < length; counter++) {
//If specific character is not a letter or digit
if (isLetterOrDigit(wordChar[counter]))
{
if (isLetterOrDigit(wordChar[counter])) {
wordChar2[counter2] = toUpperCase(wordChar[counter]);
counter2++;
}
Expand All @@ -43,62 +31,51 @@ public static boolean isPalindrome(String word)

int forwardCounter = 0;
//Reverses the char array
for (int backCounter = lengthLetterAndWord - 1; backCounter >= 0; backCounter--)
{
for (int backCounter = lengthLetterAndWord - 1; backCounter >= 0; backCounter--) {
wordChar2Reversed[forwardCounter] = wordChar2[backCounter];
forwardCounter++;
}

//Checks if the arrays are equal. If they
for(int counter3 = 0; counter3 < lengthLetterAndWord - 1; counter3++)
{
for (int counter3 = 0; counter3 < lengthLetterAndWord - 1; counter3++) {
//If wordChar2 is not equal to the reversed wordChar2, then return false.
if(wordChar2[counter3] != wordChar2Reversed[counter3])
if (wordChar2[counter3] != wordChar2Reversed[counter3])
return false;
}
return true;
}

//Returns the number of letters and numbers in the char array
private static int numOfLetterDigit(char[] charArray)
{
private static int numOfLetterDigit(char[] charArray) {
int count = 0;
for(int counter3 = 0; counter3 < charArray.length; counter3++)
{
if(isLetterOrDigit(charArray[counter3]))
{
for (int counter3 = 0; counter3 < charArray.length; counter3++) {
if (isLetterOrDigit(charArray[counter3])) {
count++;
}
}
return count;
}

//Alternative method of finding palindromes using charAt()
public static boolean isPalindrome2(String word)
{
public static boolean isPalindrome2(String word) {
int length = word.length();
for (int counter = 0; counter < (length / 2); ++counter)
{
if (word.charAt(counter) != word.charAt(length - counter - 1))
{
for (int counter = 0; counter < (length / 2); ++counter) {
if (word.charAt(counter) != word.charAt(length - counter - 1)) {
return false;
}
}
return true;
}

public static boolean isPalindromeU(String word)
{
public static boolean isPalindromeU(String word) {
StringBuilder theWord = new StringBuilder();

int length = word.length();
for (int counter = 0; counter < length; counter++)
{
for (int counter = 0; counter < length; counter++) {
//Gets char for the specific position in the string from the counter
char character = theWord.charAt(counter);
//If the character is a letter/digit
if (!isLetterOrDigit(character))
{
if (!isLetterOrDigit(character)) {
theWord.deleteCharAt(counter);
}
word = String.valueOf(theWord);
Expand All @@ -107,12 +84,15 @@ public static boolean isPalindromeU(String word)
length = word.length(); //Length = 6
StringBuilder backLetter = new StringBuilder();

for (int backCounter = length - 1; backCounter >= 0; backCounter--)
{
for (int backCounter = length - 1; backCounter >= 0; backCounter--) {
//Gets first character and last character
backLetter.append(word.charAt(backCounter));
}
System.out.println(word);
return backLetter.toString().compareTo(word) == 0;
}

public static void main(String[] args) {
System.out.println(isPalindrome("mad' dam"));
}
}
26 changes: 13 additions & 13 deletions src/main/java/chapter10/palindrome2/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static chapter10.palindrome2.Palindrome.isPalindrome;
import static chapter10.palindrome2.PalindromeExtended.isPalindrome;

public class StringTest extends JFrame
implements ActionListener
{
public class StringTest extends JFrame implements ActionListener {
private final JTextField input;
private final JTextField result;

//Code provided by Java textbook
public StringTest()
{
super("String test");
/**
* @apiNote Provided by textbook.
*/
public StringTest() {
super("StringTest");
Box box1 = Box.createVerticalBox();
box1.add(new JLabel("Input:"));
box1.add(Box.createVerticalStrut(10));
Expand All @@ -44,9 +43,11 @@ public StringTest()
input.requestFocus();
}

//Code provided by Java textbook
public void actionPerformed(ActionEvent e)
{
/**
* @apiNote Provided by textbook.
*/
@Override
public void actionPerformed(ActionEvent e) {
String str = input.getText();

str = String.valueOf(isPalindrome(str));
Expand All @@ -55,8 +56,7 @@ public void actionPerformed(ActionEvent e)
input.selectAll();
}

public static void main(String[] args)
{
public static void main(String[] args) {
StringTest window = new StringTest();
window.setBounds(100, 100, 300, 100);
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Code written as part of the Java-Methods-Program-AP-Comp-A-2021-2022 repository on GitHub.
package chapter10.removedash;

public class Main {

public static void main(String[] args) {
String stringSecurityNum = "987-65-4321";
System.out.println(removeDashes(stringSecurityNum));
}

public class DashRemover {
public static String removeDashes(String socialSecurityNum) {
return socialSecurityNum.replace("-", "");
}
}
//

public static void main(String[] args) {
System.out.println(removeDashes("987-65-4321"));
}
}
Loading