0% found this document useful (0 votes)
43 views8 pages

Iind Cycle Java Lab Program

1. The document describes a Java program to implement a scientific calculator using event-driven programming. It defines classes for the calculator panel, frame and main class. The panel class handles button clicks and calculations. It uses a GridLayout to arrange buttons and BorderLayout to position the display. On button clicks, it performs operations like addition, subtraction etc and updates the display. 2. The frame class sets up the window properties. The main class creates the frame and makes it visible, providing the entry point for the program. 3. The program demonstrates using event handlers, layout managers and parsing user input to build an interactive calculator application in Java. Button clicks trigger calculations on the current display value and update the result.

Uploaded by

G.b. Nandhini
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views8 pages

Iind Cycle Java Lab Program

1. The document describes a Java program to implement a scientific calculator using event-driven programming. It defines classes for the calculator panel, frame and main class. The panel class handles button clicks and calculations. It uses a GridLayout to arrange buttons and BorderLayout to position the display. On button clicks, it performs operations like addition, subtraction etc and updates the display. 2. The frame class sets up the window properties. The main class creates the frame and makes it visible, providing the entry point for the program. 3. The program demonstrates using event handlers, layout managers and parsing user input to build an interactive calculator application in Java. Button clicks trigger calculations on the current display value and update the result.

Uploaded by

G.b. Nandhini
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

IInd Cycle Java Lab Program

1. AIM: To implement Lisp-like list in java and write basic operations such as car, cdr and cons. Source Code
import java.util.*; class LispList { public Object CAR() { return(link.getFirst()); } public List CDR() { return(link.subList(1,link.size())); } public void CONS(LinkedList a) { link.offerFirst(a); } public void CONS(int i) { link.offerFirst(i); } public void CONS(String s) { link.offerFirst(s); } public Object getList() { return link; } public int getSize() { return (link.size()); } private LinkedList link=new LinkedList(); } public class LispListTest { public static void main(String args[]) { LispList lisp=new LispList(); Scanner read=new Scanner(System.in); LinkedList l=new LinkedList();

System.out.println("Enter three integer elements to store in LinkedList"); l.addFirst(read.nextInt()); l.addFirst(read.nextInt()); l.addFirst(read.nextInt()); lisp.CONS(l); System.out.println("Enter two integers to concatenate into a lisp list"); lisp.CONS(read.nextInt()); lisp.CONS(read.nextInt()); System.out.println("Enter a string to concatenate into a lisp list"); lisp.CONS(read.next()); System.out.println("Elements in the lisp list"+lisp.getList()); System.out.println("size of the lisp list"+lisp.getSize()); System.out.println("result of car operation in Lisp list"+lisp.CAR()); System.out.println("result of car operation in Lisp list"+lisp.CDR()); } }

2. AIM: To design classes for currency, rupee and dollar and implement object serialization.

PROGRAM1:
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<=10;i++) { Object[] obj = { new Rupee(r.nextInt(200)),new Dollar(r.nextInt(200)) }; 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); } } }

3. PROGRAM2:
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<=10;i++) { obj = (Currency)ois.readObject(); if((obj.currency).equals("$")) System.out.println(obj + " = " + obj.dollarToRupee(obj.amount)); else System.out.println(obj + " = " + obj); } ois.close(); } catch(Exception e) { System.out.println("Exception during deserialization." + e); } } }

AIM: To develop a scientific calculator using event-driven program.

Program

import java.awt.*; import java.awt.event.*; import javax.swing.*; class calculatorpanel extends JPanel { JButton display; JPanel panel; double result; String lastcommand,a; boolean start; public calculatorpanel()

{ setLayout(new BorderLayout()); result=0; lastcommand="="; start=true; display=new JButton("0"); display.setEnabled(false); add(display,BorderLayout.NORTH); ActionListener insert=new insertAction(); ActionListener command=new commandAction(); panel=new JPanel(); panel.setLayout(new GridLayout(4,5)); addbutton("sin",command); addbutton("7",insert); addbutton("8",insert); addbutton("9",insert); addbutton("/",command); addbutton("cos",command); addbutton("4",insert); addbutton("5",insert); addbutton("6",insert); addbutton("*",command); addbutton("tan",command); addbutton("1",insert); addbutton("2",insert); addbutton("3",insert); addbutton("+",command); addbutton("sqrt",command); addbutton("0",insert); addbutton(".",insert); addbutton("=",command); addbutton("-",command); add(panel,BorderLayout.CENTER); } void addbutton(String label,ActionListener listener) { JButton button=new JButton(label); button.addActionListener(listener); panel.add(button); } public void calculate(double x) { if(lastcommand.equals("+")) result+=x; else if(lastcommand.equals("-")) result-=x; else if(lastcommand.equals("*")) result*=x; else

if(lastcommand.equals("/")) result/=x; else if(lastcommand.equals("=")) result=x; else if(lastcommand.equals("sin")) result=Math.sin(Math.toRadia ns(x)); else if(lastcommand.equals("cos")) result=Math.cos(Math. toRadians(x)); else if(lastcommand.equals ("tan")) result=Math.ta n(Math.toRadi ans()); else if(lastcomman d.equals("sqrt" )) result=Math.sq rt(x); display.setText (" "+result); } class insertAction implements ActionListener { public void actionPerformed(ActionEvent e) { String input=e.getActionCommand(); if(start) { display.setText(" "); start=false; } display.setText(display.getText()+input); } } class commandAction implements ActionListener { public void actionPerformed(ActionEvent e) { String command=e.getActionCommand(); if(start) { if(command.equals("-")) {

display.setText(command); start=false; } else lastcommand=command; } else { calculate(Double.parseDouble(display.getText())); lastcommand=command; start=true; } } } } class calculatorframe extends JFrame { public calculatorframe() { setSize(350,250); setTitle("Calculator"); setLocationByPlatform(true); Toolkit kit=Toolkit.getDefaultToolkit(); Image im=kit.getImage("U:\\calicon.jpg"); setIconImage(im); calculatorpanel panel=new calculatorpanel(); add(panel); } } class calci { public static void main(String args[]) { calculatorframe cf=new calculatorframe(); cf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cf.setVisible(true); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy