Programming Problems and Solutions in Java


Problem #1


Write a program in Java which accept 2 integer start & repeat and print the pattern such that starting and ending number of pattern is - start and total numbers or rows in the pattern is equal to (repeat*2). Number in the pattern should be increase by 1 in each rows till the middle of the rows and then decrease by 1 till end of the rows.

For example- If start = 4 and repeat = 4 then
  1. start = 4 -> Starting number of pattern,
  2. (repeat * 2) = (4*2)= 8 -> number of rows to print in the pattern

   4
   55
   666
   7777
   7777
   666
   55
   4

Possible Solution


 void printPattern(int start, int repeat) {
  int k = repeat;
  for (int i = 0; i < 2 * repeat; i++) { // loop 1 start
   for (int j = 0; (j <= i && j < k); j++) { // loop 2
    System.out.print(start);
   } // end loop 2
   if (i < repeat - 1) {
    start = start + 1;
   }
   if (i > repeat - 1) {
    start = start - 1;
    k = k - 1;
   }
   System.out.println();
  } // end loop 1
 }

Output:
 
Test 1: (Start = 5 , repeat = 6)

5
66
777
8888
99999
101010101010
101010101010
99999
8888
777
66
5


Test 2: (Start = 3 , repeat = 4)

3
44
555
6666
6666
555
44
3


Fibonacci Series

Fibonacci Series is a series of numbers in which each number is sum of the two preceding numbers. Number in Fibonacci Series is called Fibonacci number. First two number of series are 0 and 1.

For example: 
 0 , 1, 1, 2, 3, 5, 8 ......

So in above series - Third number i.e 1 = 0 + 1 (sum of two preceding number)
                                 Fourth number i.e 2 = 1+1 (sum of two preceding number)
                                 Fifth number i.e 3 = 1+2 (sum of two preceding number) 
and so on ....

How to create Fibonacci Series program in Java?

We can create Fibonacci series in Java using -
  1. Programming Loops (iteration)
  2. Using recursion function

Using Programming Loop:


Here is a sample program to print Fibonacci series in Java. This program will print first 10 number of Fibonacci series.


package com.techiepappu.java;

/**
 * This Program will print Fibonacci series using loop.
 * @author http://techiepappu.blogspot.com
 */
public class FibonacciSeries1 {

 public static void main(String args[]) {
  int a = 0, b = 1, c, i, max = 10;
  // printing first 2 number in series (i.e 0 and 1)
  System.out.print(a + ", " + b);
  // using java for loop for printing rest of numbers in series.
  for (i = 2; i < max; ++i) {
   c = a + b;
   System.out.print(", " + c);
   a = b;
   b = c;
  }
 }
}

Output:
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

Using recursion function:


Recursion function is a type of function which calls itself.Every recursion should have the following characteristics.
  1. A base case which have solution (base value) and end the recursion.
  2. A way of getting problem closer to the base case.
  3. A recursive call.


Give below is a sample program to print Fibonacci series in Java using recursion. This program will print Fibonacci series as per user input.

 package com.java.techiepappu;

import java.util.Scanner;
/**
 * A sample java program to calculate and print Fibonacci series using recursion.
 *
 * @author http://techiepappu.blogspot.com
 */
public class FibonacciSeries2 {

 public static void main(String[] args) {

  int num = 0;
  System.out.println("How many numbers you want to print from Fibonacci series?: ");
  //using scanner class to read user input.
  Scanner sc = new Scanner(System.in);
  int number = sc.nextInt();
  sc.close();
  System.out.println("Fibonacci Series:");
  while (number > num) {
   System.out.println(fibonacci(num));
   num++;
  }
 }

 /**
  * This is a recursive function which returns a number from fibonacci series.
  * @param n
  * @return nth number of fibonacci series.
  */
 public static int fibonacci(int n) {
  if (n == 0 || n == 1)
   return n;
  else
   return fibonacci(n - 1) + fibonacci(n - 2);
 }
}
Output:
 
 How many numbers you want to print from Fibonacci series?: 
10
Fibonacci Series:
0
1
1
2
3
5
8
13
21
34

Installing LAMP (Linux, Apache, MySQL and PHP) On Linux (Ubuntu, Linux Mint)


This blog post will teach you how to Install Apache, MySQL and PHP on your linux system. If you are a designer or developer and use PHP, mySQL and Apache, you'll be needing these applications to be installed.

Microsoft window user can get Apache,MySQL & PHP together in XAMMP software. For more detail information regarding XAMMP software click here.

If you are using linux and you are not advance user of linux and want to install LAMP (Linux, Apache, MySQL and PHP) then this blog post will be helpful for you.

As mentioned above LAMP stands for Linux, Apache, MySQL, PHP. Installing all these software in Linux and configured them to make them work is one of difficult task for beginners.

Install Apache

Apache is web server program developed and maintained by an open community of developers under Apache Software Foundation.

First we will install apache web server

Steps:


1. Open up the Terminal (Applications > Accessories > Terminal) or press ctrl+alt+T.

2. Copy/Paste or type the following command into Terminal and then press enter:

sudo apt-get install apache2

3. The Terminal will then ask you for you're password, type it and then press enter.

Testing Apache Server Installation

To make sure everything installed correctly we will now test Apache to ensure it is working properly.

1. Open up any web browser and then enter the following into the web address:


You should see a folder entitled apache2-default/. Open it and you will see a message saying "It works!" , congrats to you! or something like that!

