Monday, March 23, 2020

WEEK 8

QUESTION 1:


// Declare a user-defined exception
class NegativeValException : public exception {     // LINE-1
public:
    virtual const char* what() const throw() {
        return "Negative value";
    }
};

// Declare a user-defined exception
class ZeroValException : public exception {         // LINE-2
public:
    virtual const char* what() const throw() {
        return "Zero";
    }
};

int main() {
    int i;
    cin >> i;
   
    try {
        if (i < 0)
            // Throw the exception object
            throw NegativeValException();                               // LINE-3

        else if (i == 0)
            // Throw the exception object
            throw ZeroValException();                               // LINE-4

QUESTION 2:


template <class T, int n>                    // LINE-1
class container {
    T arr[n];                // LINE-2

    int i;
public:
    container() : i(-1) { }

    void operator=(char data) {// LINE-3

QUESTION 3:


template <class T, int n>  // LINE-1

void mysequence<T,n>::setVal(int i, T value) {  // LINE-2

    items[i] = value;
}

template <class T, int n>   // LINE-3

T mysequence<T,n>::getVal(int i) {  // LINE-4

    return items[i];
}

Wednesday, March 18, 2020

WEEK 7 ASSIGNMENT SOLUTION

GUYS I KNOW I WAS SUPER INCONSISTENT AND LATE IN UPLOADING THE SOLUTIONS BUT I WAS HAVING A BUSY TIME IN PAST FEW WEEKS(COLLEGE WORK ) , I AM NOT GIVING ANY EXCUSES BUT JUST LETTING YOU ALL GUYS KNOW WHAT WAS THE REASON DUE TO WHICH I AM UPLOADING VIDEOS SO LATE. AND THANKS FOR WATCHING MY STUFF. 

QUESTION 1:

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;
          }

QUESTION 2:


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");
}

QUESTION 3:


// 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);
}

QUESTION 4:


//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;
  }
}

QUESTION 5:


//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));
}

Monday, March 16, 2020

WEEK 7 ASSIGNMENT SOLUTION

QUESTION 1:


Container& operator =(int val) {      // LINE-1

        this->arr[++i] = val;
        return *this;
    }

    operator int()  {      // LINE-2

        return arr[i--];
    }
};


QUESTION 2:


    virtual void print(int i)=0;          // LINE-1
};

class derived1 : public base{ // LINE-2
public:
    void print(int i) { cout << i * 1 << " "; }
};

class derived2 : public base { // LINE-3
public:
    void print(int i) { cout << i * 2 << " "; }
};

class derived3 : public base{ // LINE-4

QUESTION 3:


void updateSem(int new_sem) const{             // LINE-1

        student* ptr=const_cast<student*> (this);
      ptr->sem = new_sem;             // LINE-2
    }

    void show() const { 


QUESTION 4:


class derived1 : virtual public base {    // LINE-1
public:
    derived1();
    derived1(int _pr);
};

class derived2 : virtual public base {    // LINE-2
public:
    derived2();
    derived2(int _pr);
};

class dd : public derived2, public derived1 {          // LINE-3

Wednesday, March 11, 2020

WEEK 6 ASSIGNMENT SOLUTION

QUESTION 1:

// Write the appropriate code to extend the Thread class to complete the class Question61.
public class Question61 extends Thread{

public void run()
{
  System.out.print("Thread is Running.");
}

QUESTION 2:

// Create main class Question62 with main() method and appropriate statements in it
public class Question62{
  public static void main(String[] args){
    ThreadRun a = new ThreadRun();
    Thread b = new Thread(a);
    b.start();
  }
}


QUESTION 3:

// Create a class named MyThread and extend/implement the required class/interface

// Define a method in MyThread class to print the output

class MyThread extends B
{
  public void run()
  {
    System.out.print("NPTEL Java");
  }
}

QUESTION 4:

synchronized void print(int n){
   for(int i=1;i<=5;i++){ 
     System.out.println(n*i); 
     try{ 
      Thread.sleep(400); 
     }catch(Exception e){
        System.out.println(e);
     } 
   }
 } 


QUESTION 5:

// Write the necessary code below...
t.setPriority(Thread.MAX_PRIORITY);
t.start();
t.setName("NPTEL");

Saturday, March 7, 2020

WEEK 6 ASSIGNMENT SOLUTION

QUESTION 1:

virtual void compute(){ n = n*n; }           // LINE-1

   
    virtual void show(){ cout << n << endl; } // LINE-2
};

QUESTION 2:


Base::f();          // LINE-1 Call proper function
        cout << name << endl;
    }
    void g() {
        Base::g();          // LINE-2 Call proper function
    }
};

void Base::f(){                // LINE-3 Write proper function header
    cout << "Good day, ";
}

void Base::g(){                // LINE-4 Write proper function header


QUESTION 3:


virtual void computeSalary()=0;                      // LINE-1

    double computePF(int basic) { return (basic*pf / 100); }
};

class Manager : public Employee {
    int basic;
public:
    Manager(int b) : basic(b) { }
    void computeSalary() {

        double pf = Employee::computePF(basic);      // LINE-2

        double sal = basic + pf;
        cout << "Manager's Salary: " << sal << endl;
    }
};

class Analyst : public Employee {
    int basic;
public:
    Analyst(int b) : basic(b) { }
    void computeSalary() {

        double pf = Employee::computePF(basic);    // LINE-3

QUESTION 4:


virtual ~base() { print(); }    // LINE-1

};

class derived : public base {
public:
    derived(int _counter = 0) : base(++_counter) { cout << counter << " "; }
   
    virtual ~derived(){ print(); }   // LINE-2
};

Wednesday, March 4, 2020

WEEK 5 ASSIGNMENT SOLUTION

QUESTION 1 :


//Create a class A which implements the interface Number.
class A implements Number
{
  public int findSqr(int i)
  {
    return i*i;
  }
}

QUESTION 2:

 
//Create a class B, which implements the interface GCD.
class B implements GCD
{
  public int findGCD(int a, int b)
  {
    if(b==0)
      return a;
    else
      return findGCD(b,a%b);
  }
}

QUESTION 3:


//Read any two values for a and b.
 //Get the result of a/b;
a=input.nextInt();
b=input.nextInt();
{
  try
  {
    if(b!=0)
    {
      System.out.print(a/b);
      System.exit(0);
    }
  }
  finally
  {
    System.out.print("Exception caught: Division by zero.");
  }
}

QUESTION 4:


//Define try-catch block to save user input in the array "name",if there is an exception then catch the exception otherwise print the total sum of the array.
try
{
  for(int i=0; i<length; i++)
  {
    name[i]=sc.nextInt();
    sum=sum+name[i];
  }
  System.out.print(sum);
  System.exit(0);
}
finally
{
  System.out.print("You entered bad data.");
}

QUESTION 5:


// Put the following code under try-catch block to handle exceptions
      try
      {
switch (i)
        {
    case 0 :
int zero = 0;
j = 92/ zero;
break;
        case 1 :
int b[ ] = null;
j = b[0] ;
break;
      default:
       System.out.print("No exception");
}
      }
     catch(Exception e)
        {
          System.out.print(e);
        }