Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Saturday, 2 February 2019

FIRST WEEK TERM WORK Program 2

2) Using the concept of abstract class and dynamic method dispatch, write a Java Program that calculates and prints the areas of triangle, square and rectangle. 

package Program2;

abstract class shapes {
abstract void print();

abstract void calc();
}

class triangle extends shapes {
public int a, b, area;

triangle(int p, int q) {
a = p;
b = q;
}

void calc() {

area = a * b;

}

void print() {
System.out.println("Triangle area" + area);
}

}

class rectangle extends shapes {
public int a, b, area;

rectangle(int p, int q) {
a = p;
b = q;
}

void calc() {

area = a * b;

}

void print() {
System.out.println("Rectangle area" + area);
}

}

class square extends shapes {
public int a, area;

square(int p) {
a = p;

}

void calc() {

area = a * a;

}

void print() {
System.out.println("Square area" + area);
}

}

public class ShapesAbstract {
public static void main(String args[]) {
shapes obj = new triangle(5, 10);
obj.calc();
obj.print();
obj = new rectangle(5, 10);
obj.calc();
obj.print();
obj = new square(5);
obj.calc();
obj.print();
}

}

FIRST WEEK TERM WORK Program 4

4) “A Person has First Name, Last Name, Mobile Number & age. An Employee has Employee ID & Salary. Also, Employee IS-A Person.” Using the concept of inheritance, write a Java Program that displays all the attributes of Employee and Person for the given problem scenario.

package Program4;

class person {
String firstname, lastname;
double mobileno, age;

person(String fname, String lname, double n, double m) {
firstname = fname;
lastname = lname;
mobileno = n;
age = m;
}

void show() {
System.out.println("Firstname: " + firstname);
System.out.println("Lastname: " + lastname);
System.out.println("Mobile: " + mobileno);
System.out.println("Age: " + age);
}
}

class employee extends person {
double empid, Sal;

employee(String fname, String lname, double n, double m, double a, double b) {
super(fname, lname, n, m);
empid = a;
Sal = b;
}

void show() {
System.out.println("Firstname: " + firstname);
System.out.println("Lastname: " + lastname);
System.out.println("Mobile: " + mobileno);
System.out.println("Age: " + age);
System.out.println("empid:" + empid);
System.out.println("Sal:" + Sal);

}

}

public class Test {
public static void main(String args[]) {
person obj = new person("amit", "sharma", 8660, 20);
obj.show();
employee obj1 = new employee("amit", "sharma", 8660, 20, 1, 12000);
obj1.show();
}
}

FIRST WEEK TERM WORK Program 5

5) Write a Java Program for the following problem scenario: Create a package named “BasicMath” which has a class named Basic and has methods to perform following computations: 

a. Addition of two numbers 

b. Subtraction of two numbers 

c. Multiplication of two numbers

d. Division of two numbers

 Create another package named “AdvancedMath” which has a class named Advanced and has methods to perform following computations: 

a. Find sine of an angle 

b. Find ab 

c. Find log10 of a number

 Create a class named TestDemo defined inside a default package. This class must invoke all the methods of Basic and Advanced classes.


Default Package(Main class):

import AdvanceMath.Advance;
import BasicMath.Basic;

public class TestDemo {
public static void main(String[] args) {
Basic obj = new Basic();
Advance obj1 = new Advance();
int a;
double d;
a = obj.add(4, 5);
System.out.println("Sum= " + a);
a = obj.sub(4, 5);
System.out.println("Sub= " + a);
a = obj.mult(4, 5);
System.out.println("mult= " + a);
a = obj.div(4, 5);
System.out.println("div= " + a);
d = obj1.sin(30);
System.out.println("Sin= " + d);
d = obj1.exppow(2, 3);
System.out.println("Pow= " + d);
d = obj1.log(10);
System.out.println("Log= " + d);
}

}

SECOND WEEK TERM WORK Program 1

1) Write a Java Program that generates:
a. ArithmeticException when no arguments are passed
b. ArrayIndexOutOfBoundsException when one argument is passed
c. NullPointerException when two arguments are passed
d. NegativeArraySizeException when three arguments are passed
Display the stack trace of the exceptions generated.
When more than three arguments are passed, then the program should print all the command-
line arguments passed to it.

package exception1;

public class exceptiondemo {
public static void main(String[] args) {
int a;
int array[] = new int[3];
String s = null;
try {
if (args.length == 0) {
System.out.println("Zero Arguments");
a = 10 / 0;
}
if (args.length == 1) {
System.out.println("One Arguments");
array[6] = 10;
}
if (args.length == 2) {
System.out.println("Two Arguments");
s.length();
}
if (args.length == 3) {
System.out.println("Three Arguments");
array[-1] = 10;
} else {
System.out.println("More than 3 arguments");
for (int i = 0; i < args.length; i++) {
System.out.println(" " + args[i]);
}
}
} catch (NegativeArraySizeException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();

}

}

}

SECOND WEEK TERM WORK Program 3 (1a)

Question:

 Write a Java Program that takes a string as command-line argument and checks whether it is a palindrome or not. If the string is a palindrome, then it must print its length and convert that string from lower case to upper case letters or vice versa. If the string is not a palindrome, then
it should generate a user-defined exception StringNotPalindromeException.


Solution:
class StringNotPalindromeException extends Exception{
public StringNotPalindromeException(String message) {
super(message);
}

}
public class Exceptionpal {
public static void main(String[] args) {
char cmdline[]=args[0].toCharArray();
int n=args[0].length(),j=0,i=0;
try {
for(i=n-1;i>=n/2;i--,j++)
{
if(cmdline[i]!=cmdline[j])
{
throw new StringNotPalindromeException("String are not Palindrome");
}
}
}
catch(Exception e)
{
System.out.println("Error:");
System.out.println(e.getMessage());
}
if(i<j)
{
System.out.println("String is Palindrome");
System.out.println("String length "+args[0].length());
System.out.println("String In Lower Case:"+args[0].toLowerCase());
System.out.println("String In Upper Case:"+args[0].toUpperCase());
}
}

}