NPTEL » Programming In Java Assignment week 7 Sep-2020

NPTEL Java Assignment week 7

With the growth of Information and Communication Technology, there is a need to develop large and complex software. To meet this requirement object-oriented paradigm has been developed and based on this paradigm the Java programming language emerges as the best programming environment.

Programming in Java is a MOOC based course which is 12 weeks in duration and can fulfill the criteria of 4 credits in a year. You can visit the NPTEL SWAYAM platform and register yourself for the course. This course is brought to you by Prof. Dr. Debasis Samanta who holds a Ph.D. in Computer Science and Engineering from the Indian Institute of Technology Kharagpur.

Programming In Java 2020 Details:-

  1. Who Can Join: The undergraduate students from the engineering disciplines namely CSE, IT, EE, ECE, etc. might be interested in this course.
  2. Requirements/Prerequsites: The course requires that the students are familiar with a programming language such as C/C++ and data structures, algorithms.
  3. INDUSTRY SUPPORT:   All IT companies.

Programming In Java Assignment 7 Answers 2022

Java Assignment Week 7 Answers:-

Java Week 7: Q1.

Complete the following code/program fragment to read 3 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.


Answer/Code:-

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

//Write the appropriate code to read the 3 integer values and find their sum.
int a,b=0,sum=0;
Scanner i = new Scanner(System.in);
for(a=0; a<3; a++)
{
b=i.nextInt();
sum=sum+b;}
System.out.println(sum);}
}

Java Week 7: Q2

Complete the code to catch/find 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”.

Answer/Code:-

import java.io.;

public class Question2{

public static void main(String args[])

{ try{InputStreamReader r=new InputStreamReader(System.in);

BufferedReader br=new BufferedReader(r);

String number=br.readLine();

int x = Integer.parseInt(number);

System.out.print(xx);}
catch(Exception e){
System.out.print(“Please enter valid data”);}

}
}

Java Week 7: Q3

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
/find 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.

Answer/code:-

import java.util.*;
public class Question3 {
public static void main(String[] args) {
try{
byte barr[]={‘N’,’P’,’T’,’E’,’L’,’-‘,’J’,’A’,’V’,’A’,’J’,’A’,’N’,’-‘,’N’,’O’,’C’,’C’,’S’,’E’};
Scanner inr = new Scanner(System.in);
int n = inr.nextInt();
// Complete the code to get specific indexed byte value and its corresponding char value.
String str=new String(barr,n,1);
System.out.println(barr[n]);
System.out.print(str);}
catch (Exception e){
System.out.println(“exception occur”);}}}

NPTEL » Programming In Java Assignment week 6 Sep-2020

Java Week 7: Q4

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

Answer/Code:-

import java.io.; import java.util.;

public class Question4{
public static void main(String[] args) {
int c=0;
try{
InputStreamReader r=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(r);
String s1 = br.readLine();
//Write your code here to count the number of vowels in the string “s1”
for(int i=0;i<s1.length();i++){
char s=s1.charAt(i);
if(s==’a’||s==’A’||s==’e’||s==’E’||s==’i’||s==’I’||s==’o’||s==’O’||s==’u’|| s==’U’)
{c=c+1;}}
System.out.println(c);
}
catch (Exception e){
System.out.println(e);}
}
}

Java Week 7: Q5

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

Answer/Code:-

import java.util.*;
public class Question5 {
public static void main(String[] args) {
try{
String s1=”NPTELJAVA”;
Scanner inr = new Scanner(System.in);
int n = inr.nextInt();
char c=’a’;
//Replace the char in string “s1” with the char ‘a’ at index “n” and print the modified string
byte[] barr=s1.getBytes();
byte b1=(byte) c;
barr[n]=b1;
System.out.print(new String(barr));}
catch (Exception e){
System.out.println(“exception occur”);}
}
}

Find Other Quiz Here:

Amazon Fashion Quiz Answers & Win ₹1000

Amazon Great Indian Festival Quiz Answers: Win Rs. 50,000

NPTEL » Programming In Java Assignment week 6 Sep-2020

NOTE: These codes are based on our knowledge. Answers might be incorrect, we suggest you to not the copy-paste answers blindly.