8
05/12/2014 "Here To Help You": Java Programming Lab Part 1 http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 1/8 Home Technical Studies Online Gov Portals Salutary Links Interview Questions Question Bank Notes & for UG and PG Soft Aptitude Video Project Lab Manuals Basics and Viva E-Books Research Softwares UGC More Jobs IEEE & Elsev Contact Us Dharanyadevi blogspot subsume with E-books, Search SEARCH Thursday, August 30, 2012 Java Programming Lab Part 1 Ex. No: Rational Number Representation Aim: To write a program for Rational Number Representation Algorithm: Step 1: Start the Program Step2:Define a Class called fraction Step 3:In the class Define a Function frac() to find greatest common divisor Step 4:Define another class called ration with main() function Step 5:In the main() get the input() Step 6:Create an Object for class fraction Step 7:Calculate the numerator and denominator using the function frac() Step 6:Then print the number as numerator/denominator Program: import java.io.*; classfraction { int frac(int a,int b) { int x,y,i; for(i=min(a,b);i>0;i--) { x=a%i; y=b%i; if((x==0)&&(y==0)) break; } return i; } int min(int a,int b) { if(a>b) return b; else return a; } } class ration { 133 people like this. Like Share Dharanya Villupuram, Hindu, India Working as a lecturer in Anna University, Villupuram!!! http://annauniv.academia.edu/Dha ranyadevi View my complete profile About Me Is This Website Is Informative???? Jo with Goo Membe Already a Follow (SS) S Answ .Net A Answ .Net D and A Showing posts with label Java Programming Lab Part 1. Show all posts

Java Programming Lab Part 1

Embed Size (px)

DESCRIPTION

Java programming lab

Citation preview

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 1/8

    Home Technical Studies Online Gov Portals Salutary Links Interview Questions Question Bank Notes & for UG and PG Soft Skills

    Aptitude Video Project Lab Manuals Basics and Viva E-Books Research Softwares UGC More Jobs IEEE & Elsevier Papers

    Contact Us

    Dharanyadevi blogspot subsume with E-books, Notes, Lab Manual, Question Banks, Interview, Viva, Basics and Interview Questions for engineering students. For Any Help Contact us [email protected]

    Search

    SEARCH

    Thursday, August 30, 2012

    JavaProgrammingLabPart1

    Ex. No: Rational Number RepresentationAim:To write a program for Rational Number RepresentationAlgorithm:Step 1: Start the ProgramStep2:Define a Class called fractionStep 3:In the class Define a Function frac() to find greatest common divisorStep 4:Define another class called ration with main() functionStep 5:In the main() get the input()Step 6:Create an Object for class fractionStep 7:Calculate the numerator and denominator using the function frac() Step 6:Then print the

    number as numerator/denominator

    Program:import java.io.*;classfraction{int frac(int a,int b){int x,y,i;for(i=min(a,b);i>0;i--){x=a%i; y=b%i; if((x==0)&&(y==0)) break;}return i;}int min(int a,int b){ if(a>b) return b; else return a;}}class ration{

    133peoplelikethis.Like Share

    DharanyaVillupuram, Hindu, India

    Working as a lecturer in AnnaUniversity, Villupuram!!!http://annauniv.academia.edu/Dharanyadevi

    View my complete profile

    About Me

    Is This Website IsInformative????

    JointhissitewithGoogleFriendConnect

    Members

    Alreadyamember?

    Follow Us :)!!!

    (SS) System Software Viva Question andAnswers.Net Assembly Interview Question andAnswer Part 9.Net Deployment Interview Questionand Answer Part 4

    Showing posts with label Java Programming Lab Part 1. Show all posts

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 2/8

    public static void main(String args[]){int a,b,c,nu,di;System.out.println("Enter the numerator and denominator values:"); DataInputStream din=newDataInputStream(System.in);String x=din.readline();a=Integer.parseInt(x);String y=din.readline() b=Integer.parseInt(y); fraction f=new fraction(); c=f.frac(a,b);nu=a/c;di=b/c;System.out.println("The fraction is"+nu+"/"+di);}}Result:Thus the Program for rational number representation is executed SucessfullyOutput:Enter the Numerator and Denominator Values:355The fraction is 7/1

    Ex. No: Packages and JavaDocAim:To write a program to implement the lisp listAlgorithm:Step 1:Start the ProgramStep 2: Define a class called ListStep 3:In it define a method cons() for getting the elements to be insertedStep 4:Define another method car() to display the first element list Step 5:Define cdr() method to

    display the remaining elements of list Step 6:Define another class lisp with main() functionStep 6:In it create an object for List class and get a choiceStep 7: If choice is 1,call the cons() function

    Step 8:ElseIf choice is 2,call the car() function Step 9:ElseIf choice is 3,call the cdr() function

    Step 10:ElseIf choice is 4,Exit the program Step 11:Goto step 6Step 12:Stop the programProgram:import java.io.*; import java.util.*; import java.lang.String; class Lisp{public static void main(String[] args)throws IOException{int ch;boolean x=true;list lis=new list();DataInputStream d=new DataInputStream(System.in);System.out.println("Lisp list implementation"); System.out.println("\n\tOPTIONS\tACTIONS");System.out.println("\t~~~~~~~~~\t\t~~~~~~"); System.out.println("\t1:cons\tInsert a newelements"); System.out.println("\t2:car\t\tShows the first element in the list");System.out.println("\t3:cdr\t\tShows the rest of the element in the list");System.out.println("\t4:exit\tEnds the program");System.out.println();while(x){System.out.println("\nEnter the choice"); ch=Integer.parseInt(d.readLine()); switch(ch){case 1: lis.cons(); break; case 2: lis.car(); break; case 3: lis.cdr();

    break; case 4: x=false;System.out.println("EXIT");break;default:System.out.println("Invalid Choice");}}}}class List{int in,n=0;LinkedList l=neew LinkedList();DataInputStream dis=new DataInputStream(System.in);void cons()throws IOException{System.out.println("Enter the element to be inserted");in=Interger.parseInt(dis.readLine());l.addFirst(in);

    Yes

    No

    NeedToImprove

    Satisfactory

    Youmayselectmultipleanswers.

    Vote Showresults

    Votessofar:203Dayslefttovote:2503

    Follow by Email

    Emailaddress... Submit

    357,079

    Total Pageviews

    kathylettequotes

    Whydomenlikeintelligentwomen?Becauseoppositesattract.

    KathyLette

    Cheeky Quotes

    .Net Interview Question and AnswerPart 2.Net Interview Question and AnswerPart 3.Net Interview Question and AnswerPart 6.Net Interview Question and AnswerPart 7.Net Interview Question and AnswerPart 8.Net Interview Question Part 1.Net Interview Question Part 5Basics of CBasics of Wireless NetworkingC # Interview Question and AnswerC InterviewC Interview Question and Answer Part 1(1)C Interview Question and Answer Part 2(1)C Interview Question and Answer Part 3(1)C Interview Question and Answer Part 4(1)C Interview Question and Answer Part 6(1)C Interview Questions and Answers Part5 (1)C Program to Count the LinesCCNA InterView Question and Answers(1)COA 2 Marks ( Unit Wise )COA Question Bank 16 MarksCognizant Test PatternComputer Graphics Lab ManualComputer Network Lab ManualComputer Network-2 MarksComputer Networks 16 Marks KeyPointsComputer Networks: BasicsComputer Networks: InterviewCS1203 -SYSTEM SOFTWAREB.E/B.Tech DEGREE EXAMINATIONMay/Jun 2007CS1302 BankCS1304-System Software Question Bank(1)CS1312 Object Oriented ProgrammingFor EEECS2201 Data Structures Question Bank(1)CS2401 Computer GraphicsCTS aptitude.DBMS Lab ManualDelete Administrator PasswordDifference Between LANEC2303 Computer Architecture andOrganization Question BankEc2303 Computer Architecture AndOrganization SyllabusEC2305 Transmission Lines And WaveGuides 2 and 16 Marks Question BankEC2401 Wireless Communication 2Marks Question BankECE EC1008 High Speed NetworksNov/Dec 2009 paperEngineering Economics And FinancialAccounting MG2452 2 MarksEngineering Economics and FinancialManagement MG2452 16 MarksQuestion BankFile Copy Program in CFile Management in CFile Operation Function in CFile Program in C

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 3/8

    n++;System.out.println("After insertion"); System.out.println(l);}void car(){System.out.println("The first element in the list");System.out.println(l.getFirst());}void cdr(){System.out.println("The rest of the element in the list are"); System.out.println("[");for(int i=1;i

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 4/8

    {String dl;int d;public void getDollar()throws IOException{DataInputStream dis2=new DataInputStream(System.in); System.out.println("Enter the $ tostore:"); dl=dis2.readLine();d=Integer.parseInt(dl);}public String toString(){return "Entered Doller value="+dl+"and Eqivalent Rupee value="+d*50;}}class Currency{public static void main(String args[])throws IOException{Rupee rin=new Rupee();rin.getRupee();Dollar din=new Dollar();din.getDollar();try{FileOutputStream fos=new FileOutputStream("file1.dat"); ObjectOutputStream oos=newObjectOutputStream(fos); oos.writeObject(rin);oos.flush();oos.writeObject(din); oos.flush(); oos.close();}catch(Exception e){System.out.println("Exception during serialization:"+e); System.out.println(0);}Rupee rout;Dollar dout;try{FileInputStream fis=new FileInputStream("file1.dat");ObjectInputStream ois=new ObjectInputStream(fos); rout=(Rupee)ois.readObject();System.out.println(rout); dout=(Dollar)ois.readObject(); System.out.println(dout);ois.close();}catch(Exception e){System.out.println("Exception during deserialization:"+e);System.out.println(0);}}}Result:Thus the program for currency conversion is implemented and executed successfullyOutput:Enter the Rs to store:12Enter the $ to store:50Entered Rupee value=12Entered Dollar value=50Equivalent value=2500Ex. No: Packages and JavaDocAim:

    To develop a Java package with simple Stack and Queue classes and Use JavaDoc commentsfor documentation.Algorithm:Step 1. StartStep 2. Create two classes, Stack and Queue.Step 3. Add the line, package ckage_name>,inthefirstlineofbothclasses.Step4.Savethe.javafilesoftheclassesinafolderwiththesamenameaspackage_name.

    Step 5. Create objects using main program for both Stack and Queue classes in separate files, import thepackage in the main as import package_name.class_name; and save them in the same path as thatof the package.Step 6. Add comments to the classes in the package using Java docStep 7. Set the path variable to the path where execution will happen.Step 8. Change the directory to package_name.Step 9. Compile the classes. The .class file must be in the same location as that of the

    Third Year Question Bank:Cryptography and Network SercurityThird Year Question Bank: NetworkProgramming and ManagementUse Your Pendrive As RAMViva and Basic Question and Answer ofOperating System Part 1Viva and Basic Question and AnswerPart 4Viva and Basic Question and AnswerPart 5Viva and Basic Questions of DataStructure Part 2Viva and Basic Questions of DataStructuresViva and Basic Questions of DBMSViva and Basic Questions of JAVA Part 1(1)Viva and Basic Questions of Java Part 2(1)Viva and Basic Questions of OOPS Part 1(1)Viva and Basic Questions Part 1Viva and Basic Questions Part 3Viva and Basic Questions Part 4Viva and Basic Questions Part 6Viva and Basics of Operating SystemPart 2Web Development ToolsWeb Technology Lab ManualWhat is System Software???????Windows Run Commands and ShortCutsKeysWireless Communication 16 MarksQuestion BankWords and Characters in a Given Text(1)Write a C Program to Count the Numberof CharacterXML Interview Question and Answer

    http://www.flipkart.com

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 5/8

    .java fileStep 10. Go to bin dir and compile the main programStep 11. To view the Javadoc , javadoc d irname>ckage_name>Step12.AhtmlfilefortheJavadocumentationwillbecreated.Step 13. Stop.Program:/*Queue.java*/package pack;/**Queue with insert and delete operations@param otherPerson The person being compared to the calling object.@return Returns true if the calling object equals otherPerson./public class Queue{int queue[]=new int[10];int foq,roq;public Queue(){foq=-1;roq=-1;}public void ins(int item){if(foq==9 && roq==9) System.out.println("Queue is full"); else if(foq==-1 && roq==-1){queue[++roq]=item;++foq;}

    else queue[++roq]=item;}public void del(){int k;if(foq==-1)System.out.println("Q is empty");else{

    k=queue[foq++]; System.out.println("Deleted value:"+k);

    }}}/*Stack.java*//** This class defines an integer stack that can hold 10 values.*/package pack;public class Stack {int stck[] = new int[10];int tos;// Initialize top-of-stack public Stack() {tos = -1;}// Push an item onto the stack public void push(int item) { if(tos==9) System.out.println("Stack is

    full.");

    elsestck[++tos] = item;}// Pop an item from the stack public int pop() {if(tos < 0) {System.out.println("Stack underflow.");return 0;}elsereturn stck[tos--];}}/*Qpackage.java*/ import pack.Queue; class Qpackage{public static void main(String args[]){Queue q1=new Queue();

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 6/8

    // Add some numbers onto the Queue for(int i=0; i

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 7/8

    this.a += c2.a;this.b += c2.b;}public void subtract(ComplexNumber c2) {this.a -= c2.a;this.b -= c2.b;}public void divide(ComplexNumber c2) {double c = this.a;this.a = (this.a * c2.a + this.b * c2.b)/(c2.a * c2.a + c2.b * c2.b);this.b = (c * c2.b*-1+ this.b * c2.a)/(c2.a * c2.a + c2.b * c2.b);}public void multiply(ComplexNumber c2) {double c = this.a;this.a = this.a * c2.a - this.b * c2.b;this.b = c * c2.b + this.b - c2.a;}public String toString1(){return a + " + " + b + "i";}}class comp{public static void main(String args[])throws IOException{ComplexNumber c1=new ComplexNumber(3,4); ComplexNumber c2=new ComplexNumber(1,5);

    DataInputStream in = new DataInputStream (System.in);

    System.out.println("Enter 1. Addition 2.Subtraction 3.Division 4.Multiplication:"); String numberip =

    in.readLine();

    String a;switch (i){

    case 1: c1.add(c2); a=c1.toString1(); System.out.println(a); break;

    case 2: c1.subtract(c2); a=c1.toString1(); System.out.println(a); break;

    case 3: c1.divide(c2); a=c1.toString1(); System.out.println(a); break;

    case 4: c1.multiply(c2); a=c1.toString1(); System.out.println(a); break;default:System.out.println("Unknown object!");break;}System.out.println("No of active objects in this program"); System.out.println(c1.count);

    }}Result:

    Thus a class for Complex numbers with all methods for basic operations on complexnumbers and also with a method that returns the number of active objects was created andexecuted successfully.Output:C:\Program Files\Java\jdk1.6.0\bin>javac comp.java Note: comp.java uses or overrides a

    deprecated API. Note: Recompile with -Xlint:deprecation for details.

    C:\Program Files\Java\jdk1.6.0\bin>java compEnter 1. Addition 2.Subtraction 3.Division 4.Multiplication:14.0 + 9.0iNo of active objects in this program2C:\Program Files\Java\jdk1.6.0\bin>java compEnter 1. Addition 2.Subtraction 3.Division 4.Multiplication:22.0 + -1.0iNo of active objects in this program2C:\Program Files\Java\jdk1.6.0\bin>java compEnter 1. Addition 2.Subtraction 3.Division 4.Multiplication:30.8846153846153846 + -0.4230769230769231i

    int i = Integer.parseInt(numberip);

  • 05/12/2014 "HereToHelpYou":JavaProgrammingLabPart1

    http://dharanyadevi.blogspot.in/search/label/Java%20Programming%20Lab%20Part%201 8/8

    Older PostsHome

    Subscribe to: Posts (Atom)

    Posted by Dharanya at 11:50 PM No comments:

    Labels: Java Programming Lab Part 1

    No of active objects in this program2C:\Program Files\Java\jdk1.6.0\bin>java compEnter 1. Addition 2.Subtraction 3.Division 4.Multiplication:4-17.0 + 18.0iNo of active objects in this program2C:\Program Files\Java\jdk1.6.0\bin>

    Remaining programs is continued in Part 2

    Recommend this on Google

    Refer this site 2 ur frndz

    Dharanyadevi BlogSpot. Simple template. Powered by Blogger.