How to assign multiple classes to one html element

  • In HTML if we want to apply any styles to any elements we will use cascading style sheets.
  • By using CSS we can apply styles to an element in HTML
  • If some style is needed for more than one type of element then we cant make a style and name it and where ever that style is required we can place that style for that element.

  • So like this it is always possible to apply multiple styles or multiple classes to HTML elements.
  • We can specify more than one CSS class to an element.
  • By using class attribute we can specify multiple  CSS classes to a single element and all classes must be separated by a space.
  • For example if we are applying multiple classes to a div tag.
  • <div class="class1 class2"></div>
  • Here class is the attribute and class1 and class2 are the two different CSS classes.
  • Lets take an example of one paragraph element and two css classes 
  • Fist we define a <p> element with no styles.
  • Second one more <p> element with one style
  • Third one more <p> element with two styles.  
  • HTML multiple classes

 

#1: Html example file to show how to assign multiple CSS classes to an HTML element :

 

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <title>Cascading Style Sheet</title>
  5.  
  6. <style type="text/css">
  7.  
  8. .class1 {  
  9. text-align: center;
  10. color: red; 
  11. }
  12.  
  13. .class2 { font-size: 300%; }
  14.  
  15. </style>
  16. </head>
  17. <body>
  18. <p >   How to assign multiple classes to html element<p>
  19. <p class="class1">How to assign multiple classes to html element<p>
  20. <p class="class1 class2">How to assign multiple classes to html element<p>
  21.  
  22. </body>
  23. </html>


html multiple classes

Builder design pattern in java with example program

  • Design patterns are solutions to software design problems.
  • Design patterns classified into three types.
  • Creational, Structural and behavioral design patterns.

  • Creational patterns helps us to create objects in a manner suitable to the given situation.
  • Builder design pattern is one of the creational  design pattern in java.
  • Builder  design pattern helps us to create complex class object.
  • Builder design pattern helps us to separate the construction process of a complex object from its representation so that same object construction process can be created in different representations.
  • Means it will separate complex construction into two parts  initialization of class instance and return  class instance.
  • When a class having more number of fields and constructor of that class take care of assigning initial values. 
  • And when we want to create object of the class we need to pass all  parameters and should be in same order which constructor is accepting.
  • Builder design pattern helps us to create same class object by passing required number of fields by using separate builder class object.
  • Builder design pattern is useful when object creation is very complex.

Advantages of builder design pattern:

  • Builder design pattern simplifies complex object creation.
  • Builder design pattern provides separation between instance creation and representation
  • Re usability

 Program #1: Builder design pattern in java with example program

Employee:
  1. package com.designpatternsinjava.builderdesignpattern;

  2. public class Employee {

  3. String name;
  4. String company;
  5. int id;
  6. String passport_number;
  7. String temp_address;
  8. String perm_address;
  9. int salary;

  10. Employee(String name,String company,int id,String passport_number,String
  11. temp_address,String perm_address,int salary){
  12. this.name=name;
  13. this.company=company;
  14. this.id=id;
  15. this.passport_number=passport_number;
  16. this.temp_address=temp_address;
  17. this.perm_address=perm_address;
  18. this.salary=salary;
  19. }

  20. public String toString(){
  21.  return "Name="+name+" \n Company="+company+"\n id="+id+"\n
  22.    passport_number="+passport_number+"" +"\n temp_address="+temp_address+"\n
  23.    perm_address"+perm_address+"\n
  24.    salary="+salary;

  25. }
  26. }

EmployeeBuilder

  1. package com.designpatternsinjava.builderdesignpattern;

  2. public class EmployeeBuilder {


  3. String name;
  4. String company;
  5. int id;
  6. String passport_number;
  7. String temp_address;
  8. String perm_address;
  9. int salary;
  10. public EmployeeBuilder setName(String name) {
  11. this.name = name;
  12. return this;
  13. }

  14. public EmployeeBuilder setCompany(String company) {
  15. this.company = company;
  16. return this;
  17. }

  18. public EmployeeBuilder setId(int id) {
  19. this.id = id;
  20. return this;
  21. }

  22. public EmployeeBuilder setPassport_number(String passport_number) {
  23. this.passport_number = passport_number;
  24. return this;
  25. }

  26. public EmployeeBuilder setTemp_address(String temp_address) {
  27. this.temp_address = temp_address;
  28. return this;
  29. }

  30. public EmployeeBuilder setPerm_address(String perm_address) {
  31. this.perm_address = perm_address;
  32. return this;
  33. }

  34. public EmployeeBuilder setSalary(int salary) {
  35. this.salary = salary;
  36. return this;
  37. }
  38. public Employee build(){
  39. return new Employee(name, company, id, passport_number, temp_address,
  40. perm_address, salary);
  41. }

  42. }

BuilderDemo


builder design pattern java code

  • To create object of employee class  we need to provide all the fields values to the constructor.
  • So it is somewhat difficult to pass all values all times.
  • Employee builder class taken all variables of Employee and in setter methods accepts a value and returns EmployeBuilder object.
  • And EmployeBuilder class has build method which will  assign all values to employee and returns Employee object.
  • Employee empobj= new EmployeeBuilder().setName("Saidesh").setId(1234).build();
  • Now we create object of Employee class by creating EmployeBuilder class object and caling calling setter methods whatever we have.
  • Is is very easy to set the values because we have corresponding setter method name is same as variable name.
  • After setting the values we need  to call build method so that it will return employee object with values.

Static method vs final static method in java with example programs

  • Static methods are class level so there are not part of object.
  • So we can not override static methods but we can call super class static method using subclass name or instance also.
  • If we are trying to override static methods in sub class from super class then it will be method hiding not method overriding.

  • Means whenever we call the static method on super class will call super class static method and if we are calling method using sub class it will call sub class method.
  • So it is clear that static methods are hidden not overridden and they are part of class means class level not object level.
  • Now the question is can a method be static and final together?
  • For non static methods if we declare it as final then we are preventing that method from overriding so it can not be overridden in sub class.
  • When we declare static method as final its prevents from method hiding.
  • When we declare final static method and override in sub class then compiler shows an error
  • Compile time error: Cannot override the final method from Super
  • Lets see an example program to understand this better.

Static methods in java

Program #1: Java example program to explain about static method in java

  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * @website: www.instanceofjava.com
  4.  * @category: Deference between staic and final static methods in java
  5.  */


  6. public class Super {
  7.   
  8.  
  9.  static void method(){
  10.  
  11. System.out.println("Super class method");
  12.  }

  13. }

  1. package inheritanceInterviewPrograms;

  2. //  www.instanceofjava.com 

  3. public class Sub extends Super {
  4. static void method(){
  5.  
  6. System.out.println("Sub class method");

  7. }

  8. public static void main (String args[]) {
  9. Super.method();
  10. Sub.method();
  11.  
  12.  
  13. }
  14. }

Output:

  1. Super class method
  2. Sub class method

  • When we override static methods its not overriding it is method hiding and whenever we call method on class name it will call corresponding class method.
  • If we call methods using objects it will call same methods.

Program #2: Java example program to explain about calling super class static method using sub class in java

  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * @website: www.instanceofjava.com
  4.  * @category: Deference between staic and final static methods in java
  5.  */


  6. public class Super {
  7.   
  8.  
  9.  static void method(){
  10.  
  11. System.out.println("Super class method");
  12.  }

  13. }


  1. package inheritanceInterviewPrograms;

  2. //  www.instanceofjava.com 

  3. public class Sub extends Super {
  4. public static void main (String args[]) {
  5. Super.method();
  6. Sub.method();
  7.  
  8.  
  9. }
  10. }

Output:

  1. Super class method
  2. Super class method

  • We can call super class static methods using sub class object or sub class name also.
  • Now lets see what will happen in final static methods

Final static methods in java:

  • Can a method be static and final together in java?
  • When we declare a method as final we can not override that method in sub class.
  • In the same way when we declare a static method as final we can not hide it in sub class means we can not create same method in sub class. 
  • If we try to create same static method in sub class compiler will throw an error.
  • Lets see a java example program on final static methods in inheritance.

Program #3: Java example program to explain about final static method in java

  1. package inheritanceInterviewPrograms;
  2. /*
  3.  * @website: www.instanceofjava.com
  4.  * @category: Deference between staic and final static methods in java
  5.  */


  6. public class Super {
  7.   
  8.  
  9.  final static void method(){
  10.  
  11. System.out.println("Super class method");
  12.  }

  13. }

  1. package inheritanceInterviewPrograms;

  2. //  www.instanceofjava.com 

  3. public class Sub extends Super {
  4. static void method(){  // compiler time error:

  5. System.out.println("Sub class method");
  6.  
  7. }
  8. public static void main (String args[]) {
  9. Super.method();
  10. Sub.method();
  11.  
  12.  
  13. }
  14. }

Output:


difference between static method and final method in java

Difference between float and double java

  • Float data type in java is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the mantissa
  • Where as Double is  is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.
  • Default value of float is 0.0f.
  • Default value of double is 0.0d.
  • Floating points numbers also known as real numbers and in java there are two types of floating point one is float and another one is double.

In Java, both double ;and float  are used for storing decimal numbers, but they are not the same in the following ways:


floatdouble
Size32-bit (4 bytes)64-bit (8 bytes)
Precision~6-7 decimal digits~15-16 decimal digits
Default Type No (needs f or F)Yes (default for decimals)
Performance Faster on some processorsHigher precision, may be slower
Range ±3.4 × 10³⁸±1.8 × 10³⁰⁸
Usage When memory is a concernWhen high precision is needed

When to Use:

  • Use float when dealing with graphics or in game development when memory savings are more significant than precision.
  • Use double for scientific calculations, financial applications, or whenever high precision is critical.

  • Float specifies single precision and double specifies double precision.
  • According to the IEEE standards, float is a 32 bit representation of a real number while double is a 64 bit representation
  • Normally we use double instead of float to avoid common overflow of range of numbers
  • Check below diagram for width and range of float and double data types


float vs double java


 When do you use float and when do you use double:

  • Use double data type for all your calculations and temp variables. 
  • Use float when you need to maintain an array of numbers - float[] array (if precision is sufficient), and you are dealing with over tens of thousands of float numbers.
  • Most of the math functions or operators convert/return double, and you don't want to cast the numbers back to float for any intermediate steps.

How many significant digits have floats and doubles in java?

  • In java float can handle about 7 decimal places.
  • And double can handle about 16 decimal places

Program #1: write a java example program which explains differences between float and double in java

  1. package com.instanceofjava.floatvsdouble;

  2. import java.math.BigDecimal;


  3. public class FloatVsDouble {

  4. /**
  5. * @website: www.instanceofjava.com
  6. * @category: float vs double in java with example program
  7. */
  8. public static void main(String[] args) {
  9.     float  a=10.8632667283322234f;
  10.     double b=10.8632667283322234f;
  11.     
  12.      System.out.println("float value="+a);
  13.      System.out.println("double value="+b);
  14.         
  15.      b=10.8632667283322234d;
  16.         
  17.     System.out.println("float value="+a);
  18.     System.out.println("double value="+b);
  19.        

  20. }

  21. }

Output:

  1. float value=10.863267
  2. double value=10.863266944885254
  3. float value=10.863267
  4. double value=10.863266728332224

Difference between float and double java


How to open a webpage using java code

  • We can open a website or web page using java.
  • By calling browse() method of java.awt.Desktop.getDesktop() and passing required webpage url as an URI.
  • In order to make URI create a object fro java.net.uri class objet by passing URL of the web page which need to open
  • java.awt.Desktop.getDesktop().browse(uri);
  • Lets see a java program on how to open website url.
                



Program #1: Simple Java Program to open a website or webpage using java.awt.Desktop

  1. package com.instanceofjava.openwebpage;

  2. import java.awt.Desktop;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.net.URI;

  6. public class OpenVLCPlayer {

  7. /**
  8. * @website: www.instanceofjava.com
  9. * @category: how to open a webpage in browser using java code
  10. */
  11.  
  12. public static void main(String[] args)  {
  13. try {
  14. URI uri= new URI("http://www.instanceofjava.com");
  15. java.awt.Desktop.getDesktop().browse(uri);
  16. System.out.println("Web page opened in browser");
  17.  
  18. } catch (Exception e) {
  19. e.printStackTrace();
  20. }
  21. }

  22. }

Output:


  1. Web page opened in browser
     

how to open a webpage using java code

JSP Jstl if else statement with mutilple conditions

  • JSTL means Java Server pages standard Tag Library.
  • JSTL is a collection of useful JSP tags to simplify the JSP development.
  • Lets see how to write  if and if else statements  in java server pages using JSTL



JSTL If condition in JSP :

  • We can use JSTL tags by providing 
  • <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

Program  #1: Write a program to how to use JSTL if condition in Java server pages


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5.  
  6. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  10. <title>JSTL if condition</title>
  11. </head>
  12. <body>
  13.  
  14.  <c:set var="age" scope="session" value="${20}"/>
  15. <c:if test="${age > 18}">
  16.    <p>My age is: <c:out value="${age}"/><p>
  17. </c:if>
  18. </body>
  19. </html>
Output:


jstl if condition
JSTL If  else condition in JSP :
  • We use  c:when and c:otherwise tags in JSTL like if else in java

Program  #2: Write a program to how to use JSTL if  else condition in Java server pages


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5.  
  6. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  10. <title>JSTL if else condition</title>
  11. </head>
  12. <body>
  13.  
  14.  <h1>c:when, c:otherwise, c:choose</h1>  
  15.  
  16. <c:set  var="year" value="2016" ></c:set>  
  17. <c:choose>  
  18. <c:when test="${year%4==0}">  
  19. <c:out value="leap year"></c:out>  
  20. </c:when>  
  21. <c:otherwise>  
  22. <c:out value="Not Leap year"></c:out>  
  23. </c:otherwise>  
  24. </c:choose>
  25.  
  26. </body>
  27. </html>
Output:


jstl if else statement





JSTL  multiple If  else conditions in JSP :
  • We use  c:when and c:otherwise tags in JSTL like if else if else in java server pages.
  • Lest wee how to use if else ladder and compare string in jstl  multiple if else conditions.
  • Jstl if else statement multiple conditions

Program  #3: Write a program to how to use  comparing string in JSTL multiple  if  else condition in Java server pages


  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  2.     pageEncoding="ISO-8859-1"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.or
  4. /TR/html4/loose.dtd">
  5.  
  6. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  7. <html>
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  10. <title>JSTL multiple if else condition/ if else ladder</title>
  11. </head>
  12. <body>
  13.  
  14. <c:set  var="Day" value="Friday" ></c:set>  
  15. <c:choose>  
  16. <c:when test="${Day=='Sunday'}">  
  17. <c:out value="Its SunDay"></c:out>  
  18. </c:when> 
  19. <c:when test="${Day=='Monday'}"> 
  20. <c:out value="Its MonDay"></c:out>  
  21. </c:when>  
  22. <c:when test="${Day=='Tuesday'}"> 
  23. <c:out value="Its TuesDay"></c:out>  
  24. </c:when>  
  25. <c:when test="${Day=='Wednesday'}"> 
  26. <c:out value="Its Wednesday"></c:out>  
  27. </c:when>  
  28. <c:when test="${Day=='Thursday'}"> 
  29. <c:out value="Its ThursDay"></c:out>  
  30. </c:when>  
  31. <c:when test="${Day=='Friday'}"> 
  32. <c:out value="Its FriDay"></c:out>  
  33. </c:when>  
  34. <c:otherwise>  
  35. <c:out value="Its Saturday"></c:out>  
  36. </c:otherwise>  
  37. </c:choose>  
  38.  
  39. </body>
  40. </html>
Output:


jstl if statement multiple conditions
Select Menu