Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Array practice
- ----------------------------------------------------------
- import java.util.Scanner;
- public class ArrayPractice {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- /*Ex1
- //variables declaration
- char inputArr[] = new char[10],searchCh;
- int repeat=0;
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction 1
- System.out.println("Please type an array of 10 characters");
- //user input values into number array 1
- for (int i=0;i<inputArr.length;i+=1)
- inputArr[i] = s.next().charAt(0);
- //user instruction 2
- System.out.println("Please give a char to search for");
- //buffer clean
- System.out.println();
- //user input values into number array 2
- searchCh = s.next().charAt(0);
- //operation
- for (int i=0;i<inputArr.length;i+=1){
- repeat += inputArr[i]==searchCh ? 1 : 0 ;
- }
- //output to user
- System.out.println(
- "Said char repeat in above string - " + repeat + " times");
- //resource leak close
- s.close();
- */
- /*Ex 2
- //variables declaration
- int[] numberArr1 = new int[5], numberArr2 = new int[5];
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction 1
- System.out.println(
- "Please enter the first number array of 5 values");
- //user input values into number array 1
- for (int i=0;i<numberArr1.length;i+=1)
- numberArr1[i]= s.nextInt();
- //clearing buffer
- System.out.println();
- //user instruction 2
- System.out.println(
- "Please enter the second number array of 5 values");
- //user input values into number array 2
- for (int i=0;i<numberArr2.length;i+=1)
- numberArr2[i]= s.nextInt();
- //operation +
- System.out.print("The indexes of similar numbers are - ");
- for (int i=0;i<numberArr1.length;i+=1){
- if(numberArr1[i]==numberArr2[i])
- System.out.print(i + " ");
- }
- //resource leak close
- s.close();
- */
- /*Ex 3
- //variables declaration
- int[] numberArr1 = new int[5], numberArr2 = new int[5],
- outputArr = new int[5];
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction 1
- System.out.println(
- "Please enter the first number array of 5 values");
- //user input values into number array 1
- for (int i=0;i<numberArr1.length;i+=1)
- numberArr1[i]= s.nextInt();
- //user instruction 2
- System.out.println(
- "Please enter the second number array of 5 values");
- //user input values into number array 2
- for (int i=0;i<numberArr2.length;i+=1)
- numberArr2[i]= s.nextInt();
- //operation
- System.out.print("The sum of both arrays at similar indexes are - ");
- for (int i=0;i<numberArr1.length;i+=1){
- outputArr[i] = numberArr1[i] + numberArr2[i];
- System.out.print(outputArr[i] + " ");
- }
- //resource leak close
- s.close();
- */
- /*Ex4
- //variables declaration
- char[] charArr = new char[5];
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction
- System.out.println(
- "Please enter a number array of 5 values");
- //user input values into number array
- for (int i=0;i<charArr.length;i+=1)
- charArr[i]= s.next().charAt(0);
- //operation
- for (int i=0;i<charArr.length;i+=1){
- if(charArr[0]!=charArr[i]){
- System.out.println(
- "The characters of the array are not similar");
- break;
- }
- }
- //resource leak close
- s.close();
- */
- /*Ex6
- //variables declaration
- int[] numArr = new int[5];
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction
- System.out.println(
- "Please enter a number array of 5 values");
- //user input values into number array
- for (int i=0;i<numArr.length;i+=1)
- numArr[i]= s.next().charAt(0);
- //operation
- for (int i=1;i<numArr.length;i+=1){
- if(numArr[i]<=numArr[i-1]){
- System.out.println(
- "The numbers of the array are not in ascending order");
- break;
- }else if (i==numArr.length-1)
- System.out.println(
- "The numbers of the array are in ascending order");
- }
- //resource leak close
- s.close();
- */
- }
- }
- **************************************************************
- Method practice
- --------------------------------------------------------------
- import java.util.Scanner;
- public class Tester03 {
- public static void main(String[] args) {
- //Method practice - Ex 1
- //variables declaration
- int numberArr[] = new int[5],digit,repeats=0;
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction 1
- System.out.println("Gimme number array with 5 values");
- //user input values into our array
- for (int i=0;i<numberArr.length;i+=1)
- numberArr[i]= s.nextInt();
- //user instruction 2
- System.out.println("Gimme a number to count its repeat in the above array");
- digit = s.nextInt();
- //operation
- for (int i=0;i<numberArr.length;i+=1)
- repeats += countDigInNumber(numberArr[i],digit );
- //output to user
- System.out.println("In said array of number , the digit - "+ digit + " is repeated "+
- repeats + " times.");
- //resource leak close
- s.close();
- }
- public static int countDigInNumber(int number,int digit){
- int returnNum=0;
- do{
- if (number%10 ==digit)
- returnNum += 1;
- number/=10;
- }while(0<number);
- return returnNum;
- }
- }
- &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
- import java.util.Scanner;
- public class Tester04 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //Method practice - Ex 2
- //variables declaration
- int[] numberArr1 = new int[5], numberArr2 = new int[5],
- outputArr = new int[5];
- String outputStr = "[";
- //define scanner
- Scanner s = new Scanner(System.in);
- //user instruction 1
- System.out.println(
- "Please enter the first number array with up to 5 values, if empty - type 0");
- //user input values into number array 1
- for (int i=0;i<numberArr1.length;i+=1)
- numberArr1[i]= s.nextInt();
- //user instruction 2
- System.out.println(
- "Please enter the second number array with up to 5 values, if empty - type 0");
- //user input values into number array 2
- for (int i=0;i<numberArr2.length;i+=1)
- numberArr2[i]= s.nextInt();
- //operation + string building
- outputArr = repeatedNums(numberArr1,numberArr2);
- for (int i=0;i<outputArr.length;i+=1){
- if(outputArr[i]!=0)
- outputStr += outputArr[i];
- if (i+1<outputArr.length && outputArr[i+1] != 0)
- outputStr += ",";
- }
- outputStr += "]";
- //output to user
- System.out.println("In said 2 arrays of numbers , the repeated numbers are - "+ outputStr);
- //resource leak close
- s.close();
- }
- public static int[] repeatedNums(int[] array1,int[] array2){
- int[] returnNumArr = new int[array1.length];
- for(int i=0,counter=0;i<array1.length;i+=1){
- for(int j=0;j<array2.length;j+=1){
- if (array1[i] == array2[j]){
- returnNumArr[counter] = array1[i];
- counter +=1;
- }
- }
- }
- return returnNumArr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement