Java File Kunal
Java File Kunal
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:
Code:
class Shapes{
double len, bre, result;
Shapes(){
len=0.0;
bre=0.0;
}
Shapes(double l){
len = 1;
bre = 0.0;
}
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();
Output:
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:
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();
}
}
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;
}
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);
}
ii)
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();
}
}
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();
}
}
Code:
class Stack {
int stck[];
int tos;
Stack(int size) {
stck = new int[size];
tos = -1;
}
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());
}
}
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:
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:
Output:
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:
Code:
interface IntStack {
void push(int item);
int pop();
}
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());
}
}
Code:
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");
}
}
}
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();
}
}