+ All Categories
Home > Documents > Java Outputs

Java Outputs

Date post: 08-Apr-2018
Category:
Upload: suman-rajendran
View: 222 times
Download: 0 times
Share this document with a friend

of 44

Transcript
  • 8/7/2019 Java Outputs

    1/44

    1. Classes and Object

    import java.io.*;class Model

    {int age;String name,gen;void get(){

    DataInputStream ba; ba=new DataInputStream(System.in);try{

    System.out.println("Enter the name");name=ba.readLine();

    System.out.println("Enter the gender");gen=ba.readLine();System.out.println("Enter the age");age=Integer.parseInt(ba.readLine());

    }catch(IOException e){System.out.println("error");}

    }void out()

    {System.out.println("The Data is Inserted as ...");System.out.println("Name = "+name);System.out.println("Gender ="+gen);System.out.println("Age ="+age);

    }}class Model1{

    public static void main(String args[]){

    Model obj=new Model();obj.get();obj.out();

    }}

  • 8/7/2019 Java Outputs

    2/44

    Output:

    U:\>java Model1Enter the name: karthick Enter the gender: male

    Enter the age: 28The data is inserted as..

    Name: karthick Gender: maleAge: 28.

  • 8/7/2019 Java Outputs

    3/44

    2.Inheritance

    Single Inheritance :

    import java.io.*;class Odd{

    void oddvalue(int x){

    System.out.println("The entered number "+x+" an Odd number .");}

    }class Even extends Odd{

    void evenvalue(int x){

    System.out.println("The entered number "+x+" an Even number .");}

    }class Value{

    public static void main(String args[]){

    DataInputStream din;din=new DataInputStream(System.in);try{

    int no;System.out.print("Enter a number : ");no=Integer.parseInt(din.readLine());Even en=new Even();if ((no%2)==0){

    en.evenvalue(no);}else{

    en.oddvalue(no);

    }}catch(IOException e){

    System.out.println("Error !!!");}

    }}

  • 8/7/2019 Java Outputs

    4/44

    Output:

    U:\>java Value

    Enter a number:6

    The number as an even number

  • 8/7/2019 Java Outputs

    5/44

    Multilevel Inheritance

    import java.io.*;class Odd{

    void oddvalue(int x){System.out.println("The entered number "+x+" an Odd number .");

    }}class Even extends Odd{

    void evenvalue(int x){

    System.out.println("The entered number "+x+" an Even number .");}

    }class Less extends Even{

    void lessvalue(int x){

    System.out.println("The entered number "+x+" is lesser than Zero .");}

    }class Multivalue{

    public static void main(String args[])

    { DataInputStream din;din=new DataInputStream(System.in);try{

    int no;System.out.print("Enter a number : ");no=Integer.parseInt(din.readLine());Even en=new Even();Less ls=new Less();if (no

  • 8/7/2019 Java Outputs

    6/44

    }else{

    en.oddvalue(no);}

    }}catch(IOException e){

    System.out.println("Error !!!");}

    }}

  • 8/7/2019 Java Outputs

    7/44

    Output:

    i)U:\>java MultivalueEnter the number:3The entered number 3 as an odd number

    ii) U:\>java MultivalueEnter the number:-9The entered number-9 is less than zero.

  • 8/7/2019 Java Outputs

    8/44

    Hierarchical Inheritance

    import java.io.*;class Value{

    void content(int x){if ((x%2)==0){

    Even en=new Even();en.evenvalue(x);

    }else{

    Odd od=new Odd();od.oddvalue(x);

    }}}class Odd extends Value{

    void oddvalue(int x){

    System.out.println("The entered number "+x+" an Odd number .");}

    }class Even extends Value

    { void evenvalue(int x){

    System.out.println("The entered number "+x+" an Even number .");}

    }class Hiervalue{

    public static void main(String args[]){

    DataInputStream din;

    din=new DataInputStream(System.in);try{

    int no;System.out.print("Enter a number : ");no=Integer.parseInt(din.readLine());Odd od=new Odd();od.content(no);

  • 8/7/2019 Java Outputs

    9/44

    }catch(IOException e){

    System.out.println("Error !!!");}

    }}

  • 8/7/2019 Java Outputs

    10/44

    Output:

    i)U:\>java Hiervalue

    Enter a number:8The number 8 as an even number

    ii) U:\>java Hiervalue

    Enter a number:5The number 5 as an odd number

  • 8/7/2019 Java Outputs

    11/44

    3)Multithreading

    Synchronization

    class Share extends Thread{

    static String msg[]={"This", "is", "a", "synchronized", "variable"};Share(String threadname){

    super(threadname);}

    public void run(){

    display(getName());}

    public void display(String threadN){synchronized(Share.class){for(int i=0;i

  • 8/7/2019 Java Outputs

    12/44

    Output :

    U:\>javac SynStatement1.java

    U:\> java SynStatement1Thread One :ThisThread One :isThread One :aThread One :synchronizedThread One :variableObject ExitingThread Two :ThisThread Two :isThread Two :aThread Two :synchronized

    Thread Two :variableObject Exiting

  • 8/7/2019 Java Outputs

    13/44

    Thread priorities

    public class Main{

    public void setPrioritiesOnThreads(){

    Thread thread1 = new Thread(new TestThread(1));Thread thread2 = new Thread(new TestThread(2));

    //Setting priorities on the Thread objectsthread1.setPriority(Thread.MAX_PRIORITY);thread2.setPriority(Thread.MIN_PRIORITY);

    thread1.start();thread2.start();

    try {

    //Wait for the threads to finishthread1.join();thread2.join();

    } catch (InterruptedException ex) {

    ex.printStackTrace();}

    System.out.println("Done.");

    }public void noPrioritiesOnThreads(){

    Thread thread3 = new Thread(new TestThread(3));Thread thread4 = new Thread(new TestThread(4));thread3.start();

    thread4.start();try

    {thread3.join();thread4.join();

    }catch (InterruptedException ex){

    ex.printStackTrace();}

  • 8/7/2019 Java Outputs

    14/44

    System.out.println("Done.");}public static void main(String[] args){

    System.out.println("Thread without Priority");

    new Main().noPrioritiesOnThreads();System.out.println("Priority on Thread");new Main().setPrioritiesOnThreads();

    }class TestThread implements Runnable{

    int id; public TestThread(int id) {this.id = id;

    }public void run()

    { for (int i = 1; i

  • 8/7/2019 Java Outputs

    15/44

    Output

    G:\>java MainThread without PriorityThread3: 1Thread4: 1Thread3: 2Thread4: 2Thread3: 3Thread4: 3Thread3: 4Thread4: 4Thread3: 5Thread4: 5Done.Priority on ThreadThread1: 1Thread1: 2Thread1: 3Thread1: 4Thread1: 5Thread2: 1Thread2: 2Thread2: 3Thread2: 4Thread2: 5Done.

    Note : The Output may vary Depending on the CPU speed .

  • 8/7/2019 Java Outputs

    16/44

    4) Packages&interfaces

    Pack1 coding: package pack1; public interface Bnw

    { public void bkwt();}

    Pack2 coding: package pack2; public interface Color {

    public void colorcp();}

    Main Program :import java.lang.*;import java.io.*;import pack1.*;import pack2.*;class Photocopy implements Bnw,Color {

    public void bkwt(){

    System.out.println("Total amount for black and white copy: ");}

    public void colorcp(){

    System.out.println("Total amount for color photocopy");}

    }class Output{

    public static void main(String args[]){

    DataInputStream din=new DataInputStream(System.in);Photocopy cp=new Photocopy();

    double bnw,cn,bw,c;try{

    System.out.println("Enter number of normal pages :"); bnw=Double.parseDouble(din.readLine()); bw=bnw*6.50;System.out.println("Enter no of color pages:");cn=Double.parseDouble(din.readLine());

  • 8/7/2019 Java Outputs

    17/44

    c=cn*8;cp.bkwt();System.out.println(bw);cp.colorcp();System.out.println(c);

    }catch(IOException e){

    System.out.println("Error");}

    }}

  • 8/7/2019 Java Outputs

    18/44

    Output :

    U:\> cd pack1U:\pack1>javac Bnw.javaU:\pack1>cd..

    U:\>cd pack2U:\pack2> javac Color.javaU:\pack2>cd..U:\>javac Output.javaU:\>java OutputEnter the number of Normal pages : 3Enter the number of Color pages :1Total amount for black and white copy : 19.5Total amount for Color copy : 8.0

  • 8/7/2019 Java Outputs

    19/44

  • 8/7/2019 Java Outputs

    20/44

    Output:

    U:\ java ConvertEnter the rupee value to be converted:30

    Default dollar rate:54.60Rupee:30Dollar:1638Enter todays dollar rate:45Rupee:30Dollar rate:1350.0

  • 8/7/2019 Java Outputs

    21/44

    6. String handling

    import java.io.*;class Stringhandling{

    public static void main(String args[]){int ch;String c1,c2;System.out.println(" 1. String Constructors ");System.out.println(" 2. String Length ");System.out.println(" 3. String Concatenation ");System.out.println(" 4. Character Extraction ");System.out.println(" String Comparison ");System.out.println(" 5. Equal or Not ");System.out.println(" 6. starts with and ends with ");

    System.out.println(" 7. compareTo ");System.out.println(" Modifying a String ");System.out.println(" 8. replace ");System.out.println(" 9. trim ");System.out.println(" 10. Change Case");System.out.print("Enter the choice : ");DataInputStream din;din=new DataInputStream(System.in);try{

    ch=Integer.parseInt(din.readLine());

    switch (ch){case 1:

    char cd []={'J','A','V','A','_','U','S','I','N','G','_','O','O','P','S'};String s1 = new String(cd);String s2 = new String(cd,5,5);

    byte ascii[]={74,65,73,67};String s3=new String(ascii);System.out.println("String cd = "+s1);System.out.println("String of start index :4 and characters 5 : "+s2);System.out.println("The Character representation of ascii 74,65,73,67 is:

    "+s3); break;case 2:

    System.out.print("Enter String : ");c1=din.readLine();System.out.println("String Length of "+c1+" is : "+c1.length());

    break;case 3:

  • 8/7/2019 Java Outputs

    22/44

    System.out.print("Enter String 1 : ");c1=din.readLine();System.out.print("Enter String 2 : ");c2=din.readLine();System.out.println("The Concatenated String is "+c1+" and "+c2);

    System.out.println("The Concatenated String is "+c1.concat(c2)); break;case 4:

    c1="Sample program for Character Extraction in Java ";System.out.println("String : "+c1);char buf[]=new char[10];c1.getChars(7,15,buf,1);

    System.out.print("The Extracted String is : ");System.out.println(buf);

    break;case 5:

    System.out.print("Enter String 1 : ");c1=din.readLine();System.out.print("Enter String 2 : ");c2=din.readLine();System.out.println(c1+" is equal to "+c2+" -> "+c1.equals(c2));System.out.println(c1+" is equal(Ignoring case) to "+c2+" ->

    "+c1.equalsIgnoreCase(c2)); break;

    case 6:System.out.print("Enter String : ");c1=din.readLine();System.out.println(c1+" starts with 'Tool' -> "+c1.startsWith("Tool"));System.out.println(c1+" ends with 'bar' -> "+c1.endsWith("bar"));

    break;case 7:

    System.out.print("Enter String 1 : ");c1=din.readLine();System.out.print("Enter String 2 : ");c2=din.readLine();int no=c1.compareTo(c2);if(no==0){System.out.println(c1+" is equal to "+c2+" -> true");}else{System.out.println(c1+" is equal to "+c2+" -> false");}

    break;case 8:

  • 8/7/2019 Java Outputs

    23/44

    c1="meneging";System.out.println("Encrpted String is : "+c1);System.out.println("Key : Replace 'e' by 'a'");c2=c1.replace('e','a');System.out.println("Decrypted String is : "+c2);

    break;case 9:c1=" Jaic ";System.out.println("String :"+c1);System.out.print("Trimmed String :"+c1.trim());

    break;case 10:

    System.out.print("Enter Your name : ");c1=din.readLine();String upper=c1.toUpperCase();String lower=c1.toLowerCase();

    System.out.println("UpperCase : "+upper);System.out.println("LowerCase : "+lower); break;

    default:System.out.println(" Invalid choice ...");

    }}catch(IOException e){

    System.out.println(" Error !!!");}}

    }

  • 8/7/2019 Java Outputs

    24/44

    Output:

    U:\ java String handling1.stringconstructors2. String Length

    3.String Concatenation4. Character Extraction

    String Comparison5. Equal or Not6. starts with and ends with7. compareTo

    Modifying a String8. replace9. trim10. Change Case

    Enter the choice : 1String cd = JAVA_USING_OOPSString of start index :4 and characters 5 : USINGThe Character representation of ascii 74,65,73,67 is: JAIC

    Enter the choice : 2Enter String : book String Length of book is : 4

  • 8/7/2019 Java Outputs

    25/44

    7.Exception handling

    UserDefinedException :

    import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;class MyException extends Exception{

    MyException(){} public String toString(){return "Exception : Number should be greater than Zero";}

    } public class UserException{

    public static void main(String[] args){try {

    BufferedReader br = new BufferedReader(newInputStreamReader(System.in));System.out.println("Enter number");int n = Integer.parseInt(br.readLine());check(n);

    }catch (MyException e) {

    System.out.println(e);}catch (IOException e) {System.out.println(e);}

    }static int factorial(int no){

    if(no==1){

    return 1;}else{

    return(no*factorial(no-1));}

    }static void check(int no)throws MyException{if(no>0){int fact=0;

  • 8/7/2019 Java Outputs

    26/44

  • 8/7/2019 Java Outputs

    27/44

    Output:

    i)U:\ java UserExceptionEnter the number:6Factorial of 6 is:720

    ii)U:\ java UserExceptionEnter the number:-6Exception: number should be greater than zero.

  • 8/7/2019 Java Outputs

    28/44

    8.BIO-DATA

    import java.awt.*;import java.awt.event.*;import java.applet.*;import java.lang.Exception.*;/* */

    public class BioDataForm extends Applet implements ActionListener {

    Label l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,l11;TextArea ta;TextField t1,t2,t3,t4,t5,t6;Checkbox c1,c2,c3,c4,c5,c6,c7,c8;CheckboxGroup cbg1,cbg2;Button b1,b2;

    public void init(){

    try{

    setLayout(null);setBackground(Color.pink);l1=new Label("Bio-Data");l2=new Label("Name");l3=new Label("Age");l4=new Label("Sex");l5=new Label("Date Of Birth");l6=new Label("Marital Status");l7=new Label("Nationality");l8=new Label("Phone Number");l9=new Label("Qualification");l10=new Label("Updated Data");l11=new Label("( dd/mm/yyyy ) ");

    b1=new Button("OK"); b2=new Button("Cancel");t1=new TextField(30);t2=new TextField(3);t3=new TextField(10);t4=new TextField(10);t5=new TextField(10);t6=new TextField(10);cbg1=new CheckboxGroup();cbg2=new CheckboxGroup();c1=new Checkbox("Male",cbg1,false);c2=new Checkbox("Female",cbg1,false);c3=new Checkbox("Bachelor",cbg2,false);

  • 8/7/2019 Java Outputs

    29/44

    c4=new Checkbox("Spinster",cbg2,false);c5=new Checkbox("Married",cbg2,false);c6=new Checkbox("Divorced",cbg2,false);c7=new Checkbox("Widower",cbg2,false);c8=new Checkbox("Widow",cbg2,false);

    ta=new TextArea();add(l1);add(l2);add(l3);add(l4);add(l5);add(l6);add(l7);add(l8);add(l9);add(l10);

    add(l11);add(c1);add(c2);add(c3);add(c4);add(c5);add(c6);add(c7);add(c8);add(t1);add(t2);add(t3);add(t4);add(t5);add(t6);add(b1);add(b2);add(ta);t1.setBounds(190,80,110,20);

    t2.setBounds(190,110,110,20);t3.setBounds(190,170,110,20);t4.setBounds(190,230,110,20);t5.setBounds(190,260,110,20);t6.setBounds(190,290,110,20);l1.setBounds(420,40,110,20);l2.setBounds(60,80,110,20);l3.setBounds(60,110,110,20);l4.setBounds(60,140,110,20);l5.setBounds(60,170,110,20);l6.setBounds(60,200,110,20);

  • 8/7/2019 Java Outputs

    30/44

  • 8/7/2019 Java Outputs

    31/44

    ta.append(" Marital Status : "+c3.getLabel());}if(c4.getState()){

    ta.append(" Marital Status : "+c4.getLabel());

    }if(c5.getState()){

    ta.append(" Marital Status : "+c5.getLabel());}if(c6.getState()){

    ta.append(" Marital Status : "+c6.getLabel());}if(c7.getState()){

    ta.append(" Marital Status : "+c7.getLabel());}if(c8.getState()){

    ta.append(" Marital Status : "+c8.getLabel());}if(ae.getSource()==b1){

    ta.append(" Nationality : "+t4.getText());ta.append(" Phone Number : "+t5.getText());ta.append(" Qualification : "+t6.getText());

    }if(ae.getSource()==b2){

    ta.setVisible(false);setBackground(Color.red);

    }}}

  • 8/7/2019 Java Outputs

    32/44

    OUTPUT :

  • 8/7/2019 Java Outputs

    33/44

  • 8/7/2019 Java Outputs

    34/44

  • 8/7/2019 Java Outputs

    35/44

    9.Moving Balls

    /**/import java.awt.*;

    import java.applet.*;import java.lang.Thread;import java.lang.*;

    public class Moving_ball extends Applet{

    Thread t;int i;int x=34,y=14;

    public void init(){

    t=new Thread();

    } public void paint(Graphics g){

    for(i=1;i

  • 8/7/2019 Java Outputs

    36/44

    Output :

    U:\>javac Moving_ball.java

    U:\>appletviewer Moving_ball.java

  • 8/7/2019 Java Outputs

    37/44

    10 .Calculator

    //

    import java.awt.*;import java.awt.event.*;

    public class Calcul extends java.applet.Applet implements ActionListener {TextField txtTotal = new TextField("");

    Button button[] = new Button[10];

    Button divide = new Button("/");Button mult = new Button("*");

    Button plus = new Button ("+");Button minus = new Button("-");Button isequalto = new Button("=");Button clear = new Button("C");double num ,numtemp ;int counter;String strnum = "",strnumtemp = "" ;String op = "";

    public void operation() {counter ++;

    if (counter == 1) {numtemp = num;strnum = "";num = 0;

    }else{if (op == "+") numtemp += num;else if (op == "-") numtemp -= num;else if (op == "*") numtemp = numtemp * num;else if (op == "/") numtemp = numtemp / num;strnumtemp = Double.toString(numtemp);txtTotal.setText(strnumtemp);

    strnum = "";num = 0;}

    }

    public void init() {setLayout(null);plus.setForeground(Color.black);

  • 8/7/2019 Java Outputs

    38/44

    minus.setForeground(Color.black);divide.setForeground(Color.black);

    isequalto.setForeground(Color.black);mult.setForeground(Color.black);clear.setForeground(Color.black);

    for(int i = 0;i

  • 8/7/2019 Java Outputs

    39/44

    divide.addActionListener(this);mult.addActionListener(this);isequalto.addActionListener(this);clear.addActionListener(this);}

    public void stop() {for(int i = 0;i

  • 8/7/2019 Java Outputs

    40/44

    operation();op = "*";

    }

    if (e.getSource() == isequalto) {

    if (op == "+") numtemp += num;else if (op == "-") numtemp -= num;else if (op == "*") numtemp = numtemp * num;else if (op == "/") numtemp = numtemp / num;strnumtemp = Double.toString(numtemp);txtTotal.setText(strnumtemp);strnumtemp = "";numtemp = 0;strnum = "";num = 0;counter = 0;

    }if (e.getSource() == clear) {txtTotal.setText("0");strnumtemp = "";numtemp = 0;strnum = "";num = 0;counter = 0;

    }}

    }

  • 8/7/2019 Java Outputs

    41/44

  • 8/7/2019 Java Outputs

    42/44

  • 8/7/2019 Java Outputs

    43/44

  • 8/7/2019 Java Outputs

    44/44


Recommended