KatsolAgency

Programming languages and web development including java , html, css ,java script,Tutorials Android phone tricks and Tech news around the world.

Saturday, August 19, 2017

CSS ─ Syntax

CSS Syntax 

Katsolagency

A CSS comprises of style rules that are interpreted by the browser and then applied to
the corresponding elements in your document. A style rule is made of three parts:

☹ Selector: A selector is an HTML tag at which a style will be applied. This could be
any tag like

or
etc.
☹ Property: A property is a type of attribute of HTML tag. Put simply, all the HTML
attributes are converted into CSS properties. They could be color, border, etc.
☹ Value: Values are assigned to properties. For example, color property can have
the value either red or #F1F1F1 etc.


     You can put CSS Style Rule Syntax as follows:

selector { property: value }
Example: You can define a table border as follows:
table{ border :1px solid #C00; }
Here table is a selector and border is a property and the given value 1px solid #C00 is
the value of that property.

   You can define selectors in various simple ways based on your comfort. Let me put these
selectors one by one.
The Type Selectors
This is the same selector we have seen above. Again, one more example to give a color
to all level 1 headings:

1
2
3
h1 {
 color: #36CFFF;
}

The Universal Selectors

Rather than selecting elements of a specific type, the universal selector quite simply
matches the name of any element type:

1
2
3
* {
 color: #000000;
}
This rule renders the content of every element in our document in black.

The Descendant Selectors

Suppose you want to apply a style rule to a particular element only when it lies inside a
particular element. As given in the following example, the style rule will apply to
element only when it lies inside the
    tag.

ul em {
 color: #000000;
}

The Class Selectors

You can define style rules based on the class attribute of the elements. All the elements
having that class will be formatted according to the defined rule.
.black {
 color: #000000;
}
This rule renders the content in black for only
elements with class attribute set to
black.
You can apply more than one class selectors to a given element. Consider the following
example:
This para will be styled by the classes center and bold.

The ID Selectors

You can define style rules based on the id attribute of the elements. All the elements
having that id will be formatted according to the defined rule.

1
2
3
#black {
 color: #000000;
}
This rule renders the content in black for every element with id attribute set to black in
our document. You can make it a bit more particular. For example:

h1#black {
 color: #000000;
}
This rule renders the content in black for only

elements with id attribute set


to black.
The true power of id selectors is when they are used as the foundation for descendant
selectors. For example:

#black h2 {
 color: #000000;
}
In this example, all level 2 headings will be displayed in black color when those headings
will lie within tags having id attribute set to black.
The Child Selectors
You have seen the descendant selectors. There is one more type of selector, which is
very similar to descendants but have different functionality. Consider the following
example:
body > p {
 color: #000000;
}
This rule will render all the paragraphs in black if they are a direct child of the
element. Other paragraphs put inside other elements like
or
would not have
any effect of this rule.

The Attribute Selectors

You can also apply styles to HTML elements with particular attributes. The style rule
below will match all the input elements having a type attribute with a value of text:
input[type="text"]{
 color: #000000;
}

The advantage to this method is that the element is
unaffected, and the color applied only to the desired text fields.

There are following rules applied to attribute selector.
 p[lang] - Selects all paragraph elements with a lang attribute.
 p[lang="fr"] - Selects all paragraph elements whose lang attribute has a value
of exactly "fr".
 p[lang~="fr"] - Selects all paragraph elements whose lang attribute contains
the word "fr".
 p[lang|="en"] - Selects all paragraph elements whose lang attribute contains

values that are exactly "en", or begin with "en-".

Monday, August 14, 2017

CSS

Cascading Style Sheets

Cascading Style Sheets

Introduction To CSS

What is CSS?

CSS, which  referred to as Cascading Style Sheets, is a simple design language intended
to simplify the process of making web pages presentable.
CSS handles the look and feel part of a web page. Using CSS, you can control the color
of the text, the style of fonts, the spacing between paragraphs, how columns are sized
and laid out, what background images or colors are used, as well as a variety of other
effects.
CSS is easy to learn and understand but it provides a powerful control over the
presentation of an HTML document. Most commonly, CSS is combined with the markup
languages HTML or XHTML.

     Benefits of CSS in Web development    

1: Global web standards 

 Now HTML attributes are being deprecated and it is
being recommended to use CSS. So it’s a good idea to start using CSS in all the
HTML pages to make them compatible with future browsers.

2: Superior styles to HTML 

 CSS has a much wider array of attributes than HTML,
so you can give a far better look to your HTML page in comparison to HTML
attributes.

3: Multiple Device Compatibility 

 Style sheets allow content to be optimized for
more than one type of device. By using the same HTML document, different
versions of a website can be presented for handheld devices such as PDAs and
cellphones or for printing.

4: Easy maintenance 

 To make a global change, simply change the style, and all
the elements in all the web pages will be updated automatically.

5: Pages load faster 

If you are using CSS, you do not need to write HTML tag
attributes every time. Just write one CSS rule of a tag and apply it to all the
occurrences of that tag. So, less code means faster download times.

6: CSS saves time 

 You can write CSS once and then reuse the same sheet in
multiple HTML pages. You can define a style for each HTML element and apply it
to as many web pages as you want.

People always ask Who Creates and Maintains CSS (Cascading Style Sheets)?

Cascading Style Sheets is created and maintained through a group of people within the W3C called the CSS
Working Group. The Cascading Style Sheets (CSS) Working Group creates documents called specifications.
When a specification has been discussed and officially ratified by the W3C members, it
becomes a recommendation.
These ratified specifications are called recommendations because the W3C has no control
over the actual implementation of the language. Independent companies and
organizations create that software.
NOTE: The World Wide Web Consortium or W3C is a group that makes
recommendations about how the Internet works and how it should evolve.

CSS Syntax

A CSS rule has two main parts: a selector, and one or more declarations:
The selector is normally the HTML element you want to style.
Each declaration consists of a property and a value.
The property is the style attribute you want to change. Each property has a value.

CSS Example

CSS declarations always ends with a semicolon, and declaration groups are surrounded by curly brackets:


1
p {color:red;text-align:center;}

To make CSS code  more readable, you can put one declaration on each line, like this:

1
2
3
4
5
p
{
color:red;
text-align:center;
}

Comments in CSS 

Comments are used to explain your code, and may help you when you edit the source code at a later date. Comments are ignored by browsers.
see the example below

1
2
3
4
5
6
7
8
/*This is a comment*/
p
{
text-align:center;
/*This is a comment too*/
color:black;
font-family:arial;
}


Tuesday, August 1, 2017

JAVA PROGRAMS

Monday, July 31, 2017

Nested Switch in java

Program6 - Nested Switch in java


Java Statements Example

This example shows how to use nested switch statements in a
java program.
Like any other Java statements, switch statements can also be nested in each other as given in
below example.


public class NestedSwitchExample {
public static void main(String[] args) {

int i = 0;
int j = 1;
switch(i)
{
case 0:
switch(j)
{
case 0:
System.out.println("i is 0, j is 0");
break;
case 1:
System.out.println("i is 0, j is 1");
break;
12
default:
System.out.println("nested default
case!!");
}
break;
default:
System.out.println("No matching case found!!");
}
}
}

Output would be,
i is 0, j is 1

Sunday, July 30, 2017

Program5 - Fibonacci Series

Program5 - Fibonacci Series

 Fibonacci Series Java Example

 Fibonacci Series Java Example

This Fibonacci Series Java Example shows how to create and print
Fibonacci Series using Java.
public class JavaFibonacciSeriesExample {
public static void main(String[] args) {
//number of elements to generate in a series
int limit = 20;
long[] series = new long[limit];
//create first 2 series elements
series[0] = 0;
series[1] = 1;
//create the Fibonacci series and store it in an array
for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2];
}
//print the Fibonacci series numbers
System.out.println("Fibonacci Series upto " + limit);
for(int i=0; i< limit; i++){
System.out.print(series[i] + " ");
}
}
}

