implement getline

Wednesday, April 29, 2015

#include<iostream>
using namespace std;
int main()
{
    char name[10];
    cout<<"\nEnter your name: ";
    cin.getline(name,20);
    cout<<"\nYour name is ";
    cout.write(name,20);


}

Sum of digits



Version-I


import java.util.Scanner;
class Sum
{
public static void main(String []args)
{
int n,sum=0,sum1=0;
Scanner kb=new Scanner(System.in);
System.out.println("Enter a number");
n=kb.nextInt();
while(n!=0)
{
int r=n%10;
sum=sum+r;
n=n/10;
while(sum>9)
{
int x;
x=sum%10;
sum1=sum1+x;
sum=sum/10;
}
}

System.out.println("Single digit result is:  "+(sum1+sum));
}
}


Version-II

import java.util.Scanner;
class Sum
{
public static void main(String []args)
{
      int n,rem,sum = 0;
Scanner kb=new Scanner(System.in);
      System.out.println("Enter a number: ");
      n=kb.nextInt();
      while(n > 0)
      {
                 while(n != 0)
                 {
                      rem = n%10;
                      sum = sum+rem;
                      n=n/10;
                 }
                 if(sum > 9)
                 {
                        n = sum;
                        sum = 0;
                 }
                  
      }
      System.out.println("Single digit result is: "+sum);
}

}

Java applet program to demonstrate Anonymous class and Nesting of class

Wednesday, April 15, 2015





import java.awt.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class MyApplet7 extends Applet
{
    public void init()
    {

        this.addMouseListener(new MouseAdapter()
        {       //automatically created anonymous inner class
                public void mouseClicked(MouseEvent e)
                {
                    int x=e.getX();
                    int y=e.getY();
                    showStatus("You Clicked at "+x+","+y);
                }   //method close
        });   //anonymous class close
    }   //init close

}

/*
Note: In this program two bytecode is created
(1) MyApplet7.class
(2) MyApplet7$1.class
Second byte code indicate existance of anonymous class

*/




HTML CODE(Standard Structure):

<html>
<head>
<title>My anonymous program</title>
</head>
<body>
<applet
code="MyApplet7.class"
width="400"
height="400">
</applet>
</body>
</html>

Java GUI program to develop a frame of 400*400 size with yellow background and text displayed at the centre




import java.awt.*;
import java.awt.event.*;
class MyFrame1 extends Frame
{
public MyFrame1(String title)
{
super(title); /*to access the constructor 
of frame class*/
this.setSize(400,400); //set size of frame
this.setBackground(Color.yellow); //set colour of frame
this.setVisible(true);
MyWindowAdapter obj=new MyWindowAdapter();
this.addWindowListener(obj);

}
public void paint(Graphics g)
{
Font f=new Font("Arial",Font.BOLD,24);
g.setFont(f);
g.drawString("Good Evening",120,200);
}
}
class Testf
{
public static void main(String[] args)
{
MyFrame1 fr=new MyFrame1("Sourabh Frame");
}
}
class MyWindowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e) //to close the frame
{
System.exit(0); //otherwise frame will not close
}
}