Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.04 KB | None | 0 0
  1. Array practice
  2. ----------------------------------------------------------
  3. import java.util.Scanner;
  4.  
  5. public class ArrayPractice {
  6.  
  7.     public static void main(String[] args) {
  8.         // TODO Auto-generated method stub
  9.     /*Ex1
  10.         //variables declaration
  11.         char inputArr[] = new char[10],searchCh;
  12.         int repeat=0;
  13.  
  14.         //define scanner
  15.         Scanner s = new Scanner(System.in);
  16.        
  17.         //user instruction 1
  18.         System.out.println("Please type an array of 10 characters");
  19.        
  20.         //user input values into number array 1
  21.         for (int i=0;i<inputArr.length;i+=1)
  22.             inputArr[i] = s.next().charAt(0);
  23.        
  24.         //user instruction 2
  25.         System.out.println("Please give a char to search for");
  26.        
  27.         //buffer clean
  28.         System.out.println();
  29.        
  30.         //user input values into number array 2
  31.         searchCh = s.next().charAt(0);
  32.        
  33.         //operation
  34.         for (int i=0;i<inputArr.length;i+=1){          
  35.             repeat += inputArr[i]==searchCh ? 1 : 0 ;
  36.         }
  37.        
  38.         //output to user
  39.         System.out.println(
  40.                 "Said char repeat in above string - " + repeat + " times");
  41.        
  42.         //resource leak close
  43.         s.close();
  44.     */
  45.        
  46.     /*Ex 2
  47.         //variables declaration
  48.         int[] numberArr1 = new int[5], numberArr2 = new int[5];
  49.  
  50.         //define scanner
  51.         Scanner s = new Scanner(System.in);
  52.        
  53.         //user instruction 1
  54.         System.out.println(
  55.                 "Please enter the first number array of 5 values");
  56.  
  57.         //user input values into number array 1
  58.         for (int i=0;i<numberArr1.length;i+=1)
  59.             numberArr1[i]= s.nextInt();            
  60.        
  61.         //clearing buffer
  62.         System.out.println();
  63.        
  64.         //user instruction 2
  65.         System.out.println(
  66.                 "Please enter the second number array of 5 values");
  67.        
  68.         //user input values into number array 2
  69.         for (int i=0;i<numberArr2.length;i+=1)
  70.             numberArr2[i]= s.nextInt();
  71.  
  72.         //operation +
  73.         System.out.print("The indexes of similar numbers are - ");
  74.         for (int i=0;i<numberArr1.length;i+=1){        
  75.             if(numberArr1[i]==numberArr2[i])
  76.                 System.out.print(i + " ");
  77.         }
  78.        
  79.         //resource leak close
  80.         s.close();
  81.     */
  82.        
  83.     /*Ex 3
  84.         //variables declaration
  85.         int[] numberArr1 = new int[5], numberArr2 = new int[5],
  86.             outputArr = new int[5];
  87.  
  88.         //define scanner
  89.         Scanner s = new Scanner(System.in);
  90.        
  91.         //user instruction 1
  92.         System.out.println(
  93.                 "Please enter the first number array of 5 values");
  94.  
  95.         //user input values into number array 1
  96.         for (int i=0;i<numberArr1.length;i+=1)
  97.             numberArr1[i]= s.nextInt();
  98.  
  99.         //user instruction 2
  100.         System.out.println(
  101.                 "Please enter the second number array of 5 values");
  102.        
  103.         //user input values into number array 2
  104.         for (int i=0;i<numberArr2.length;i+=1)
  105.             numberArr2[i]= s.nextInt();
  106.  
  107.         //operation
  108.         System.out.print("The sum of both arrays at similar indexes are - ");
  109.         for (int i=0;i<numberArr1.length;i+=1){
  110.             outputArr[i] = numberArr1[i] + numberArr2[i];
  111.             System.out.print(outputArr[i] + " ");
  112.         }
  113.        
  114.         //resource leak close
  115.         s.close();
  116.     */
  117.        
  118.     /*Ex4
  119.         //variables declaration
  120.         char[] charArr = new char[5];
  121.        
  122.         //define scanner
  123.         Scanner s = new Scanner(System.in);
  124.        
  125.         //user instruction
  126.         System.out.println(
  127.                 "Please enter a number array of 5 values");
  128.  
  129.         //user input values into number array
  130.         for (int i=0;i<charArr.length;i+=1)
  131.             charArr[i]= s.next().charAt(0);
  132.  
  133.         //operation
  134.         for (int i=0;i<charArr.length;i+=1){
  135.             if(charArr[0]!=charArr[i]){
  136.                 System.out.println(
  137.                     "The characters of the array are not similar");
  138.                 break;
  139.             }
  140.         }
  141.        
  142.         //resource leak close
  143.         s.close();
  144.     */
  145.        
  146.     /*Ex6
  147.         //variables declaration
  148.         int[] numArr = new int[5];
  149.        
  150.         //define scanner
  151.         Scanner s = new Scanner(System.in);
  152.        
  153.         //user instruction
  154.         System.out.println(
  155.                 "Please enter a number array of 5 values");
  156.  
  157.         //user input values into number array
  158.         for (int i=0;i<numArr.length;i+=1)
  159.             numArr[i]= s.next().charAt(0);
  160.  
  161.         //operation
  162.         for (int i=1;i<numArr.length;i+=1){
  163.             if(numArr[i]<=numArr[i-1]){
  164.                 System.out.println(
  165.                     "The numbers of the array are not in ascending order");
  166.                 break;
  167.             }else if (i==numArr.length-1)
  168.                 System.out.println(
  169.                 "The numbers of the array are in ascending order");
  170.         }
  171.        
  172.         //resource leak close
  173.         s.close();
  174.     */
  175.     }
  176.  
  177. }
  178. **************************************************************
  179. Method practice
  180. --------------------------------------------------------------
  181. import java.util.Scanner;
  182.  
  183. public class Tester03 {
  184.  
  185.     public static void main(String[] args) {
  186.    
  187.         //Method practice - Ex 1  
  188.        
  189.         //variables declaration
  190.         int numberArr[] = new int[5],digit,repeats=0;
  191.  
  192.         //define scanner
  193.         Scanner s = new Scanner(System.in);
  194.        
  195.         //user instruction 1
  196.         System.out.println("Gimme number array with 5 values");
  197.        
  198.         //user input values into our array
  199.         for (int i=0;i<numberArr.length;i+=1)
  200.             numberArr[i]= s.nextInt();
  201.        
  202.         //user instruction 2
  203.         System.out.println("Gimme a number to count its repeat in the above array");
  204.         digit = s.nextInt();
  205.  
  206.         //operation
  207.         for (int i=0;i<numberArr.length;i+=1)
  208.             repeats += countDigInNumber(numberArr[i],digit );
  209.        
  210.         //output to user
  211.         System.out.println("In said array of number , the digit - "+ digit + " is repeated "+
  212.                 repeats + " times.");
  213.        
  214.         //resource leak close
  215.         s.close();
  216.     }
  217.    
  218.     public static int countDigInNumber(int number,int digit){
  219.         int returnNum=0;
  220.         do{
  221.             if (number%10 ==digit)
  222.                 returnNum += 1;
  223.             number/=10;
  224.         }while(0<number);      
  225.         return returnNum;
  226.     }  
  227. }
  228. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  229. import java.util.Scanner;
  230.  
  231. public class Tester04 {
  232.  
  233.     public static void main(String[] args) {
  234.         // TODO Auto-generated method stub
  235.     //Method practice - Ex 2  
  236.        
  237.         //variables declaration
  238.         int[] numberArr1 = new int[5], numberArr2 = new int[5],
  239.         outputArr = new int[5];
  240.         String outputStr = "[";
  241.  
  242.         //define scanner
  243.         Scanner s = new Scanner(System.in);
  244.        
  245.         //user instruction 1
  246.         System.out.println(
  247.                 "Please enter the first number array with up to 5 values, if empty - type 0");
  248.        
  249.         //user input values into number array 1
  250.         for (int i=0;i<numberArr1.length;i+=1)
  251.             numberArr1[i]= s.nextInt();            
  252.  
  253.         //user instruction 2
  254.         System.out.println(
  255.                 "Please enter the second number array with up to 5 values, if empty - type 0");
  256.        
  257.         //user input values into number array 2
  258.         for (int i=0;i<numberArr2.length;i+=1)
  259.             numberArr2[i]= s.nextInt();
  260.            
  261.  
  262.         //operation + string building
  263.         outputArr = repeatedNums(numberArr1,numberArr2);
  264.         for (int i=0;i<outputArr.length;i+=1){         
  265.             if(outputArr[i]!=0)
  266.                 outputStr += outputArr[i];
  267.             if (i+1<outputArr.length && outputArr[i+1] != 0)
  268.                 outputStr += ",";
  269.         }
  270.         outputStr += "]";
  271.        
  272.         //output to user
  273.         System.out.println("In said 2 arrays of numbers , the repeated numbers are - "+ outputStr);
  274.        
  275.         //resource leak close
  276.         s.close();
  277.     }
  278.  
  279.     public static int[] repeatedNums(int[] array1,int[] array2){
  280.         int[] returnNumArr = new int[array1.length];
  281.         for(int i=0,counter=0;i<array1.length;i+=1){
  282.             for(int j=0;j<array2.length;j+=1){
  283.                 if (array1[i] == array2[j]){
  284.                     returnNumArr[counter] = array1[i];
  285.                     counter +=1;
  286.                 }
  287.             }      
  288.         }      
  289.         return returnNumArr;
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement