PROGRAM:
package linearsearchexample;
import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter those " + n + " elements");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search)
{
System.out.println(search + " is present at location " + (c + 1) + ".");
}
}
if (c == n)
System.out.println(search + " isn't present in array.");
}
}
OUTPUT:
Enter number of elements
5
Enter those 5 elements
1
2
3
4
5
Enter value to find
3
3 is present at location 3.
RESULT:
Thus the Java program to implement linear search was executed and verified
successfully.
PROGRAM:
package main;
import java.util.Scanner;
class Main {
int binarySearch(int array[], int element, int low, int high) {
while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == element)
return mid;
if (array[mid] < element)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
public static void main(String args[])
{
Main obj = new Main();
int[] array = { 3, 4, 5, 6, 7, 8, 9 };
int n = array.length;
Scanner input = new Scanner(System.in);
System.out.println("Enter element to be searched:");
int element = input.nextInt();
input.close();
int result = obj.binarySearch(array, element, 0, n - 1);
if (result == -1)
System.out.println("Not found");
else
System.out.println("Element found at index " + result);
}
}
OUTPUT:
Enter element to be searched:
3
Element found at index 0
RESULT:
Thus the Java program to implement binary search was executed and verified
successfully.
PROGRAM:
package selection;
class Selection
{
void sort(int array[])
{
int n = array.length;
for (int i = 0; i < n-1; i++)
{
int min_element = i;
for (int j = i+1; j < n; j++)
if (array[j] < array[min_element])
min_element = j;
int temp = array[min_element];
array[min_element] = array[i];
array[i] = temp;
}
}
void printarrayay(int array[])
{
int n = array.length;
for (int i=0; i<n; ++i)
System.out.print(array[i]+" ");
System.out.println();
}
public static void main(String args[])
{
Selection ob = new Selection();
int array[] = {15, 10, 99, 53, 36};
ob.sort(array);
System.out.println("Sorted arrayay");
ob.printarrayay(array);
}
}
OUTPUT:
Sorted arrayay
10 15 36 53 99
RESULT:
Thus the Java program to implement selection sort was executed and verified
successfully.
PROGRAM:
package codescracker;
import java.util.Scanner;
public class CodesCracker
{
public static void main(String[] args)
{
int n, i, j, element;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the Size of Array: ");
n = scan.nextInt();
int[] arr = new int[n];
System.out.print("Enter " +n+ " Elements: ");
for(i=0; i<n; i++)
arr[i] = scan.nextInt();
for(i=1; i<n; i++)
{
element = arr[i];
for(j=(i-1); j>=0 && arr[j]>element; j--)
arr[j+1] = arr[j];
arr[j+1] = element;
}
System.out.println("\nThe new sorted array is: ");
for(i=0; i<n; i++)
System.out.print(arr[i]+ " ");
}
}
OUTPUT:
Enter the Size of Array: 4
Enter 4 Elements: 3
4
2
1
The new sorted array is:
1234
RESULT:
Thus the Java program to implement insertion sort was executed and verified
successfully.
PROGRAM:
package stackimp;
import java.util.*;
class Stack {
int top;
int maxsize = 5;
int[] stack_arry = new int[maxsize];
Stack()
{
top = -1;
}
boolean isEmpty(){
return (top < 0);
}
boolean push (int val)
{
if(top == maxsize-1)
{
System.out.println("Stack Overflow !!");
return false;
}
else {
top++;
stack_arry[top]=val;
return true;
}
}
boolean pop () {
if (top == -1) {
System.out.println("Stack Underflow !!");
return false;
}
else {
System.out.println("\nItem popped: " + stack_arry[top--]);
return true;
}
}
void display () {
System.out.println("Printing stack elements .....");
for(int i = top; i>=0;i--) {
System.out.print(stack_arry[i] + " ");
}
}
}
public class StackImp {
public static void main(String[] args) {
Stack stck = new Stack();
System.out.println("Initial Stack Empty : " + stck.isEmpty());
stck.push(10);
stck.push(20);
stck.push(30);
stck.push(40);
System.out.println("After Push Operation...");
stck.display();
stck.pop();
stck.pop();
System.out.println("After Pop Operation...");
stck.display();
}
}
OUTPUT:
Initial Stack Empty : true
After Push Operation...
Printing stack elements .....
40 30 20 10
Item popped: 40
Item popped: 30
After Pop Operation...
Printing stack elements .....
20 10
RESULT:
Thus the Java program to develop Stack data structure using classes and objects was
executed and verified successfully.
PROGRAM:
package queueimp;
class Queue
{
private static int front, rear, capacity;
private static int queue[];
Queue(int size) {
front = rear = 0;
capacity = size;
queue = new int[capacity];
}
static void queueEnqueue(int item) {
if (capacity == rear) {
System.out.printf("\nQueue is full\n");
return;
}
else {
queue[rear] = item;
rear++;
}
return;
}
static void queueDequeue() {
if (front == rear) {
System.out.printf("\nQueue is empty\n");
return;
}
else {
for (int i = 0; i < rear - 1; i++) {
queue[i] = queue[i + 1];
}
if (rear < capacity)
queue[rear] = 0;
rear--;
}
return;
}
static void queueDisplay()
{
int i;
if (front == rear) {
System.out.printf("Queue is Empty\n");
return;
}
for (i = front; i < rear; i++) {
System.out.printf(" %d , ", queue[i]);
}
return;
}
static void queueFront()
{
if (front == rear) {
System.out.printf("Queue is Empty\n");
return;
}
System.out.printf("\nFront Element of the queue: %d", queue[front]);
return;
}
}
public class QueueArrayImplementation {
public static void main(String[] args) {
Queue q = new Queue(4);
System.out.println("Initial Queue:");
q.queueDisplay();
q.queueEnqueue(10);
q.queueEnqueue(30);
q.queueEnqueue(50);
q.queueEnqueue(70);
System.out.println("Queue after Enqueue Operation:");
q.queueDisplay();
q.queueFront();
q.queueEnqueue(90);
q.queueDisplay();
q.queueDequeue();
q.queueDequeue();
System.out.printf("\nQueue after two dequeue operations:");
q.queueDisplay();
q.queueFront();
}
}
OUTPUT:
Initial Queue:
Queue is Empty
Queue after Enqueue Operation:
10 , 30 , 50 , 70 ,
Front Element of the queue: 10
Queue is full
10 , 30 , 50 , 70 ,
Queue after two dequeue operations: 50 , 70 ,
Front Element of the queue: 50
RESULT:
Thus the Java program to develop Queue data structure using classes and objects was
executed and verified successfully.
PROGRAM:
package employee;
import java.util.Scanner;
class Employee {
int id;
String name,address,mail,mno;
Scanner get=new Scanner(System.in);
Employee()
{
System.out.println("Enter name of the Employee");
name=get.nextLine();
System.out.println("Enter mail id of the employee:");
mail=get.nextLine();
System.out.println("Enter Address of the employee:");
address=get.nextLine();
System.out.println("Enter mobile no. of the employee:");
mno=get.nextLine();
System.out.println("Enter id:");
id=get.nextInt();
}
void display()
{
System.out.println("Employee name:"+name);
System.out.println("id:"+id);
System.out.println("mail id:"+mail);
System.out.println("Address:"+address);
System.out.println("mobile no:"+mno);
}
}
class programmer extends Employee
{
float grosssalary,netsalary;
float bpay;
programmer()
{
System.out.println("Enter basic pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
@Override
void display()
{
System.out.println("============="+"\n"+"programmer"+"\n"+"=========="+"\n");
super.display();
System.out.println("Gross salary:"+grosssalary);
System.out.println("Net salary:"+netsalary);
}
}
class AssistantProfessor extends Employee
{
float grosssalary,netsalary;
float bpay;
AssistantProfessor(){
System.out.println("Enter basic pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
@Override
void display()
{
System.out.println("====="+"\n"+"AssistantProfessor"+"\n"+"==="+"\n");
super.display();
System.out.println("Gross salary:"+grosssalary);
System.out.println("Net salary:"+netsalary);
}
}
class AssociateProfessor extends Employee
{
float grosssalary,netsalary;
float bpay;
AssociateProfessor()
{
System.out.println("Enter basic pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
@Override
void display()
{
System.out.println("======"+"\n"+"AssociateProfessor"+"\n"+"=========="+"\n");
super.display();
System.out.println("Gross salary:"+grosssalary);
System.out.println("Net salary:"+netsalary);
}
}
class Professor extends Employee
{
float grosssalary,netsalary;
float bpay;
Professor()
{
System.out.println("Enter basic pay:");
bpay=get.nextFloat();
grosssalary=(0.97f*bpay)+(0.10f*bpay)+(0.12f*bpay)+(0.001f*bpay)+bpay;
netsalary=(0.97f*bpay)+(0.10f*bpay)+bpay;
}
@Override
void display()
{
System.out.println("======="+"\n"+"Professor"+"\n"+"======"+"\n");
super.display();
System.out.println("Gross salary:"+grosssalary);
System.out.println("Net salary:"+netsalary);
}
}
public class Employees
{
public static void main(String[] args)
{
System.out.println("=="+"\n"+"Enter professor Details"+"\n"+"===="+"\n");
Professor ob1=new Professor();
ob1.display();
AssociateProfessor ob2=new AssociateProfessor();
System.out.println("===="+"\n"+"Enter AssociateProfessor Details"+"\n"+"====="+"\n");
ob2.display();
AssistantProfessor ob3=new AssistantProfessor();
System.out.println("======"+"\n"+"Enter Assistant Professor
Details"+"\n"+"======"+"\n");
ob3.display();
programmer ob4=new programmer();
System.out.println("===="+"\n"+"Enter programmer Details"+"\n"+"====="+"\n");
ob4.display();
}
}
OUTPUT:
==
Enter professor Details
====
Enter name of the Employee
Naveen
Enter mail id of the employee:
245@gmail.cpm
Enter Address of the employee:
3rd street Kannan nagar
Enter mobile no. of the employee:
9090909090
Enter id:
47
Enter basic pay:
1000
=======
Professor
======
Employee name:Naveen
id:47
mail id:245@gmail.cpm
Address:3rd street Kannan nagar
mobile no:9090909090
Gross salary:2191.0
Net salary:2070.0
Enter name of the Employee
RESULT:
Thus the Java application to implement employee payroll was executed successfully.
PROGRAM:
package pkgabstract;
abstract class shape
{
public int x,y;
public abstract void printArea();
}
class Rectangle extends shape
{
public void printArea()
{
System.out.println("Area of the rectangle is"+x*y);
}
}
class Triangle extends shape
{
public void printArea()
{
System.out.println("Area of triangle is"+(x*y)/2);
}
}
class circle extends shape
{
public void printArea()
{
System.out.println("Area of circle is"+(22*x*x)/7);
}
}
public class Abstract
{
public static void main(String args[])
{
Rectangle r=new Rectangle();
r.x=10;
r.y=20;
r.printArea();
System.out.println("------");
Triangle t=new Triangle();
t.x=30;
t.y=35;
t.printArea();
System.out.println("------");
circle c=new circle();
c.x=2;
c.printArea();
System.out.println("-----");
}
}
OUTPUT:
Area of the rectangle is200
------
Area of triangle is525
------
Area of circle is12
------
RESULT:
Thus the Java program to implement abstract class was executed successfully.
PROGRAM:
package shapedemo;
import java.io.*;
import java.util.*;
abstract class Shape
{
abstract void numberofsides();
}
class Trapezoid extends Shape
{
void numberofsides()
{
System.out.println("Trapezoid has four sides");
}
}
class Triangle extends Shape
{
void numberofsides()
{
System.out.println("Triangle has three sides");
}
}
class Hexagon extends Shape
{
void numberofsides()
{
System.out.println("Hexagon has Six sides");
}
}
class ShapeDemo
{
public static void main(String args[])
{
Trapezoid t=new Trapezoid();
Triangle r=new Triangle();
Hexagon h=new Hexagon();
Shape s;
s=t;
s.numberofsides();
s=r;
s.numberofsides();
s=h;
s.numberofsides();
}
}
OUTPUT:
Trapezoid has four sides
Triangle has three sides
Hexagon has Six sides
RESULT:
Thus the Java program to implement abstract class was executed successfully.
PROGRAM:
package pkginterface;
import java.io.*;
interface Area
{
final static float pi = 3.12F;
float compute(float x, float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*y);
}
}
class Circle implements Area
{
public float compute(float x,float y)
{
return(pi*x*x);
}
}
class Interface
{
public static void main(String args[])
{
Rectangle rect=new Rectangle();
Circle cir=new Circle();
System.out.println("Area of Rectangle="+rect.compute(10,20));
System.out.println("Area of circle="+cir.compute(10,0));
}
}
OUTPUT:
Area of Rectangle=200.0
Area of circle=312.0
RESULT:
Thus the Java program to implement interface was executed and verified successfully.
PROGRAM:
package javaexception;
import java.lang.Exception;
class MyException extends Exception
{
int a;
MyException(int b)
{
a=b;
}
public String toString()
{
return("Exception Number="+a);
}
}
class JavaException
{
public static void main(String args[])
{
try
{
throw new MyException(2);
}
catch(MyException e)
{
System.out.println(e);
}
}
OUTPUT:
Exception Number=2
RESULT:
Thus the Java program to implement exception handling and creation of user defined
exception has been executed successfully.
PROGRAM:
package mthread;
import java.util.*;
class even implements Runnable
{
public int x;
public even(int x)
{
this.x=x;
}
public void run()
{
System.out.println("ThreadName:EvenThread and"+x+"is even Number and square
of"+x+"is:"+x*x);
}
}
class odd implements Runnable
{
public int x;
public odd(int x)
{
this.x=x;
}
public void run()
{
System.out.println("Thread name:ODD Thread and"+x+"is odd number and cube
of"+x+"is:"+x*x*x);
}
}
class A extends Thread
{
public String tname;
public Random r;
public Thread t1,t2;
public A(String s)
{
tname=s;
}
public void run()
{
int num=0;
Random r=new Random();
try
{
for(int i=0;i<50;i++)
{
num=r.nextInt(100);
System.out.println("Main Thread and generated number is"+num);
if(num%2==0)
{
Thread t1=new Thread(new even(num));
t1.start();
}
else
{
Thread t2=new Thread(new odd(num));
t2.start();
}
Thread.sleep(10);
System.out.println("---------");
}
}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
}
public class Mthread
{
public static void main(String args[])
{
A a=new A("One");
a.start();
}
}
OUTPUT:
Main Thread and generated number is79
Thread name:ODD Thread and79is odd number and cube of79is:493039
---------
Main Thread and generated number is18
ThreadName:EvenThread and18is even Number and square of18is:324
---------
Main Thread and generated number is53
Thread name:ODD Thread and53is odd number and cube of53is:148877
---------
Main Thread and generated number is26
ThreadName:EvenThread and26is even Number and square of26is:676
---------
Main Thread and generated number is20
ThreadName:EvenThread and20is even Number and square of20is:400
---------
Main Thread and generated number is55
Thread name:ODD Thread and55is odd number and cube of55is:166375
---------
RESULT:
Thus the Java program that implements a multi-threaded application has been executed
successfully.
PROGRAM:
package FileCo;
import java.io.File;
public class FileCo{
public static void p(String s)
{
System.out.println(s);
}
public static void main(String args[])
{
File f1 = new
File("C:\\Users\\Admin\\Documents\\NetBeansProjects\\Interface\\src\\pkginterface\\Interface.ja
va");
p("File Name:"+f1.getName());
p("Path:"+f1.getPath());
p("AbsPath:"+f1.getAbsolutePath());
p("Parent:"+f1.getParent());
p(f1.exists()?"exists":"does not exist");
p(f1.canWrite()?"is Writable":"is not writable");
p(f1.canRead()?"is readable":"is not readble");
p("is"+(f1.isDirectory()?" ":"not"+"a directory"));
p(f1.isFile()?"is normal file":"might be a named pipe");
p(f1.isAbsolute()?"is absolute":"is not absolute");
p("File last modified:"+f1.lastModified());
p("File size:"+f1.length()+"Bytes");
}
}
OUTPUT:
File Name:Interface.java
Path:C:\Users\Admin\Documents\NetBeansProjects\Interface\src\pkginterface\Interface.java
Abs Path:C:\Users\Admin\Documents\NetBeansProjects\Interface\src\pkginterface\Interface.java
Parent:C:\Users\Admin\Documents\NetBeansProjects\Interface\src\pkginterface
exists
is Writable
is readable
isnota directory
is normal file
is absolute
File last modified:1669364601846
File size:855Bytes
RESULT:
Thus the Java program to perform the file operations has been executed successfully.
PROGRAM:
package mainclass;
public class MainClass
{
public static <T extends Comparable<T>> T maximum(T x, T y, T z) {
T max = x;
if (y.compareTo(max) > 0)
max = y;
if (z.compareTo(max) > 0)
max = z;
return max;
}
public static void main(String args[])
{
System.out.printf("Maximum of %d, %d and %d is %d\n\n", 3, 4, 5, maximum(3, 4, 5));
System.out.printf("Maximum of %.1f, %.1f and %.1f is %.1f\n\n", 6.6, 8.8, 7.7,
maximum(6.6,8.8, 7.7));
System.out.printf("Maximum of %s, %s and %s is %s\n", "pear", "apple", "orange",
maximum(
"pear", "apple", "orange"));
}
}
OUTPUT:
Maximum of 3, 4 and 5 is 5
Maximum of 6.6, 8.8 and 7.7 is 8.8
Maximum of pear, apple and orange is orange
RESULT:
Thus the Java application to demonstrate the features of generic classes was implemented
successfully.
PROGRAM:
package TLights;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import static java.awt.Font.BOLD;
abstract class A extends JFrame implements ItemListener{
public JLabel l1,l2;
public JRadioButton r1,r2,r3;
public ButtonGroup bg;
public JPanel p,p1;
public A(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,1));
setSize(800,400);
p=new JPanel(new FlowLayout());
p1=new JPanel(new FlowLayout());
l1=new JLabel();
Font f=new Font("Verdana",Font.BOLD,60);
l1.setFont(f);
add(l1);
p.add(l1);
add(p);
l2=new JLabel("Select Lights");
p1.add(l2);
JRadioButton r1=new JRadioButton("RED LIGHT");
r1.setBackground(Color.red);
p1.add(r1);
r1.addItemListener(this);
JRadioButton r2=new JRadioButton("YELLOW LIGHT");
r2.setBackground(Color.YELLOW);
p1.add(r2);
r2.addItemListener(this);
JRadioButton r3=new JRadioButton("GREEN LIGHT");
r3.setBackground(Color.GREEN);
p1.add(r3);
r3.addItemListener(this);
add(p1);
bg=new ButtonGroup();
bg.add(r1);
bg.add(r2);
bg.add(r3);
setVisible(true);
}
public void itemStateChanged(ItemEvent i){
JRadioButton jb=(JRadioButton)i.getSource();
switch(jb.getText()){
case"RED LIGHT":{
l1.setText("STOP");
l1.setForeground(Color.red);
}
break;
case"YELLOW LIGHT":{
l1.setText("Ready");
l1.setForeground(Color.YELLOW);
}
break;
case"GREEN LIGHT":{
l1.setText("GO");
l1.setForeground(Color.GREEN);
}
break;
}
}
}
public class TLights{
public static void main(String[] args){
A a=new A() {};
}
}
OUTPUT:
RESULT:
Thus the application for Traffic Light simulation using JavaFX controls, layouts and
menus has been simulated successfully.
MINI
PROJECT
AIM:
To develop an application for converting currency, distance and time to different units
using Java.
PROGRAM:
package mypack;
import java.util.Scanner;
class currency
{
public currency()
{
char us_dollar_sym=36;
char rupee_sym=163;
char yen_sym=165;
char euro_sym=8364;
String us_dollar="Dollars";
String rupee="Rupees";
String yen="Yen";
String euro="Euros";
double rate=0;
System.out.println("Welcome to the currency converter program\n");
System.out.println("Use the following codes to input your currency choices:\n1-US-
dollars\n2-Euros\n3-Indian Rupees\n4-Japanese Yen\n");
System.out.println("Please choose the input currency:");
Scanner in=new Scanner(System.in);
int choice=in.nextInt();
String inType=null;
switch(choice)
{
case 1:inType="US Dollars>>"+us_dollar_sym;break;
case 2:inType="Euros>>"+euro_sym;break;
case 3:inType="Indian Rupess>>"+rupee_sym;break;
case 4:inType="Japanese Yen>>"+yen_sym;break;
default:
System.out.println("Please restart the program &enter a number from the list:");
return;
}
System.out.println("Please choose the output currency");
int output=in.nextInt();
System.out.println("Now enter the input in"+inType);
double input=in.nextDouble();
if(choice==output)
System.out.println("Same currency no need to convert");
if(choice==1&&output==2)
{
double dollar_euro_rate=83;
rate=input*dollar_euro_rate;
System.out.printf("%s"+input+"at a conversion rate of"+dollar_euro_rate+"Dollar to
%s=%2f\n",(char)us_dollar_sym,euro,rate);
}
else if(choice==1&&output==3)
{
double dollar_rupee_rate=66.82;
rate=input*dollar_rupee_rate;
System.out.printf("%s"+input+"at a conversion rate of"+dollar_rupee_rate+"Dollar to
%s=%2f\n",(char)us_dollar_sym,rupee,rate);
}
else if(choice==1&&output==4)
{
double dollar_yen_rate=109.12;
rate=input*dollar_yen_rate;
System.out.printf("%s"+input+"at a conversion rate of"+dollar_yen_rate+"Dollars to
%s=%2f\n",(char)us_dollar_sym,yen,rate);
}
if(choice==2&&output==1)
{
double euro_dollar_rate=1.20;
rate=input*euro_dollar_rate;
System.out.printf("%s"+input+"at a conversion rate of"+euro_dollar_rate+"Euros to
%s=%2f\n",(char)euro_sym,us_dollar,rate);
}
else if(choice==2&&output==3)
{
double euro_rupee_rate=80.03;
rate=input*euro_rupee_rate;
System.out.printf("%s"+input+"at a conversion rate of"+euro_rupee_rate+"Euros to
%s=%2f\n",(char)euro_sym,rupee,rate);
}
else if(choice==2&&output==4)
{
double euro_yen_rate=130.69;
rate=input*euro_yen_rate;
System.out.printf("%s"+input+"at a conversion rate of"+euro_yen_rate+"Euros to
%s=%2f\n",(char)euro_sym,yen,rate);
}
if(choice==3&&output==1)
{
double rupee_dollar_rate=0.015;
System.out.printf("%s"+input+"at a conversion rate of"+rupee_dollar_rate+"Pounds to
%s=%2f\n",(char)rupee_sym,us_dollar,rate);
}
else if(choice==3&&output==2)
{
double rupee_euro_rate=0.012;
System.out.printf("%s"+input+"at a conversion rate of"+rupee_euro_rate+"Pounds to
%s=%2f\n",(char)rupee_sym,euro,rate);
}
else if(choice==3&&output==4)
{
double rupee_yen_rate=1.641;
rate=input*rupee_yen_rate;
System.out.printf("%s"+input+"at a conversion rate of"+rupee_yen_rate+"Pounds to
%s=%2f\n",(char)rupee_sym,yen,rate);
}
if(choice==4&&output==1)
{
double yen_dollar_rate=0.0092;
System.out.printf("%s"+input+"at a conversion rate of"+yen_dollar_rate+"Yen to
%s=%2f\n",(char)yen_sym,us_dollar,rate);
}
else if(choice==4&&output==2)
{
double yen_euro_rate=0.0077;
System.out.printf("%s"+input+"at a conversion rate of"+yen_euro_rate+"Yen to
%s=%2f\n",(char)yen_sym,euro,rate);
}
else if(choice==4&&output==3)
{
double yen_rupee_rate=0.61;
System.out.printf("%s"+input+"at a conversion rate of"+yen_rupee_rate+"Yen to
%s=%2f\n",(char)yen_sym,rupee,rate);
}
System.out.println("THANK YOU FOR USING CURRENCY CONVERTER");
}
}
class DConverter
{
public DConverter()
{
int num;
double meters;
Scanner keyboard=new Scanner(System.in);
System.out.print("Enter a distance ion meters.eg.100:");
meters=keyboard.nextDouble();
while(meters<=0)
{
String error="Please enter a num greater than zero";
System.out.println(error);
meters=keyboard.nextDouble();
}
System.out.print("\n Enter 1-4 from the menu options:");
System.out.println("\n1.Convert to Kilometers\n2.Convert to inches\n"+"3.Convert to
feet\n4.End");
num=keyboard.nextInt();
while(num<=0||num>=5)
{
String Invalid="Invalid option entered.Please use 1 through 4";
System.out.println(Invalid);
meters=keyboard.nextDouble();
}
switch(num)
{
case 1:dpKilometers(meters);break;
case 2:dpInches(meters);break;
case 3:dpFeet(meters);break;
case 4:System.exit(0);
}
}
public static void dpKilometers(double meters)
{
double km;
km=meters*.001;
System.out.println(meters+"meters equals to"+km+"Kilometers");
}
public static void dpInches(double meters)
{
double in;
in=meters*39.37;
System.out.println(meters+"meters equals to"+in+"inches");
}
public static void dpFeet(double meters)
{
double ft;
ft=meters*3.281;
System.out.println(meters+"meters equals to"+ft+"Feet");
}
}
class TConverter
{
public TConverter()
{
String hr="Hours";
String min="Minutes";
String sec="Seconds";
double time=0,rate=0;
System.out.println("WELCOME TO THE TIME CONVERTER PROGRAM\n");
System.out.println("Use the following codes to input your Time choices:\n1-Hours\n2-
Minutes\n3-Seconds\n");
System.out.println("Please choose the input time format:");
Scanner in=new Scanner(System.in);
int choice=in.nextInt();
String inType=null;
switch(choice)
{
case 1:inType="Hours>>";break;
case 2:inType="Minutes>>";break;
case 3:inType="Seconds>.";break;
default:
System.out.println("Please restart program & enetr a number from the list");
return;
}
System.out.println("Please choose the output time format");
int output=in.nextInt();
System.out.printf("Now enter the inputin"+inType);
double input=in.nextDouble();
if(choice==output)
System.out.println("Same these format no need to convert");
if(choice==1&&output==2)
{
double hrs_min_rate=60;
rate=input*hrs_min_rate;
System.out.printf(input+"Hours converted to"+rate+"Minutes");
}
else if(choice==1&&output==3)
{
double hrs_sec_rate=3600;
rate=input*hrs_sec_rate;
System.out.printf(input+"Hours converted to"+rate+"Seconds");
}
if(choice==2&&output==1)
{
double min_hrs_rate=0.01667;
rate=input*min_hrs_rate;
System.out.printf(input+"Minutes converted to"+rate+"Hours");
}
else if(choice==2&&output==3)
{
double min_sec_rate=60;
rate=input*min_sec_rate;
System.out.printf(input+"Minutes converted to"+rate+"Seconds");
}
if(choice==3&&output==1)
{
double sec_hrs_rate=0.0002778;
rate=input*sec_hrs_rate;
System.out.printf(input+"Seconds converted to"+rate+"Hours");
}
else if(choice==3&&output==2)
{
double sec_min_rate=0.01667;
rate=input*sec_min_rate;
System.out.printf(input+"Seconds converted to"+rate+"Minutes");
}
System.out.println("THANK YOU FOR USING THE TIME CONVERTER");
}
}
public class MyPack
{
public static void main(String args[])
{
currency c=new currency();
DConverter dc=new DConverter();
TConverter tc=new TConverter();
}
}
OUTPUT:
Welcome to the currency converter program
Use the following codes to input your currency choices:
1-US-dollars
2-Euros
3-Indian Rupees
4-Japanese Yen
Please choose the input currency:
1
Please choose the output currency
2
Now enter the input inUS Dollars>>$
20
$20.0at a conversion rate of83.0Dollar to Euros=1660.000000
THANK YOU FOR USING CURRENCY CONVERTER
Enter a distance ion meters.eg.100:50
Enter 1-4 from the menu options:
1.Convert to Kilometers
2.Convert to inches
3.Convert to feet
4.End
1
50.0meters equals to0.05Kilometers
WELCOME TO THE TIME CONVERTER PROGRAM
Use the following codes to input your Time choices:
1-Hours
2-Minutes
3-Seconds
Please choose the input time format:
1
Please choose the output time format
2
Now enter the inputinHours>>3
3.0Hours converted to180.0MinutesTHANK YOU FOR USING THE TIME CONVERTER
BUILD SUCCESSFUL (total time: 6 minutes 28 seconds)
RESULT:
Thus the mini project using Java for converting currency, distance and time to different
units has been implemented successfully.