Skip to main content

Posts

Showing posts from October, 2017

ASCII Code in Java (Part 2)

> The ASCII Code: (Part 2) Printing the ASCII code for a character: Printing the character based on the ASCII code input (Reverse of the previous program): Printing the ASCII code every character in an input String: Click HERE for ASCII Code Information (Part 1) Click HERE for the Methods of Character class.

ASCII Code in Java (Part 1)

> The ASCII Code: (Part 1) ASCII stands for "American Standard Code for Information Interchange". As you may remember (Grade 9), computers only work with HIGH(1) and LOW(0) electrical states, known as bits, with correspond to mathematical Base 2 numbers. That is, computers only understand binary language. ASCII codes represent text (or other things) in computers. Assume that you are working with MS Word, or PPT or any other tool that uses text based inputs from user. You need to type a sentence that computer is not aware of. ASCII codes help to exchange this information between user and computer. When you type a character, it is converted into ASCII code first and then into Binary, which makes the computer understand what is to be typed. Hence every key on the keyboard has a specific ASCII code which is used to make the computer understand our language. If you press 4 from keyboard, then keyboard send the value 100 (value equival

Character class in Java (Part 1)

> The Character Class: (Part 1) A character data type represents a single character. The character data type, char, is used to represent a single character. A character literal is enclosed in single quotation marks. For example: (char literals) char letter = 'A'; char numChar = '4'; Here, the first statement assigns character A to the char variable letter. The second statement assigns digit character 4 to the char variable numChar. NOTE: A string literal must be enclosed in quotation marks (" "). A character literal is a single character enclosed in single quotation marks (' '). Therefore, "A" is a string, but 'A' is a character. Also, a string is a combination of one or more characters. We can also see Strings as "A Sequence of Characters". Every key on the keyboard has a specific integer value. It is known as the ASCII code. See HERE for more information. Methods of Character class:

Arrays in Java (Part 6)

> Arrays in Java: (Part 6) One Dimensional Array : 7. Linear Search in Java (for integers) 8. Linear Search in Java (for Strings) 9. Print the strings that ends with a vowel from an array of strings Example: (Linear Search in Java -> for Integers) Example: Linear Search in Java (for Strings) Example: (Linear Search - find all the possible locations of the key entered by user)

Arrays in Java (Part 5)

> Arrays in Java: (Part 5) One Dimensional Array : 4. Print the strings that are of even length from an array of strings 5. Print the strings that start with a vowel from an array of strings 6. Print the strings that ends with a vowel from an array of strings Example: (Print the strings that are of even length) Example: (Print the strings that start with a vowel) Example: (Print the strings that end with a vowel)

Arrays in Java (Part 4)

> Arrays in Java: (Part 4) One Dimensional Array : 1. Sum all elements of integer array 2. Find maximum and minimum element 3. Find the longest string (largest length string) Example: (Sum all elements of integer array) Example: (Find maximum and minimum element from integer array) Example: (Find longest string in a string array in java)

Arrays in Java (Part 3)

> Arrays in Java: (Part 3) One Dimensional Array (Accepting array and printing programs): Example: (Print static array (int) in Java) Example: (Print Dynamic array (int) in Java) Example: (Print static array (String) in Java) Example: (Print dynamic array (String) in Java)

Arrays in Java: (Part 2)

> Arrays in Java: (Part 2) One Dimensional Array: - Array in the simplest form is one dimensional array (One-D). It has only rows for storing records. One D array of size 6 can be pictured as follows: Creating one dimensional arrays (STATIC): Step 1: Declaration: The syntax of declaring STATIC one dimensional arrays is: datatype array_name[]; OR datatype []array_name; Step 2: Initializing: The syntax of initializing STATIC one dimensional array is: array_name = {value1, value2, value3 … valueN}; As the values are given in “Initializing” step in STATIC array, there is no need of third step here. [Step1 and 2 can be combined as follows:] Declaring an Integer data type array : int m[] = {21,85,48,54,78,96,321,54,47,87}; Declarin

Arrays in Java (Part 1)

> Arrays in Java: (Part 1) Introduction: So far, we have only used simple variables to store values of different data types. However, in certain situations, it will be easier to write programs that uses list type structure to store similar type of data. For example, if we want to record the student marks for a class of 60 students, we have to create variables like student_1, student_2, student_3… student_60. (Similarly, if we are to store the details like Student_Name, Student_Email of N number of students, its hard to store information in variables) This is tedious and it increases the complexity and length (number of Lines Of Code: LOC of programs). This problem can be simplified by using arrays that uses a common name with a subscript representing the index of each element. If we use array for storing marks of 60 students, it will go like this: student[1], student[2], student[3]… student[60]. An array is an ordered sequence of finite data items of the same data t

String class in Java (Part 4)

> The String Class: (Part 4) Indexing Methods of String class: Example: (indexOf -> ch -> two arguments) Example: (indexOf -> ch -> one argument) Example: (indexOf -> string -> two arguments) Example: (indexOf -> string -> one argument) lastIndexOf methods resemble indexOf, only difference is -> they search for the last occurrences instead of the first occurrences. Example: All lastIndexOf Methods: Click HERE for the Simple Methods of String class. Click HERE for the Comparison Methods of String class. Click HERE for the Substring Methods of String class.

String class in Java (Part 3)

> The String Class: (Part 3) Substring Methods of String class: Note 1: Make sure that substring is the only String class method in Java, that does not follow Camel-case. Note 2: Indexes are managed as shown in the following example: For example: Considering String message = "Welcome to Java"; Example: (substring ->one argument) Example: (substring ->two arguments) Click HERE for the Simple Methods of String class. Click HERE for the Comparison Methods of String class.

String Class in Java (Part 2)

> The String Class: (Part 2) Comparison Methods of String class: Example: (equals) Example: (equalsIgnoreCase) Example: (compareTo) Example: (compareToIgnoreCase) Example: (startsWith) Example: (endsWith) Example: (contains) Click HERE for the Simple Methods of String class. Click HERE for the Substring Methods of String class.

String Class in Java (Part 1)

> The String Class: (Part 1) The String class is under Lang package in Java. But as this package is by-default imported in Java, there is not need to write the import statement to use String in your program. The char type represents only one character. To represent a string of characters, we can use the data type called String. For example, the following code declares message to be a string with the value "Welcome to Java". String message = "Welcome to Java"; A String object is immutable: Its content cannot be changed once the string is created. Creating Strings: A String is combination of one or more characters. There are three methods to create Strings: By creating object of String class: Syntax: String object_name = new String(“any_string”); For example: String name = new String(“Mr. Harvey Specter”);

Data types in Java (Part 2)

> Data types in Java: (Part 2) Floating point numbers: - Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or for calculating math functions such as sine and cosine, result in a value whose precision requires a floating-point type. Float: Use: The type float specifies a single-precision value that uses 32 bits of storage. Variables are declared as float when fractional component is needed. Declaration: float f = 10.1; or float f = 10.1F; Double: Use: The type double specifies a double-precision value that uses 64 bits of storage. Double precision is actually faster than single precisi

Data types in Java (Part 1)

> Data types in Java: (Part 1) Data Types: The type of data a variable can hold. The 8 primitive datatypes are grouped together into 4 main groups: Integers: This group includes byte, short, int, and long, which are for whole-valued signed numbers. Floating-point numbers: This group includes float and double, which represent numbers with fractional precision. Characters: This group includes char, which represents symbols in a character set, like letters and numbers. Boolean: This group includes boolean, which is a special type for representing true/false values. Let us see each one in detail: Integers: Java defines four integer types: byte, short, int, and long. All of these are signed, positive and negative values. Java does not support unsigned, positive-only integers. The width of an integer defines the behavior it defines for variables and expressions of that type. The Java run-time environment is free

Characteristics of Java OR Java Buzzwords (Part 3)

> Java Properties: (Part 3) Distributed: Java is designed for the distributed environment of the Internet, because it handles TCP/IP protocols. The protocols designed for inter-process communications between client and server, or host machine and server machine can be easily implemented in java. The network layer facilities are provided by inbuilt libraries and functions in java. Secure: No pointers are used in Java. Bytecode verifier: The JVM verifies all bytecode before it is executed. This verification consists primarily of three types of checks: Branches are always to valid locations Data is always initialized and references are always type-safe Access to private or package private data and methods is rigidly controlled. Class loader: Classes are introduced into the Java environment when they are referenced by name in a class th

Characteristics of Java OR Java Buzzwords (Part 2)

> Java Properties: (Part 2) Robust: Java is robust because it is more reliable. It ensures the reliability by providing early checking for possible errors. It eliminates error causing constructs such as pointers. Java restricts the programmers in a few key areas, to force them to find mistakes early in program development. At the same time, Java frees from having to worry about many of the most common causes of programming errors. Because Java is a strictly typed language, it checks the code at compile time. However, it also checks your code at run time. Consider two main reasons of program failure: Memory management mistakes and Mishandled exceptional conditions (run time errors). Memory management is difficult and tedious in c/c++, the programmers have to manually allocate and free memory. This sometimes leads to problems, because programmers will either forget to free memory that has been previously allocated or, worse, try to

Characteristics of Java OR Java Buzzwords (Part 1)

> Java Properties: The Java programming language is a high-level language that can be characterized by all the following buzzwords: Simple: Java is designed for easy and effective programming. If the concepts of object oriented programming are understood properly, the implementation of programs in Java becomes easy. Because Java inherits the syntax of C/C++ and many object-oriented features of C++, it is easy and simple to program. Object oriented: Java follows all the concepts of object oriented programming which includes three basic OOP features: Encapsulation: Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. Encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface or class.

Relevant to YOU: