ADVANCE PROGRAMMMING LAB-2
LAB RECORD
Submitted by
Jaivardhan Singh(201B128)
Batch B4(BX)
Submitted to: Dr. Ravindra Singh Sir
2020-2021
Department of Computer Science & Engineering
JAYPEE UNIVERSITY OF ENGINEERING & TECHNOLOGY,
AB ROAD, RAGHOGARH, DT. GUNA-473226 MP, INDIA
1
S. No. Date Lab Exercise Page No. Rem
INDEX
2
LAB Exercise -1
1. Write a program in Java to print “Hello World”.
public class Main
{
public static void main (String[] args)
{
System.out.println("Hello World");
}
}
2. Write a program in Java to print these patterns:
public class Main
{
public static void main(String[] args)
{
for (int i=0; i<4; i++)
{
for (int j=4-i; j>1; j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("* ");
}
System.out.println();
}
}
}
public class Main
{
public static void main(String[] args)
{
for (int i=4; i>=1; i--)
{
for (int k=4; k>=i; k--)
{
3
System.out.print(" ");
}
for (int j=1; j<=2*i; j++ )
{
if(j%2!=0)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
3. Write a program in Java to print the table of a number received through command line
argument, e.g.
If user gives 4 through command line argument, the program should print:
4 x 1= 4
4 x 2= 8
............
4x10= 40
public class Main
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
for(int i=1;i<=10;i++)
{
System.out.println("4 x "+i+"="+i*a);
}
}
}
LAB Exercise 2
4
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 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)
{
5
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 ();
6
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 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 ();
}
}
7
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
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 Inverse 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");
}
}
9
LAB Exercise 3
1. Test for inheritance
class Motherther{
int x=4;
void show()
{
System.out.println("The value of x = "+x);
}
}
class Child extends Mother{
public class Main{
public static void main (String[] args) {
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}
}
2. Test for Overriding
class Motherther{
void show()
{
System.out.println("Hello World!"");
}
}
class Child extends Mother{
void show()
{
System.out.println("Hello JUET!");
}
public class Main{
public static void main (String[] args) {
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}
}
10
3. Test for Polymorphism
class Motherther{
void show()
{
System.out.println("Hello World!"");
}
}
class Child extends Mother{
void show()
{
System.out.println("Hello JUET!");
}
public class Main{
public static void main (String[] args) {
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
Mother m1 = new Child();
m1.show();
}
}
(b)
class Motherther{
void show()
{
System.out.println("Hello World!"");
}
}
class Child extends Mother{
public static void show()
{
System.out.println("Hello JUET!");
}
public class Main{
public static void main (String[] args) {
Mother m = new Mother();
m.show();
Child ch = new Child();
11
ch.show();
Mother m1 = new Child();
m1.show();
}
}
4. Parameterized Constructor
class One{
One(int x)
{
System.out.println("Hello World" + x);
}
}
class Two extends One{
Two(int x1)
{
super(x1);
System.out.println("Hello JUET");
}
}
public class Main{
public static void main (String[] args) {
One o = new One(5);
Two t = new Two(5);
}
}
12