Home » Articles posted by All In One Tutorial

Author Archives: All In One Tutorial

Array Programs in Scala

Question :: Write a Scala Program to
1. Insert elements and print elements in array.
2. print nth element of an array.
3. Concatenate two arrays.
4. insert elements in a two dimensional array and printing it

Program ::

Output ::

Scala Programming

How To set up Scala in Eclipse and windows OS :: 

1 : Download and Install jdk8 from Oracle download link .

2 : Right-click on ‘My Computer’ and select ‘Properties’ ,then select Advance system settings.
3 : Click on the ‘Environment variables’ button under the ‘Advanced’ tab.you can edit  path by appending     ;C:\Program Files\Java\jdk1.8.0_181\bin”

4 : Launch eclipse.

5  : Go to Help -> Eclipse Market Place and click on it.

6 : In the  search box type “scala” and click on search symbol.

7 : Select “Scala IDE 4.7x” and click on install.

8 : Select required plugins and confirm and install

9 : Scala Installation is successful now and you need to restart eclipse for it to reflect.

 

First Hello World Program Using Scala :: 

1 : Launch Eclipse .

2 : Click on File -> New -> Scala Project -> Give a Project Name -> Finish

 

3 : Click on src ,create Scala Object “HelloWorld ” and save it

4 : Right Click on the File “HelloWorld.scala” and run as Scala Application

Output ::

Java 8 Date Time API

Java 8 Date Time API is one of the best features in Java8.

Why Java 8 Date Time API needed ?
1. Prior to Java8 we have SimpleDateFormatter and java.util.Date and both are not Thread safe.Every application now a days requires concurrency and multithreading support.So slowly application developers started preferring 3rd party library JodaTime over java.util.Date package.This was a concern for Java Team and they come up with Java Date Time API which is thread safe.
2. Prior to Java 8 some of the date and time classes has poor API design in terms of date range.For example,default years in java.util.Date start at 1900,default months start at 1, and default days start at 0, so not very good.
3. Also Developers need to code a lot to handle TimeZone due to poor API design.

Program :::

Output ::

Sort Map by Key and by Value using Java8

Question : Write a program using java 8
1. sort a map by key in natural order
2. sort a map by key in reverse order
3. sort a map by value in natural order
4. sort a map by value in reverse order

Solution ::
The below program takes a hashmap as input and sorts the map by key and by value.

Java Enum

Java Enum Overview ::

1. In java an enum type is a special data type which enables a variable to hold a set of predefined constants.It means basically enum  is a datatype which can hold a set of constants.
2. Directions(EAST,WEST,NORTH,SOUTH) is an example of enum or DAY(SUN,MON,TUE,WED,THU,FRI,SAT) is  an example of enum.
3. enum can be traversed using values() method.
4. enum can have constructor,fields and also user defined methods.
5. enum internally extends enum class ,so it can not extend any other class .enum can implement multiple interfaces.

6. enum can be used in switch statements,enum is final so we can use for Singleton,enum ensures type safety as well.These are some of the reasons why we use enum.

7. You can use valueOf(String arg) to get a particular enum and you can use valueOf(Class<T>  arg, String arg1) to get a particular enum of a class Type.
8. enum was introduced in Java 5 version jdk 1.5

You can directly create enum java file by following the below image.

Let us create an enum called Apartment.

Output ::

Java Regular Expressions

Java Regular Expressions ::

1. java.util.regex API is used for pattern matching using regular expressions
2. java.util.regex.Pattern and java.util.regex.Matcher are the two main classed in java.util.regex API.
3. class java.util.regex.Pattern extends Object class and implements Serializable class.
4. A regular expression, specified as a string, must first be compiled into an instance of Pattern class. The resulting pattern can then be used to create a Matcher object that can match arbitrary character sequences against the regular expression. All of the state involved in performing a match resides in the matcher, so many matchers can share the same pattern.

Validate Email address with regular expression

Write a Program to validate Email address with regular expression ?

ValidEmailMain.java

Output ::

Validate IP address with regular expression

Write a Program to validate IP address with regular expression ?

ValidIPMain.java

Output ::

Dom Parser

In this post ,we are going to see how to parse below organization.xml file using DOM Parser.Before that let us understand  about DOM.

DOM Overview::

  1. Document Object Model (DOM) is an official recommendation of the World Wide Web Consortium (W3C).
  2. Using DOM parser ,you can get the entire xml document as a tree structure.

organization.xml

The below class shows you how to parse a xml and get the xml document in tree node structure and then visiting each node of the tree.
DomParserMain.java

OutPut ::

HTTP status codes

http Status Codes :: are the 3 digit codes sent in the response from the server to the requesting client.http status code is part of the HTTP/1.1 standard RFC 7231.

Out of the 3 digits in http status code ,there are officially mainly 5 values 1,2,3,4,5 for 1st digit.
Below are the major status codes you need to know.

1×× Informational : An informational response indicates that the request was received and understood by server.

    http Status code       Description 
 100 Continue
 101 Switching Protocols It means client requested  server to switch protocols and server successfully does the same.
 102 Processing It means the server has received and is processing the request from the client, but no response is available yet to client.
103 Early Hints It is basically used to return response headers before final HTTP message.

2×× Success : This series of status codes indicate that the action requested by the client was received, understood and accepted by server.

    http Status code       Description 
 200 OK
 201 Created
 202 Accepted
 203 Non-authoritative Information
 204 No Content
 205 Reset Content
 206 Partial Content
 207 Multi-Status
 208 Already Reported
 226 IM Used

3×× Redirection : 

This series of status codes indicate that  the client must take additional action to complete the request. Many of these status codes are used in URL Redirection.

    http Status code       Description 
 300 Multiple Choices
 301 Moved Permanently
 302 Found
 303 See Other
 304 Not Modified
 305 Use Proxy
 306 Switch Proxy
 307 Temporary Redirect
 308 Permanent Redirect

4×× Client Error : This series of status codes indicate that  error seems to have been caused by the client.Mostly  syntax issue at client request. 

    http Status code       Description 
 400 Bad Request
 401 Unauthorized
 402 Payment Required
 403 Forbidden
 404 Not Found
 405 Method Not Allowed
 406 Not Acceptable
 407 Proxy Authentication Required
 408 Request Timeout
 409 Conflict
 410 Gone
 411 Length Required
 412 Precondition Failed
 413 Payload Too Large
 414 Request-URI Too Long
 415 Unsupported Media Type
 416 Requested Range Not Satisfiable
 417 Expectation Failed
 418 I’m a teapot
 421 Misdirected Request
 422 Unprocessable Entity
 423 Locked
 424 Failed Dependency
 426 Upgrade Required
 428 Precondition Required
 429 Too Many Requests
 431 Request Header Fields Too Large
 444 Connection Closed Without Response
 451 Unavailable For Legal Reasons
 499 Client Closed Request

5×× Server Error : This series of status codes indicate that server  has encountered an error and it is  incapable of performing the request i.e. server has failed to fulfill a valid request.

    http Status code       Description 
 500 Internal Server Error
 501 Not Implemented
 502 Bad Gateway
 503 Service Unavailable
 504 Gateway Timeout
 505 HTTP Version Not Supported
 506 Variant Also Negotiates
 507 Insufficient Storage
 508 Loop Detected
 510 Not Extended
 511 Network Authentication Required
 599 Network Connect Timeout Error

 

Reference ::