Output of the Fibonacci Series Java Example would be
Fibonacci Series upto 20
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181

Saturday, July 29, 2017

Program4 - Determine If Year Is Leap Year

Determine If Year Is Leap YearDetermine If Year Is Leap Year


Determine If Year Is Leap Year Java Example
This Determine If Year Is Leap Year Java Example shows how to
determine whether the given year is leap year or not.



public class DetermineLeapYearExample {
public static void main(String[] args) {

//year we want to check
int year = 2004;
//if year is divisible by 4, it is a leap year
if(year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))
System.out.println("Year " + year + " is a leap year");
else
System.out.println("Year " + year + " is not a leap year");
}
}

Output of the example would be
Year 2004 is a leap year
NEXT

Program3 - Compare Two Numbers using else-if

Program3 - Compare Two Numbers using else-if


Compare Two Numbers Java Example
This Compare Two Numbers Java Example shows how to compare two numbers
using if else if statements.

public class CompareTwoNumbers {
public static void main(String[] args) {
//declare two numbers to compare
int num1 = 324;
int num2 = 234;
if(num1 > num2){
System.out.println(num1 + " is greater than " + num2);
}
else if(num1 < num2){

System.out.println(num1 + " is less than " + num2);
}
else{
System.out.println(num1 + " is equal to " + num2);
}
}
}

Output of Compare Two Numbers Java Example would be
324 is greater than 234
NEXT

Program2 Factorial of a number

Program2 - Factorial of a number


This simple java program it shows how to calculate
Factorial of a number.

public class NumberFactorial {
public static void main(String[] args) {
int number = 5;
/*
* Factorial of any number is! n.
* For example, factorial of 4 is 4*3*2*1.
*/
int factorial = number;
for(int i =(number - 1); i > 1; i--)

{
factorial = factorial * i;
}
System.out.println("Factorial of a number is " + factorial);
}
}


Output of the Factorial program would be
Factorial of a number is 120
NEXT

Program1 – List of even numbers

Program1   List of even numbers


In this List Even Numbers Java program Example shows how to find and list even
numbers between 1 and any given number.

