Hi Friends....just had some free time, so thought of creating a "Simple Calculator" in Java...As claims it is "Simple" and don't expect anything Big out of it (Just some basic operations).....I have added the source code for it too....
(You can use it to Create your own Calculator and may be which is more efficient than mine..Just in case u find any flaws in this, pls do mail me abt it..) Screenshot of "Calculator" that I created in Java
Design classes for Currency, Rupee, and Dollar. Write a program that randomly generates Rupee and Dollar objects and write them into a file using object serialization. Write another program to read that file, convert to Rupee if it reads a Dollar, while leave the value as it is if it reads a Rupee.
Program
(credits to Kumresan Sir)
SerializationWrite.java
import java.io.*; import java.util.*;
class Currency implements Serializable { protected String currency; protected int amount;
public Currency(String cur, int amt) { this.currency = cur; this.amount = amt; } public String toString() { return currency + amount; } public String dollarToRupee(int amt) { return "Rs" + amt * 45; } } class Rupee extends Currency { public Rupee(int amt) { super("Rs",amt); } } class Dollar extends Currency { public Dollar(int amt) { super("$",amt); } } public class SerializationWrite { public static void main(String args[]) { Random r = new Random(); try { Currency currency; FileOutputStream fos = new FileOutputStream("serial.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); System.out.println("Writing to file using Object Serialization:"); for(int i=1;i<=25;i++) { Object[] obj = { new Rupee(r.nextInt(5000)),new Dollar(r.nextInt(5000)) }; currency = (Currency)obj[r.nextInt(2)]; // RANDOM Objects for Rs and $ System.out.println(currency); oos.writeObject(currency); oos.flush(); } oos.close(); } catch(Exception e) { System.out.println("Exception during Serialization: " + e); } } }
SerializationRead.java
import java.io.*; import java.util.*;
public class SerializationRead { public static void main(String args[]) { try { Currency obj; FileInputStream fis = new FileInputStream("serial.txt"); ObjectInputStream ois = new ObjectInputStream(fis); System.out.println("Reading from file using Object Serialization:"); for(int i=1;i<=25;i++) { obj = (Currency)ois.readObject(); if((obj.currency).equals("$")) System.out.println(obj + " = " + obj.dollarToRupee(obj.amount)); else System.out.println(obj + " = " + obj);
System.out.println("\n The Contents of the List are " + v);
System.out.print("\n The CAR of this List....");
System.out.println(a.car(v));
System.out.print("\n The CDR of this List....");
v1 = a.cdr(v);
System.out.println(v1);
System.out.println("\n The Contents of this list after CONS..");
v1 = a.cons("Gambhir",v);
System.out.print(" " + v1);
v.removeAllElements();
v1.removeAllElements();
System.out.println("\n\n Creating a List of Integers....\n");
System.out.println("\n The Contents of the List are " + v);
System.out.print("\n The CAR of this List....");
System.out.println(a.car(v));
System.out.print("\n The CDR of this List....");
v1 = a.cdr(v);
System.out.println(v1);
System.out.println("\n The Contents of this list after CONS..");
v1 = a.cons(9,v);
System.out.print(" " + v1);
}
}
Output
Creating a List of Strings....
The Contents of the List are [S.R.Tendulkar, M.S.Dhoni, V.Sehwag, S.Raina]
The CAR of this List....[S.R.Tendulkar]
The CDR of this List....[M.S.Dhoni, V.Sehwag, S.Raina]
The Contents of this list after CONS..
[Gambhir, S.R.Tendulkar, M.S.Dhoni, V.Sehwag, S.Raina]
Creating a List of Integers....
The Contents of the List are [3, 0, 2, 5]
The CAR of this List....[3]
The CDR of this List....[0, 2, 5]
The Contents of this list after CONS..
[9, 3, 0, 2, 5]
Implement Lisp-like list in Java. Write basic operations such as 'car', 'cdr', and 'cons'. If L is a list [3, 0, 2, 5], L.car() returns 3, while L.cdr() returns [0,2,5].
Lisp Operations
Car
The car of a list is, quite simply, the first item in the list. For Ex: car of L.car() of a list [3 0 2 5] returns 3.
Cdr
The cdr of a list is the rest of the list, that is, the cdr function returns the part of the list that follows the first item. For ex: cdr of a list [3 0 2 5] returns [0 2 5]
Cons
The cons function constructs lists. It is useful to add element to the front of the list; it is the inverse of car and cdr. For example, cons can be used to make a five element list from the four element list.
(source – Wikipedia)
Length
Returns the length of the list.
Program
import java.util.*;
import java.io.*;
// To implement LISP-like List in Java to perform functions like car, cdr, cons.
class Lisp
{
Vector v = new Vector(4,1);
int i;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader inData = new BufferedReader(isr);
String str;
//Create a LISP-Like List using VECTOR
public void create()
{
int n;
System.out.print("\n Enter the Size of the list:");
//Get Input from the user...Use I/p stream reader for it...
try
{
str = inData.readLine();
n = Integer.parseInt(str);
for(i=0;i
{
int temp;
System.out.print("\n Enter the element " + (i+1) + " of the list:");
str = inData.readLine();
temp=Integer.parseInt(str);
v.addElement(new Integer(temp));
}
}
catch (IOException e)
{
System.out.println("Error!");
System.exit(1);
}
}
//Display the content of LISP-List
public void display()
{
int n=v.size();
System.out.print("[ ");
for(i=0;i
{
System.out.print(v.elementAt(i) + " ");
}
System.out.println("]");
}
/*
The car of a list is, quite simply, the first item in the list.
For Ex: car of L.car() of a list [3 0 2 5] returns 3.
Gives the CAR of LISP-List
*/
public void car()
{
System.out.println("\n CAR of the List: " + v.elementAt(0));
}
/*
The cdr of a list is the rest of the list, that is, the cdr function returns the part of the list that follows the first item.
For ex: cdr of a list [3 0 2 5] returns [0 2 5]
*/
public void cdr()
{
int n=v.size();
System.out.print("\n The CDR of the List is:");
System.out.print("[ ");
for(i=1;i
{
System.out.print(v.elementAt(i) + " ");
}
System.out.println("]");
}
/*
The cons function constructs lists. It is useful to add element to the front of the list; it is the inverse of car and cdr. For example, cons can be used to make a five element list from the four element list.
For ex: adding 7 to [3 0 2 5], cons(7) gives [7 3 0 2 5]
*/
public void cons(int x)
{
v.insertElementAt(x,0);
System.out.print("\n The List after using CONS and adding an element:");
display();
}
// Returns the length of the LISP-List
public int length()
{
return v.size();
}
}
class Lisp_List
{
public static void main(String[] args)
{
Lisp a = new Lisp();
a.create();
System.out.print("\n The contents of the List are:");
a.display();
a.car();
a.cdr();
a.cons(5);
System.out.println("\n\n The length of the list is " + a.length());
}
}
Output
Enter the Size of the list:4
Enter the element 1 of the list:3
Enter the element 2 of the list:0
Enter the element 3 of the list:2
Enter the element 4 of the list:5
The contents of the List are:[ 3 0 2 5 ]
CAR of the List: 3
The CDR of the List is:[ 0 2 5 ]
The List after using CONS and adding an element:[ 5 3 0 2 5 ]
void gearSystem() { System.out.println("\n automatic gearing system.. So don't worry !!"); } void start() { engstatus=true; System.out.println("\n engine is ON "); } void moveForward() {
if(engstatus==true) System.out.println("\nCar is moving in forward direction .. Slowly gathering speed "); else System.out.println("\n Car is off...Start engine to move"); } void moveBackward() { if(engstatus=true) System.out.println("\n Car is moving in reverse direction ..."); else System.out.println("\n Car is off...Start engine to move"); } void applyBrake() { System.out.println("\n Speed is decreasing gradually"); } void turnLeft() { System.out.println("\n Taking a left turn.. Look in the mirror and turn "); } void turnRight() { System.out.println("\n Taking a right turn.. Look in the mirror and turn "); } void stop() { engstatus=false; System.out.println("\n Car is off "); } } class Volvo extends Vehicle { void truckManufacture() { System.out.println("Volvo Truck Manufactures"); } void gearSystem() { System.out.println("\nManual Gear system...ooops....take care while driving ") ; } void start() { engstatus=true; System.out.println("\n engine is ON "); } void moveForward() {
if(engstatus==true) System.out.println("\n truck is moving in forward direction .. Slowly gathering speed "); else System.out.println("\n truck is off...Start engine to move"); } void moveBackward() { if(engstatus=true) System.out.println("\n truck is moving in reverse direction ..."); else System.out.println("\n truck is off...Start engine to move"); } void applyBrake() { System.out.println("\n Speed is decreasing gradually"); } void turnLeft() { System.out.println("\n Taking a left turn.. Look in the mirror and turn "); } void turnRight() { System.out.println("\n Taking a right turn.. Look in the mirror and turn "); } void stop() { engstatus=false; System.out.println("\n Truck is off "); } }
import java.util.Date; import java.util.Calendar; import java.util.GregorianCalendar; import java.text.SimpleDateFormat; import java.util.*; /** * Used to provide an example that exercises most of the functionality of the * java.util.Calendar Class * @author Vivek Venkatesh G * @version */ public class CalendarExample {
/** * Calendar's getTime() method returns a Date Object * This can then be passed to println() to print today's date (and time) in the traditional format * @param No parameter for this function * @return No return value for this function */ public static void doCalendarTimeExample() { System.out.println("CURRENT DATE/TIME\n\n"); Date now = Calendar.getInstance().getTime(); System.out.println("Calendar.getInstance().getTime():"+ now); System.out.println(); }
/** * Simple Date Format from java.text.package * @param for this no parameter required */
public static void doSimpleDateFormat() { System.out.println("Simple Date Format \n\n"); // Get Today's Date Calendar now = Calendar.getInstance(); SimpleDateFormat formatter = new SimpleDateFormat("E yyyy.MM.dd 'at' hh:mm:ss a zzz"); System.out.println(" It is now :"+ formatter.format(now.getTime())); System.out.println(); }
/** * Simple Date difference from java.text.Package */ public static void doDataDifference() { System.out.println("DIFFERENCE BETWEEN TWO DATES\n\n"); Date startDate1 = new GregorianCalendar(1994,02,14,14,00).getTime(); Date endDate1 = new Date(); long diff = endDate1.getTime() - startDate1.getTime(); System.out.println("Difference between " + endDate1); System.out.println(" and "+ startDate1 + "is" + (diff/(1000L*60L*60L*24L)) + "days"); System.out.println(); }
public static void doGetMethods() { System.out.println("CALENDAR GET METHODS"); Calendar c=Calendar.getInstance();