import java.awt.*; import java.applet.*; /* <applet code="sim" width=300 height=300> </applet> */ public class sim extends Applet { String msg=" "; public void init() { msg+="init()--->"; setBackground(Color.orange); } public void start() { msg+="start()--->"; setForeground(Color.blue); } public void paint(Graphics g) { msg+="paint()--->"; g.drawString(msg,200,50); } }
WOW CODE BY TECHVIKINDIA
Friday 13 November 2015
Java applet program that displays a simple message
Friday 30 October 2015
Write a program to implement all string operations.
import java.util.*;
class StringOperation
{
public static void main(String[] args)
{
String first="",second="";
Scanner sc=new Scanner(System.in);
System.out.println("String Operation");
System.out.println();
System.out.print("Enter the first Sting: ");
first=sc.nextLine();
System.out.print("Enter the second Sting: ");
second=sc.nextLine();
System.out.println("The strings are: "+first+" , "+second);
System.out.println("The length of the first string is :"+first.length());
System.out.println("The length of the second string is :"+second.length());
System.out.println("The concatenation of first and second string is :"+first.concat(" "+second));
System.out.println("The first character of " +first+" is: "+first.charAt(0));
System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
System.out.print("Enter the occurance of a character in "+first+" : ");
String str=sc.next();
char c=str.charAt(0);
System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first);
System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7));
System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));
boolean check=first.equals(second);
if(!check)
System.out.println(first + " and " + second + " are not same.");
else
System.out.println(first + " and " + second + " are same.");
}
}
class StringOperation
{
public static void main(String[] args)
{
String first="",second="";
Scanner sc=new Scanner(System.in);
System.out.println("String Operation");
System.out.println();
System.out.print("Enter the first Sting: ");
first=sc.nextLine();
System.out.print("Enter the second Sting: ");
second=sc.nextLine();
System.out.println("The strings are: "+first+" , "+second);
System.out.println("The length of the first string is :"+first.length());
System.out.println("The length of the second string is :"+second.length());
System.out.println("The concatenation of first and second string is :"+first.concat(" "+second));
System.out.println("The first character of " +first+" is: "+first.charAt(0));
System.out.println("The uppercase of " +first+" is: "+first.toUpperCase());
System.out.println("The lowercase of " +first+" is: "+first.toLowerCase());
System.out.print("Enter the occurance of a character in "+first+" : ");
String str=sc.next();
char c=str.charAt(0);
System.out.println("The "+c+" occurs at position " + first.indexOf(c)+ " in " + first);
System.out.println("The substring of "+first+" starting from index 3 and ending at 6 is: " + first.substring(3,7));
System.out.println("Replacing 'a' with 'o' in "+first+" is: "+first.replace('a','o'));
boolean check=first.equals(second);
if(!check)
System.out.println(first + " and " + second + " are not same.");
else
System.out.println(first + " and " + second + " are same.");
}
}
Saturday 24 October 2015
Write an applet program to design a simple calculator in java
Java applet program for calculator
import java.applet.*;import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;;
public class calculator extends Applet implements ActionListener, TextListener
{
String s,s1,s2,s3,s4;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
Button add,sub,eq,cl,mul,div;
TextField t1;
int a,b,c;
public void init()
{
t1=new TextField(10);
b1=new Button("1");
b2=new Button("2");
b3=new Button("3");
b4=new Button("4");
b5=new Button("5");
b6=new Button("6");
b7=new Button("7");
b8=new Button("8");
b9=new Button("9");
b0=new Button("0");
add=new Button("+");
sub=new Button("-");
mul=new Button("*");
div=new Button("/");
eq=new Button("=");
cl=new Button("Clear");
GridLayout gb=new GridLayout(4,5);
setLayout(gb);
add(t1);
add(b1);
add(b2);
add(b3);
add(b4);
add(b5);
add(b6);
add(b7);
add(b8);
add(b9);
add(b0);
add(add);
add(sub);
add(mul);
add(div);
add(eq);
add(cl);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
eq.addActionListener(this);
cl.addActionListener(this);
paint();
//t1.addTextListener(this);
}
public void paint()
{
setBackground(Color.green);
}
public void actionPerformed(ActionEvent e)
{
s=e.getActionCommand();
if(s.equals("0")||s.equals("1")||s.equals("2")||
s.equals("3")||s.equals("4")||s.equals("5")||s.equals("6")||s.equals("7")||s.equals("8")||
s.equals("9")||s.equals("0"))
{
s1=t1.getText()+s;
t1.setText(s1);
}
if(s.equals("+"))
{
s2=t1.getText();
t1.setText("");
s3="+";
}
if(s.equals("-"))
{
s2=t1.getText();
t1.setText("");
s3="-";
}
if(s.equals("*"))
{
s2=t1.getText();
t1.setText("");
s3="*";
}
if(s.equals("*"))
{
s2=t1.getText();
t1.setText("");
s3="*";
}
if(s.equals("="))
{
s4=t1.getText();
a=Integer.parseInt(s2);
b=Integer.parseInt(s4);
if(s3.equals("+"))
c=a+b;
if(s3.equals("-"))
c=a-b;
t1.setText(String.valueOf(c));
}
if(s.equals("Clear"))
{
t1.setText("");
}
}
public void textValueChanged(TextEvent e)
{
}
}
OUTPUT:-
Monday 19 October 2015
Fibonacci Series using Recursion in Java
Fibonacci series in Java
Write a Java program to print Fibonacci series up to a given number or create simple Java program to calculate Fibonacci number is common Java questions on fresher interview and homework. Fibonacci series is also a popular topic on various programming exercises in school and colleges. Fibonacci series is series of natural number where next number is equivalent to sum of previous two number e.g. fn = fn-1 + fn-2. First two numbers of Fibonacci series is always 1, 1. In this Java program example for Fibonacci series we create function to calculate Fibonacci number and then print those numbers on Java console. Another twist in this questions is that some time interviewer ask to write Java program for Fibonacci numbers using recursion, so its better you prepare for both iterative and recursive version of Fibonacci number. One more coding question which is quite popular is printing prime numbers in Java which I have discussed earlier. In this tutorial we will see example of both recursive and iterative algorithm for Fibonacci series in Java.
After asking to write simple Java program to print Fibonacci series and later asking for Fibonacci series using recursion, another important question interviewer ask is how do you improve your Fibonacci function both iterative and recursive one? A technique called memorization can be used to drastically improve performance of method which calculates Fibonacci number. if you look at the method it repetitive creates same Fibonacci number e.g. In order to calculate 10th Fibonacci number function first create first 9 Fibonacci number, this could be very time consuming if you just increase the upper limit from 10 to 10K. In memorization programming technique result of earlier calculation is cached and reused. So you don't need to create same Fibonacci number if you already have calculated it. You can write code for Fibonacci series with memorization by just using a HashMap and checking if Fibonacci number for a corresponding number is already exits or not and calculate only if it doesn't exist.
Write a Java program to print Fibonacci series up to a given number or create simple Java program to calculate Fibonacci number is common Java questions on fresher interview and homework. Fibonacci series is also a popular topic on various programming exercises in school and colleges. Fibonacci series is series of natural number where next number is equivalent to sum of previous two number e.g. fn = fn-1 + fn-2. First two numbers of Fibonacci series is always 1, 1. In this Java program example for Fibonacci series we create function to calculate Fibonacci number and then print those numbers on Java console. Another twist in this questions is that some time interviewer ask to write Java program for Fibonacci numbers using recursion, so its better you prepare for both iterative and recursive version of Fibonacci number. One more coding question which is quite popular is printing prime numbers in Java which I have discussed earlier. In this tutorial we will see example of both recursive and iterative algorithm for Fibonacci series in Java.
Here is
complete code example of printing Fibonacci Series in Java. Fibonacci series is
calculated using both Iterative and recursive method and written in Java
programming language. We have two function in this example, fibonacci(int number)
and fibonacci2(int number). First one print Fibonacci series using recursion
and second one using for loop. You can test this code on your computer as well.
One you create your Java source file, just compile and run. It will ask you to
enter the number till which you want to see the series. Once you enter then
number, it will print the Fibonacci series in console.
import java.util.Scanner;
/**
* Java program to
calculate and print Fibonacci number using both recursion
* and Iteration.
* Fibonacci number is sum
of previous two Fibonacci numbers fn= fn-1+ fn-2
* first 10 Fibonacci
numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
*
* @author Javin
*/
public class FibonacciCalculator
{
public static void main(String args[]) {
//input to print Fibonacci series upto how many numbers
System.out.println("Enter number upto which Fibonacci series to
print: ");
int number = new Scanner(System.in).nextInt();
System.out.println("Fibonacci series upto " + number +" numbers : ");
//printing Fibonacci series upto number
for(int i=1; i<=number; i++){
System.out.print(fibonacci2(i) +" ");
}
}
/*
* Java program for
Fibonacci number using recursion.
* This program uses
tail recursion to calculate Fibonacci number for a given number
* @return Fibonacci
number
*/
public static int fibonacci(int number){
if(number == 1 || number == 2){
return 1;
}
return fibonacci(number-1) + fibonacci(number -2); //tail recursion
}
/*
* Java program to
calculate Fibonacci number using loop or Iteration.
* @return Fibonacci
number
*/
public static int fibonacci2(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){
//Fibonacci number is sum of previous two Fibonacci number
fibonacci =
fibo1 + fibo2;
fibo1 = fibo2;
fibo2 =
fibonacci;
}
return fibonacci; //Fibonacci number
}
}
Output:
Enter number upto which Fibonacci series to print:
12
Fibonacci series upto 12 numbers :
1 1 2 3 5 8 13 21 34 55 89 144
After asking to write simple Java program to print Fibonacci series and later asking for Fibonacci series using recursion, another important question interviewer ask is how do you improve your Fibonacci function both iterative and recursive one? A technique called memorization can be used to drastically improve performance of method which calculates Fibonacci number. if you look at the method it repetitive creates same Fibonacci number e.g. In order to calculate 10th Fibonacci number function first create first 9 Fibonacci number, this could be very time consuming if you just increase the upper limit from 10 to 10K. In memorization programming technique result of earlier calculation is cached and reused. So you don't need to create same Fibonacci number if you already have calculated it. You can write code for Fibonacci series with memorization by just using a HashMap and checking if Fibonacci number for a corresponding number is already exits or not and calculate only if it doesn't exist.
Here is
the code example of printing Fibonacci number with memoization technique :
/*
* Java Program to
calculate Fibonacci numbers with memorization
* This is quite fast
as compared to previous Fibonacci function
* especially for
calculating factorial of large numbers.
*/
public static int improvedFibo(int number){
Integer fibonacci
= cache.get(number);
if(fibonacci != null){
return fibonacci; //fibonacci number from cache
}
//fibonacci number not in cache, calculating it
fibonacci =
fibonacci2(number);
//putting
fibonacci number in cache for future request
cache.put(number,
fibonacci);
return fibonacci;
}
//comparison of performance of Fibonacci number with
memorization
int number = 100000000;
long startTime = System.nanoTime();
int result = fibonacci2(number); //fibonacci number
with memorization
long elapsedTime = System.nanoTime() - startTime;
System.out.println("Time taken to calculate Fibonacci number upto 100M without
memorization:" + elapsedTime);
startTime = System.nanoTime();
result = improvedFibo(number); //Fibonacci number
with memorization
elapsedTime = System.nanoTime() - startTime;
System.out.println("Time taken to calculate Fibonacci number upto 100M with
memorization:" + elapsedTime);
Output:
Time taken to calculate Fibonacci number upto 100M without memorization:149479613
Time taken to calculate Fibonacci number upto 100M with memorization:118972384
Subscribe to:
Posts (Atom)