Building JAVA Program Chapter 4 6. If/options. List and explain. 9. What express
ID: 3721042 • Letter: B
Question
Building JAVA Program Chapter 4
6. If/options. List and explain.
9. What expressions do we use to test multiple conditions and give example code?
11. List the 6 differences between char and string.
12. Can you use an escape sequence with a char value?
13. How are values of type char stored? Why is it stored this way?
14. List the methods of character class and explain each.
15. How does cumulative concatenation works with chars? Show an example.
16. System.out.printf what does the f stand for? What does it create?
22. What expression do you need for string length to prevent a "String index out of bounds" error?
23. Explain the 5 guidelines of "Procedural design heuristics" and how they can help us design better programs.
Explanation / Answer
6)Answer:
The Java if statement enables your Java programs to make decisions about what code to execute depending on the state of variables, or values returned from methods. Here is a simple Java if example:
The if statement in this example tests the boolean variable isValid and based on its value (either true or false) it executes one of two different blocks of code. If the isValid variable has the value of true, the first block is executed. If not, the code inside the else block is executed.
The expression inside the parentheses is called the condition. The condition can be any Java expression, as long as the result of the expression is a boolean result (either true or false).
In the example above, the condition was whether the isValid variable was true or false.
If the block of code to be executed is just a single statement, you do not need the brackets { } around them, in the if statements. Here is an example:
However, it is good practice to put the brackets around the statements, even if there is only one statement to execute. Often during development you may start with a single statement that needs to be executed inside an if or else block, but later have to add more statements to the blocks. This can lead to errors that are hard to spot. Look at this if statement:
Now imagine I have to increment a valid counter if isValid is true. Naively I might change the code to this:
But now only the validCount++ statement belongs to the if statement. The System.out.println() statement will always be executed. Or, imagine if I had switched the statements like this:
Now only the System.out.println() statement belongs to the if statement. The validCount++ statement will always be executed.
To avoid this error I almost always put the brackets around the blocks to execute, even if there is only one statement to execute in the block. Here is how that could look:
When the brackets are there, it is easier to remember to insert new statements inside the brackets
9)Answer:
Allows you to check multiple conditions; provides a conditional check that only executes if the first condition fails. Can only be used after an "if" statement block.
Can be followed with an "else" or "else if" statement to test additional conditions.
if ( x == 5 )
{
Console.WriteLine("x is equal to 5");
}
else if ( x == null )
{
Console.WriteLine("x is not defined!");
}
else
{
Console.WriteLine("x something else");
}
Answer is
else if( <condition> )
{
<statements>
}
11)Answer:
differences between char and string:
String:
Strings have helper functions and manage char arrays automatically. You can concatenate strings,while for a char array you would need to copy it to a new array.
->Strings can change their length at runtime.
->A char array is harder to manage than a string and certain functions may only accept a string as input, requiring you to convert the array to a string.
->It's better to use strings, they were made so that you don't have to use arrays.
Char is a single character, a String is a sequence of characters (possibly just one). In addition, String is an full-blown object, a char primitive data type is just two bytes in unicode.
A character constant like '!' represents a single character. A string literal between double quotes usually represents multiple characters. A string literal like "!" seems to represent a single character, but it actually contains two: the ! you requested, and the which terminates all strings in C.
Characters in C are represented by small integers corresponding to their character set values (see also question 8.6). Strings are represented by arrays of characters; you usually manipulate a pointer to the first character of the array. It is never correct to use one when the other is expected. To append a ! to a string, use
13)Answer:
Whenever a character value is given to a variable of type char, its ASCII value gets stored (and not the character value). While printing a character, if we use %c, then its character value will be displayed and if we use %d, then its integer value (ASCII value) will be displayed.
14)Answer:
a. Java boolean isLetter (char ch)
A boolean isLetter method is used in Java, for checking whether the given char value(ch) is a letter or not, i.e. [A-Z],[a-z], it will return true if it is a letter else false.
We can also write ASCII value for the letter because Java has an implicit typecasting from char to int. Before moving towards example let’s revise Java Syntax and Java Datatypes.
Syntax
Example
Output
true
false
b. Java boolean isDigit(char ch)
Boolean isDigit method in Java is used to determine whether the given character is a digit or not.
Syntax
Example
c. Java boolean is Whitespace(char ch)
A whitespace in Java is used as a space, tab, or a new line, and this method determines whether the given char is a whitespace or not.
Syntax
Example
Output
false
true
true
true
true
false
For, Java Variables and Java Operators follow this links.
d. Java Boolean isUpperCase(char ch)
Boolean isUpperCase method in Java is used, to determine whether the given char value is uppercase or not.
Syntax
Example
Output
true
false
true
e. Java boolean isLowerCase(char ch)
Boolean isLowerCase(char ch) method in Java is used to determine whether the given char value is lowercase or not.
Syntax
Example
Output
false
true
true
f. Java char toUpperCase(char ch)
Char toUpperCase(char ch) method in Java is used when the uppercase of the given java character is needed.
Syntax
Example
Output
A
65
48
g. Java char ToLowerCase(char ch)
Char ToLowerCase(char ch) method in Java is used when the lowercase of the given java char value is required.
Syntax
Example
Output
a
97
48
h. Java toString(char ch)
In toString(char ch) method in Java, a string class object is returned for the specified char. We cannot use ASCII value here.
Syntax
Example
Output
X
y
16)Answer:
f is formatted output. The format specifier is a parameter of the printf function. The parameters of printf can also vary in use hence printf is variadic:
printf( "%3.2f ", someNumber );
//prints a float 3 digits wide and 2 decimal places in precision
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.