This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
System.out.println("\nEnter a number:");num=Integer.parseInt(ob.readLine());System.out.println("\nMENU\n1.Factorial\n2.Fibnocci Series\n3.Exit");System.out.println("Enter your option:");op=Integer.parseInt(ob.readLine());switch(op){case 1:i.fact(num);break;case 2:i.fib(num);break;case 3:break;}}while(op!=3);}}
6(b)Applet to convert Fahrenheit to Celsiusimport java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.text.*;public class TempConversion extends Applet { private Label lblFahrenheit, lblCelsius; private TextField txtFahrenheit, txtCelsius; private Button Calculate; private Panel panel;. public void init () { setBackground (Color.cyan); panel = new Panel (); panel.setLayout (new GridLayout (5, 1, 12, 12));
lblFahrenheit = new Label ("Fahrenheit"); txtFahrenheit = new TextField (10); lblCelsius = new Label ("Celsius"); txtCelsius = new TextField (10); Calculate = new Button ("Calculate"); Calculate.addActionListener (new CalculateListener ()); panel.add (lblFahrenheit);
void read() throws IOException{DataInputStream in=new DataInputStream(System.in);System.out.println("\nEnter the details");System.out.println("\nId:");id=Integer.parseInt(in.readLine());System.out.println("\nName:");name=in.readLine();System.out.println("\nMarks1:");marks1=Integer.parseInt(in.readLine());System.out.println("\nMarks2:");marks2=Integer.parseInt(in.readLine());System.out.println("\nMarks3:");marks3=Integer.parseInt(in.readLine()); }void show(){System.out.println("\ndetails");System.out.println("\nId:"+id);System.out.println("\nName:"+name);
System.out.println("\nMarks1:"+marks1);System.out.println("\nMarks2:"+marks2);System.out.println("\nMarks3:"+marks3);}}class sports_student extends student { int extra_marks; void input() throws IOException{DataInputStream in=new DataInputStream(System.in);System.out.println("\n Enter the Extra marks for sports student:");extra_marks=Integer.parseInt(in.readLine()); } void show(){System.out.println("\nSports weightage:"+extra_marks);} }class Overridding{
public static void main(String[] args) throws IOException{ student s=new student();sports_student s1=new sports_student();student r;s.read();s1.input();r=s;r.show();r=s1;r.show();}}10(b) Reading a file,Checking for file existence and typeimport java.util.Scanner;import java.io.File;public class Filedemo {public static void main(String[] args){Scanner input=new Scanner(System.in);System.out.println("\nEnter the file name:");String s=input.nextLine();File f1=new File(s);System.out.println("File Name:"+f1.getName());System.out.println("Path:"+f1.getPath());System.out.println("Abs Path:"+f1.getAbsolutePath());System.out.println("Parent:"+f1.getParent());System.out.println("This file is:"+(f1.exists()?"Exists":"Does not exists"));System.out.println("Is file:"+f1.isFile());System.out.println("Is Directory:"+f1.isDirectory());System.out.println("Is Readable:"+f1.canRead());System.out.println("IS Writable:"+f1.canWrite());System.out.println("File Size:"+f1.length()+"bytes");System.out.println("Is Hidden:"+f1.isHidden());}}OUTPUTE:\pgm>java FiledemoEnter the file name:server2.javaFile Name:server2.javaPath:server2.javaAbs Path:E:\pgm\server2.javaParent:nullThis file is:ExistsIs file:true
Is Directory:falseIs Readable:trueIS Writable:trueFile Size:249bytesIs Hidden:false11(a)Java pgm for in-built exceptionimport java.io.*;class Exception{public static void main(String args[])throws IOException{DataInputStream n=new DataInputStream(System.in); try{int a,b=42,c,d;a=args.length;c=b/a;System.out.println("C="+c);System.out.println("Enter a Number:");d=Integer.parseInt(n.readLine());}catch(ArithmeticException e){System.out.println("CaughtException:"+e);}catch(NumberFormatException e){System.out.println("CaughtException:"+e);}}}
11(b)Applet to display college informationimport java.awt.*;import java.applet.*;import java.lang.*;/*<applet code="LayCollege" width=250 height=250></applet>*/public class LayCollege extends Applet{TextArea t1;Label l1,l2;public void init(){l1=new Label("Narayanaguru College");l2=new Label("Manjalumoodu,Marthandom,Tamil Nadu.");
String str="Narayanaguru College Is the Best College In Tamilnadu. It provides Lot of degrees in Engineering. College is managed by Sidhardhan. \n Best College in India.\n Courses \n--------\n1.ECE \n2.EEE\n3.MCA ";t1=new TextArea(str);setLayout(new BorderLayout());add(l1,BorderLayout.NORTH);add(t1,BorderLayout.CENTER);add(l2,BorderLayout.SOUTH);}}
13(a)Java pgm to perform arithmetic op using packagesPackagepackage arith_pack;public class Arithmetic{public int a,b,c;public Arithmetic(int i,int j){a=i;b=j;}public void add(){c=a+b;System.out.println("Sum:"+c);}public void sub(){c=a-b;System.out.println("Difference:"+c);}public void mul(){c=a*b;System.out.println("Product:"+c);}public void div(){c=a/b;System.out.println("Division:"+c);}}Package Implementationimport arith_pack.*;import java.io.*;class Arith
{public static void main(String arg[]) throws IOException{DataInputStream n=new DataInputStream(System.in);int i,j;System.out.println("Enter First Value:");i=Integer.parseInt(n.readLine());System.out.println("Enter Second Value:");j=Integer.parseInt(n.readLine());Arithmetic ar=new Arithmetic(i,j);ar.add();ar.sub();ar.mul();ar.div();}}
13(b) Applet program—to display arc on mouse click and display your name on mouse dragimport java.awt.*;import java.awt.event.*;import java.applet.*;/*<applet code="MouseEvents" width=300 height=100></applet>*/public class MouseEvents extends Applet implements MouseListener, MouseMotionListener {
{DataInputStream in=new DataInputStream(System.in);System.out.println("\nThe Cash Register:");for(j=1;j<=i;j++){System.out.println(name[j]+":"+price[j]);}if(tot>200){disc=tot*0.05;System.out.println("Discount:"+disc);discprice=tot-disc;System.out.println("Discounted price:"+discprice);}System.out.println("\nEnter the tendered amount:");tendamt=Double.parseDouble(in.readLine());System.out.println("Tender:"+tendamt);change=tendamt-discprice;System.out.println("Change:"+change);}void input()throws IOException{DataInputStream in=new DataInputStream(System.in);System.out.println("\nCASH REGISTER");System.out.println("\nEnter item name and price");String str;i=1;do{System.out.println("Enter the article"+i+":");name[i]=in.readLine();str=name[i].toString();if(str.equals("Exit")){name[i]=" ";display();System.exit(0);}System.out.println("\nEnter the price of "+name[i]+":");price[i]=Double.parseDouble(in.readLine());tot=tot+price[i];i=i+1;System.out.println(i);}while(!str.equals("Exit"));}