Friday, April 11, 2014

Java Examples executed

1. List Iterator with input from console

The following is a an example run for inputting an ArrayList from console and reads out. This example is for novices only............

package iteratortut;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.ListIterator;

public class Iteratortut {
    public static void main(String[] args) throws IOException {
        ArrayList<String> vegs=new ArrayList<String>();
        ListIterator ltr=null;
        BufferedReader bufferRead=new BufferedReader(new InputStreamReader(System.in));
        boolean inputreq=true;
        String s=null;
        String veginput=null;
           while(inputreq){
            System.out.println("Whether you want to continue : ");
            try{
            s=bufferRead.readLine();
                }catch(IOException e){
                e.printStackTrace();
                }
           
            if (s.equals("no")){
               inputreq=false;
            }
           
            System.out.println("The vegetable name is= ");
            veginput=bufferRead.readLine();
            vegs.add(veginput);
        }
       
        for(String veg: vegs){
            System.out.println("hi"+veg);
              }
       
    }
   
}

2. Challenge:  the sum of the digits of 2 to the 10th power is: 2^10 = 1024 => 1+0+2+4 => 7

What is the sum of the digits of 2^50?

 package powaddition;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;

public class Powaddition {
    public static void main(String[] args) {
       int number=0;
       int power=0;
       int cycle=0;
      //Input number from console       
        System.out.println("What is the power: " );
        Scanner pscn=new Scanner(System.in);
        power=pscn.nextInt();

        System.out.println("What is the number: " );
        Scanner scn=new Scanner(System.in);
        number = (int) Math.pow(scn.nextInt(), power);

//How many cycle this will iterate through
        cycle=String.valueOf(number).length();
//Length of array to be created
         int[] intarry=new int[String.valueOf(number).length()];
     
//Call the function to perform seperation
 while(cycle>0){
            int res=addpower(number,cycle);
            System.out.println("the value returned is "+res);
            intarry[cycle-1]=res;
            number=(int) (number-res*Math.pow(10, cycle-1));
            cycle=cycle-1;
            }
       int sum=0;
       int iter=0;
        for(Integer n:intarry){
           sum=sum+intarry[iter];
           iter++;
                 }
        System.out.println("Sum is="+sum);
    }
//Function that performs separation
 
   public static int addpower(double d, int n){
               int num=(int) ((int)d/Math.pow(10, n-1));
                System.out.println("The rerune value is "+ num);
       return num;
      
           }
   
}

1 comment: