Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

My Day 16 in Bangalore: Exploring Java Libraries, String & Math Functions with Real-Life Examples

A Day in Bangalore, Exploring Java Libraries, String & Math Functions with Real-Life Examples – March 8, 2025

Today marks my 16th day in Bangalore, on the 8th of March, 2025. My alarm rang at 4:30 AM, and I woke up feeling refreshed. I had my breakfast, which was sehri, then offered namaz before heading back to bed. I thought I would take a short nap for about half an hour, so I set my alarm for 7:30 AM. However, I fell asleep again, and the next time I opened my eyes, it was already 7:55 AM, almost 8 AM! I quickly got up, got ready, and rushed to the institute. I was 20 minutes late, and by the time I arrived, the teacher had already started dictating notes. I sat down and started writing whatever I could from what was being dictated.

After a while, my friends called me over to sit with them, so I joined them. The first period was over, and I quickly noted everything that had been taught. After this, the teacher gave us a short 20-minute break. We all went near the park, but since we didn’t have enough time, we skipped entering the park. My friends had breakfast, and we headed back to the institute as soon as we could.

The second class began with a new topic—Java Libraries. After a while, the teacher gave another 20-minute break. During this break, the teacher attended a felicitation party for the students who had been placed the previous month. We all went out for coffee, and while sipping our drinks, my friend Rishika asked why I seemed upset. I shared my thoughts, which helped me feel a bit better.

The next class started, and the teacher continued teaching about building functions in Java. By 1:30 or 1:45 PM, the class ended. The teacher mentioned that the next topic—Math Functions—would be better to start after lunch, as we would need to revise it later. So, we had a break for lunch. My friends went to the park to have lunch, but I went to the masjid to offer Juhar Namaz. I planned to sleep afterward, but two of my friends called, so I decided to meet them. After chatting with them for a while, I went back to class.

By this time, the teacher began explaining Math Functions in Java. I was very sleepy during this session since I hadn’t napped during lunch. I did my best to stay awake, but it was a challenge! As soon as the class was over, I went to the Attitude Class to punch in my attendance. After that, I returned to my room, grabbed my bag, and spent some time scrolling through social media. Soon, I felt sleepy again, so I took a 1-hour nap.

After waking up, I went to offer Asr Namaz. I then had iftar, which included a few new dishes. After iftar, I offered Maghrib Namaz and went to a nearby restaurant to have some Biryani. While eating, I received a call from my mother asking whether I had had my dinner yet. After the meal, I went to an ice cream stall and had a Badam Shake, which was filling.

Later in the evening, I got mentally disturbed by an old acquaintance, which made me anxious and distracted. This mental disturbance delayed the writing of this blog, but I managed to finish it eventually.

When I went downstairs for dinner, the kitchen had already closed, and the food was finished. Only a small portion was left, so I quickly washed my tiffin and grabbed what I could. I knew that wouldn’t be enough for me, so I went out to buy milk. After visiting 4-5 shops and not finding a small quantity, I settled for a half-liter of milk and two buns. I returned to my PG, had the milk and buns, and then went to sleep, mentally exhausted. I woke up later and wrote this blog.

That’s it for today. See you tomorrow!


Java Concepts Explained

In today’s classes, we covered several important Java concepts that are crucial for any beginner to understand. Let’s break them down:

Java Libraries

In Java, libraries are collections of pre-written code that can be used by developers to avoid writing repetitive code. These libraries are groups of related classes and functions that can be imported and used in programs. Java provides built-in libraries, such as the Math library, which help us perform specific tasks without having to write our own algorithms from scratch.

Types of Libraries:

  1. Built-in Libraries: These are the libraries that come with the Java standard API, like java.util, java.lang, java.io, etc.
  2. User-defined Libraries: These libraries are created by users to perform specific tasks. For instance, if you are working on a project and need to write a set of classes for file handling, you can create your own library.

String Functions in Java

Strings are one of the most commonly used data types in Java. Here are some important functions that help manipulate strings:

  • length()
    This function returns the number of characters in a string.
    Example: javaCopyString text = "Hello"; System.out.println(text.length()); // Output: 5
  • charAt()
    This function returns the character at a specified index in the string.
    Example: javaCopyString text = "Hello"; System.out.println(text.charAt(0)); // Output: 'H'
  • indexOf()
    This function returns the index of the first occurrence of a specified character or substring.
    Example: javaCopyString text = "Hello"; System.out.println(text.indexOf('e')); // Output: 1
  • lastIndexOf()
    This function returns the index of the last occurrence of a specified character or substring.
    Example: javaCopyString text = "Hello"; System.out.println(text.lastIndexOf('l')); // Output: 3
  • toUpperCase()
    This function converts all characters in the string to uppercase.
    Example: javaCopyString text = "hello"; System.out.println(text.toUpperCase()); // Output: "HELLO"
  • toLowerCase()
    This function converts all characters in the string to lowercase.
    Example: javaCopyString text = "HELLO"; System.out.println(text.toLowerCase()); // Output: "hello"
  • startsWith()
    This function checks whether a string starts with a specified prefix.
    Example: javaCopyString text = "Hello"; System.out.println(text.startsWith("He")); // Output: true
  • endsWith()
    This function checks whether a string ends with a specified suffix.
    Example: javaCopyString text = "Hello"; System.out.println(text.endsWith("lo")); // Output: true
  • contains()
    This function checks if the string contains a specific substring.
    Example: javaCopyString text = "Hello"; System.out.println(text.contains("ell")); // Output: true
  • substring()
    This function extracts a substring from the string starting from a specified index.
    Example: javaCopyString text = "Hello"; System.out.println(text.substring(1, 4)); // Output: "ell"
  • equals()
    This function compares two strings for equality.
    Example: javaCopyString text1 = "Hello"; String text2 = "Hello"; System.out.println(text1.equals(text2)); // Output: true
  • split()
    This function splits the string into an array of substrings based on a delimiter.
    Example: javaCopyString text = "Hello World"; String[] words = text.split(" "); System.out.println(words[0]); // Output: "Hello"
  • toCharArray()
    This function converts the string into an array of characters.
    Example: javaCopyString text = "Hello"; char[] chars = text.toCharArray(); System.out.println(chars[0]); // Output: 'H'

Math Functions in Java

Java provides a Math library with several built-in functions to perform mathematical calculations.

  • Math.pow()
    This function returns the value of the first argument raised to the power of the second argument.
    Example: javaCopySystem.out.println(Math.pow(2, 3)); // Output: 8.0
  • Math.sqrt()
    This function returns the square root of a number.
    Example: javaCopySystem.out.println(Math.sqrt(9)); // Output: 3.0
  • Math.cbrt()
    This function returns the cube root of a number.
    Example: javaCopySystem.out.println(Math.cbrt(27)); // Output: 3.0
  • Math.random()
    This function generates a random number between 0 (inclusive) and 1 (exclusive).
    Example: javaCopySystem.out.println(Math.random()); // Output: Random value between 0 and 1

You can also scale this random value to get larger numbers, for example, for generating OTPs.

  • OTP Generation Example: javaCopyint otp = (int) (Math.random() * 1000000); // Generates a 6-digit OTP System.out.println("Generated OTP: " + otp);

Scanner Library for User Input

In Java, the Scanner class is used to get input from the user. Here are some of the methods from the Scanner library:

  • nextInt()
    Reads an integer from the user.
    Example: javaCopyScanner scanner = new Scanner(System.in); int num = scanner.nextInt();
  • nextFloat()
    Reads a float from the user.
    Example: javaCopyfloat value = scanner.nextFloat();
  • nextDouble()
    Reads a double from the user.
    Example: javaCopydouble value = scanner.nextDouble();
  • next()
    Reads a single word (a string) from the user.
    Example: javaCopyString name = scanner.next();
  • nextLine()
    Reads an entire line of text from the user.
    Example: javaCopyString sentence = scanner.nextLine();
  • nextCharAt()
    Reads the next character input.
    Example: javaCopychar character = scanner.next().charAt(0);

Conclusion

Today we learned many useful concepts and functions in Java, such as how to manipulate strings, use math functions, and work with input from the user. By understanding these Java libraries and methods, you will be able to write cleaner and more efficient code. Keep practicing, and happy coding!

Also see: The Z Blogs

my other Blog: The Z Blog ZB

Do share with your friends and family
The Z blogs
The Z blogs
Articles: 148

Leave a Reply

Your email address will not be published. Required fields are marked *