0% found this document useful (0 votes)
63 views8 pages

Lab 06 Characters, Strings, and Mathematical Functions: Objective

The document describes a lab on characters, strings, and mathematical functions in Java. The objective is to familiarize students with character and string data types and how to manipulate them. Students will learn to write programs using char and String data types, solve problems using Character and String methods, and solve math problems using Math functions. The lab includes practice, exercises, and individual activities to reinforce these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views8 pages

Lab 06 Characters, Strings, and Mathematical Functions: Objective

The document describes a lab on characters, strings, and mathematical functions in Java. The objective is to familiarize students with character and string data types and how to manipulate them. Students will learn to write programs using char and String data types, solve problems using Character and String methods, and solve math problems using Math functions. The lab includes practice, exercises, and individual activities to reinforce these concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab 06 Characters, Strings, and Mathematical Functions

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.

Current Lab Learning Outcomes (LLO)


By completion of the lab the students should be able to
1. Learn how to write Java programs that uses data types of char and String.
2. Solve different problems using appropriate methods of Character class and instances methods of references
variables of type String.
3. Solve mathematical problems using appropriate function from the class Math.

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

Short Exercise (5 minutes)


The above program is case sensitive, rewrite it such that become case insensitive and display the position of
second string inside the first string if it is a substring of it.
Individual Activities: (60 minutes)

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

Here is a sample run:


4. (Check SSN) Write a program that prompts the user to enter a Social Security number in the format DDD-
DD-DDDD, where D is a digit. Your program should check whether the input is valid. Here are sample
runs:

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

- Declaring variable of char type


char letter = 'A';
char letter = '\u0041'; // A’s Unicode value is 0041
Both statements assign character A to the char variable letter
- Reading a Character from the Console
The following statements use to read the value from keyboard to the variable of type, char, ch.
Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch= input.nextLine().charAt(0);
- Java provides many methods inside the Character class. (See the appendix below).
The String Type

- 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.

- Declaring a variable of String type


String name;

– Assign a value to the String variable


name= "Muhammad Alzahrani";

Or, you can combine the deceleration and assignment statements as follows:
String name= "Muhammad Alzahrani";

Reading a String from the Console


The following statements use to read the value from keyboard to the variable of type, String, str.
Scanner input = new Scanner(System.in);
System.out.println("Enter a line: ");
String str = input.nextLine();

- 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)!

Example: Displaying 2 digits after decimal point


double x = 16.404674;
System.out.printf("x is %4.2f", x);
The Output will be : x is 16.40

- Common format specifiers are shown in Figure (2).

Figure 2: The commong format specifiers

Extra (supplementary) Materials (if any)


None
Appendix

Some helpful methods including mathematical, characters and string manipulation methods:

Figure 3: The exponent methods in the Math class

Figure 4: The rounding methods in the Math class

Figure 5: The min, max, abs, and random methods


Figure 6: The methods in the Character class

Figure 7: Simple methods for String object

Figure 8: Comparison methods for String object

Figure 9: The methods of finding substrings in the String class

You might also like