Translate
Sunday, 10 December 2017
Pattern in Java
Pattern in Java
class NumberPattern
{
public static void main(String[] args)
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
User code
import java.util.Scanner;
public class MainClass
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");
int rows = sc.nextInt();
System.out.println("Here is your pattern....!!!");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+" ");
}
System.out.println();
}
//Close the resources
sc.close();
}
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Thursday, 30 November 2017
Wednesday, 22 November 2017
font in css
<!DOCTYPE html>
<html>
<head>
<title>List Style</title>
<style>
.ul1 {
list-style-type: circle;
}
.ul2 {
list-style-type: square;
}
.ol1 {
list-style-type: upper-roman;
}
.ol2 {
list-style-type: lower-alpha;
}
</style>
</head>
<body>
<p>Example of unordered lists:</p>
<ul class="ul1">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="ul2">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<p>Example of ordered lists:</p>
<ol class="ol1">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ol class="ol2">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol> <ol class="ol2">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
List Style css
<!DOCTYPE html>
<html>
<head>
<title>List Style</title>
<style>
.ul1 {
list-style-type: circle;
}
.ul2 {
list-style-type: square;
}
.ol1 {
list-style-type: upper-roman;
}
.ol2 {
list-style-type: lower-alpha;
}
</style>
</head>
<body>
<p>Example of unordered lists:</p>
<ul class="ul1">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<ul class="ul2">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ul>
<p>Example of ordered lists:</p>
<ol class="ol1">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
<ol class="ol2">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol> <ol class="ol2">
<li>Coffee</li>
<li>Tea</li>
<li>Coca Cola</li>
</ol>
</body>
</html>
Saturday, 18 November 2017
text and background color css
<!DOCTYPE html>
<html>
<head>
<title>Background and text color</title>
<style>
.clr_cls {
background-color: red;
color: yellow;
}
</style>
</head>
<body>
<div class="clr_cls">
Mafasict
</div>
</body>
</html>
Friday, 17 November 2017
Java while loop example
While Loop Example
import java.util.Scanner;
class WhileLoop {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
System.out.println("Input an integer");
while ((n = input.nextInt()) != 0) {
System.out.println("You entered " + n);
System.out.println("Input an integer");
}
System.out.println("Out of loop");
}
}
Input an integer
7
You entered 7
Input an integer
-2
You entered -2
Input an integer
9546
You entered 9546
Input an inte
ger 0
Out of loop
Wednesday, 15 November 2017
Java 1st program
First Program
class Simple {
public static void main (string args []) {
System.out.println ("Hello Java");
}
}
save this file as Simple.java
To compile: javac Simple.java
To execute: java Simple
It will give output as Hello Java
Let's see what this is:
class keyword is used to declare a class in java.
Public keyword is an access modifier which represents visibility, it means it is visible to all.
static is a keyword, if we declare any method as static, it is known as static method. The main method is executed by the JVM, it does not require the object to invoke the main method. So it saves memory.
The void is the return type of method, it means it does not return any value.
main is a entry point of the program. Execution of programs starts from main. It is called by Runtime System
String [] args is used for command line argument. We will learn it later.
System.out.println () is used print statement.
Monday, 13 November 2017
Java switch statement
5.2 Switch statement
A switch statement is used instead of nested if...else statements. It is multiple branch decision statement.
A switch statement tests a variable with list of values for equivalence. Each value is called a case. The case value must be a constant i.
SYNTAX
switch(expression){
case constant:
//sequence of optional statements
break; //optional
case constant:
//sequence of optional statements
break; //optional
.
.
.
default : //optional
//sequence of optional statements
}
Individual case keyword and a semi-colon (:) is used for each constant.
Switch tool is used for skipping to particular case, after jumping to that case it will execute all statements from cases beneath that case this is called as ''Fall Through''.
In the example below, for example, if the value 2 is entered, then the program will print two one something else!
switch(i)
{
case 4: System.out.println(''four'');
break;
case 3: System.out.println(''three'');
break;
case 2: System.out.println(''two'');
case 1: System.out.println(''one'');
default: System.out.println(''something else!'');
}
To avoid fall through, the break statements are necessary to exit the switch.
If value 4 is entered, then in case 4 it will just print four and ends the switch.
The default label is non-compulsory, It is used for cases that are not present.
Sunday, 12 November 2017
java if statement
5.1 If statement
if statement
An if statement contains a Boolean expression and block of statements enclosed within braces.
if(conditional expression)
//statement or compound statement;
else
//optional
//statement or compound statement;
//optional
If the Boolean expression is true then statement block is executed otherwise (if false) program directly goes to next statement without executing Statement block.
if....else
If statement block with else statement is known as as if...else statement. Else portion is non-compulsory.
if ( condition_one )
{
//statements
}
else if ( condition_two )
{
//statements
}
else
{
//statements
}
If the condition is true, then compiler will execute the if block of statements, if false then else block of statements will be executed.
nested if...else
when a series of decisions are involved, we may have to use more than one if...else statement in nested form as follows:
if(test condition1)
{
if(test condition2)
{
//statement1;
}
else
{
//statement2;
}
}
else
{
//statement3;
}
//statement x;
Java program operator
4.1 Operators
Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:
- Arithmetic Operators
- Relational Operators
- Bitwise Operators
- Logical Operators
- Assignment Operators
- Misc Operators
Arithmetic Operators:
Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra.
arithmetic operators:
+ Additive operator (also used for String concatenation)
- Subtraction operator
* Multiplication operator
/ Division operator
% Remainder operator
Relational Operators:
There are following relational operators supported by Java language
> Greater than
< Less than
== Equal to
!= Not equal to
>= Greater than or equal to
<= Less than or equal to
Bitwise Operators:
Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
Bitwise operator works on bits and performs bit-by-bit operation.
~ Unary bitwise complement
<< Signed left shift
>> Signed right shift
>>> Unsigned right shift & Bitwise AND
^ Bitwise exclusive OR
| Bitwise inclusive OR
Logical Operators:
The following table lists the logical operators:
&& Conditional-AND
|| Conditional-OR
?: Ternary (shorthand for if-then-else statement)
Assignment Operators:
There are following assignment operators supported by Java language:
= Simple assignment operator
+= Add AND assignment operator
-= Subtract AND assignment operator
*= Multiply AND assignment operator
/= Divide AND assignment operator
%= Modulus AND assignment operator
<<= Left shift AND assignment operator.
>>= Right shift AND assignment operator
&= Bitwise AND assignment operator.
^= bitwise exclusive OR and assignment operator.
|= bitwise inclusive OR and assignment operator.
Increment and Decrement Operators
Increment and decrement operators are used to add or subtract 1 from the current value of oprand.
++ increment
-- decrement
Increment and Decrement operators can be prefix or postfix.
In the prefix style the value of oprand is changed before the result of expression and in the postfix style the variable is modified after result.
For eg.
a = 9;
b = a++ + 5;
/* a=10 b=14 */
a = 9;
b = ++a + 5;
/* a=10 b=15 */
Miscellaneous Operators
There are few other operators supported by Java Language.
Conditional Operator ( ? : )
Conditional operator is also known as the ternary operator.
The operator is written as:
variable x = (expression) ? value if true : value if false
Instance of Operator:
This operator is used only for object reference variables.
instanceof oper
ator is wriiten as:
( Object reference variable ) instanceof (class/interface type)
Subscribe to:
Posts (Atom)
Featured post
check box
<!DOCTYPE html> <html> <head> <title>Check Box</title> </head> <body> <input ty...
-
<!DOCTYPE html> <html> <head> <title>List Style</title> <style> .ul1 { list-...
-
<!DOCTYPE html> <html> <head> <title>Check Box</title> </head> <body> <input ty...