201B013 Advanced Programming Lab - 2
201B013 Advanced Programming Lab - 2
Batch :- B1 (BX)
Enrollment No :- 201B013
Submitted To :- Dr Ravindra Kumar Singh
}
}
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);
}
}
}
output = fact(n - 1) * n;
return output;
}
}
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);
}
}
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("");
}}}
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 {
child.m0(); //inheritence
child.m1(); //overriding
bChild.m0(); //testForPolymorphism
}}
class Base {
Base(int x) {
System.out.println("class Base");
void m0() {
void m1() {
System.out.println("m1 called");
System.out.println("m2 called");
Child(int x) {
super(x);
System.out.println("class Child");
}
void m0() {
void m1() {
System.out.println("m1 overriden");
System.out.println("static m2 overriden");
}}