Advertisement
apez1

Assignment 7: Methods Sampler Platter

Nov 30th, 2018
569
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.59 KB | None | 0 0
  1. package project;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class newassignment7 {
  6.  
  7.  
  8.     public static String duplicate(String input1) {
  9.    
  10.         int dup = 0;
  11.         if(input1.length()%2 == 0) {                                        //Checks if length is even
  12.             dup = (input1.length()*input1.length())*2;                          //then multiply by itself and * 2
  13.         }
  14.         else {                                                              //If odd
  15.             dup = input1.length()*input1.length();                              //multiply * itself
  16.         }
  17.        
  18.         int c = 0;                         
  19.         char[] newc = new char[dup];                                        //create new array of chars
  20.  
  21.         for(int i = 0; i < input1.length();i++) {                           //for loop runs # of times as the input
  22.                    
  23.             for(int j = 0; j < (dup/input1.length());j++){                  //2nd loop runs how many time each char repeats
  24.                
  25.                 newc[c] = input1.charAt(i);
  26.                 c++;                                                        //char at value
  27.                
  28.             }  
  29.         }  
  30.        
  31.         System.out.print("The duplicated String is: ");
  32.         for(int i = 0; i < newc.length;i++) {
  33.             System.out.print(newc[i]);
  34.            
  35.         }
  36.         return input1; 
  37.     }  
  38.    
  39.     public static String isEdhesivePalindrome(String input2) {
  40.            
  41.         int flag = 0;
  42.        
  43.         if(input2.length() <3 || input2.length()>15) {                      //Checks for length
  44.             flag = 1;
  45.             //break;
  46.         }
  47.            
  48.         char[] newc = new char[input2.length()];                            //Creates array of chars                                   
  49.  
  50.         int c = 0;
  51.        
  52.         for(int i = 0; i < newc.length;i++) {
  53.             newc[i] = input2.charAt(c);
  54.             c++;
  55.            
  56.         }      
  57.        
  58.         for(int i = 0; i < newc.length;i++) {                               //Changes all possible inputs
  59.  
  60.             if(newc[i] == '4') {
  61.                 newc[i] = 'a';
  62.             }
  63.             if(newc[i] == '3') {
  64.                 newc[i] = 'e';
  65.             }
  66.             if(newc[i] == '0') {
  67.                 newc[i] = 'o';
  68.             }      
  69.         }
  70.    
  71.        
  72.         String b = new String(newc);                                        //creates new string
  73.    
  74.         System.out.println('\n'+ "string: " + b + '\n');   
  75.         int half = b.length()/2;                                            //calculate half of the string
  76.  
  77.         for(int i = 0; i < half; i++) {                                     //first half comparison
  78.            
  79.             for(int j = b.length()-1; j > half; j--) {                      //second half comparison
  80.                
  81.                 System.out.println(newc[i] + "compared to: " + newc[j] );       //debugging
  82.                 if(newc[i] != newc[j]) {                                    //if theyre not equal flag 
  83.                     flag = 1 ;
  84.                     break;
  85.                 }
  86.                
  87.                 i++;
  88.            
  89.             }
  90.  
  91.         }
  92.    
  93.         if(flag == 1 ) {
  94.            
  95.             System.out.println("Too bad, that isn't an Edhesive Palindrome.");
  96.         }
  97.         if(flag == 0) {
  98.            
  99.             System.out.println("Nice, you found an Edhesive Palindrome!");
  100.            
  101.         }
  102.         return input2; 
  103.            
  104.     }
  105.    
  106.     public static Double numberScramble(Double input3) {
  107.        
  108.         Double num = input3;
  109.         double end = 0;
  110.        
  111.         if(num < 0) {
  112.             end = 0.0;
  113.             System.out.println("The scrambled number is: " + end);
  114.         }
  115.         else {
  116.            
  117.             num = num + 5;
  118.             num = num /2;
  119.             num = Math.sqrt(num);
  120.             System.out.println("The scrambled number is: " + num);
  121.    
  122.         }
  123.        
  124.         return input3;
  125.     }
  126.    
  127.    
  128.    
  129.     public static void main(String[] args) {
  130.  
  131.         Scanner scanner = new Scanner(System.in);  
  132.        
  133.         System.out.println("Welcome to the Methods Sampler Platter. Please enter a String to duplicate.");
  134.         String input1 = scanner.nextLine();
  135.        
  136.         duplicate(input1);
  137.        
  138.         System.out.println('\n' + "Next, please enter a String to check for Edhesive Palindromes.");
  139.         String input2 = scanner.nextLine().toLowerCase();
  140.                
  141.         isEdhesivePalindrome(input2);
  142.        
  143.         System.out.println("Almost done! Please enter a number to scramble.");
  144.         double input3 = scanner.nextDouble();
  145.        
  146.         numberScramble(input3);
  147.  
  148.    
  149.        
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement