Chapter 4 concepts and methods

Mr. Gottsacker
Computer Programming


Contents

  1. Mathematical methods
    1. Trigonometric methods
    2. Exponent methods
    3. Rounding methods
    4. Other number methods
  2. Character data type
    1. Special characters
    2. Comparing characters
    3. Character methods
  3. Strings
    1. Getting Strings from console
    2. Simple methods
    3. String comparison methods
    4. String processing
    5. Converting between strings and numbers

Methods

Methods contain groups of statements that perform a specific task. They make your life easier by providing an interface into common functionality. This chapter is an introduction to methods because you will use a handful of built-in methods. Eventually (in chapter 6), you will write your own methods.

Static methods can be invoked without using an object. They are not tied to an object instance. The syntax is ClassName.methodName(arguments).

Instance methods must be invoked from a specific object instance.


Mathematical methods

These methods are all static methods. They belong to the Math class, so they should be prefixed with Math when using them in programs.

Math.PI is an object of the Math class, not a method. It holds the double value that is closest to the actual value of pi.

Trigonometric methods

Exponent Methods

Rounding methods

Minimum, maximum, absolute value, random numbers




Character data type and methods

You have printed text using System.out.print() with a message in double quotes. These text entries are made up of characters. In Java, each character in a string of text is of the char data type. When using the char type, single quotes are used. For example:


char letter = 'a';
                


Special characters

There are special characters in Java for representing things like whitespace and quotation marks. Unlike traditional letter and number characters, they are represented with two physical keystrokes. They are always prefixed with a backslash. These are the ones you may commonly use:

Escape sequence Character name Example code Example output
\t tab
System.out.print("1 \t 2 \t 3");
1 	 2 	 3
\n newline
System.out.print("This is a long message \n More of the long message.");
This is a long message
More of the long message
\" quotation mark
System.out.print("He said, \"Mr. Gottsacker is a 10X coder.\"");
He said, "Mr. Gottsacker is a 10X coder."
\\ backslash
System.out.print("\\t is a tab character.");
\t is a tab character.

Comparing and testing characters

A char can be cast to an int, and vice versa. This is because computers store and process characters according to an encoding scheme. Recall that at their lowest level, computers process all information as 0s and 1s. Encoding schemes are responsible for mapping textual characters to binary numbers. We can use the more comprehensible decimal representation of these binary numbers to more quickly understand how this works.

Java supports the Unicode encoding scheme, which includes the ASCII code for representing common American characters. When you cast a char to an int, you get its ASCII decimal value. Some commonly used characters and their ASCII decimal mappings are included below. A full table can be found at this link.

Characters Decimal code value
'0' to '9' 48 to 57
'A' to 'Z' 65 to 90
'a' to 'z' 97 to 122
Example of char decimal representation

char letter = 'A';
System.out.println(letter); 	 	 // prints: A 
int decimalLetter = (int) letter;
System.out.println(decimalLetter); 	 // prints: 65
            

Because the ASCII encoding maps characters alphabetically, you can compare characters like you compare numbers. Note that capital letters come before lowercase letters. Consider the following boolean comparisons:


boolean chComp = 'a' < 'b'; 	 // true. ASCII for 'a' (97) is less than the ASCII for 'b' (98).
boolean chComp = 'b' < 'B'; 	 // false. ASCII for 'b' (98) is greater than the ASCII for 'B' (66).
boolean chComp = '1' < '7'; 	 // true. ASCII for '1' (49) is less than the ASCII for '7' (55).
            

You may need to check what kind of character you have in a program. You can leverage the decimal representations discussed above to perform this check. The following code snippet is a way to accomplish this, assuming the variable ch stores a character:


if (ch >= 'A' && ch <= 'Z')
	System.out.println(ch + " is an uppercase letter.");
else if (ch >= 'a' && ch <= 'z')
	System.out.println(ch + " is a lowercase letter.");
else if (ch >= '0' && ch <= '9')
	System.out.println(ch + " is a numeric character.");
            

Character methods

The Character class also provides a handful of static methods to do this as well. You can invoke these with Character.methodName(ch), where ch is a variable of the char type.


Strings

The char data type can only hold a single character. When processing or printing text, programmers typically want to use a series of characters. For this, Java has the String data type. The String type is a reference type, which differs from the primitive types we have seen thus far. Every String variable references an object, which has properties and methods. The syntax for invoking an object instance's methods is referenceVariable.methodName(arguments).

Reading Strings from the console

Strings may be collected from the user in ways similar to those of numbers. Use a Scanner object's next() method to collect a single word, or its nextLine() method to collect a whole line of text.


Scanner in = new Scanner(System.in);
System.out.print("What is your first name?");
String firstName = in.next();
in.nextLine(); 	 // needed to eat up the hanging newline character in System.in

System.out.print("What is your full name?");
String fullName = in.nextLine();
                

Simple String methods

String comparison methods

String processing

Converting between strings and numbers

You can convert a number to a string using the concatenation operator:


int num = 789;
String numStr = num + ""; 	 // num holds "789"