Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses
Showing posts with label SCJP. Show all posts
Showing posts with label SCJP. Show all posts

How to implement Comparable Interface in Java with an example ?

Program to demonstrate how to implement Comparable Interface in Java with an example.

package com.hubberspot.code;

import java.util.ArrayList;
import java.util.Collections;

public class Student implements Comparable {

 public String name;
 public double percentage;

 Student(String name, double percentage)
 {
  this.name = name; 
  this.percentage = percentage;
 }

 public String toString()
 {
  return "\nName = " + name + "\n"+
    "Percentage = " + percentage+"\n";
 }


 @Override
 public int compareTo(Object o) {
  Student other = (Student) o;

  Double percentage1 = (Double) this.percentage;
  Double percentage2 = (Double) other.percentage;

  return percentage1.compareTo(percentage2);

 }


 public static void main(String[] args) {

  ArrayList list = new ArrayList();

  list.add(new Student("Dinesh",76.2));
  list.add(new Student("Jonty",96.5));
  list.add(new Student("Gunjan",81.7));
  list.add(new Student("Parishrut",62.1));

  System.out.println("Before Sorting : \n" + list);

  Collections.sort(list);

  System.out.println("\nAfter Sorting : \n" + list);

 }
}



Output of the program :



How to compare Strings in Java with or without case sensitivity ?

Program to compare Strings in Java with or without case sensitivity

package com.hubberspot.example;

public class StringComparison {
 
   public static void main(String[] args) {
  
 String first = "StringComparison";
 String second = "StringComparison";
 String third = "stringcomparison";
 String fourth = "TesTcoMpaRisOn";
 String fifth = "STRINGCOMPARISON";
  
 System.out.println("first.equals(second) : "+first.equals(second));
 System.out.println("first.equals(third) : "+first.equals(third));
 System.out.println("first.equals(fourth) : "+first.equals(fourth));
 System.out.println("first.equals(fifth) : "+first.equals(fifth));
 System.out.println();
 System.out.println("first.equalsIgnoreCase(second) : "
                    +first.equalsIgnoreCase(second));
 System.out.println("first.equalsIgnoreCase(third) : "
                    +first.equalsIgnoreCase(third));
 System.out.println("first.equalsIgnoreCase(fourth) : "
                    +first.equalsIgnoreCase(fourth));
 System.out.println("first.equalsIgnoreCase(fifth) : "
                    +first.equalsIgnoreCase(fifth));
 System.out.println();
  
 System.out.println("second.equals(third) : "+second.equals(third));
 System.out.println("second.equals(fourth) : "+second.equals(fourth));
 System.out.println("second.equals(fifth) :    "+second.equals(fifth));  
 System.out.println();
 System.out.println("second.equalsIgnoreCase(third) : "
                    +second.equalsIgnoreCase(third));
 System.out.println("second.equalsIgnoreCase(fourth) : "
                    +second.equalsIgnoreCase(fourth));
 System.out.println("second.equalsIgnoreCase(fifth) : "
                    +second.equalsIgnoreCase(fifth));  
 System.out.println();
  
 System.out.println("third.equals(fourth) : "+third.equals(fourth));
 System.out.println("third.equals(fifth) : "+third.equals(fifth));
 
 System.out.println("third.equalsIgnoreCase(fourth) : "
                     +third.equalsIgnoreCase(fourth));
 System.out.println("third.equalsIgnoreCase(fifth) : "
                     +third.equalsIgnoreCase(fifth));
  
 System.out.println();
 System.out.println("fourth.equals(fifth) : "+fourth.equals(fifth));
 System.out.println();
 System.out.println("fourth.equalsIgnoreCase(fifth) : "
                     +fourth.equalsIgnoreCase(fifth));
 System.out.println();
   }

}




Output of the program :




Program demonstrating Switch statement working in Java

Program to demonstrate working of Switch statements in Java



package com.hubberspot.examples;

import java.util.Scanner;

public class SwitchTest {

 
    public static void main(String[] args) {
      
     Scanner scanner = new Scanner(System.in);
      
        System.out.println("This seasons IPL Teams : ");
 System.out.println("***********************************");         
        System.out.println("1. Deccan Chargers");
        System.out.println("2. Kolkata Night Riders");
        System.out.println("3. Mumbai Indians");
        System.out.println("4. Chennai Super Kings");
        System.out.println("5. Rajasthan Royals");
        System.out.println("6. Royal Challengers Bangalore");
        System.out.println("7. Kings XI Punjab");
        System.out.println("8. Pune Warriors India");
        System.out.println("9. Delhi Daredevils");
        System.out.print("");
        System.out.println("***********************************");
        System.out.print("Please choose your favorite IPL team: ");
  
        int team = scanner.nextInt();    
  
        System.out.print("So you like: ");
         
        switch (team) {
            case 1:
                System.out.println("Deccan Chargers"); 
                break;
            case 2:
                System.out.println("Kolkata Night Riders"); 
                break;
            case 3:
                System.out.println("Mumbai Indians"); 
                break;
            case 4:
                System.out.println("Chennai Super Kings"); 
                break;
            case 5:
                System.out.println("Rajasthan Royals"); 
                break;
            case 6:
                System.out.println("Royal Challengers Bangalore"); 
                break;
            case 7:
                System.out.println("Kings XI Punjab"); 
                break;
            case 8:
                System.out.println("Pune Warriors India"); 
                break;
            case 9:
             System.out.println("Delhi Daredevils"); 
             break;                 
            default:
                System.out.println("Invalid IPL team choosen");
        }
    }
}





Output of the program :



How Java Source File Structure is written in a program ?.

Java Source File Structure

A simple source file is considered to be a Java File, if it follows some rules and regulations. The Java Source file maintains a order and various elements that occur in that source file should follow the order. Java Source File structure should follow rules and order as follows :

1. Java source file should start with a package declaration. The package declaration is optional for Java source file. If the package declaration is default than the package declaration is optional.

2. After the package declaration there comes a section of import declarations. Import declarations can be zero or more. These import statements introduce type or static member names in the source code. Its mandatory to place all import declarations before actual class declarations in Java source code.

3. After import statements comes the type declarations statements. These statements have type declarations which are considered top-level. There can be any number of such type declarations, generally consisting of class , interface and enum etc. The order of type declarations is not mandatory.

4. The Java source file should have one and only one public class. The class name which is defined as public should be the name of Java source file along with .java extention.

What syntax goes in a Class Declaration in Java ?

Introduction : 

A class is  nothing but a blue print for Java object. It is the logical representation of object. An Object is physical form of class. The syntax for representing a class in Java consists of many elements : 

General Syntax of Class Declarations in Java  

< class modifiers > class < class name > < type parameter list >
                         < extends clause > < implements clause >

{   // body of class 

< field declarations >

< method declarations >

< nested class declarations >

< nested interface declarations >

< nested enum declarations >

< constructor declarations >

< initializer blocks >

} 


 
© 2021 Learn Java by Examples Template by Hubberspot