0% found this document useful (0 votes)
63 views23 pages

Java File Kunal

The document describes 10 programming experiments in Java. Experiment 1 designs a program to find the largest of two integers. Experiment 2 defines a class with overloaded constructors and instantiates objects. Experiment 3 demonstrates a nested class. Experiment 4 prints solutions to a quadratic equation. Experiment 5 calculates the Fibonacci sequence using iterative and recursive methods. Experiment 6 finds prime numbers up to a given integer. Experiment 7 reads integers and calculates their sum. Experiment 8 implements a stack. Experiment 9 checks if a string is a palindrome. Experiment 10 creates an abstract class Shape with subclasses for shapes and their number of sides.

Uploaded by

Divyansh Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
63 views23 pages

Java File Kunal

The document describes 10 programming experiments in Java. Experiment 1 designs a program to find the largest of two integers. Experiment 2 defines a class with overloaded constructors and instantiates objects. Experiment 3 demonstrates a nested class. Experiment 4 prints solutions to a quadratic equation. Experiment 5 calculates the Fibonacci sequence using iterative and recursive methods. Experiment 6 finds prime numbers up to a given integer. Experiment 7 reads integers and calculates their sum. Experiment 8 implements a stack. Experiment 9 checks if a string is a palindrome. Experiment 10 creates an abstract class Shape with subclasses for shapes and their number of sides.

Uploaded by

Divyansh Verma
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 23

Experiment-1

Aim: Design a java program to find the biggest of two given integers.

Code:

class LargestOfTwo
{
int num1, num2;
LargestOfTwo(int a, int b)
{
num1=a;
num2=b;
}
void Largest()
{
if (num1>num2)
{
System.out.println("Largest is " + num1);
}
else if (num2>num1)
{
System.out.println("Largest is " + num2);
}
else
{
System.out.println("Equal numbers");
}
}
public static void main(String[] args)
{
LargestOfTwo LO = new LargestOfTwo(11,22);
LO.Largest();
}
}

Output:

Kunal Mishra 05213203120 Page 1


Experiment-2

Aim: Design a java program to define a class, describe its constructor,


overload the constructor and instantiate the object.

Code:

class Shapes{
double len, bre, result;

Shapes(){
len=0.0;
bre=0.0;
}

Shapes(double l){
len = 1;
bre = 0.0;
}

Shapes(double l, double b){


len = l;
bre = b;
}

void area(){
if(len!=0.0 && bre == 0.0){
result = len*len;
System.out.println("Area of Square = "+result);
}
if(len!=0.0 && bre !+ 0.0){
result = len*bre;
System.out.println("Area of Rectangle="+result);
}else if(len == 0.0 && bre == 0.0){
System.out.println("No Output");
}
}
}

class ShapeClass{
public static void main(String args[]){
double para1, para2;
para1 = 10.0;
para2 = 20.0;
Shapes s1 = new Shapes();
Shapes s2 = new Shapes(para1);
Shapes s3 = new Shapes(para1, para2);
s2.area();
s1.area();

Kunal Mishra 05213203120 Page 2


s3.area();
}
}

Output:

Kunal Mishra 05213203120 Page 3


Experiment-3

Aim: Design a java program to demonstrate the use of nested class.

Code:

class Outer{
private int outer_x = 100;

void test(){
Inner inner = new Inner();
inner.display();
inner.hello();
}

class Inner{
private void hello(){
System.out.println("Private Method Hello() of Inner Class");
}
void display(){
System.out.println("Display: outer_x="+outer_x);
Outer o = new Outer();
System.out.println("Display: outer_x="+o.outer_x);
}
}
}

class NestClass{
public static void main(String args[]){
Outer outer = new Outer();
outer.test();
}
}

Output:

Kunal Mishra 05213203120 Page 4


Experiment-4

Aim: Design a java program to print all the real solutions of the quadratic
equation ax^2+bx+c=0, read in the values of a, b, c and use the quadratic
formula. If the discriminant b^2 -4ac is negative, display a message stating
that there are no real solutions.
Code:
import java.util.*;
class Quadratic {
double a, b, c, r1, r2;
double D;

void inputEquation() {
System.out.println("Enter the values of a,b and c for quadratic equation ax^2+bx+c=0");
Scanner sc = new Scanner(System.in);
a = sc.nextDouble();
b = sc.nextDouble();
c = sc.nextDouble();
}

void findRoots() {
D = b * b - 4 * a * c;
if (D < 0) System.out.println("There are no real solutions");
else {
r1 = (-b + Math.sqrt(D)) / (2 * a);
r2 = (-b - Math.sqrt(D)) / (2 * a);
}
}

void displayRoots() {
System.out.println("Root 1: " + r1);
System.out.println("Root 2: " + r2);
}
}

class MyELClass {
public static void main(String args[]) {
Quadratic obj = new Quadratic();
obj.inputEquation();
obj.findRoots();
obj.displayRoots();
}
}

Kunal Mishra 05213203120 Page 5


Output:

Kunal Mishra 05213203120 Page 6


Experiment-5

Aim: i.) Design a java program that uses Iterative method to print the nth
value of the Fibonacci series sequence.
ii.)Design a java program that uses Recursive method to print the nth value
of the Fibonacci series sequence

Code:i)

import java.util.*;
class FiboIter {
static int fib(int n) {
int num1 = 0, num2 = 1, ans = 0;
if (n == 0) return num1;
for (int i = 2; i <= n; i++) {
ans = num1 + num2;
num1 = num2;
num2 = ans;
}
return ans;
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = sc.nextInt();
System.out.println(fib(n));
}
}

ii)

import java.util.*;

class FiboRecur {
static int fib(int n) {
if (n == 0 || n == 1) return n;
return fib(n - 1) + fib(n - 2);
}

public static void main(String args[]) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter the value of n: ");
int n = sc.nextInt();
System.out.println(fib(n));
}
}

Kunal Mishra 05213203120 Page 7


Output:
i)

ii)

Kunal Mishra 05213203120 Page 8


Experiment-6

Aim: Design a java program to prompt user for an integer and print all the
prime numbers up to that integer.

Code:

import java.util.*;

class Prime {
int value, num;
int i, flag;

void inputNumber() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a Number: ");
value = sc.nextInt();
}

void displayPrime() {
for (num = 2; num <= value; num++) {
flag = 0;
for (i = 2; i <= num - 1; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0) {
System.out.println("\t-> " + num);
}
}
}
}

class PrimeMain {
public static void main(String a[]) {
Prime obj = new Prime();
obj.inputNumber();
obj.displayPrime();
}
}

Kunal Mishra 05213203120 Page 9


Output:

Kunal Mishra 05213203120 P a g e 10


Experiment-7

Aim: Design a java program that reads a line of integers, then display each
integer and the sum of all integers.

Code:

import java.util.*;

