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

Sourav Java

The document contains 13 experiments on Java programming concepts like quadratic equations, string checking, Fibonacci series, inheritance, abstraction, encapsulation, polymorphism, interfaces and method overloading. Each experiment contains the code for a program to demonstrate the concept along with expected output.

Uploaded by

Vikas Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Sourav Java

The document contains 13 experiments on Java programming concepts like quadratic equations, string checking, Fibonacci series, inheritance, abstraction, encapsulation, polymorphism, interfaces and method overloading. Each experiment contains the code for a program to demonstrate the concept along with expected output.

Uploaded by

Vikas Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Sourav Singh Yadav 41222166

Java Practicals

Submitted to: Submitted by:

Ms. Shivani Anand Sourav Singh Yadav

(4122216)

This Photo by Unknown Author is

Page 1 of 17
Sourav Singh Yadav 41222166

INDEX
S. EXPERIMENT NAME Teachers
NO. sign.

1 Write a program quadratic equation


2 To check whether is a string is alphabet or not by if else.

3 To check for palindrome string


4 To find Fibonacci series
5 Simple calculator using switch case
6 Write a program for Encapsulation
7 Write a program for Inheritance

8 Write a program for Method overriding

9 Write a program for This keyword


10 Write a program for Super keyword
11 Write a program for Abstract keyword
12 Write a program for Interface
13 Write a program for Method overloading
14 Write a program for Constructor overloading

Page 2 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 1
Aim :- Write a program quadratic equation
Code :-
import java.util.*;
public class Practical1{
public static void Quadratic(int a,int b,int c){
int d = b*b-4*a*c;
if(d>=0){
double m = (-b+Math.sqrt(d))/(2.0*a);
if(d>0){
double n = (-b-Math.sqrt(d))/(2.0*a);
System.out.print("the zeros of euatuon are "+m+" and "+n);
}else{
System.out.print("the zeros of euatuon are "+m+" and "+m);
}
}else{
System.out.println("Inconsistent eqaution!");
}
}
public static void main(String args[]){
Quadratic(1,-4,4);
}
}

Output :-

Page 3 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 2
Aim :- To check whether a string is alphabetic or not using if else.

Code :-
import java.util.*;
public class Practical2{
public static boolean Check(String s){
boolean flag = false;
for(int i = 0;i<s.length();i++){
if(!Character.isLetter(s.charAt(i))){
return false;
}
}
return true;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string : ");
String s = sc.next();
// System.out.print(s);
System.out.print(Check(s));
}

Page 4 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 3
Aim :- To check for palindrome string.
Code :-
import java.util.*;
public class Practical3{
public static boolean Check(String s){
boolean flag = false;
for(int i = 0;i<s.length()/2;i++){
if(s.charAt(i) != s.charAt(s.length()-i-1)){
return false;
}
}
return true;
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a string : ");
String s = sc.next();
// System.out.print(s);
System.out.print(Check(s));
}

} Output :-

Page 5 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 4
Aim :- To find Fibonacci series.
Code :-
import java.util.*;
public class Practical4{
public static void Fib(int s){
int a = 0;
int b = 1;
int next = a+b;
System.out.print(a+" "+b+" ");
for(int i = 0;i<s;i++){
System.out.print(next+" ");
a = b;
b = next;
next = a+b;
}
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
int s = sc.nextInt();
// System.out.print(s);
Fib(s);
}

}} Output :-
Page 6 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 5
Aim :- Simple calculator using switch case
Code :-
import java.util.Scanner;
public class Practical5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char operator = scanner.next().charAt(0);
double result;
switch (operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':

Page 7 of 17
Sourav Singh Yadav 41222166

if (num2 != 0) {
result = num1 / num2;
} else {
System.out.println("Error: Division by zero!");
return;
}
break;
default:
System.out.println("Error: Invalid operator!");
return;
}
System.out.println("Result: " + result);
scanner.close();
}
}

Output :-

Page 8 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 6
Aim :- Write a program for Encapsulation.
Code :-
public class Practical6 {
private int data;

public int getData() {


return data;
}

public void setData(int data) {


this.data = data;
}

public static void main(String[] args) {


Practical6 obj = new Practical6();
obj.setData(10);
System.out.println("Data: " + obj.getData());
}
}

Output :-

Page 9 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 7
Aim :-Write a program for Inheritance.
Code :-
class Animal {
void eat() {
System.out.println("Animal is eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking...");
}
}
public class Practical7 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal class
dog.bark();
}
}

Output :-

Page 10 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 8
Aim :-Write a program for Method overriding.
Code :-
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class Practical8 {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Dog barks
}
}

Output :-

Page 11 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 9
Aim :- Write a program for This keyword.

Code :-
public class Practical9 {
private int number;
public Practical9(int number) {
this.number = number;
}
public void printNumber() {
System.out.println("Number: " + this.number);
}
public static void main(String[] args) {
Practical9 obj = new Practical9(42);
obj.printNumber(); // Output: Number: 42
}
}

Output :-

Page 12 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 10
Aim :- Write a program for Super keyword
Code :-
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Calling superclass constructor
System.out.println("Dog constructor");
}
}
public class Practical10 {
public static void main(String[] args) {
Dog dog = new Dog();
}
}

Output :-

Page 13 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 11
Aim :- Write a program for Abstract keyword.
Code :-
abstract class Shape {
abstract void draw();
}

class Rectangle extends Shape {


void draw() {
System.out.println("Drawing Rectangle");
}
}

public class Practical11 {


public static void main(String[] args) {
Shape shape = new Rectangle();
shape.draw(); // Output: Drawing Rectangle
}
}

Output :-

Page 14 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 12
Aim :- Write a program for Interface.
Code :-
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
System.out.println("Dog barks");
}
}

public class Practical12 {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Dog barks
}
}

Output :-

Page 15 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 13
Aim :- Write a program for Method overloading.
Code :-
interface Animal {
void sound();
}

class Dog implements Animal {


public void sound() {
System.out.println("Dog barks");
}
}

public class Practical13 {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Output: Dog barks
}
}

Output :-

Page 16 of 17
Sourav Singh Yadav 41222166

EXPERIMENT – 14
Aim :- Write a program for Constructor overloading.
Code :-
public class Practical14 {
Practical14() {
System.out.println("Default constructor");
}

Practical14(int x) {
System.out.println("Parameterized constructor with value: " + x);
}

public static void main(String[] args) {


Practical14 obj1 = new Practical14(); // Output: Default constructor
Practical14
obj2 = new Practical14(10); // Output: Parameterized constructor with value: 10
}
}

Output :-

Page 17 of 17

You might also like