0% found this document useful (0 votes)
9 views5 pages

John Llyod E

Uploaded by

etackenneth961
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

John Llyod E

Uploaded by

etackenneth961
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

John Llyod E. Etac 1st year BS I.

T com prog module 5 199 pages

Module 5 Mastery Check

1. Show two ways to declare a one-dimensional array of 12 doubles.


Ans- double[] myArray = {0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,11.0,};
double[] myArray = new double [12];

2. Show how to initialize a one-dimensional array of integers to the values 1 through 5.

int[] myArray = {1, 2, 3, 4, 5};

3. Write a program that uses an array to find the average of 10 double values. Use any 10

values you like.

public class AverageCalculator {

public static void main(String[] args) {

double[] values = {21.5, 12.1, 17.6, 15.3, 26.0, 12.8, 19.2, 14.7, 11.4, 25.1};

double sum = 0;

for (double value : values) {

sum += value;

double average = sum / values.length;

System.out.println("Average of the 10 double values: " + average);

4. Change the sort in Project 5-1 so that it sorts an array of strings. Demonstrate that it works.

import java.util.Arrays;

public class StringSorter {

public static void main(String[] args) {

String[] strings = {"num5", "num1", "num4", "num3", "num2"};


Arrays.sort(strings);

System.out.println("Sorted array of strings:");

for (String str : strings) {

System.out.println(str);

5. What is the difference between the String methods indexOf( ) and lastIndexOf( )?

- indexOf() finds the first occurrence of a substring from the beginning of the string,

lastIndexOf() finds the last occurrence of a substring starting from the end of the string.

6. Since all strings are objects of type String, show how you can call the length( ) and

charAt( ) methods on this string literal: "I like Java".

-public class StringMethodsDemo {

public static void main(String[] args) {

int length = "I like Java".length();

System.out.println("Length of the string: " + length); // Output: 10

char character = "I like Java".charAt(5);

System.out.println("Character at index 5: " + character); // Output: 'k'

7. Expanding on the Encode cipher class, modify it so that it uses an eight-character string

as the key.

public class Encode {

private String key;


public Encode(String key) {

if (key.length() != 8) {

throw new IllegalArgumentException("The key must be exactly 8 characters long");

this.key = key;

public String encode(String message) {

StringBuilder encodedMessage = new StringBuilder();

for (int i = 0; i < message.length(); i++) {

char originalChar = message.charAt(i);

char keyChar = key.charAt(i % 8);

char encodedChar = (char) (originalChar ^ keyChar);

encodedMessage.append(encodedChar);

return encodedMessage.toString();

public static void main(String[] args) {

String key = "abcdefgh";

Encode encoder = new Encode(key);

String message = "Hello, world!";

String encodedMessage = encoder.encode(message);

System.out.println("Encoded message: " + encodedMessage);

}
8. Can the bitwise operators be applied to the double type?

-No, bitwise operators cannot be directly applied to the double type in most programming languages.

9. Show how this sequence can be rewritten using the ? operator.

if(x < 0) y = 10;

else y = 20;

-y = (x < 0) ? 10 : 20;

10. In the following fragment, is the & a bitwise or logical operator? Why?

boolean a, b;

// ...

if(a & b) …

---The & operator in the expression if(a & b) is a bitwise operator. It performs a bitwise AND operation
on the individual bits of the boolean values a and b. This means that it evaluates both a and b regardless
of their values and returns true only if both a and b are true, otherwise it returns false.

If you intended to use a logical AND operator that short-circuits, meaning it evaluates b only if a is true,
you would use the && operator instead of &.

11. Is it an error to overrun the end of an array? Is it an error to index an array with a

negative value?

----Yes. All array indexes start at zero.

12. What is the unsigned right-shift operator?

- (>>>)
13. Rewrite the MinMax class shown earlier in this chapter so that it uses a for-each style

for loop.public class MinMax {

public static void main(String[] args) {

int[] nums = { 99, -10, 100123, 18, -978, 5623, 463, -9, 287, 49 };

int min, max;

min = max = nums[0];

for (int num : nums) {

if (num < min) min = num;

if (num > max) max = num;

System.out.println("Min and max: " + min + " " + max);

14. Can the for loops that perform sorting in the Bubble class shown in Project 5-1 be

converted into for-each style loops? If not, why not?

-No, the for loops that perform sorting in the Bubble class shown in Try This 5-1 cannot be converted
into for-each style loops.

You might also like