0% found this document useful (0 votes)
16 views

Java File

The document contains 5 questions about Java programming concepts. Each question provides sample code to demonstrate: 1. Creating a Rectangle class with methods to calculate area and perimeter using this and super keywords. 2. Designing a Bank account class using inheritance and static methods for withdrawal and deposit functions. 3. Using abstract classes and methods to create Motorbike classes for different bike types. 4. Handling exceptions using try-catch blocks with multiple catch blocks. 5. Implementing nested try statements to handle exceptions at different code blocks.

Uploaded by

Sunny Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java File

The document contains 5 questions about Java programming concepts. Each question provides sample code to demonstrate: 1. Creating a Rectangle class with methods to calculate area and perimeter using this and super keywords. 2. Designing a Bank account class using inheritance and static methods for withdrawal and deposit functions. 3. Using abstract classes and methods to create Motorbike classes for different bike types. 4. Handling exceptions using try-catch blocks with multiple catch blocks. 5. Implementing nested try statements to handle exceptions at different code blocks.

Uploaded by

Sunny Raj
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Q1.

WAP to create a simple class to find out the area and perimeter of rectangle and
box using super and this keyword.

import java.util.*;

// Rectangle Class File


public class Rectangle {

// Variable of data type double


double length;
double width;

// Area Method to calculate the area of Rectangle


void Area()
{
double area;
area = this.length * this.width;
System.out.println("Area of rectangle is : "
+ area);
}

// Perimeter Method to calculate the Perimeter of


// Rectangle
void Perimeter()
{
double perimeter;
perimeter = 2 * (this.length + this.width);
System.out.println("Perimeter of rectangle is : "
+ perimeter);
}
}

class Use_Rectangle {

public static void main(String args[])


{
// Object of Rectangle class is created
Rectangle rect = new Rectangle();

// Assigning the value in the length variable of


// Rectangle Class
rect.length = 22.854;

// Assigning the value in the width variable of


// Rectangle Class
rect.width = 30.65;

System.out.println("Length = " + rect.length);


System.out.println("Width = " + rect.width);
// Calling of Area method of Rectangle Class
rect.Area();

// Calling of Perimeter method of Rectangle Class


rect.Perimeter();
}
}
output

Q2. WAP to design a class account using the inheritance and static that show
all function of bank (withdrawal, deposit).
class account {

// Initial balance ₹100

static int total = 100;

// Money withdrawal method. Withdraw only if total money

// greater than or equal to the money requested for

// withdrawal

static synchronized void withdrawn(String name,

int withdrawal)

if (total >= withdrawal) {

System.out.println(name + " withdrawn "

+ withdrawal);

total = total - withdrawal;

System.out.println("Balance after withdrawal: "

+ total);

/* Making the thread sleep for 1 second after

each withdrawal.*/

try {

Thread.sleep(1000);

catch (InterruptedException e) {
e.printStackTrace();

// If the money requested for withdrawal is greater

// than the balance then deny transaction

else {

System.out.println(name

+ " you can not withdraw "

+ withdrawal);

System.out.println("your balance is: " + total);

// Making the thread sleep for 1 second after

// each transaction failure

// Try block to check for exceptions

try {

// Making thread to sleep for 1 second

Thread.sleep(1000);

// Catch bloc kto handle exceptions

catch (InterruptedException e) {

// Display the line number where exception

// occurred

// Using printStackTrace() method

e.printStackTrace();

// Method - Deposit method


// Accepting money whenever deposited

static synchronized void deposit(String name,

int deposit)

System.out.println(name + " deposited " + deposit);

total = total + deposit;

System.out.println("Balance after deposit: "

+ total);

// Making the thread sleep for 1 second

// after each deposit

// Try block to check for exceptions

try {

// Making thread to sleep for 1 second

Thread.sleep(1000);

// Catch block to handle InterruptedException

// exception

catch (InterruptedException e) {

e.printStackTrace();

// Method - Withdraw

// It is called from ThreadWithdrawal class using

// the object of Bank class passed from the main method

class ThreadWithdrawal extends Thread {

// Attributes of this class


Bank object;

String name;

int rupee;

// Constructor of this class

ThreadWithdrawal(Bank ob, String name, int money)

// This keyword refers to parent class

this.object = ob;

this.name = name;

this.rupee = money;

// run() method for the thread

public void run() { object.withdrawn(name, rupee); }

// Deposit method is called from ThreadDeposit class using

// the object of Bank class passed from the main method*/

// Class 2

// Helper class extending Thread class

class ThreadDeposit extends Thread {

Bank object;

String name;

int rupee;

ThreadDeposit(Bank ob, String name, int money)

this.object = ob;

this.name = name;

this.rupee = money;

}
public void run() { object.deposit(name, rupee); }

// Class 3

// Main class

class GFG {

// Main driver method

public static void main(String[] args)

// Declaring an object of Bank class and passing the

// object along with other parameters to the

// ThreadWithdrawal and ThreadDeposit class. This

// will be required to call withdrawn and deposit

// methods from those class

// Creating object of above class inside main()

Bank obj = new Bank();

// Creating threads

ThreadWithdrawal t1

= new ThreadWithdrawal(obj, "Shivam", 20);

ThreadWithdrawal t2

= new ThreadWithdrawal(obj, "Puneet", 40);

ThreadDeposit t3

= new ThreadDeposit(obj, "Akash", 35);

ThreadWithdrawal t4

= new ThreadWithdrawal(obj, "soniya", 80);

ThreadWithdrawal t5

= new ThreadWithdrawal(obj, "Shubham", 40);

// When a program calls the start() method, a new

// thread is created and then the run() method is


// executed

t1.start();

t2.start();

t3.start();

t4.start();

t5.start();

output

Q3. WAP to design a class using abstract methods and classes.


abstract class MotorBike {

abstract void brake();

class SportsBike extends MotorBike {

// implementation of abstract method

public void brake() {

System.out.println("SportsBike Brake");

class MountainBike extends MotorBike {


// implementation of abstract method

public void brake() {

System.out.println("MountainBike Brake");

class Main {

public static void main(String[] args) {

MountainBike m1 = new MountainBike();

m1.brake();

SportsBike s1 = new SportsBike();

s1.brake();

output

Q4. WAP to handle the exception using try and multiple catch block.
import java.util.Scanner;

public class exce

public static void main(String args[])

Scanner scn = new Scanner(System.in);

try

int n = Integer.parseInt(scn.nextLine());

if (98%n == 0)

System.out.println(n + " is a factor of 98");

}
catch (NumberFormatException | ArithmeticException ex)

System.out.println("Exception encountered " + ex);

output

Q.5 WAP that implement the Nested try statements


class Nesting {

// main method

public static void main(String args[])

// main try-block

try {

// try-block2

try {

// try-block3

try {

int arr[] = { 1, 2, 3, 4 };

System.out.println(arr[10]);

// handles ArithmeticException if any

catch (ArithmeticException e) {

System.out.println("Arithmetic exception");

System.out.println(" try-block1");

}
// handles ArithmeticException if any

catch (ArithmeticException e) {

System.out.println("Arithmetic exception");

System.out.println(" try-block2");

// handles ArrayIndexOutOfBoundsException if any

catch (ArrayIndexOutOfBoundsException e4) {

System.out.print("ArrayIndexOutOfBoundsException");

System.out.println(" main try-block");

catch (Exception e5) {

System.out.print("Exception");

System.out.println(" handled in main try-block");

output

Q.6 WAP to show Even and Odd


import java.util.*;

// Main class

// TestEvenOddByCheckingLSB

public class GFG {

// Method 1

// To test number is even or odd

public static String testOddEvenByCheckingLSB(int a)

{
if (a != 0) {

if (Integer.toBinaryString(a).endsWith("0")) {

return "Even";

else {

return "Odd";

// Here we will land if

// it does not ends with 0

else {

return "Zero";

// Method 2

// Main driver method

public static void main(String[] args)

// Iterationg over using for loop

for (int i = 0; i <= 10; i++) {

// Calling the function and printing

// corresponding number is even or odd

System.out.println(

i + " : " + testOddEvenByCheckingLSB(i));

Output
Q.7 WAP to show string reverse
import java.lang.*;

import java.io.*;

import java.util.*;

// Class of ReverseString

class ReverseString {

public static void main(String[] args)

String input = "Puneet";

// getBytes() method to convert string

// into bytes[].

byte[] strAsByteArray = input.getBytes();

byte[] result = new byte[strAsByteArray.length];

// Store result in reverse order into the

// result byte[]

for (int i = 0; i < strAsByteArray.length; i++)

result[i] = strAsByteArray[strAsByteArray.length - i - 1];

System.out.println(new String(result));

}
Output

Q.8 WAP to show matrix


import java.io.*;

import java.util.*;

class matrix {

public static void print2D(int mat[][])

// Loop through all rows

for (int[] row : mat)

// converting each row as string

// and then printing in a separate line

System.out.println(Arrays.toString(row));

public static void main(String args[])

throws IOException

int mat[][] = { { 6, 7, 9, 4 },

{ 5, 6, 5, 8 },

{ 9, 15, 11, 19 } };

output
Q.9 WAP for factorial of a number
class Factorial {
int factorial(int n)

// single line to find factorial

return (n == 1 || n == 0) ? 1 : n * factorial(n - 1);

// Driver Code

public static void main(String args[])

Factorial obj = new Factorial();

int num = 6;

System.out.println("Factorial of " + num +

" is " + obj.factorial(num));

Output

Q.10 WAP to show matrix


class A {

public void print_A() { System.out.println("Class A"); }

class B extends A {

public void print_B() { System.out.println("Class B"); }

class C extends A {

public void print_C() { System.out.println("Class C"); }

class D extends A {
public void print_D() { System.out.println("Class D"); }

// Driver Class

public class Test {

public static void main(String[] args)

B obj_B = new B();

obj_B.print_A();

obj_B.print_B();

C obj_C = new C();

obj_C.print_A();

obj_C.print_C();

D obj_D = new D();

obj_D.print_A();

obj_D.print_D();

Output

You might also like