There are two arrays of length 100 each. Each of these has initially n ....


  1. There are two arrays of length 100 each. Each of these has initially n (n<100) elements. First array contains names and the second array contains numbers such that ith name in array1 corresponds to ith number in array2.  Write a program which asks the user to enter a name prints its corresponding number in array2. And if that name is not present in Array1, ask the user to input a number and then if the size permits, add the name and number to array1 and array2 respectively at the end and update the size(n) of list.

 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.Buffer;


public class arraypro {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        final int i=6;
        String names[]=new String[6];
        names[0]="rama";
        names[1]="madhu";
        names[2]="sudha";
        names[3]="vagi";
        int[] number=new int[6];
        number[0]=21;
        number[1]=32;
        number[2]=43;
        number[3]=56;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       
        int a;
        boolean c=true;
        do{
        boolean b=false;
        System.out.println("Enter any name: ");
        String s=br.readLine();
        for(a=0;a<names.length && names[a]!=null;a++){
            if(names[a].equals(s)){
                System.out.println("Corresponding number is "+number[a]);
                b=true;
                break;
            }
        }
        if(a<=names.length && !b){
            if(names.length<=i){
                names[a]=s;
                System.out.println("Enter number ");
                number[a]=Integer.parseInt(br.readLine());
            }
            else{
                System.out.println("Out of range");
            }
           
        }
       
        for(int x=0;x<names.length && names[x]!=null;x++){
            System.out.println(names[x]+"\t"+number[x]);
        }
        //System.out.println(names.toString()+"\n"+number.toString());
        System.out.println("Do u  want to continue (y/n):");
        String d=br.readLine();
        if(d.equals("n") || a+1==names.length){
            c=false;
        }
        }while(c);
    }

}

Comments

  1. class Number{
    int num;
    public Number(int num){
    this.num = num;
    }

    abstract class Getter{
    public abstract void getNum();
    public abstract void getSqr();
    public void getAll(){
    getNum();
    getSqr();
    }
    }

    }

    class MyClass extends Number.Getter{
    }

    (a) Complete the code for MyClass in which you've to pass a integer to Number class and call getAll() method from MyClass' constructor to get desired results.

    (b) Create an arraylist of MyClass and print all results by passing 1-10 in 10 arraylist object.


    please provide the code.
    gopaljeegupta1@gmail.com

    ReplyDelete

Post a Comment

Please post comments here:-)

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers