[Go to site: main page, start]

0% found this document useful (0 votes)
5 views19 pages

Java Practical File

The document contains a series of Java programming exercises, each with a specific aim and corresponding code implementation. Topics covered include quadratic equations, string checks, palindromes, Fibonacci series, a simple calculator, encapsulation, inheritance, method overriding, the 'this' and 'super' keywords, abstract classes, interfaces, method overloading, and constructor overloading. Each practical example demonstrates fundamental programming concepts and techniques in Java.

Uploaded by

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

Java Practical File

The document contains a series of Java programming exercises, each with a specific aim and corresponding code implementation. Topics covered include quadratic equations, string checks, palindromes, Fibonacci series, a simple calculator, encapsulation, inheritance, method overriding, the 'this' and 'super' keywords, abstract classes, interfaces, method overloading, and constructor overloading. Each practical example demonstrates fundamental programming concepts and techniques in Java.

Uploaded by

prince2094qs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Practical 01

Aim:- Write a Program for Quadratic Equation.

Code:-

import [Link];

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

int a,b,c;

[Link]("Enter the first coefficent: ");

a = [Link]();

[Link]("Enter the second coefficent: ");

b = [Link]();

[Link]("Enter the third coefficient: ");

c = [Link]();

int d = b*b - 4*a*c;

