CSE110 Methods & Final Review Lab (No Submission)
CSE110 Methods & Final Review Lab (No Submission)
[No Submission]
You must not hard code any of the tasks, which means your code should work for any
valid user input.
Methods
evenChecker(17); Odd!!
Note: You must call the methods from CW-1B and CW-1C, otherwise this
task would be considered invalid.
C. Write a method called findSpace that takes two values in its parameters one
is an integer diameter and another one is a String. Using the given diameter,
this method should calculate the Area of a circle or the Volume of a sphere
depending on the value of the second parameter. Finally, it should print the
result inside the method.
Note: You must call the method written in task CW-2A & CW-2B,
otherwise this task would be considered invalid.
findSpace(5,"sphere"); 33.5103
5. Write a method called calcTax that takes 2 arguments which are your age
then your salary. The method must calculate and return the tax as per the
following conditions:
Strings
Task 1
Write a method modifyStrings() that takes in three given strings S, S1, and S2 consisting of
different numbers of characters respectively, the task is to modify the string S by replacing all
the substrings S1 with the string S2 in the string S and printing the modified string S.
Strings + Arrays
Task 3
Given an array of email addresses, print the number of valid email addresses satisfying the
following conditions.
Task 4
Write a method called isHappyNumber which takes an integer in its parameter to check whether
a number is a happy number or not. If the number is a happy number then the method returns
boolean true otherwise it returns boolean false. In number theory, a happy number is a number
which eventually reaches 1 when replaced by the sum of the square of each digit. For instance,
13 is a happy number because 12 + 32 = 10 and 12 + 02 = 1. On the other hand, 4 is not a happy
number because the process continues in an infinite cycle without ever reaching 1. Unhappy
number ends in a cycle of repeating numbers which contains 4 .
System.out.println(check)
System.out.println(check)
Task 5
Write a method called toDecimal which takes a binary number as a string in its parameter to
convert the binary number to its decimal number and return the decimal value. After returning
the decimal value, write another method called toHex which takes the converted decimal value in
its parameter and calculates the hexadecimal value and then return the hex value.
Task 6
Trace the following code and write the outputs.
class Trace02 {
public static void main(String args[]) {
int[] arr1 = {3, 1, 4, 1, 5, 9, 2};
int[] arr2 = {10, 20, 30, 40, 50, 60, 70};
int x = 0, y = 0;
while (x < arr1.length - 1) {
arr2[x] = arr1[y] * (x + 1) - arr2[y];
y = 1;
while (y <= x) {
arr2[x] = arr2[x] + arr1[y] - y;
y = y + 1;
}
System.out.println(arr2[x]);
x = x + 1;
}
System.out.println(arr2[arr1.length - 1]);
}
}
Task 7
You are given an integer array. You need to identify all the prime numbers and perfect
numbers within the array and print the indices along with these numbers from the
original array.
Perfect Numbers:
0: 6
2: 28
Task 8
Trace the following code and write the outputs.
public class tracing1 {
public static void main(String[] args){
int i = 1;
int [] a = {5,6,7,8,9};
while (i <= 5){
int j = a[i%a.length];
while (j > 1){
System.out.print(j--);
if (j == 2)
break;
}
System.out.println("***");
++i;
}
double x = 7;
double y = 8;
double z = 9;
System.out.println(x < y || y > z);
System.out.println(x < y && y > z);
System.out.println(x < y);
System.out.println(x + y < z);
System.out.println((x + y)-6 < z);
}
}
Task 9
Your professor expects only As, Bs, and Cs. In the following program, write a method called
getScores that takes as input corresponding arrays studentGrades and studentScores. Write a
method called getScores that assigns index i in studentGrades based on index i in
studentScores. If a grade is A, assign 100. If a grade is B, assign 90. If a grade is C, assign 70. If
a grade is anything else, assign 0.
Task 10
A. Write a method called convertToCm(), that takes as input a type double and returns the
value converted from inches to centimeters.
Hint: There are 2.54 centimeters in an inch
double t = convertToCm(16);
40.64 cm
System.out.println(t + " cm");
B. Create an array of type double of length 5 called cheetos_inches, that stores the length of
each of the Cheetos from the user. Send the array of length in inches into a method called
findAvgCm() that returns the average length of the Cheetos in cm to 2 decimal places. The
method findAvgCm() uses convertTocm() to convert the length of each Cheetos from inches to
cm.
Note: You must call the method written in [Method Task A], otherwise this task would be
considered invalid.
Task 11
A. Write a method called isVowel which takes a string in its parameter and counts all the
vowels in the String. If any vowel exists in the string then the method returns the count.
The quick brown fox jumps over the lazy dog Number of vowels in the string: 11
B. Write a method called isConsonant which takes a string in its parameter and counts all
the consonants in the String. If any consonant exists in the string then the method returns
the count.
The quick brown fox jumps over the lazy dog Number of consonants in the string: 24
C. Write a method called vowel/consonantSum which takes an array of strings in its parameter
and returns the summation of the number of vowels/consonants.
Note: You must call the methods written in tasks A/B, otherwise this task will be considered
invalid.
String [] names = {"Bob", "Alice", "Max", "Marry", The total number of vowels in the
"Rosy"}; array is: 7
System.out.println( "The total number of vowels in the array
is:" + vowelSum(names)); The total number of consonants
System.out.println( "The total number of consonants in the in the array is: 13
array is:" + consonantSum(names));