Tuesday, April 23, 2013

Breaks a five-digit number


// Program breaks apart a five-digit number

import javax.swing.JOptionPane;

public class Five {

public static void main( String args[] )
 {
 int number;
 String inputString;

 // read the five digit number from user as a string
 inputString = JOptionPane.showInputDialog(
 "Enter five digit integer:" );

 number = Integer.parseInt( inputString );

 // determine the 5 digits
 int digit1, digit2, digit3, digit4, digit5;
 digit1 = number / 10000;
 digit2 = number % 10000 / 1000;
 digit3 = number % 10000 % 1000 / 100;
 digit4 = number % 10000 % 1000 % 100 / 10;
 digit5 = number % 10000 % 1000 % 100 % 10;

 // create the result string
 String resultString = digit1 + " " + digit2 + " " +
 digit3 + " " + digit4 + " " + digit5 ;

 // display results
 JOptionPane.showMessageDialog( null, resultString,
 "Digits in " + number, JOptionPane.INFORMATION_MESSAGE );

 System.exit( 0 );

 } // end method main

 } // end class Five


PROGRAM OUTPUT :

Odd or Even Number



 Program that determines if a number is odd or even.

import javax.swing.JOptionPane;

public class OddEven {

// main method begins execution of Java application
public static void main( String args[] )
 {
 String input; // string entered by user
 String result; // output display string
 int number; // number

 // read from user as a string
 input = JOptionPane.showInputDialog( "Enter integer:" );

 // convert number from type String to type int
 number = Integer.parseInt( input );

 // initialize result to empty String
 result = "";

 if ( number % 2 == 0 )
 result = "Number is even.";

 if ( number % 2 != 0 )
 result = "Number is odd.";

 // Display results
 JOptionPane.showMessageDialog( null, result, "Calculation Results",
 JOptionPane.INFORMATION_MESSAGE );

 System.exit( 0 ); // terminate application

 } // end method main

 } // end class OddEven


PROGRAM OUTPUT :



Product of 3 integers


Calculate the product of three integers.

// Java packages
 import javax.swing.JOptionPane;

public class Product {

public static void main( String args[] )
 {
 int x; // first number
 int y; // second number
 int z; // third number
 int result; // product of numbers

 String xVal; // first string input by user
 String yVal; // second string input by user
 String zVal; // third string input by user

 xVal = JOptionPane.showInputDialog( "Enter first integer:" );
 yVal = JOptionPane.showInputDialog( "Enter second integer:" );
 zVal = JOptionPane.showInputDialog( "Enter third integer:" );

 x = Integer.parseInt( xVal );
 y = Integer.parseInt( yVal );
 z = Integer.parseInt( zVal );

 result = x * y * z;

 JOptionPane.showMessageDialog( null, "The product is " + result );

 System.exit( 0 );

 } // end method main

 } // end class Product


PROGRAM OUTPUT

Sunday, April 21, 2013

Larger of two numbers


// Program that determines the larger of two numbers.

import javax.swing.JOptionPane;

public class Larger {

// main method begins execution of Java application
public static void main( String args[] )
 {
 String firstNumber;
 String secondNumber; // second string entered by user
 String result;
 int number1;
 int number2;

 // read first number from user as a string
 firstNumber = JOptionPane.showInputDialog( "Enter first integer:" );

 // read second number from user as a string
 secondNumber =
 JOptionPane.showInputDialog( "Enter second integer:" );


 // convert numbers from type String to type int
 number1 = Integer.parseInt( firstNumber );
 number2 = Integer.parseInt( secondNumber );

 // initialize result to empty String
 result = "";

 if ( number1 > number2 )
 result = number1 + " is larger.";

 if ( number1 < number2 )
 result = number2 + " is larger.";

 if ( number1 == number2 )
 result = "These numbers are equal.";

 // Display results
 JOptionPane.showMessageDialog( null, result, "Comparison Results",
 JOptionPane.INFORMATION_MESSAGE );

 System.exit( 0 ); // terminate application

 } // end method main

 } // end class Larger





Println and Print

Println prints on a new line, where as print Prints on the same line.

Example for Println: 

Java Self-Review Exercise 2


State whether each of the following is true or false. If false, explain why.

1.) Comments cause the computer to print the text after the // on the screen when the program
executes.

2.) All variables must be given a type when they are declared.

3.) Java considers the variables number and NuMbEr to be identical.

4.) False. The remainder operator can also be used with noninteger operands in Java.

5.) The arithmetic operators *, /, %, + and - all have the same level of precedence.

6.) Method Integer.parseInt converts an integer to a String.




Answers:

1.  ANS: False. Comments do not cause any action to be performed when the program is executed.
They are used to document programs and improve their readability.

2.  ANS: True

3.  ANS: False. Java is case sensitive, so these variables are distinct.

4.  ANS: False. The operators *, / and % are on the same level of precedence, and the operators
+ and - are on a lower level of precedence.

5.  ANS: False. The modulus operator can also be used with noninteger operands in Java.

6.  ANS: False. Integer.parseInt method converts a String to an integer (int) value.

Java Self-Review Exercises


Fill in the blanks in each of the following statements:
1.) A begins the body of every method, and a(n) ends the body of
every method.

2.) Every statement ends with a(n) .

3.) The statement (presented in this chapter) is used to make decisions.

4.)  Begins an end-of-line comment.

5.)  , , and are called white space.

6.) Class contains methods that display message dialogs and input dialogs.

7.) Are reserved for use by Java.

8.) Java applications begin execution at method .

9.) Methods and display information in the command window.

10.) A(n) method is called by using its class name followed by a dot (.) and its
method name.


ANSWERS:
1.   ANS: left brace ({), right brace (})
2.   ANS: semicolon (;)
3.   ANS: if
4.   ANS: //
5.   ANS: Blank lines, space characters, newline characters and tab characters
6.   ANS: JOptionPane
7.   ANS: Keywords
8.   ANS: main
9.   ANS: System.out.print and System.out.println
10. ANS: static

Hello World

Your first program must be named " Hello World " means that you are ready to do the challenge without escaping it. the program codes follows :

your project name should be the same as the name below.

public class Hello {  
    
    public static void main(String[] args) {
   
   
    System.out.println("Hello World!");
    }
}

Saturday, April 20, 2013

Installing Java

you need first to download the JCreator or Eclipse and its corresponding JDK
links provided below