<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: LING TI LEE</title>
    <description>The latest articles on DEV Community by LING TI LEE (@tilee).</description>
    <link>https://dev.to/tilee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2205489%2F2bf73df7-40fa-4f29-8c4b-352a1f5848a2.jpg</url>
      <title>DEV Community: LING TI LEE</title>
      <link>https://dev.to/tilee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tilee"/>
    <language>en</language>
    <item>
      <title>Java Learning Journey 1.0</title>
      <dc:creator>LING TI LEE</dc:creator>
      <pubDate>Sat, 19 Oct 2024 09:56:28 +0000</pubDate>
      <link>https://dev.to/tilee/java-learning-journey-10-14fh</link>
      <guid>https://dev.to/tilee/java-learning-journey-10-14fh</guid>
      <description>&lt;p&gt;I recently learned Java through the practices in [&lt;a href="https://exercism.org/tracks/java/exercises" rel="noopener noreferrer"&gt;https://exercism.org/tracks/java/exercises&lt;/a&gt;]. My current progress is 13 out of a total of 148 practices. I would like to share what I learned.&lt;/p&gt;

&lt;p&gt;This post introduce my understanding about .split(), .trim(), .isDigit(), .isLetter(), Comparable&amp;lt;T&amp;gt;, User-defined Java Exceptions, and Interface.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) .split() &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: The &lt;code&gt;.split()&lt;/code&gt; method divides String into an array based on the separator [1].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public String[] split(String regex, int limit)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameter&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;regex: (required field) pattern of separator&lt;/li&gt;
&lt;li&gt;limit: (optional field) maximum length of the returned array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main{

    public void getProductPrice(String products){
        double totalPrice = 0.0;
        StringBuilder priceDetails = new StringBuilder();

        String[] singleProduct = products.split("; ");

        for(int i = 0; i &amp;lt; singleProduct.length; i++){
            String[] productInfo = singleProduct[i].split(", ");
            totalPrice += Double.parseDouble(productInfo[2]);

            priceDetails.append(productInfo[2]);
            if(i &amp;lt; singleProduct.length - 1){
                priceDetails.append(" + ");
            }
        }

        System.out.println(priceDetails + " = " + totalPrice);
    }

    public static void main(String arg[]){
        Main obj = new Main();
        obj.getProductPrice("1, dragonfruit, 12.50; 2, guava, 23.45; 3, avocado, 395.67");
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12.50 + 23.45 + 395.67 = 431.62
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2) .trim() &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: The &lt;code&gt;.trim()&lt;/code&gt; method removes whitespace from both ends of a string [2].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public String trim()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameter&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no parameter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main{
    public static void main(String args[]){
        String str = "   You can do it!   ";
        System.out.println(str);
        System.out.println(str.trim());
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   You can do it!   
You can do it!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3) .isDigit() &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: The &lt;code&gt;.isDigit()&lt;/code&gt; method determines whether a character is digit or not [3].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static boolean isDigit(char ch)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameter&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ch: (required field) the character value to be tested&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main{

    // return true when the given parameter has a digit
    public boolean searchDigit(String str){
        for(int i = 0; i &amp;lt; str.length(); i++){
            // charAt() method returns the character at the specified index in a string
            if(Character.isDigit(str.charAt(i))){
                return true;
            }
        }
        return false;
    }

    // print digit index and value
    public void digitInfo(String str){
        for(int i = 0; i &amp;lt; str.length(); i++){
            if(Character.isDigit(str.charAt(i))){
                System.out.println("Digit: " + str.charAt(i) + " found at index " + i);
            }
        }
    }

    public static void main(String args[]){
        Main obj = new Main();
        String[] strList = {"RT7J", "1EOW", "WBJK"};

        for(String str : strList){
            if(obj.searchDigit(str)){
                obj.digitInfo(str);
            }else{
                System.out.println("No digit");
            }
        }

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Digit: 7 found at index 2
Digit: 1 found at index 0
No digit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4) .isLetter() &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: The &lt;code&gt;.isLetter()&lt;/code&gt; method determines whether a character is letter or not [4].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public static boolean isLetter(char ch)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Parameter&lt;/strong&gt;: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ch: (required field) the character value to be tested&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Main{

    // check whether phoneNum has letter
    public void searchLetter(String phoneNum){
        boolean hasLetter = false;

        for(int i = 0; i &amp;lt; phoneNum.length(); i++){
            if(Character.isLetter(phoneNum.charAt(i))){
                hasLetter = true;
                // return letter value and index
                System.out.println(phoneNum + " has letter '" + phoneNum.charAt(i) + "' at index " + i);
            }
        }

        // phone number is valid when no letter
        if(!hasLetter){
            System.out.println(phoneNum + " is valid");
        }

        System.out.println();
    }

    public static void main(String args[]){
        Main obj = new Main();
        String[] phoneNum = {"A0178967547", "0126H54786K5", "0165643484"};

        for(String item: phoneNum){
            obj.searchLetter(item);
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;A0178967547 has letter 'A' at index 0

0126H54786K5 has letter 'H' at index 4
0126H54786K5 has letter 'K' at index 10

0165643484 is valid
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5) Comparable&amp;lt;T&amp;gt; &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: The &lt;code&gt;Comparable&amp;lt;T&amp;gt;&lt;/code&gt; interface is used to define a natural ordering for a collection of objects, and it should be implemented in the class of the objects being compared [5]. The type parameter &lt;code&gt;T&lt;/code&gt; represents the type of objects that can be compared.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: Employee.java
public class Employee implements Comparable&amp;lt;Employee&amp;gt;{
    private String email;
    private String name;
    private int age;

    public Employee(String email, String name, int age){
        this.email = email;
        this.name = name;
        this.age = age;
    }

    // The Comparable interface has a method called compareTo(T obj). 
    // This method helps decide how to order objects, so they can be sorted in a list
    @Override
    public int compareTo(Employee emp){
        // compare age: 
        // return this.age - emp.age;
        // (this.age - emp.age) = negative value means this.age before emp.age; 
        // (this.age - emp.age) = positive means this.age after emp.age

        // compare email:
        return this.email.compareTo(emp.email);
    }

    @Override
    public String toString(){
        return "[email=" + this.email + ", name=" + this.name + ", age=" + this.age +"]";
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: Main.java
import java.util.Arrays;

public class Main {
    public static void main(String args[]){
        Employee[] empInfo = new Employee[3];
        empInfo[0] = new Employee("joseph@gmail.com", "Joseph", 27);
        empInfo[1] = new Employee("alicia@gmail.com", "Alicia", 30);
        empInfo[2] = new Employee("john@gmail.com", "John", 24);

        Arrays.sort(empInfo);
        System.out.println("After sorting:\n" + Arrays.toString(empInfo));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;After sorting:
[[email=alicia@gmail.com, name=Alicia, age=30], [email=john@gmail.com, name=John, age=24], [email=joseph@gmail.com, name=Joseph, age=27]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  6) User-defined Java Exceptions &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Definition&lt;/strong&gt;: User-defined Java Exception is a custom exception that a developer creates for handling specific error conditions [6].&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: InsufficientFundsException.java
public class InsufficientFundsException extends RuntimeException {
    public InsufficientFundsException(String message){
        super(message);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: Main.java
public class Main{
    private static double currentSaving = 1000.0;

    public static String bankAccount(double withdrawMoney){
        if(withdrawMoney &amp;gt; currentSaving){
            // user-defined exception
            throw new InsufficientFundsException("Insufficient balance");
        }

        return "Withdraw success, remaining balance RM " + (currentSaving - withdrawMoney);
    }

    public static void main(String args[]){
        try{
            System.out.println(bankAccount(1500.0));
        } catch (InsufficientFundsException e){
            System.out.println(e.getMessage());
        }
    }
} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Insufficient balance
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  7) Interface &lt;a&gt;&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Interfaces in Java allow users to invoke the same method across various classes, each implementing its own logic [7]. In the example below, the method &lt;code&gt;calculatePrice()&lt;/code&gt; is called in different classes, such as &lt;code&gt;Fruit&lt;/code&gt; and &lt;code&gt;DiscountFruit&lt;/code&gt;, with each class applying its own unique calculation logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: FruitPrice.java
interface FruitPrice{
    void calculatePrice(int quantity);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: Fruit.java
public class Fruit implements FruitPrice{
    private String fruit;
    private double price;

    public Fruit(String fruit, double price){
        this.fruit = fruit;
        this.price = price;
    }

    @Override
    public void calculatePrice(int quantity){
        System.out.println("Total price for " + quantity + " " + fruit + " is RM " + price*quantity);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: DiscountFruit.java
public class DiscountFruit implements FruitPrice{
    private String fruit;
    private double price;
    private int percentage;

    public DiscountFruit(String fruit, double price, int percentage){
        this.fruit = fruit;
        this.price = price;
        this.percentage = percentage;
    }

    @Override
    public void calculatePrice(int quantity){
        double total = price * quantity * ((100.0-percentage)/100.0);
        System.out.println("After " + percentage + "% discount, the price for " + quantity + " " + fruit + " is RM " + total);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// file: Main.java
public class Main{
    public static void main(String args[]){
        Fruit apple = new Fruit("apple", 2.0);
        apple.calculatePrice(5);

        DiscountFruit orange = new DiscountFruit("orange", 2, 10);
        orange.calculatePrice(5);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Total price for 5 apple is RM 10.0
After 10% discount, the price for 5 orange is RM 9.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;p&gt;[1] &lt;a href="https://javarush.com/en/groups/posts/en.2907.split-method-in-java-split-string-into-parts" rel="noopener noreferrer"&gt;JavaRush, split method in java: split string into parts, 8 August 2023&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[2] &lt;a href="https://www.w3schools.com/java/ref_string_trim.asp" rel="noopener noreferrer"&gt;W3Schools, Java String trim() Method&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[3] &lt;a href="https://www.geeksforgeeks.org/character-isdigit-method-in-java-with-examples/" rel="noopener noreferrer"&gt;GeeksforGeeks, Character isDigit() method in Java with examples, 17 May, 2020&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[4] &lt;a href="https://www.geeksforgeeks.org/character-isdigit-method-in-java-with-examples/" rel="noopener noreferrer"&gt;tutorialspoint, Java - Character isLetter() method&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[5] &lt;a href="https://www.digitalocean.com/community/tutorials/comparable-and-comparator-in-java-example" rel="noopener noreferrer"&gt;DigitalOcean, Comparable and Comparator in Java Example, August 4, 2022&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[6] &lt;a href="https://www.shiksha.com/online-courses/articles/user-defined-exception-in-java-blogId-148713#:~:text=User%2Ddefined%20exceptions%20are%20used,to%20the%20application's%20unique%20requirements." rel="noopener noreferrer"&gt;Shiksha, Understanding User Defined Exception in Java, Apr 25, 2024&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;[7] &lt;a href="https://www.scientecheasy.com/2019/06/java-interface-use.html/" rel="noopener noreferrer"&gt;Scientech Easy, Use of Interface in Java with Example, July 9, 2024&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>java</category>
    </item>
  </channel>
</rss>