Install PHP

After successfull installation of Apache we will install PHP 5.

Step 1. Open Terminal again if it is not opened already

Step 2. Copy/Paste or type the following line into Terminal and press enter:

sudo apt-get install php5 libapache2-mod-php5

Step 3. Once PHP is installed, we have to restart Apache server in order for PHP to work and be compatible with Apache. Type the following code in Terminal to restart Apache:

sudo /etc/init.d/apache2 restart

Test PHP

Let check whether php is installed corectly or not
Step 1. In the terminal copy/paste or type the following line to create a test web page

sudo gedit /var/www/phptest.php

This will open up a file called phptest.php.

Step 2. Copy/Paste this line into the phptest file:

<?php phpinfo(); ?>

Step 3. Save and close the file.
Step 4. Now open you're web browser and type the following into the web address:

http://localhost/phptest.php or http://127.0.1.1/phptest.php

It will show all information about your php.

If you are able so see information about your php then you have successfully install php and Apache. Now we will install MySQL.

Install MySQL

MySQL is a widely used open source database.
Following are steps to install MySQL in linux:
Step 1. Once again open Terminal and then copy/paste or type this line:

sudo apt-get install mysql-server

Step 2. For other computers on your network to view the server you have created, you must first edit the "Bind Address". Begin by opening up Terminal to edit the my.cnf file.

gksudo gedit /etc/mysql/my.cnf

Change the line

bind-address = 127.0.0.1

and change the 127.0.0.1 to your IP address.

Step 3. This is where things may start to get tricky. Begin by typing the following into Terminal:

mysql -u root

Following that copy/paste or type this line:

mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('yourpassword');

Step 4. We are now going to install a program called phpMyAdmin which is an easy tool to edit your databases. Copy/paste or type the following line into Terminal:

sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

After that is installed our next task is to get PHP to work with MySQL. To do this we will need to open a file entitled php.ini. To open it type the following:

gksudo gedit /etc/php5/apache2/php.ini

Now we have to uncomment the following line by removing the semicolon (;).

Change this line:

;extension=mysql.so
To :
extension=mysql.so

Now just restart Apache

sudo /etc/init.d/apache2 restart

Test phpmyadmin

Open web browser and enter http://localhost/phpmyadmin

If you get a 404 error then You need to configure apache2.conf to work with Phpmyadmin.

sudo gedit /etc/apache2/apache2.conf

Include the following line at the end of the file, save and quit.

include /etc/phpmyadmin/apache.conf

Now restart Apache server again to reflect changes

sudo /etc/init.d/apache2 restart

Congratulation!!! you have done it !!

References and useful links-
---------------------------------------------


Java - A brief Introduction


Java is a simple, secure, robust, portable, object-oriented high-level programming language originally released by Sun Microsystems in 1995. Sun Microsystem was later acquired by Oracle Corporation in 2010.

Java is an architectural-neutral and platform-independent language promised Write Once, Run Anywhere(WORA), providing no-cost run-times on popular platforms.

Java is the world’s famous programming language. Yes, it is true!!! Java is the most popular language as per the TIOBE Programming Community index.

As per java.com 

·         97% of Enterprise Desktops Run Java
·         89% of Desktops (or Computers) in the U.S. Run Java
·         9 Million Java Developers Worldwide
·         #1 Choice for Developers
·         #1 Development Platform
·         3 Billion Mobile Phones Run Java
·         100% of Blu-ray Disc Players Ship with Java
·         5 Billion Java Cards in Use
·         125 million TV devices run Java
·         5 of the Top 5 Original Equipment Manufacturers Ship Java ME

This is enough to prove Java is number 1 ;). What do you think? 


Brief History of Java


Java Programming Language was developed by James Gosling along with Mike Sheridan and Patrick Naughton while they were working at Sun Microsystems in 1991. They were part of a team having code name Green. Initially, it was named Oak Programming Language. Oak was renamed Java in 1995.

Java releases


In 1995 Sun Microsystem formally released the first version of Java. Given below the history of Java releases in tabular form:

Java Release version
Code Name
Release year
Java version 1.0
Oak
1995
Java version 1.1
-
1997
Java version 1.2(Java 2)
Playground
1998
Java version 1.3
Kestral
2000
Java version 1.4
Merlin
2002
Java version 1.5 (Java SE 5)
Tiger
2004
Java version 1.6 (Java SE 6)
Mustang
2006
Java version 1.7 (Java SE 7)
Dolphin
2011
Java version 1.8 (Java SE 8)
-
2014

Interested in more details about Java version history and releases? – Click here.


Download Java


Software Developers: JDK (Java SE Development Kit). For Java Developers.

The JDK is a superset of the JRE and contains everything in the JRE, plus tools such as compilers and debuggers and monitoring tools for development. A JRE provides a Java Virtual Machine (JVM), the standard class libraries, and other components to run applications written in the Java programming language.

Java SE includes:
  • The Java Development Kit (JDK), including the JavaFX Software Development Kit (SDK),
  • Java Runtime Environment (JRE), (Server and regular packages)
  • JavaFX Runtime,
  • JRockit JDK
Download the latest version of Java SE from the Oracle website:
http://www.oracle.com/technetwork/java/javase/downloads/index.html

JRE: (Java Runtime Environment). Covers most end-users needs. Contains everything required to run Java applications on your system.
The latest version of JRE can be downloaded from -> https://java.com/en/download/


References: