You are on page 1of 7

Factorial import java.util.

Scanner; public class Factorial { public static void main (String[ ] args) { System.out.println("Enter the number for " + "factorial calculations : "); Scanner scanner = new Scanner(; int a = scanner.nextInt(); int result = fact(a); System.out.println("Factorial of " +a+ " is :"+result); } static int fact(int b){ if(b <= 1) return 1; else return b * fact(b-1); }


Fibonacci Series Program Code:import java.util.Scanner; public class Fibonacci { public static void main (String[ ] args) { System.out.println("Enter the number of terms you want from fibonacci series : "); Scanner scanner = new Scanner(; int i = scanner.nextInt(); fiboPrint(i); } public static final void fiboPrint(long n){ int f0 = 0; int f1 = 1; for (int i = 0; i < n; ++i) { System.out.println(f0 + " "); final int temp = f1; f1= f1+ f0;

awt.. import java. // create a TextField add(tf).Applet. it does not have to be set explicitly here. /* Class illustrates simple Flow Layout of three components.*. // this. Example 3 .applet. See it here. See it here.The html page is narrower than the first example. // create a Button add(b). Layout Examples This page contains simple example applets that demonstrate Layout Managers. */ public class Flow1 extends Applet { public void init() { // if you wanted to explicitly set the Flow Layout Manager you would type // setLayout ( new FlowLayout() ). Since Flow Layout is the default one for Applets (and Panels).} } } f0 = temp.setLayout ( new FlowLayout() ). these three components will flow from left to right in one row. See it here. ..If the width of the html page is wide enough. // create a Label add(label). // and add it } } Example 1 . or.Even narrower. // add it TextField tf = new TextField("Java"). Label label = new Label("Name:"). Flow Layout import java. // add it Button b = new Button("OK"). Example 2 .

row < 5.*. public class Flow2 extends Applet { public void init() { setLayout(new FlowLayout(FlowLayout.awt.This flow example changes the default alignment used when adding the components. row ++) { add (new Label("Label " + row)). Border Layout . // 5 rows. It uses right alignment. import java.applet. And here is the same applet in a narrower page. 3 columns.Applet.awt. import java. for (int i = 0. add (new Button("Button " + row)).Applet. i < 4.Example 4 . Grid Layout import java. add (new TextField("TextField " + row)). no gaps for (int row = 0. } } } Example 1 Example 2 is a narrower page with the same applet.*. } } Here is a html page with this applet.RIGHT)). Example 3 shows what it looks like if we ask for horizontal and vertical gaps betwwen components. import java.applet. public class Grid extends Applet { public void init() { // we must explicitly set GridLayout as the manager setLayout (new GridLayout(5. 3)). i++) add(new Button("Button #" + i)).

Label. /* This applet demonstrates using the CardLayout manager. // panel to hold three buttons Button first. import java. .yellow). third. except a blue panel is added to the Center region.awt.EAST). Card Layout import java.*. } } See this one here. add(l. West.setBackground(Color.awt. l. // the container that will hold the various "cards" Panel firstP. public class Border1 extends Applet { public void init() { setLayout (new BorderLayout()). second. // the card layout object public void init() { //create cardPanel which is the panel that will contain the three "cards" cardPanel = new Panel().First example puts components at North. Example 2 puts components South.*. */ public class Card1 extends Applet implements ActionListener { Panel cardPanel. // each of these panels will constitute the "cards" Panel buttonP. and West.CENTER). BorderLayout.Applet. secondP. Scrollbar sb2 = new Scrollbar(). BorderLayout. Pressing one of three buttons will cause a different "card" to be displayed. // the three buttons CardLayout ourLayout. West. Example 4 is like example 3.applet.event. Label l = new Label("This is NORTH". import java.Applet. BorderLayout. East. South.WEST). and East. import java. import java. Scrollbar sb1 = new Scrollbar(). and East Example 3 puts components North. add(sb1.applet.awt.*. add(sb2. thirdP.NORTH).

thirdP = new Panel(). BorderLayout. buttonP. . //green } //-----------------------------------// respond to Button clicks by showing the so named Panel // note use of the CardLayout method show(Container. buttonP. // add the three card panels to the card panel container // method takes 1.setLayout (ourLayout).) an Object (the card) 2. "Second").add(third). //create three buttons and add ActionListener first = new Button("First"). // Panel's default Layout manager is FlowLayout buttonP. "Second").blue). if (e.getSource() == second) ourLayout. second. //set card Panel's layout to be our Card Layout cardPanel.add(secondP. //blue cardPanel.addActionListener(this).addActionListener(this).show(cardPanel.add(second).setBackground(Color.setBackground(Color. second = new Button("Second"). this.//create the CardLayout object ourLayout = new CardLayout().add(thirdP. BorderLayout. "Third"). "First"). //create three dummy panels (the "cards") to show firstP = new Panel().add(first). //yellow cardPanel. thirdP. third = new Button("Third").add(buttonP.setLayout(new BorderLayout()). "First"). secondP = new Panel().getSource() == first) ourLayout.setBackground(Color.add(cardPanel.add(firstP.CENTER). "identifying string") public void actionPerformed(ActionEvent e) { if (e. secondP. third. //button Panel goes South.yellow). //create Panel for the buttons and add the buttons to it buttonP = new Panel(). card panels go Center this. first.addActionListener(this). //setLayout for applet to be BorderLayout this.) an identifying String // first one added is the visible one when applet appears firstP.SOUTH).

add(two).} } // end class if (e. nPanel. // give it color so you can see it one = new Button("One"). inside of init. lbl3. The applet is the main container and will use a BorderLayout. three = new Button("Three"). Do not redeclare such a component inside of init(). */ Panel nPanel. tcPanel.CENTER)). "Third"). and South. It would LOOK like you had a button three on the applet. bcPanel. three. // north panel will hold three button nPanel. public class Mixed extends Applet { /* This example demonstrates nesting of containers inside other containers. five. import java.awt. . Button three = new Button ("Three"). two = new Button("Two"). sPanel. It will use three Panels at North. nPanel.*. Each container can have its own Layout Manager. Mixed Layout import java. public void init() { nPanel = new Panel(). /* Here is a bad idea. lbl2.add(one). we can achieve finer degress of control over component placement. The Center panel itself will contain 2 other panels inside of it. but it would not generate any action events.getSource() == third) ourLayout.setBackground(Color. cPanel. lbl1. */ // setLayout for North Panel and add the buttons nPanel. six. Center.setLayout (new FlowLayout(FlowLayout. Label bottom. Each method in this class can have access to them. four. */ /* Note how all components are declared as class instance variables. If you declared here. See this applet here. Button one. the class instance Button three would be "lost".

yellow). sPanel = new Panel().add(bcPanel).CENTER).setBackground(Color.setBackground(Color.add(lbl1).NORTH).add(five). four = new Button("Four"). //set Layout and add Label sPanel.setLayout (new GridLayout(1. sPanel.add(six). // top panel on center panel holds three labels tcPanel. bcPanel.add(tcPanel). } } .setLayout (new BorderLayout()). 5)).setLayout (new GridLayout(3. 5.add(lbl2). lbl3 = new Label("Three").add(lbl3). cPanel. six = new Button("Six").gray). no gaps cPanel.setLayout(new GridLayout(2. tcPanel. // bottom panel on center panel hold three buttons bcPanel. 1 col. add(cPanel. 5. BorderLayout.SOUTH). tcPanel. 1)). // the labels lbl2 = new Label("Two"). 1.nPanel. 3. //set Layout for the Applet itself and add the panels this. add(nPanel. // give it color so you can see it lbl1 = new Label("One"). // center panel holds two other panels tcPanel = new Panel(). add(sPanel.add (bottom). // give it color so you can see it bcPanel = new Panel().green). // the buttons five = new Button("Five"). //add two center panels (top and bottom) to the center panel cPanel.add(three).add(four). 3 columns. //set Layout for bottom center panel and add buttons bcPanel.setBackground(Color. // give it color so you can see it bottom = new Label("This is South"). BorderLayout. //set Layout for top center Panel and add labels tcPanel. BorderLayout. 5)). bcPanel. // south Panel will just hold one Label sPanel. // 2 rows. 5 pixel gap tcPanel. // 3 rows.CENTER)). 1 col. // 1 row.setLayout (new FlowLayout(FlowLayout. 5 pixel gap bcPanel. cPanel = new Panel().