0% found this document useful (0 votes)
74 views11 pages

Lab Exercise - 1: Q1. Ans

The document contains a lab exercise with 8 questions on Java programming concepts. The questions cover printing patterns, calculating factorials and Fibonacci sequences, checking for palindromes, reversing strings, and diamond patterns. Later questions involve inheritance, overriding, polymorphism, and parameterized constructors. Sample code is provided as the answer for each question to demonstrate the concept.

Uploaded by

Amitesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views11 pages

Lab Exercise - 1: Q1. Ans

The document contains a lab exercise with 8 questions on Java programming concepts. The questions cover printing patterns, calculating factorials and Fibonacci sequences, checking for palindromes, reversing strings, and diamond patterns. Later questions involve inheritance, overriding, polymorphism, and parameterized constructors. Sample code is provided as the answer for each question to demonstrate the concept.

Uploaded by

Amitesh Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lab Exercise – 1

Q1. Write a program in Java to print “Hello World”.


Ans:
class First
{
public static void main ( String args[ ])
{
System.out.println("Hello World");
}
}
Output:

Q2: Write a program to print the following patterns:


Ans:
i)
class Main
{
public static void main (String args[])
{
int a = Integer.parseInt(args[0]);
for (int i = 0; i < a; i++)
{
for (int j = 0; j < a - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output:

1
ii)
public class Main
{
public static void main (String args[])
{
int a = Integer.parseInt(args[0]);
for (int i = a; i >= 0; i--)
{
for (int j = 0; j < a - i; j++)
{
System.out.print(" ");
}
for (int k = 0; k <= i; k++)
{
System.out.print("* ");
}
System.out.println();
}
}
}

Output:

Q3: Write a program in Java to print the table of a number received through command
line argument.
Ans:
class Main
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
for(int i = 1; i <= 10; i++)
System.out.println(a + " X " + i + " = " + a * i);
}
}

2
Lab Exercise – 2

Q1. Write a Java program to perform basic Calculator operations.


Ans:
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbrs: ");
double fir = sc.nextDouble();
double sec = sc.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char oper = sc.next().charAt(0);
double res;
switch(oper)
{
case '+':
res = fir + sec;
break;

case '-':
res = fir - sec;
break;

case '*':
res = fir * sec;
break;

case '/':
res = fir / sec;
break;

default:
System.out.print("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", fir, oper, sec, res);
}
}

Q2: Write a Java program to calculate a Factorial of a number.

3
Ans:
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, f = 1;
System.out.println("Enter an integer to calculate its factorial");
Scanner in = new Scanner(System.in);
n = in.nextInt();
if (n < 0)
System.out.println("Number should be non-negative.");
else
{
for (c = 1; c <= n; c++)
f = f*c;

System.out.println("Factorial of "+n+" is = "+f);


}
}
}

Q3: Write a Java program to calculate Fibonacci Series up to n numbers.


Ans:
import java.util.Scanner;
class Fibonacci
{
public static void main(String[] args)
{
int n, a = 0, b = 0, c = 1;
Scanner s = new Scanner(System.in);
System.out.print("Enter value of n:");
n = s.nextInt();
System.out.print("Fibonacci Series:");
for(int i = 1; i <= n; i++)
{
a = b;
b = c;
c = a + b;
System.out.print(a+" ");
}
}
}

4
Q4: Write a Java program to find out whether the given String is Palindrome or not.
Ans:
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);

System.out.println("Enter a string to check if it's a palindrome");


original = in.nextLine();

int length = original.length();

for (int i = length - 1; i >= 0; i--)


reverse = reverse + original.charAt(i);

if (original.equals(reverse))
System.out.println("The string is a palindrome.");
else
System.out.println("The string isn't a palindrome.");
}
}

Q5: Write a Java Program to reverse the letters present in the given String.
Ans:
import java.util.*;
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to reverse");
original = in.nextLine();
int length = original.length();
for (int i = length - 1 ; i >= 0 ; i--)
reverse = reverse + original.charAt(i);
System.out.println("Reverse of the string: " + reverse);
}
}

5
Q6: Write a program in Java for Diamond Pattern.
Ans:
import java.util.Scanner;
class Diamond
{
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("");
}
}
}

6
Q7. Write a Java Program to check whether the given array is Mirror Inverse or not.
Ans:
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[] = {3,4,2,0,1};
if (isMirrorInverse(arr))
System.out.println("Yes");
else
System.out.println("No");
}
}

Q8: Write a Java program to calculate Permutation and Combination of 2 numbers.


Ans:
import java.util.Scanner;
class percomb
{
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();
7
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))));
}
}

8
Lab Exercise – 3

Q1: Test Inheritance


Ans:
class Mother
{
int x;
void show()
{
System.out.println("I am Mother");
}
}
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();
}
}

Q2: Test Overriding


Ans:
class Mother
{
int x;
void show()
{
System.out.println("I am Mother");
}
}
class Child extends Mother
{
void show()
{
System.out.println("I am Child");
}
}
9
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}

Q3: Test Polymorphism


Ans:
class Child extends Mother
{
void show()
{
System.out.println("I am Child");
}
}
class Mother
{
int x;
void show()
{
System.out.println("I am Mother");
}
}
public class Main
{
public static void main (String args[]){
Mother m1 = new Child();
m1.show();
}
}

Q4: Parameterized Constructor


Ans:
class One
{
int x;
public One(int a)
{
this.x = a;
}
void show()
10
{
System.out.println(x);
}
}
class Two extends One
{
public Two()
{
super(10);
}
}
public class Main
{
public static void main (String args[]){
One o = new One(3);
o.show();
Two t = new Two();
t.show();
}
}

11

You might also like