Сообщения

Collections

Изображение
                                                                               Chapter2:Recursion -- Printing odd or even number with recursion: public static void odd ( int a , int b ) { if ( a == b ) return ; if ( a % 2 == 1 ) System . out . println ( a ); odd (++ a , b ); } 15.How to write recursion in 3 steps? package org.example.recursion ; public class Main { public static void main ( String [] args ) { System . out .println( factorial1 ( 5 )) ; System . out .println( factorial2 ( 5 )) ; System . out .println( factorial3 ( 5 )) ; } public static Integer factorial1 ( Integer n ) { return n == 0 ? 1 : n * factorial1 ( n - 1 ) ; } public static Integer factorial2 ( I...

Lesson: Enum

package org.enums ; public enum Operation { SUM ( '+' ) , SUBTRACT ( '-' ) , MULTIPLY ( '*' ) , DIVIDE ( '/' ) ; private char c ; Operation ( char c ) { this . c = c ; } public char getC () { return c ; } }   package org.enums ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner in = new Scanner( System . in ) ; char c = in .nextLine().charAt( 0 ) ; Operation operation = Operation . SUM ; double calculate = calculate ( 5 , 3 , operation ) ; System . out .println( calculate ) ; System . out .println( operation .getC()) ; if ( c == operation .getC()) { System . out .println( "Equal" ) ; } } public static double calculate ( double a , double b , Operation operation ) { // burada restrict edirik if ( operation == Operation . SUM ) { return a +...

SOLID principles

 S - Single Responsibility There should never be more than one reason for a class to change.

Lesson: Design Patterns

                                                                                    Singleton  -- Constructor protected olduqda, o zaman singleton design pattern-i qirmaq olur. Bunun ucun araliq bir class yaradiriq ve esas class-i extend edirik. package org.singleton ; public class Test { private static Test test = null ; private Test () {} public static Test getInstance () { if ( test == null ) test = new Test() ; return test ; } } package com.example.ms7.creational_patterns ; public final class Singleton { private static Singleton instance ; private Singleton () { } public static synchronized Singleton getInstance () { if ( instance == null ) { synchronize...

Lesson: Interface

By default yeni susmaya gore interface-lerin icindeki istenilen metod  public abstract sayilir.  By default istenilen vairable public static final sayilir. *** Be cautious: package az.etibarli ; public interface Flyable { void fly (); } package az.etibarli ; public class Parent implements Flyable { @Override public void fly () { System . out . println ( "Parent fly" ); } } package az.etibarli ; public class Child implements Flyable { @Override public void fly () { System . out . println ( "Child fly" ); } } package az.etibarli ; import java.sql.SQLException ; public class Main { public static void main ( String [] args ) throws SQLException { Parent parent = new Parent (); Flyable flyable = parent ; Child child = ( Child ) flyable ; child . fly (); } } -- Marker interface: bow bir interface-dir. Filter meqsedi ile qoyulur. Mes: Serializable. File yazib oxumaq ucun istifade olunur.

Lesson: Exception

Изображение
1. Problem: package az.etibarli ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner in = new Scanner ( System . in ); int a = in . nextInt (); int b = in . nextInt (); System . out . println ( a / b ); System . out . println ( "Continue..." ); } }  -- Solution: package az.etibarli ; import java.util.Scanner ; public class Main { public static void main ( String [] args ) { Scanner in = new Scanner ( System . in ); int a = in . nextInt (); int b = in . nextInt (); try { System . out . println ( a / b ); } catch ( Exception e ) { e . printStackTrace (); } System . out . println ( "Continue..." ); } } Demeli try-catch bloku iwletmedikde JVM exceptionu tutacaq printStackTrace() metodunu cagiracaq ve sistemden cixacaq. -- eslinde printStackTrace nece iwleyir: package az.etibarli ; public ...