class IntData {
int arr[];
int i, sum;
IntData(int size) {
arr = new int[size];
sum = 0;
}

void input() {
System.out.println("Enter Array Elements: ");
Scanner sc = new Scanner(System.in);
for (i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
}

void display() {
System.out.println("Entered Array Elements are: ");
for (i = 0; i < arr.length; i++) {
System.out.println("-> " + arr[i]);
}
}
void sumCal() {
for (i = 0; i < arr.length; i++) {
sum = sum + arr[i];
}
System.out.println("The Sum is: " + sum);
}
}
class SumOfInt {
public static void main(String a[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Array Size: ");
int n = sc.nextInt();
IntData obj = new IntData(n);
obj.input();
obj.display();
obj.sumCal();
}
}

Kunal Mishra 05213203120 P a g e 11


Output:

Kunal Mishra 05213203120 P a g e 12


Experiment-8

Aim: Design a java program to implement stack concept.

Code:

class Stack {
int stck[];
int tos;
Stack(int size) {
stck = new int[size];
tos = -1;
}

void push(int item) {


if (tos == stck.length - 1) System.out.println("Stack is full");
else stck[++tos] = item;
}

int pop() {
if (tos < 0) {
System.out.println("Stack underflow");
return 0;
} else {
return stck[tos--];
}
}
}

class StackClass {
public static void main(String args[]) {
Stack mystack1 = new Stack(5);
Stack mystack2 = new Stack(8);
for (int i = 0; i < 5; i++) mystack1.push(i);
for (int i = 0; i < 8; i++) mystack2.push(i);
System.out.println("Values in Stack1:");
for (int i = 0; i < 5; i++) System.out.println(mystack1.pop());
System.out.println("Values in Stack2:");
for (int i = 0; i < 8; i++) System.out.println(mystack2.pop());
}
}

Kunal Mishra 05213203120 P a g e 13


Output:

Kunal Mishra 05213203120 P a g e 14


Experiment-9

Aim: Design a java program to check whether a string is palindrome or


not.

Code:

import java.util.*;
class P_Check {
void check(String data) {
String rdata = "";
int i, len;
len = data.length();
for (i = len - 1; i >= 0; i--) rdata = rdata + data.charAt(i);
if (data.equals(rdata)) System.out.println("The Following string is a palindrome.");
else System.out.println("The Following string is not a palindrome.");
}
}

class Palindrome {
public static void main(String a[]) {
String name;
P_Check pc = new P_Check();
Scanner in = new Scanner(System.in);
System.out.println("Enter a string: ");
name = in .nextLine();
pc.check(name);
}
}

Output:

Kunal Mishra 05213203120 P a g e 15


Experiment-10

Aim: Design a java program to create an abstract class named Shape, that
contains an empty method called numberOfSides(). Provide four classes
named Trapezoid, Triangle, Rectangle and Hexagon such that each one of
the classes contains only the method numberOfSides(), that contains
number of sides in each geometrical figure.

Code:

abstract class MyArea {


abstract void area();
}
class SQArea extends MyArea {
double s;
void area() {
double areasq;
s = 10.0;
areasq = s * s;
System.out.println("Area of Square: " + areasq);
}
}
class RectArea extends MyArea {
double l, b;
void area() {
double arearect;
l = 109.0;
b = 2.0;
arearect = l * b;
System.out.println("Area of Rectangle: " + arearect);
}
}
class ShapeAbstractClass {
public static void main(String a[]) {
MyArea sa = new SQArea();
sa.area();
MyArea ra = new RectArea();
ra.area();
// MyArea ma = new MyArea();
// ma.area(); MyArea sa = new SQArea(); sa.area(); MyArea ra = new RectArea();
ra.area();
}
}

Output:

Kunal Mishra 05213203120 P a g e 16


Experiment-11

Aim: Design a java program to show dynamic polymorphism.

Code:

class MyArea {
void area() {
System.out.println("A concrete function called Area()");
}
}
class SQArea extends MyArea {
double s;
void area() {
double areasq;
s = 10.0;
areasq = s * s;
System.out.println("Area of Square " + areasq);
}
}
class RECTArea extends MyArea {
double l, b;
void area() {
double arearect;
l = 10.0;
b = 2.0;
arearect = l * b;
System.out.println("Area of Rectangle " + arearect);
}
}
class ClassForArea {
public static void main(String a[]) {
MyArea ma;
ma = new SQArea();
ma.area();
ma = new RECTArea();
ma.area();
}
}

Output:

Kunal Mishra 05213203120 P a g e 17


Experiment-12

Aim: Design a java program to implement interfaces.

Code:

interface IntStack {
void push(int item);
int pop();
}

class DynStack implements IntStack {


private int stck[];
private int tos;
DynStack(int size) {
stck = new int[size];
tos = -1;
}

public void push(int item) {


if (tos == stck.length - 1) {
int temp[] = new int[stck.length * 2];
for (int i = 0; i < stck.length; i++) temp[i] = stck[i];
stck = temp;
stck[++tos] = item;
} else stck[++tos] = item;
}

public int pop() {


if (tos < 0) {
System.out.println("Stack underflow.");
return 0;
} else return stck[tos--];
}
}

class Interface {
public static void main(String args[]) {
DynStack mystack1 = new DynStack(5);
DynStack mystack2 = new DynStack(8);
for (int i = 0; i < 7; i++) mystack1.push(i);
for (int i = 0; i < 10; i++) mystack2.push(i);
System.out.println("Stack in mystack1:");
for (int i = 0; i < 7; i++) System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for (int i = 0; i < 10; i++) System.out.println(mystack2.pop());
}
}

Kunal Mishra 05213203120 P a g e 18


Output:

Kunal Mishra 05213203120 P a g e 19


Experiment-13

Aim: Design a java program to create a customized exception and also


make use of all 5 exception keywords.

Code:

class NSalException extends Exception {


public NSalException(String s) {
super(s);
}
}
class PSalException extends RuntimeException {
public PSalException(String s) {
super(s);
}
}

class Employee {
public void decideSal(String s1) throws NSalException, PSalException,
NumberFormatException {
int sal = Integer.parseInt(s1);
if (sal <= 0) {
NSalException no = new NSalException("Invalid Salary");
throw (no);
} else {
PSalException po = new PSalException("Valid Salary");
throw (po);
}
}
}

class java13 {
public static void main(String args[]) {
try {
Employee e = new Employee();
e.decideSal(args[0]);
} catch (NumberFormatException nfe) {
System.err.println("Please enter integer values.");
} catch (NSalException no) {
System.err.println("Negative Salary.");
} catch (PSalException po) {
System.err.println("Valid Positive Salary.");
} finally {
System.out.println("Finally Block executing");
}
}
}

Kunal Mishra 05213203120 P a g e 20


Output:

Kunal Mishra 05213203120 P a g e 21


Experiment-14

Aim: Design a java program to sort list of names in ascending order.

Code:

import java.util.*;
class SortNames {
String name[] = new String[5];
int i, n = 5;
SortNames() {
for (i = 0; i < n; i++) {
name[i] = null;
}
}
void inputNames() {
name[0] = "Manish";
name[1] = "Vivek";
name[2] = "Tushhaar";
name[3] = "Sushant";
name[4] = "Rajeev";
}
void bubbleSort() {
int j;
String temp;
for (i = 0; i < n; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (name[j].compareTo(name[j + 1]) > 0) {
temp = name[j];
name[j] = name[j + 1];
name[j + 1] = temp;
}
}
}
}
void displayNames() {
for (i = 0; i < n; i++) {
System.out.println(name[i]);
}
}
}
class java14 {
public static void main(String a[]) {
SortNames sn = new SortNames();
sn.inputNames();
sn.bubbleSort();
sn.displayNames();
}
}

Kunal Mishra 05213203120 P a g e 22


Output:

Kunal Mishra 05213203120 P a g e 23

You might also like