Powered by Blogger.

Java | Write a Fibonacci Series Program in Java

Fibonacci Program:-

Question-1: Write a Java program that will display the Fibonacci sequence is generated by adding the previous two terms. By starting with 1, 1, and 2, and continue up to the first 10 terms.

Program Solution:

public class Fibonacci{
  public static void main(String[]args){
  int x=1, y=1, z=0;
  for(int i =1; i<=10;i++){
  if(i==1){
  System.out.print(x+” ”+y);
  }
  z=x+y;
  System.out.print(” ”+z);
  x=y;
  y=z;
    }
  }
}

Write a Java program that loops 1-50 Series Program | About Java

Question-1: Write a Java program that loops 1-50 and print foo for every multiple of 3, bar for every multiple of 5 and baz for every multiple of 3 and 5.

Series Program:

 public class Series{
  public static void main(String[]args){
  for(int i=1;i<=50;i++){
  System.out.print(i);
  if(i%3==0 && i%5==0)
  System.out.print(”−baz”);
  else if(i%3==0)
  System.out.print(”−foo”);
  else if(i%5==0)
  System.out.print(”−bar”);
  System.out.println();
    }
  }
 }

/** and /* in Java Commenting Code | How to give comments in Java?

Java programs can have two kinds of comments. implementation comments and documentation comments. Implementation comments are those found in C++, which are delimited by /*...*/, and //. Documentation comments (known as "doc comments") are Java-only, and are delimited by /**...*/. Doc comments can be extracted to HTML files using the javadoc tools.


In Java Technology, we can add comments to Java source code in three ways which are given bellow:

1 public class TestComment{
2 // comment on one line
3 public static void main (String [ ] args){
4 /* comment on one
5 * or more lines
6 */
7 System.out.println(”Comments in Java”);
8 }
9 /** documentation comment
10 * can also span one or more lines
11 */
12 }

Java Source File | Source File Layout Program

A Java technology source file declares firstly package statement (only one package), secondly import statements, and finally classes. Which are following bellow:

1. package <topPackageName> .[< subP ackageName >];
2. import<packageName> .[subP ackageName]*;
3. <classDeclaration>*

The package statement is a way to group related classes. The package statement place at beginning of the source file and only one package statement declaration is permitted. If a Java technology source file does not contain a package declaration then the classes declared in the file belong to the default package. The package names are hierarchical and are separated by dots. The package name to be entirely lower case.

           The import statement is used to make classes in other packages accessible to the current class. Normally, the Java compiler places the .class files in the same directory as the source file present. We can reroute the class file to another directory using the -d option of the javac command (javac -d. ClassName.java).

1 package bankingPrj;
2
3 public class Account{
4 public String accountName = ”Saving Account”;
5
6 public void showAccName( ){
7 System.out.println(accountName);
8 }
9
10 public static void main (String [ ] args ){
11 Account acc = new Account ( );
12 acc.showAccName( );
13 }
14 }

To compile this code, open a command window or terminal and change to the directory where the program is saved and type javac -d. Account.java then a package or folder will be create in the same directory named bankingPrj. Now you can run this Account.class file in bankingPrj package by typing the command java bankingPrj.Account;

1 package bankingPrj;
2
3 import bankingPrj.Account;
4
5 public class Coustomer{
6 public String name = ”James Bond”;
7
8 public static void main (String [ ] args ){
9 Account acc = new Account ( );
10 acc.showAccName ( );
11 Coustomer cous = new Coustomer ( );
12 System.out.println (cous.name);
13 }
14 }

Similarly, we can compile and run this code.

Line 3 we import the Account class from bankingPrj package.

Line 9 we create object of Account class.

Line 10 we call the method of Account class.

What Is an Object?

Object: Instance of class are called object.

Syntax of Object:

public class Tom {
   public Tom(String name) {
      // This constructor has one parameter, name.
      System.out.println("Passed Name is :" + name );
   }

   public static void main(String []args) {
      // Following statement would create an object myTom
      Puppy myTom = new Tom( "nazrul" );
   }
}


Output: 
Passed Name is : nazrul

What is a Class?

Class: Class is a collection of property and method.

Syntax of Class: 

class class_name{
         field;
         method;
}

Sockets or Networking in Java

Socket is the Java programming model to establish the communication link between two processes. A socket can hold input and output stream. A process sends data to another process through the network by writing to the output stream associated with the socket. A process reads data written the another process by reading from the input stream associated with the socket.

Code 5−7: The HostServer.java application

import java.net.*;
import java.io.*;

public class HostServer{
  public static void main (String [ ] args){
    ServerSocket s = null;
    try{
      s = new ServerSocket(5432);
      Socket s1 = s.accept();
      OutputStream s1out = s1.getOutputStream ();
      BufferedWriter bw = new BufferedWriter(new OutputStreamWriter (s1out 12 bw.write (”Hellow Net World”);
      bw.close();
      s1.close();
    }catch (IOException e){
      e.printStackTrac e ();
    }
  }
}

import java.net.*;
import java.io.*;

public class ClientServer{
  public static void main (String [ ] args){
    try{
       Socket s1 = new Socket(”127.0.0.1”, 5432);
       InputStream is = s1.get InputSt ream();
       DataInputStream dis = new DataInputStream(is);
       System.out.println(dis.readUTF());
       dis.close();
       s1.close();
    }catch (ConnectException e1){
       System.out.println(e1);
    }catch(IOException e2){
       e2.printStackTrace();
    }
  }
}