public class ListEvenNumbers {
public static void main(String[] args) {
//define limit
int limit = 50;
System.out.println("Printing Even numbers between 1 and " +
limit);
for(int i=1; i <= limit; i++){
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
}

Output of List Even Numbers Java Example would be
Printing Even numbers between 1 and 50
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
NEXT

For loop in Java with example

For loop in Java with example

forloop by katsolagency
By Iorkua Daniel | For loop in Java with example


In Java we have three types of basic loops: for, while & do-while. In this article we are going to discuss about for loop. We will cover following topics in this article:

  1.  What is a for loop?
  2. syntax of for loop
  3. example of for loop
  4. for loop flow diagram
  5. infinite


What is For loop?

It executes a block of statements repeatedly until the specified condition returns false.
Syntax of for loop:

The syntax of for loop looks like the code below;
(initialization; condition; increment/decrement) {
    statement(s) //block of statements
}

Note: Mind the semicolon (;) after initialization and condition in the above syntax.
Initialization expression executes only once during the beginning of loop
Condition(Boolean Expression) gets evaluated each time the 
loop iterates. Loop executes the block of statement repeatedly until this condition returns false.
Increment/Decrement It executes after each iteration of loop.

LOLz  Confused⧭⧭??
There is not get confused of , 
Don’t worry let see an example to understand it better.
For loop example:
class ForLoopExample {
    public static void main(String args[]){
         for(int i=10; i>1; i--){
              System.out.println("The value of i is: "+i);
         }
    }
}

This is how  here the output of this program will look:
The value of i is: 10
The value of i is: 9
The value of i is: 8
The value of i is: 7
The value of i is: 6
The value of i is: 5
The value of i is: 4
The value of i is: 3
The value of i is: 2

Explanation of  the above program:
int i=1 is initialization expression
i>1 is condition(Boolean expression)
i– Decrement operation
Flow diagram of for loop

Infinite for loop:

 The importance of Boolean expression and increment/decrement operation co-ordination:

class ForLoopExample2 {
    public static void main(String args[]){
         for(int i=1; i>=1; i++){
              System.out.println("The value of i is: "+i);
         }
    }
}
The above program is almost the same with the first one, but here we  are going to Increment   i++ 
i-- as of the first one
This is an infinite loop as the condition would never return false.
The initialization step is setting up the value of variable i to 1,
since we are incrementing the value of i, it would always be greater
 than 1 (the Boolean expression: i>1) so it would never return false.
  This would eventually lead to the infinite loop condition. Thus it
  is important to see the co-ordination among Boolean expression and
  increment/decrement to determine whether the loop would terminate at
   some point of time or not.

Here is another example of infinite for loop:

// infinite loop
for ( ; ; ) {
    // statement(s)

}

For loop example to iterate an array:
Here we are iterating and displaying array elements using the for loop.

class ForLoopExample3 {
    public static void main(String args[]){
         int arr[4]={2,11,45,9};
         //i starts with 0 as array index starts with 0 too
         for(int i=0; i<4; i++){
              System.out.println(arr[i]);
         }
    }
}


Output:
2
11
45
9

Friday, July 28, 2017

Java 101: Regular expressions in Java

The first half of this tutorial introduced you to regular expressions and the Regex API. You learned about the Pattern class, then worked through examples demonstrating regex constructs, from basic pattern matching with literal strings to more complex matches using ranges, boundary matchers, and quantifiers.
Pattern, Matcher, and PatternSyntaxException classes. You'll also be introduced to two tools that use regular expressions to simplify common coding tasks. The first extracts comments from code for documentation purposes. The second is a reusable library for performing lexical analysis, which is an essential component of assemblers, compilers, and similar software.

Explore the Regex API

Pattern, Matcher, and PatternSyntaxException are the three classes that comprise the Regex API. Each class offers methods that you can use to integrate regexes into your code.

Pattern methods


An instance of the Pattern class describes a compiled regex, also known as a pattern. Regexes are compiled to increase performance during pattern-matching operations. The following static methods support compilation.

Thursday, July 27, 2017

Classes and Object in java

java classes and object

Classes and Object in java programming language

Classes, fields, methods, constructors, and objects are the building blocks of object-based Java applications. This article will teach you how to declare classes, describe attributes via fields, describe behaviors via methods, initialize objects via constructors, and instantiate objects from classes and access their members. Along the way you'll also learn about setters and getters, method overloading, setting access levels for fields, constructors, and methods, and more. If you want to go a little further with fields and methods, you may also download the free Java 101 primer showcasing constants, recursion, and other techniques in object-based programming.

Class declaration

A class is a template for manufacturing objects. You declare a class by specifying the class keyword followed by a non-reserved identifier that names it. A pair of matching open and close brace characters ({ and }) follow and delimit the class's body. This syntax appears below:
class identifier
{
   // class body
}

By convention, the first letter of a class's name is uppercased and subsequent characters are lowercased (for example, Employee). If a name consists of multiple words, the first letter of each word is uppercased (such as SavingsAccount). This naming convention is called camelcasing.

The following example declares a class named Book:
class Book
{
   // class body
}

A class's body is populated with fields, methods, and constructors. Combining these language features into classes is known as encapsulation. This capability lets us program at a higher level of abstraction (classes and objects) rather than focusing separately on data structures and functionality.
Utility classes

A class can be designed to have nothing to do with object manufacturing. Instead, it exists as a placeholder for class fields and/or class methods. Such a class is known as a utility class. An example of a utility class is the Java standard class library's Math class. See the Fields and methods in Java primer for another example.
Multi-class applications and main()

A Java application is implemented by one or more classes. Small applications can be accommodated by a single class, but larger applications often require multiple classes. In that case one of the classes is designated as the main class and contains the main() entry-point method. For example, Listing 1 presents an application built using three classes: A, B, and C; C is the main class.
Listing 1. A Java application with multiple classes

class A
{
}

class B
{
}

class C
{
   public static void main(String[] args)
   {
      System.out.println("Application C entry point");
   }
}
You could declare these three classes in a single source file, such as D.java. You would then compile this source file as follows:

javac D.java

The compiler generates three class files: A.class, B.class, and C.class. Run this application via the following command:

java C

You should observe the following output:

Application C entry point

Alternatively, you could declare each class in its own source file. By convention, the source file's name matches the class name. You would declare A in A.java, for instance. You could then compile these source files separately:

javac A.java
javac B.java
javac C.java

To save time, you could compile all three source files at once by replacing the file name with an asterisk (but keep the .java file extension):

javac *.java

Either way, you would run the application via the following command:

java C

Public classes

Java lets you declare a class with public access via the public keyword. When you declare a class public, you must store it in a file with the same name. For example, you would store public class C {} in C.java. You may declare only one public class in a source file.

When designing multi-class applications, you will designate one of these classes as the main class and locate the main() method in it. However, there is nothing to prevent you from declaring main() methods in the other classes, perhaps for testing purposes. This technique is shown in Listing 2.
Listing 2. Declaring more than one main() method

class A
{
   public static void main(String[] args)
   {
      System.out.println("Testing class A");
   }
}

class B
{
   public static void main(String[] args)
   {
      System.out.println("Testing class B");
   }
}

class C
{
   public static void main(String[] args)
   {
      System.out.println("Application C entry point");
   }
}

After compiling the source code, you would execute the following commands to test the helper classes A and B, and to run the application class C:

java A
java B
java C

You would then observe the following lines of output, one line per java command:

Testing class A
Testing class B
Application C entry point

Be careful with main()

Placing a main() method in each class can be confusing, especially if you forget to document the main class. Also, you might forget to remove these methods before putting the application into production, in which case their presence would add bulk to the application. Furthermore, someone might run one of the supporting classes, which could disrupt the application's environment.
Fields: Describing attributes

A class models a real-world entity in terms of state (attributes). For example, a vehicle has a color and a checking account has a balance. A class can also include non-entity state. Regardless, state is stored in variables that are known as fields. A field declaration has the following syntax:

[static] type identifier [ = expression ] ;

A field declaration optionally begins with keyword static (for a non-entity attribute) and continues with a type that's followed by a non-reserved identifier that names the field. The field can be explicitly initialized by specifying = followed by an expression with a compatible type. A semicolon terminates the declaration.

The following example declares a pair of fields in Book:

class Book
{
   String title;
   int pubYear; // publication year
}

The title and pubYear fields store values for a specific book. However, you might want to store state that is independent of any particular book. For example, you might want to record the total number of Book objects created. Here's how you would do it:

class Book
{
   // ...

   static int count;
}

This example declares a count integer field that stores the number of  Book objects created. The declaration begins with the static keyword to indicate that there is only one copy of this field in memory. Each Book object can access this copy, and no object has its own copy. For this reason, count is known as a class field.

Initialization

The previous fields were not assigned values. When you don't explicitly initialize a field, it's implicitly initialized with all of its bits set to zero. You interpret this default value as false (for boolean), '\u0000' (for char), 0 (for int), 0L (for long), 0.0F (for float), 0.0 (for double), or null (for a reference type).

However, it is also possible to explicitly initialize a field when the field is declared. For example, you could specify static int count = 0; (which isn't necessary because count defaults to 0), String logfile = "log.txt";, static int ID = 1;, or even double sinPIDiv2 = Math.sin(Math.PI / 2);.

Although you can initialize an instance field through direct assignment, it's more common to perform this initialization in a constructor, which I'll demonstrate later. In contrast, a class field (especially a class constant) is typically initialized through direct assignment of an expression to the field.

Friday, July 21, 2017

A simple Java Calculator

A simple Java Calculator 

In simple java calculator there are nine JButtons to represent the numbers 1 to 9, and three JButtons for addition, subtraction and totaling the result. A JTextField at the top keeps track of the numbers being pressed and the result of the arithmetic operation.

The purpose of this Java program is to show how to implement an ActionListener interface for handling JButton button event clicks by using the containing class, an inner class and an anonymous inner class.
Written by a KatsolAgency developer (@Daniel)
 well the output is not all that cool but you  can improve thank you

package mycalc;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Container;
public class Mycalc implements ActionListener{
    JFrame guiFrame;
    JPanel buttonPanel;
    JTextField numberCalc;
    int calcOperation = 0;
    int currentCalc;
    
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
     
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
         {
             
            @Override
             public void run()
             {
                 
                 new Mycalc();         
             }
         });
              
    }
    
    public Mycalc()
    {
        guiFrame = new JFrame();
        
        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("Simple Calculator");
        guiFrame.setSize(300,300);
      
        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        
        numberCalc = new JTextField();
        numberCalc.setHorizontalAlignment(JTextField.RIGHT);
        numberCalc.setEditable(false);
        
        guiFrame.add(numberCalc, BorderLayout.NORTH);
        
        buttonPanel = new JPanel();
               
        //Make a Grid that has three rows and four columns
        buttonPanel.setLayout(new GridLayout(4,3));   
        guiFrame.add(buttonPanel, BorderLayout.CENTER);
        
        //Add the number buttons
        for (int i=1;i<10;i++)
        {
            addButton(buttonPanel, String.valueOf(i));
        }

        JButton addButton = new JButton("+");
        addButton.setActionCommand("+");
        
        OperatorAction subAction = new OperatorAction(1);
        addButton.addActionListener(subAction);
        
        JButton subButton = new JButton("-");
        subButton.setActionCommand("-");
        
        OperatorAction addAction = new OperatorAction(2);
        subButton.addActionListener(addAction);
        
        JButton equalsButton = new JButton("=");
        equalsButton.setActionCommand("=");
        equalsButton.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
                if (!numberCalc.getText().isEmpty())
                {
                    int number = Integer.parseInt(numberCalc.getText()); 
                    if (calcOperation == 1)
                    {
                        int calculate = currentCalc  + number;
                        numberCalc.setText(Integer.toString(calculate));
                    }
                    else if (calcOperation == 2)
                    {
                        int calculate = currentCalc  - number;
                        numberCalc.setText(Integer.toString(calculate));
                    }
                }
            }
        });
        
        buttonPanel.add(addButton);
        buttonPanel.add(subButton);
        buttonPanel.add(equalsButton);
        guiFrame.setVisible(true);  
    }
    
    //All the buttons are following the same pattern
    //so create them all in one place.
    private void addButton(Container parent, String name)
    {
        JButton but = new JButton(name);
        but.setActionCommand(name);
        but.addActionListener(this);
        parent.add(but);
    }
    
    //As all the buttons are doing the same thing it's
    //easier to make the class implement the ActionListener
    //interface and control the button clicks from one place
    @Override
    public void actionPerformed(ActionEvent event)
    {
        //get the Action Command text from the button
        String action = event.getActionCommand();
        
        //set the text using the Action Command text
        numberCalc.setText(action);       
    }
    
    private class OperatorAction implements ActionListener
    {
        private int operator;
        
        public OperatorAction(int operation)
        {
            operator = operation;
        }
        
        public void actionPerformed(ActionEvent event)
        {
            currentCalc = Integer.parseInt(numberCalc.getText()); 
            calcOperation = operator;
        }
    }
}