if (d > 0){

[Link]("Two real roots");

int root1 = (-b + (int)[Link](d))/(2*a);

int root2 = (-b - (int)[Link](d))/(2*a);

[Link]("The roots are: " + root1 + " and " + root2);

else if (d == 0){

[Link]("The roots are real and equal.");

int root = -b/(2*a);

[Link]("Root: " + root);

else{

[Link]("No real roots.");

[Link]();

}
Output:-
Practical 02

Aim:- To check whether is a string is alphabet or not by if else.

Code:-

import [Link];

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a string:");

String inputString = [Link]();

boolean isAlphabet = true;

for (int i = 0; i < [Link](); i++) {

if (![Link]([Link](i))) {

isAlphabet = false;

break;

if (isAlphabet) {

[Link]("The string contains only alphabets.");

} else {

[Link]("The string contains non-alphabetic characters.");

[Link]();

Output:-
Practical 03

Aim:- To check palindrome for a string.

Code:-

import [Link];

public class Main {

static boolean isPalindrome(String str) {

int start = 0;

int end = [Link]() - 1;

while (start < end) {

if ([Link](start) != [Link](end)) {

return false;

start++;

end--;

return true;

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter a string:");

String str = [Link]();

if (isPalindrome(str)) {

[Link]("The string is a palindrome.");

} else {

[Link]("The string is not a palindrome.");

[Link]();

}
Output:-
Practical 04

Aim:- To find Fibonacci series.

Code:-

import [Link];

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Enter the Number: ");

int num = [Link]();

int firstTerm = 0,secondTerm = 1;

for (int i = 1; i <= num; i++) {

[Link](firstTerm + " ");

int nextTerm = firstTerm + secondTerm;

firstTerm = secondTerm;

secondTerm = nextTerm;

[Link]();

Output:-
Practical 05

Aim:- Simple calculator using switch case.

Code:-

import [Link];

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner([Link]);

[Link]("Simple Calculator");

[Link]("Available Operations:");

[Link]("1. Addition (+)");

[Link]("2. Subtraction (-)");

[Link]("3. Multiplication (*)");

[Link]("4. Division (/)");

[Link]("Enter the operation: ");

String operation = [Link]();

[Link]("Enter the first number: ");

double num1 = [Link]();

[Link]("Enter the second number: ");

double num2 = [Link]();

double result;

switch (operation) {

case "+":

result = num1 + num2;

[Link]("Result: " + result);

break;

case "-":

result = num1 - num2;

[Link]("Result: " + result);

break;
case "*":

result = num1 * num2;

[Link]("Result: " + result);

break;

case "/":

if (num2 != 0) {

result = num1 / num2;

[Link]("Result: " + result);

} else {

[Link]("Error: Division by zero!");

break;

default:

[Link]("Invalid operation!");

[Link]();

Output:-
Practical 06

Aim:- Implement the Encapsulation in Java

public class Main {

public static void main(String[] args) {

Student student = new Student();

[Link]("Prince");

[Link](20);

[Link]("Name: " + [Link]());

[Link]("Age: " + [Link]());

class Student {

private String name;

private int age;

public String getName() {

return name;

public void setName(String name) {

[Link] = name;

public int getAge() {

return age;

public void setAge(int age) {

[Link] = age;

Output:-
Practical 07

Aim:- Implement the Inheritance in Java.

Code:-

class Shape{

public int sides;

public Shape(int sides){

[Link]= sides;

public void getShape(){

[Link]([Link]);

class Triangle extends Shape{

public int perimeter;

public Triangle (int sides, int perimeter){

super(sides);

[Link]= perimeter;

public class prac6 {

public static void main(String args[]){

Triangle t1= new Triangle(5, 20);

[Link]();

Output:-
Practical 08

Aim:- Implement Method overriding in Java.

Code:-

class Parent {

void show() { [Link]("Parent's show()"); }

// Inherited class

class Child extends Parent {

// This method overrides show() of Parent

@Override void show()

[Link]("Child's show()");

// Driver class

public class prac8 {

public static void main(String[] args)

// If a Parent type reference refers

// to a Parent object, then Parent's

// show is called

Parent obj1 = new Parent();

[Link]();

// If a Parent type reference refers

// to a Child object Child's show()

// is called. This is called RUN TIME

// POLYMORPHISM.

Parent obj2 = new Child();

[Link]();
}

Output:-
Practical 09

Aim:- Implement This keyword in Java

Code:-

class Test {

int a;

int b;

Test(int a, int b)

this.a = a;

this.b = b;

void display()

[Link]("a = " + a + " b = " + b);

public class Main {

public static void main(String[] args)

Test object = new Test(10, 20);

[Link]();

Output:-
Practical 10

Aim:- Implement Super keyword in Java

Code:-

class Vehicle {

int maxSpeed = 120;

class Car extends Vehicle {

int maxSpeed = 180;

void display()

[Link]("Maximum Speed: "

+ [Link]);

public class Main {

public static void main(String[] args)

Car small = new Car();

[Link]();

Output:-
Practical 11

Aim:- Implement Abstract keyword in Java.

Code:-

abstract class Animal {

public abstract void animalSound();

public void sleep() {

[Link]("Zzz");

class Pig extends Animal {

public void animalSound() {

[Link]("The pig says: wee wee");

class Main {

public static void main(String[] args) {

Pig myPig = new Pig();

[Link]();

[Link]();

Output:-
Practical 12

Aim:- Implement Interface in Java

Code:-

interface Animal {

public void animalSound();

public void sleep();

class Pig implements Animal {

public void animalSound() {

[Link]("The pig says: wee wee");

public void sleep() {

[Link]("Zzz");

class Main {

public static void main(String[] args) {

Pig myPig = new Pig();

[Link]();

[Link]();

Output:-
Practical 13

Aim:- Implement Method overloading in Java

Code:-

public class Main {

static int plus(int x, int y) {

return x + y;

static double plus(double x, double y) {

return x + y;

public static void main(String[] args) {

int myNum1 = plus(10, 15);

double myNum2 = plus(7.9, 14.12);

[Link]("int: " + myNum1);

[Link]("double: " + myNum2);

Output:-
Practical 14

Aim:- Implement Constructor overloading in Java.

Code:-

class Box {

double width, height, depth;

Box(double w, double h, double d)

width = w;

height = h;

depth = d;

Box() { width = height = depth = 0; }

Box(double len) { width = height = depth = len; }

double volume() { return width * height * depth; }

public class Main {

public static void main(String args[])

Box mybox1 = new Box(10, 20, 15);

Box mybox2 = new Box();

Box mycube = new Box(7);

double vol;

vol = [Link]();

[Link]("Volume of mybox1 is " + vol);

vol = [Link]();

[Link]("Volume of mybox2 is " + vol);

vol = [Link]();

[Link]("Volume of mycube is " + vol);

}
}

Output:-

You might also like