import java.util.*;
/**
* Solve the methods below
*
* Mr. Memmo
* 10/29
* NOTE: for all of these. They should work if I switch values
*/
public class ProgrammingChallenge
{
public ProgrammingChallenge()
{
String[] letters = {"a","a","a","a","b","c","c","a","a","d","e","e","e","e"};
String[] words = {"tent","push","level","radar","hello","stats","madam","world"};
removeConsecutives(letters);
isPalindrome(words);
math(30, 60);
}
/*"a","a","b","c","c","a"
* create an arraylist that removes the consecutive
* thus, the above array would become an
* arraylist that held the following values
* a,b,c,a
* print the contents of the arraylist to the screen
* at end of method.
*/
public void removeConsecutives(String[] a)
{
String [] newarr = {};
List<String> list = new ArrayList<String>();
for (int i = 0; i < a.length-1; i++){
if (a[i + 1].equals(a[i])){
a[i] = null;
}
if(a[i] != null){
list.add(a[i]);
}
}
System.out.println(list);
}
/*
* Is the string a palindrome
*/
public void isPalindrome(String[] a){
List<String> non = new ArrayList<String>();
String non1 = "";
int r = 0;
int red = 0;
for (int i = 0; i < a.length; i ++){
String temp = a[i];
int len = a[i].length();
//System.out.println(non1);
for (int b = 0; b < len; b++){
//System.out.println(len);
if (b == 0){
non1 = non1 + (temp.substring(len - 1));
}
non1 = non1 + (temp.substring(len + red, len + r));
//System.out.print(non1);
//System.out.println("1");
r--;
red = r - 1;
if (len + red < 0){
red = red + 1;
}
if (len + r < 0){
r = r + 1;
}
}
String s = a[i];
System.out.println(non1);
if (non1.equals(s)){
System.out.println(s + ": This is a palindrome");
}
non1 = "";
r = 0;
red = 0;
}
}
public int math(int x, int y)
{
int c = 0;
for (int i = 1; i < y; i++){
if (x%i == 0 && y%i==0){
c = i;
}
}
System.out.println(c);
return c;
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.