Programming In Java | Week 7

Programming In Java | Week 7

Course Link: https://onlinecourses.nptel.ac.in/noc23_cs74/course

Programming Assignment

1. Complete the following code fragment to read three integer values from the keyboard and find the sum of the values. Declare a variable “sum” of type int and store the result in it.

Solution:

import java.util.*;
public class Question1{
        public static void main (String[] args){

           int i,n=0,sum=0;
           Scanner in = new Scanner(System.in);
           for(i=0;i<3;i++)
           {
              n = in.nextInt();
              sum =sum+n;
           }

2. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “Please enter valid data” .If there is no such exception, it will print the “square of the number”.

Solution:

  try{
       InputStreamReader r=new InputStreamReader(System.in);
       BufferedReader br=new BufferedReader(r);
       String number=br.readLine();
       int x = Integer.parseInt(number);
       System.out.print(x*x);
     }
  catch(Exception e){
       System.out.print("Please enter valid data");
     }

3. A byte char array is initialized. You have to enter an index value”n”. According to index your program will print the byte and its corresponding char value. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, it will print the required output.

Solution:

        	  String s2 = new String(barr,n,1);
              System.out.println(barr[n]);
        	  System.out.print(s2);
        }

4. The following program reads a string from the keyboard and is stored in the String variable “s1”. You have to complete the program so that it should should print the number of vowels in s1 . If your input data doesn’t have any vowel it will print “0”.

    			for(int i=0;i<s1.length();i++)
                {
                    char s2=s1.charAt(i);
                    if(s2=='e' || s2=='E'|| s2=='a' || s2=='A' || s2=='i'
                       || s2=='I' || s2=='o' || s2=='O' || s2=='u' || s2=='U')
                   	{
                     	c=c+1;
                    }
             	}

5. A string “s1” is already initialized. You have to read the index “n” from the keyboard. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, your program should replace the char “a” at the index value “n” of the “s1” ,then it will print the modified string.

Solution:

    		byte[] barr=s1.getBytes();

            byte b1 = (byte) c;
            barr[n]=b1;
    		System.out.print(new String(barr));
	    }

* The material and content uploaded on this website are for general information and reference purposes only !

Please do it by your own first!

DMCA.com Protection Status

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments



0
Would love your thoughts, please comment.x
()
x