Documente Academic
Documente Profesional
Documente Cultură
Program
1. To find factorial of a given number.
2. To demo Friend Function.
3. To demo for enumerated data to find sum, difference, product, & division of 2 numbers.
4. To add 2 complex numbers using Operator Overloading.
5. To find sum of 2 numbers of type integer & double using function Overloading.
6. To maintain candidate information.
7. To calculate simple & compound interest.
8. To show object as argument
9. To calculate the area & circumference of the circle using constructor.
10. To calculate the square of a given number using inline function.
11. To illustrate single inheritance in public mode.
12. To illustrate object created and destroyed.
13. To illustrate multiple inheritance to find product of 2 numbers.
14. To demo an object of class type & initialization of its members using overloaded
function.
15. To overload volume ( ) function using function overloading.
16. To find maximum data item of similar data type using function template.
17. To illustrate the definition & declaration of a class template for accepting 2 numbers &
find their sum.
18. To illustrate virtual function & object pointer using runtime polymorphism.
19. To illustrate set type, set position, set precision & set fill function.
20. To illustrate a disc file for reading its counts.
Write a C++ Program to demo for enumerated data to find sum, difference, product, & division
of 2 numbers.
Write a C++ Program to illustrate object created and destroyed.
JAVA ASSIGNMENTS
Program for counting no. of Chars, Words and Lines in a file
import java.lang.*;
import java.io.*;
import java.util.*;
class WordCount
{
public static void main(String arg[]) throws Exception
{
int char_count=0;
int word_count=0;
int line_count=0;
String s;
StringTokenizer st;
BufferedReader buf=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("Enter filename : ");
s=buf.readLine();
buf=new BufferedReader(new FileReader(s));
while((s=buf.readLine())!=null)
{
line_count++;
st=new StringTokenizer(s," ,;:.");
while(st.hasMoreTokens())
{
word_count++;
s=st.nextToken();
char_count+=s.length();
}
}
System.out.println("Character Count : "+char_count);
System.out.println("Word Count : "+word_count);
}
}
Fibonacci numbers (Right side)
import java.text.*;
public class TestRight {
public static void main(String args[]) {
long f1 = 1;
long f2 = 1;
RightFormat rf = new RightFormat(20);
System.out.println("Test of RightFormat(20) on Fibonacci numbers:");
for(int ix = 0; ix < 32; ix++) {
System.out.println(rf.format(Long.toString(f1)));
System.out.println(rf.format(Long.toString(f2)));
f1 = f1 + f2;
f2 = f2 + f1;
}
}
}
Demonstrating the runnable interface
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class RandomCharacters extends JApplet implements Runnable, ActionListener
{
private String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private JLabel outputs[];
private JCheckBox checkboxes[];
private final static int SIZE = 3;
private Thread threads[];
private boolean suspended[];
public void init()
{
outputs = new JLabel[ SIZE ];
checkboxes = new JCheckBox[ SIZE ];
threads = new Thread[ SIZE ];
}
}
catch ( InterruptedException e )
{
System.err.println( "sleep interrupted" );
}
displayChar = alphabet.charAt( (int) ( Math.random() * 26 ) );
outputs[ index ].setText( currentThread.getName() +
": " + displayChar );
}
System.err.println( currentThread.getName() + " terminating" );
}
private int getIndex( Thread current )
{
for ( int i = 0; i < threads.length; i++ )
if ( current == threads[ i ] )
return i;
return -1;
}
public synchronized void stop()
{
// stop threads every time stop is called
// as the user browses another Web page
for ( int i = 0; i < threads.length; i++ )
threads[ i ] = null;
notifyAll();
}
public synchronized void actionPerformed( ActionEvent e )
{
for ( int i = 0; i < checkboxes.length; i++ )
{
if ( e.getSource() == checkboxes[ i ] )
{
suspended[ i ] = !suspended[ i ];
outputs[ i ].setBackground( !suspended[ i ] ? Color.green :
Color.red );
if ( !suspended[ i ] )
notify();
return;
}
}
}
}