Professional Documents
Culture Documents
ALGO
ALGO
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
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);
}
}
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;
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);
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");
}
}
8
Lab Exercise – 3
}
public class Main
{
public static void main (String args[]){
Mother m = new Mother();
m.show();
Child ch = new Child();
ch.show();
}
}
11
Lab Exercise – 4
12
Q2: Writing Beautiful Code
Ans:
package animals;
public class Animals
{
private String name,voice;
Animals(String name,String voice)
{
this.name = name; this.voice = voice;
}
protected void makeVoice()
{
System.out.println(this.name + " " + this.voice);
}
}
public class Voice
{
private Animals []animals = new Animals[5];
protected void prepareVoice()
{
animals[0] = new Animals("Cow","Moo");
animals[1] = new Animals("Dog","Bark");
animals[2] = new Animals("Pig","Oink");
animals[3] = new Animals("Goat","Bleat");
animals[4] = new Animals("Lion","Roar");
}
protected void hear()
{
prepareVoice();
for(int i=0;i<5;i++)
{
animals[i].makeVoice();
}
}
}
public class Voice2
{
public static void main(String[] args)
{
Voice voice = new Voice();
voice.hear();
}
}
13
Q3:
Ans:
package com.juet;
public class Pack1
{
protected void display()
{
System.out.println("Inside package com.juet and class Pack1");
}
}
package com.jiet; import com.juet.Pack1;
public class Pack2
{ public static void main(String[] args) {
Pack1 pack = new Pack1();
pack.display();
}
}
Error
Correct:
package com.jiet;
import com.juet.Pack1;
public class Pack2 extends Pack1
{
public static void main(String[] args)
{
Pack2 pack = new Pack2();
pack.display();
}
}
To access the protected member of Pack1 we need make Pack2 as sub child of Pack1. Now through
object of Pack2 we can access display method.
14
Lab Exercise – 5
Q1. Write the missing code in the following class definitions. Write a simple client for testing.
Ans:
public class Class_1
{
private int x;
private int y;
public Class_1()
{
x = 0;
y = 0;
}
16
Lab Exercise – 6
Q1. Write a program that demonstrates the use of static keywords in outer class, inner class
and methods.
Ans:
import java.util.*;
class Class2
{
public static void fun2()
{
System.out.println("Outer Class static");
}
static class Class1{
public static void fun1()
{
System.out.println("Inner class static");
}
}
}
class L6_1
{
public static void main(String args[])
{
Class2 cl2=new Class2();
Class2.Class1 cl1 = new Class2.Class1();
cl2.fun2();
cl1.fun1();
}
}
Q3. Write a program which demonstrates the use of method overriding and method overloading.
Ans:
import java.util.*;
class L6_3
{
void fun1()
{
System.out.println("Mother class_Overriding");
}
void fun2(int a)
{
System.out.println("Overloading with parameter func");
}
void fun2()
{
System.out.println("Overloading without parameter func");
}
}
class L6_3
{
public static void main(String args[])
{
Class1 cl1 = new Class1();
cl1.fun1();
cl1.fun2(5);
cl1.fun2();
}
}
Q4. Write a program that implements method hiding. How is it different from method overriding.
Ans:
import java.util.*;
class Class1
{
public static void fun1()
{
18
System.out.println("Mother class");
}
}
class Class2 extends Class1
{
public static void fun1()
{
System.out.println("Child class");
}
}
Class L6_4
{
public static void main(String args[])
{
Class1 obj=new Class1();
Class1 obj1=new Class2();
obj.fun1();
obj1.fun1();
}
}
class L6_6
{
public static void main(String args[])
19
{
Class1 cl1=new Class1();
cl1.fun();
cl1.fun1();
cl1.call();
cl1.fun3();
}
}
Q7. WAP which demonstrates the use of instances and static initialization blocks.
Ans:
import java.util.*;
class Class1pai
{
public static void fun1()
{
System.out.println("Mother class");
}
}
class L6_7
{
public static void main(String args[])
{
Class1 obj = new Class1();
Class1 obj1 = new Class2();
obj.fun1();
obj1.fun1();
}
}
20
Lab Exercise – 7
Q1. Create an abstract class 'Parent' with a method 'message'. It has two subclasses each
having a method with the same name 'message' that prints "This is first subclass" and "This is
second subclass" respectively. Call the methods 'message' by creating an object for each
subclass.
Ans:
abstract class Parent{
void message(){
}
}
class first extends Parent{
void message(){
System.out.println("This is First Subclass");
}
}
class Second extends Parent{
void message(){
System.out.println("This is Second Subclass");
}
}
class L_7{
public static void main(String[] args) {
Parent first = new first();
Parent second = new Second();
first.message();
second.message();
}
}
Q2. Create an abstract class 'Bank' with an abstract method 'getBalance'. Rs 100, Rs 150 and
Rs 200 are deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are
subclasses of class 'Bank', each having a method named 'getBalance'. Call this method by
creating an object of each of the three classes.
Ans:
abstract class Bank{
abstract void getBalance();
}
class Bank1 extends Bank{
void getBalance(){
System.out.println("Rs. 100");
}
}
class Bank2 extends Bank{
void getBalance(){
21
System.out.println("Rs. 150");
}
}
class Bank3 extends Bank{
void getBalance(){
System.out.println("Rs. 200");
}
}
class L_7{
public static void main(String[] args) {
Bank Bank1 = new Bank1();
Bank Bank2 = new Bank2();
Bank Bank3 = new Bank3();
Bank1.getBalance();
Bank2.getBalance();
Bank3.getBalance();
}
}
Q3. We have to calculate the percentage of marks obtained in three subjects (each out of 100)
by student A and in four subjects (each out of 100) by student B. Create an abstract class
'Marks' with an abstract method 'getPercentage'. It is inherited by two other classes 'A' and
'B' each having a method with the same name which returns the percentage of the students.
The constructor of student A takes the marks in three subjects as its parameters and the
marks in four subjects as its parameters for student B. Create an object for each of the two
classes and print the percentage of marks for both the students.
Ans:
abstract class Marks
{
abstract void getpercentage(int a, int b,int c);
abstract void getpercentage(int a, int b,int c,int d);
}
class A extends Marks
{
void getpercentage(int a, int b,int c)
{
int p=((a+b+c)/300)*100;
System.out.println("Student A got"+p+"Percentage");
}
}
class B extends Marks
{
void getpercentage(int a, int b,int c,int d)
{
int p=((a+b+c+d)/400)*100;
22
System.out.println("Student B got"+p+"Percentage");
}
}
class L_7
{
public static void main(String[] args)
{
Marks A =new A();
Marks B= new B();
A.getpercentage(70, 50, 80);
B.getpercentage(55, 78, 65, 96);
}
}
23
Lab Exercise – 8
Main.java
import java.util.Random;
public class Main{
public static void main(String []args){
Llists E1 = new Llists();
Llists E2 = new Llists();
Llists E3 = new Llists();
Llists E4 = new Llists();
Double prvs1 = 0.0,prvs2 = 0.0,prvs3 = 0.0,prvs4 = 0.0;
for(int i=0;i<7;i++){
Random rand = new Random();
Double time1 = rand.nextDouble();
Double time2 = rand.nextDouble();
Double time3 = rand.nextDouble();
Double time4 = rand.nextDouble();
prvs1 += time1;
prvs2 += time2;
prvs3 += time3;
prvs4 += time4;
E1.insert(prvs1,"E1");
E2.insert(prvs2,"E2");
E3.insert(prvs3,"E3");
E4.insert(prvs4,"E4");
}
Events a = new Mergell().merge(E1.head,E2.head);
Events b = new Mergell().merge(E3.head,E4.head);
Events fn = new Mergell().merge(a,b);
Events tmp = fn;
while(fn!=null){
System.out.println("Time = " + fn.time + " Events Type = " + fn.eventType);
fn = fn.next;
}
}
}
Events.java
Lists.java
Mergell.java
26
Lab Exercise – 9
Q1. Make a class University. This class has a method ‘exam( )’. exam( ) returns an exception
‘ExamSuspendException’ if stranger things happen in the University. You can implement
this by having a Boolean variable (say boolean strangerThings) set to true or false. Create
another class ‘JUET’. JUET has a method named ‘t1( )’. t1( ) makes a call to exam( ) along
with other normal statements. Make a class ExamSuspendException and declare it
extending the class Exception. Override its printStackTrace( ) method. Make a Test class
and test your code with four possible scenarios:
class University
{
boolean strangerThings = false;
void exam(boolean strangerThings) throws ExamSuspendException
{
if (strangerThings)
{
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1()
{
try
{
exam(true);
}
catch (ExamSuspendException e)
{
e.printStackTrace();
}
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace()
{
System.out.println("Exam Suspended");
}
}
public class Fa
{
public static void main(String[] args)
{
27
JUET juet = new JUET();
juet.t1();
}
}
class University
{
boolean strangerThings = false;
void exam(boolean strangerThings ) throws ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1() throws ExamSuspendException {
exam(true);
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Fb
{
public static void main(String[] args) {
JUET juet = new JUET();
juet.t1();
}
}
class University
{
boolean strangerThings = false;
void exam(boolean strangerThings ) throws ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1() throws ExamSuspendException {
28
exam(true);
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Fc
{
public static void main(String[] args) {
JUET juet = new JUET();
try {
juet.t1();
} catch (ExamSuspendException e) {
e.printStackTrace();
}
}
}
class University
{
boolean strangerThings = false;
void exam(boolean strangerThings ) throws ExamSuspendException {
if (strangerThings) {
throw new ExamSuspendException();
}
}
}
class JUET extends University
{
void t1() throws ExamSuspendException {
exam(true);
}
}
class ExamSuspendException extends Exception
{
@Override
public void printStackTrace() {
System.out.println("Exam Suspended");
}
}
public class Fd
{
public static void main(String[] args) throws ExamSuspendException {
JUET juet = new JUET();
juet.t1();
29
}
}
Q2. Exceptions are polymorphic Create Exception classes for Cloth, Pant, Shirt and Tshirt.
Override printStackTrace ( ). Create a class called ‘LaundryTest’. This class has a method
called unift ( ) which is a risky function and throws PantException, ShirtException,
TshirtException or ClothException, if there is an issue in respective garment type.
LaundryTest also has a main method, which calls unfit( ). Do the following one-by-one and
observe the output:
a) Create multiple catch statements, in order Tshirt, Shirt, Pant, Cloth, in the main to
handle different exceptions. The last statement inside the main method should be
System.out.println(“Last line of main”);
import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTesta {
void unfit(String type) throws TshirtException, ShirtException, PantException, ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
30
}
public static void main(String[] args) {
LaundryTesta laundryTest = new LaundryTesta();
System.out.println("Enter the type of cloth as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Scanner(System.in).nextLine());
} catch (TshirtException e) {
e.printStackTrace();
} catch (ShirtException e) {
e.printStackTrace();
} catch (PantException e) {
e.printStackTrace();
} catch (ClothException e) {
e.printStackTrace();
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}
b) Make a catch block return nothing. Add finally block after all catch blocks. Write
something dummy inside finally as well.
import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
31
class LaundryTestb {
void unfit(String type) throws TshirtException, ShirtException, PantException,
ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
}
public static void main(String[] args) {
LaundryTestb laundryTest = new LaundryTestb();
System.out.println("Enter the type of cloth as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Scanner(System.in).nextLine());
} catch (TshirtException e) {
} catch (ShirtException e) {
} catch (PantException e) {
} catch (ClothException e) {
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}
import java.util.Scanner;
class ClothException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Cloth Exception");
}
}
class PantException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Pant Exception");
}
}
class ShirtException extends Exception {
@Override
32
public void printStackTrace() {
System.out.println("Shirt Exception");
}
}
class TshirtException extends Exception {
@Override
public void printStackTrace() {
System.out.println("Tshirt Exception");
}
}
class LaundryTestc {
void unfit(String type) throws TshirtException, ShirtException, PantException,
ClothException {
if (type.equals("Tshirt")) {
throw new TshirtException();
} else if (type.equals("Shirt")) {
throw new ShirtException();
} else if (type.equals("Pant")) {
throw new PantException();
} else if (type.equals("Cloth")) {
throw new ClothException();
}
}
public static void main(String[] args) {
LaundryTestc laundryTest = new LaundryTestc();
System.out.println("Enter the type of cloth as Tshirt, Shirt, Pant or Cloth");
try {
laundryTest.unfit(new Scanner(System.in).nextLine());
} catch (ClothException e) {
e.printStackTrace();
} catch (TshirtException e) {
e.printStackTrace();
} catch (ShirtException e) {
e.printStackTrace();
} catch (PantException e) {
e.printStackTrace();
} finally {
System.out.println("very dummy");
}
System.out.println("Last line of main");
}
}
33