Skip to main content

Posts

Showing posts from February, 2023

Reverse a Number In Java

  You can copy below program directly and execute it in editor : package com.crtr4u.www; public class ReverseNumber { public static void main(String[] args) { int originalNumber =3456789, reverseNumber = 0, remainder; while (originalNumber != 0) { remainder = originalNumber % 10; reverseNumber = reverseNumber * 10 + remainder; originalNumber /= 10; }; System.out.println ("reverse Number: " + reverseNumber); } } Output : reverse Number: 9876543

Create Microservice and Register with Eureka Server Step By Step

 Step 1 : File -> New -> Spring Starter Project -> Add Configuration as below and Next : Step 2 :   Add Dependencies and click Finish : Step 3 : Edit properties file as : server.port=0 spring.application.name=AccountService eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/ Step 4 : add @EnableDiscoveryClient annotation at main class : package com.crtr4u.www; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class AccounMicroserviceApplication { public static void main(String[] args) { SpringApplication.run(AccounMicroserviceApplication.class, args); } } Step 5 : create a simple restcontroller as below : package com.crtr4u.www; import org.spring...

Start Up Eureka Service Discovery In Spring Boot

Today , we are going to see Spring Boot Eureka Server Configuration  in STS  : Step 1: Create Spring Starter Project in Spring Tool Suite : Step 2: Do Java,Maven Configuration as below : Step 3 : select Dependencies Eureka Discovery Client and Eureka Server Step 4 : click Next - > Finish Step 5 : Once all dependencies downloaded successfully , then add annotation  @EnableEurekaServer on main class as below : Step 6 : Edit Properties file as below : server.port=8001 spring.application.name=discoveryservice eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone= http://localhost:8001/eureka Step 7: Run main class as spring boot app : Step 8 : Once Application started succesfuly check in browser :                  now visit url :  http://localhost:8001/ it will show UI as below  : Finally our Eureka Server started as shown above ,lets understa...

sort list of objects using comparator in java

 sort list of objects using comparator in java one of the most important question for interviews of java programmer is how to sort list of objects in java , lets understand the solution using scenario of Patients , we will create a class Patient with two properties patientId and patientName and then we will provide options to sort list of patients by patientId and PatientName. Step 1 :  create Patient.java class as below : package com.sorting.sv; public class Patient { private int patientId; private String patientName; public Patient() { } public Patient(int patientId, String patientName) { this.patientId = patientId; this.patientName = patientName; } public int getPatientId() { return patientId; } public void setPatientId(int patientId) { this.patientId = patientId; } public String getPatientName() { return patientName; } public void setPatientName(String patientName) { this.patientName = patientName; } @Override public String toString() { retu...

count frequency of characters in string using java

count frequency of characters in string using java  In most of the interviews , we always get this question from interviewer : program to count frequency of characters in string ? program to count number of times character occurred in given String? program to count number of times character repeated in statement ? today we are going to see the solution for this , using java we will print count of frequency of characters in string : import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class CountFrequency { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String input = null; int option=0; try { do { System.out.println("Enter 1 to test new String: "); System.out.print("\nEnter 2 for exit : "); try { option = scn.nextInt(); } catch(Exception e) { System.out.println("Wrong input ,Please Enter 1 or 2 "); e.printStackTrace(); ...

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Sorting Program Using Stream API and sorted() method in java Stream Interface

Sorting Using Stream API sorted() method in java : copy below code and study , to understand different sorting techniques using java Stream API :  I know , this program is very big , but its enough to practice whole sorting using Stream API in java  ,due to time limitation , i am skipping theory part we will see theory in depth in upcoming articles , till that time use this program and try to understand sorting using stream in java :  Step 1: write Student class as below : package com.crtr4u.www; public class Student { int id; String name; String department; public Student() { super(); } public Student(int id, String name, String department) { super(); this.id = id; this.name = name; this.department = department; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department;...

Stream In Java

stream in java 8 : Stream API is used to process collection of objects. stream() method is present in Collection interface from package java.util . Stream is an interface present in java.util package, filter() and map() methods are present in Stream interface. -->when to use filter()  method ? If we want to filter elements from collection based on some boolean condition then we can use filter() method.  for example  : filtering even or odd numbers from list of numbers. -->When to use map()  method ? If we want to create new object for every object in collection then we should use map() method. for example : if we want to increase every number in list by 2 , here every object is getting modified ,we have to use map() in this type of scenario. In below example , we used both filter() and map() methods on stream , inside filter method we applied some condition and in map method we are transforming our object to different one. package com.crtr4u.www; import java...

Why to learn python ?

 5 reasons to learn Python right now (1) Easy to learn: Python is the easiest programming language to learn. (2) High Salary: Did you know that the average Python programmer in the U.S. makes approximately $116 thousand dollars a year. (3) Scalability: It’s true, Python is easy to learn. But it’s also an extremely powerful language that can help you create top-tier apps. In fact, Google, Instagram, YouTube, and Spotify are all built using Python. (4) Versatility: What’s more, Python is by far the most versatile programming language in the world today! From web development to data science, machine learning, computer vision, data analysis and visualization, scripting, gaming, and more, Python has the potential to deliver growth to any industry. (5) Future-proof Career:   The high demand and low supply of Python developers make it the ideal programming language to learn today. Whether you’re eyeing a career in machine learning or artificial intelligence, learning Python is an ...

Collections - List Interface In Java

Iterable Interface - Root Interface of all collection classes. Collection Interface - extends Iterable Interface. List , Queue , and Set Interfaces extends Collection Interface. List Interface In Java : List can have duplicate values. List interface is implemented by classes : ArrayList,LinkedList,Vector,Stack. ArrayList : ArrayList maintain insertion order and is non-synchronized , elements stored in ArrayList can be accessed randomly.It internally uses dynamic array,In ArrayList searching is fast because of indexing. In below example we have created an ArrayList , added elements in it and iterated using for each loop and using iterator . import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class CollectionDemo { public static void main(String[] args) { List<String> studentList= new ArrayList<String>(); studentList.add("Neel"); studentList.add("Pooja"); studentList.add("Shivam"); System.out.println(...

Java 8 features - Functional Interface and Lambda Expression

Functional Interface in Java Functional Interface is defined as an interface with only one abstract method .one of the example of Functional interface in java.lang package is Runnable with only one abstract method run() . we can use Annotation , @FunctionalInterface to create Functional interface in java , actually this annotation is not mandatory , but if we write this annotation , and in future if somebody try to add new abstract method in our interface then he will get Error as  : Invalid '@FunctionalInterface' annotation. Functional Interfaces are also called as Single Abstract Method ( SAM ) interfaces. Using Functional Interfaces,Anonymous Inner classes are implemented. Example : public class TestAnonymousInnerClass { public static void main(String[] args) { new Thread(new Runnable() { @Override public void run() { System.out.println("Implementing Runnable"); } }).start(); } } Output : Implementing Runnable Lambda Expressions in java Lam...

Java 8 features ,Default Methods and Static Methods

 Java as an Object oriented programming language was invented in the early 90s. In year 2005 , added major changes such as : 1.Enhanced for loops 2.Generics 3.Type-safe enumerations. 4.Annotations. again after 10 years , in 2015 java 8 was introduced with lot of new features : such as : 1.Lambda Expressions 2.Method References 3.Default methods 4.New Stream API 5.New Date/Time API 6.Nashorn -> now in java 8 ,interface introduced with some new changes. interface can now define static methods. interface can now define default methods. package com.suv.java8; public class Java8Demo implements Calculator { public static void main(String[] args) { System.out.println("Welcome to https://www.crtr4u.com/"); Java8Demo jd = new Java8Demo(); jd.msgDisplay(); Calculator.sayHi(); } } interface Calculator { default void msgDisplay() { System.out.println("this is default method introduced in java 8 interface"); } static void sayHi() { System.out.println("Hi...