Oop2 6.27.15
Oop2 6.27.15
REG NO:CT205/104966/20
6.2(Sum the digits in an integer) Write a method that computes the sum of the digits in an integer.
Use the following method header:
For example, sumDigits(234) returns 9 (= 2 + 3 + 4). (Hint: Use the % opera tor to extract digits and the
/operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4). To
remove 4 from 234, use 234 / 10 (= 23). Use a loop to repeatedly extract and remove the digit until all
the digits are extracted. Write a test program that prompts the user to enter an integer then displays
the sum of all its digits.
import java.util.Scanner;
int sum = 0;
while (n != 0) {
return sum;
}
public static void main(String[] args) {
6.3 (Palindrome integer) Write the methods with the following headers
: // Return the reversal of an integer, e.g., reverse(456) returns 654 public static int reverse(int
number)
Use the reverse method to implement isPalindrome. A number is a palin drome if its reversal is the
same as itself. Write a test program that prompts the user to enter an integer and reports whether
the integer is a palindrome.
import java.util.Scanner;
int reversed = 0;
// Loop to reverse the digits
while (number != 0) {
return reversed;
if (isPalindrome(number)) {
} else {
}
input.close(); // Close the Scanner
*6.7 (Financial application: compute the future investment value) Write a method that computes
future investment value at a given interest rate for a specified number of years. The future investment
is determined using the formula in Programming Exercise 2.21.
futureInvestmentValue =
investmentAmount * (1 + monthlyInterestRate)numberOfYears*12
For example, if you enter amount 1000, annual interest rate 3.25%, and number of years 1, the future
investment value is 1032.98.
import java.util.Scanner;
return futureValue;
*6.20 (Count the letters in a string) Write a method that counts the number of letters in a string using
the following header:
Write a test program that prompts the user to enter a string and displays the num ber of letters in the
string.
import java.util.Scanner;
int letterCount = 0;
if (Character.isLetter(s.charAt(i))) {
return letterCount;
**7.3 (Count occurrence of numbers) Write a program that reads the integers between 1 and 50 and
counts the occurrences of each. Assume the input ends with 0
import java.util.Scanner;
while (true) {
if (number == 0) {
break;
}
// If the number is between 1 and 50, count it
} else {
if (counts[i] > 0) {
System.out.println((i + 1) + " occurs " + counts[i] + " time" + (counts[i] > 1 ? "s" : ""));
7.8 (Average an array) Write two overloaded methods that return the average of an array with the
following headers:
Write a test program that prompts the user to enter 10 double values, invokes this method, then
displays the average value.
import java.util.Scanner;
int sum = 0;
sum += num;
double sum = 0;
sum += num;
doubleArray[i] = input.nextDouble();
}
// Call the average method for doubles and display the result
input.close();
7.9 (Find the largest element) Write a method that finds the largest element in an array of double
values using the following header:
double max = array[0]; // Initialize max with the first element of the array
Write a test program that prompts the user to enter ten numbers, invokes this method to return the
maximum value, and displays the maximum value. Here is a sample run of the program:
import java.util.Scanner;
return max;
doubleArray[i] = input.nextDouble();
// Call the max method to find the largest element and display the result
input.close();
7.14 (Computing lcm) Write a method that returns the lcm (least common multiple) of an unspecified
number of integers. The method header is specified as follows:
Write a test program that prompts the user to enter five numbers, invokes the method to find the
lcm of these numbers, and displays the lcm.
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
if (numbers.length == 0) {
return lcm;
// Example usage
7.15 (Eliminate duplicates) Write a method that returns a new array by eliminating the duplicate
values in the array using the following method header:
Write a test program that reads in 10 integers, invokes the method, and displays the distinct numbers
separated by exactly one space.
import java.util.Scanner;
import java.util.Arrays;
int newSize = 0;
// Loop through the list and add only distinct elements to tempArray
if (list[i] == tempArray[j]) {
isDuplicate = true;
break;
}
if (!isDuplicate) {
tempArray[newSize] = list[i];
newSize++;
return distinctArray;
System.out.println("Enter 10 integers:");
numbers[i] = input.nextInt();
input.close();