Lab 06 Characters, Strings, and Mathematical Functions: Objective
Lab 06 Characters, Strings, and Mathematical Functions: Objective
Objective
The purpose of this lab is to familiarize students with characters and strings data types and how to
manipulate characters and string values using methods of characters and strings. It also provides
additional mathematical functions. There are some exercises, through which they will understand
the concept learned in this chapter.
Lab Requirements
NetBeans IDE 8.0.1 or higher
Lab Assessment/Activities
Practice Activity with Lab Instructor (10 minutes)
(Check substring) Write a program that prompts the user to enter two strings and reports whether the second
string is a substring of the first string.
Solution:
1. Open NetBeans and create a new project
2. Create a new java main class and write its name as CheckSubstring
3. Write the following code inside the main method, Figure (1).
Figure 1: The program to check if one string is a substring from the other
1. (Math Methods) Write a program that generates two random double numbers between -20 and 20 and
displays the maximum number after finds the absolutes values for negative numbers.
2. (Vowel or consonant?) Write a program that prompts the user to enter a letter and check whether the letter
is a vowel or consonant. Here is a sample run:
3. (Student major and status) Write a program that prompts the user to enter two characters and displays the
major and status represented in the characters. The first character indicates the major and the second is
number character 1, 2, 3, 4, which indicates whether a student is a freshman, sophomore, junior, or senior.
Suppose the following characters are used to denote the majors:
M: Mathematics
C: Computer Science
I: Information Technology
5. (Order three cities) Write a program that prompts the user to enter three cities and displays them in
ascending order. Here is a sample run:
Lab Description
Theory Review (5 minutes)
▪ Java provides many useful methods in the Math class for performing common mathematical functions.
There are many beneficial methods inside the Math class. (See the appendix below).
- The char type represents only one character. To represent a string of characters, use the data type called
String which is a predefined class in the Java library.
Or, you can combine the deceleration and assignment statements as follows:
String name= "Muhammad Alzahrani";
- Java provides many of methods that operate on Strings. (See the appendix below)
Formatting Output
- You can use the System.out.printf method to display formatted output. When printing double values,
often we do not need or want all the decimals. In fact, often we want only two (for money)!
Some helpful methods including mathematical, characters and string manipulation methods: