Advanced Programming Lab - 2
Course Code: 18B17CI573
Name :- Abhyansh Shrivastava
Batch :- B1 (BX)
Enrollment No :- 201B013
Submitted To :- Dr Ravindra Kumar Singh
BTech. (CSE V SEM)
Department of Computer Science and Engineering
Jaypee University of Engineering and Technology, Guna
S. No. Date Lab Exercise Page No Remarks
Lab Exercise-1
1.
public class Main // Class name starts with capital
{ // Command line argument
public static void main (String[]args) // Function name starts with lowercase
{ // static variable can be accessed without making an object of the
class
System.out.println ("Hello World"); // out is an object
}
}
2.
public class Main{
public static void main(String[]args){
for(int i=4; i>=1; i--){
for(int j=i; i>=1; j--)
System.out.print(" ");
for(int k=1; k<=5-i; k++)
System.out.print("*");
System.out.println();
}}}
3.
public class Main
{
public static void main(String[] args) {
for(int i=1; i<=10; i++){
System.out.printf ("%s x %d = %d\n", args[0], i, Integer.parseInt(args[0]) * i);
}}}
Lab Exercise 2
1.
Write a Java program to perform basic calculator
operations. import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner reader = new
Scanner(System.in);
System.out.print("Enter two numbers:
"); double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /):
"); char operator = reader.next().charAt(0);
double result;
switch (operator) {
case '+':
result = first +
second; break;
case '-':
result = first -
second; break;
case '*':
result = first *
second; break;
case '/':
result = first /
second; break;
default:
System.out.printf("Error! operator is not
correct"); return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
}
}
2. Write a Java program to calculate a factorial of a
number. import java.util.Scanner;
public class Factorial {
public static void main(String args[]) {
Scanner scanner = new
Scanner(System.in);
System.out.println("Enter the number:");
int num = scanner.nextInt();
int factorial = fact(num);
System.out.println("Factorial of entered number is: " + factorial);\
}
static int fact(int n)
{ int output;
if (n == 1)
{ return
1;
}
output = fact(n - 1) * n;
return output;
}
}
3. Write a Java program to calculate Fibonacci Series up to n
numbers. public class Fibonacci {
public static void main(String[] args)
{ int n = 100, t1 = 0, t2 = 1;
System.out.print("Upto " + n + ": ");
while (t1 <= n) {
System.out.print(t1 + " +
"); int sum = t1 + t2;
t1 = t2;
t2 =
sum;
}}}
4. Write a Java program to find out whether the given String is Palindrome
or not. import java.util.Scanner;
public class Palindrome {
static void checkPalindrome(String input)
{ boolean res = true;
int length = input.length();
for (int i = 0; i <= length / 2; i++) {
if (input.charAt(i) != input.charAt(length - i - 1))
{ res = false;
break;
}
}
System.out.println(input + " is palindrome = " + res);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your Statement:
"); String str = sc.nextLine();
checkPalindrome(str);
}
}
5. Write a Java program to calculate Permutation and Combination of 2
numbers. import java.util.Scanner;
public class nprandncr {
public static int fact(int num) {
int fact = 1, i;
for (i = 1; i <= num;
i++) { fact = fact * i;
}
return fact;
}
public static void main(String args[])
{ int n, r;
Scanner scan = new
Scanner(System.in);
System.out.print("Enter Value of n :
"); n = scan.nextInt();
System.out.print("Enter Value of r :
");
r = scan.nextInt();
System.out.print("NCR = " + (fact(n) / (fact(n - r) * fact(r))));
System.out.print("nNPR = " + (fact(n) / (fact(n - r))));
}
}
6. Write a program in Java to find out the Alphabet and Diamond Pattern.
Alphabet
import
java.util.Scanner;
public class PatternA {
void display(int n) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n / 2; j++) {
if ((j == 0 || j == n / 2) && i != 0 || i == 0 && j != n / 2 || i == n /
2) System.out.print("*");
else
System.out.print("
");
}
System.out.println();
}
}
public static void main(String[] args)
{ Scanner sc = new
Scanner(System.in); PatternA a =
new PatternA(); a.display(7);
}
}
Diamond
import java.util.Scanner;
public class
DiamondPattern {
public static void main(String args[])
{ int n, i, j, space = 1;
System.out.print("Enter the number of rows: ");
Scanner s = new Scanner(System.in);
n = s.nextInt();
space = n - 1;
for (j = 1; j <= n; j++) {
for (i = 1; i <= space; i++) {
System.out.print(" ");
}
space--;
for (i = 1; i <= 2 * j - 1; i++) {
System.out.print("*");
}
System.out.println("");
}
space = 1;
for (j = 1; j <= n - 1; j++)
{ for (i = 1; i <= space;
i++) { System.out.print("
");
}
space++;
for (i = 1; i <= 2 * (n - j) - 1; i++) {
System.out.print("*");
}
System.out.println("");
}}}
7. Write a Java program to reverse the letters present in the given
string. public class Stringreverse {
public static void main(String[] args)
{ String str = "Welcome To
Edureka"; String[] strArray =
str.split(" ");
for (String temp: strArray) {
System.out.println(temp);
}
for (int i = 0; i < 3; i++) {
char[] s1 =
strArray[i].toCharArray(); for (int j
= s1.length - 1; j >= 0; j--) {
System.out.print(s1[j]); }
System.out.print(" ");
}}}
8. Write a Java program to check whether the given array is Mirror Inverted
or not. public class MirrorInverse {
static boolean isMirrorInverse(int arr[]) {
for (int i = 0; i < arr.length; i++) {
if (arr[arr[i]] != i)
return false;
}
return true;
}
public static void main(String[] args)
{ int arr[] = {1,2,3,0};
if (isMirrorInverse(arr))
System.out.println("Yes");
else
System.out.println("No");
}
}
Lab Exercise-3
class Main {
public static void main(String args[]) {
Child child = new Child(2);
child.m0(); //inheritence
child.m1(); //overriding
child.m2(); //static overriding
Base bChild = new Child(3); //Parameterized Constructors
bChild.m0(); //testForPolymorphism
}}
class Base {
Base(int x) {
System.out.println("class Base");
void m0() {
System.out.println("class Base m0 called");
void m1() {
System.out.println("m1 called");
static void m2() {
System.out.println("m2 called");
class Child extends Base {
Child(int x) {
super(x);
System.out.println("class Child");
}
void m0() {
System.out.println("class Child m0 called");
void m1() {
System.out.println("m1 overriden");
static void m2() {
System.out.println("static m2 overriden");
}}