You are on page 1of 8

Assessment Task.

[Module 7]
1. True
2. True
3. True
4. True
5. False
6. True
7. False
8. False

What is the output of each of the following program segments?

A) x = 10;
System.out.println(secret(x));

OUTPUT: 4

B) x = 5, y = 8;

System.out.println(another(x, y));
OUTPUT: 4

C) x = 10;

k = secret(x);
System.out.println( x + “ “ + k + “ “ + another(x, k));
OUTPUT: 10 4 0
D) x = 5, y = 8;

System.out.println(another(x, y));
OUTPUT: 4

Programming Problems:
1. Write a value-returning method, isVowel, that returns the value true if a given character is a
vowel, and otherwise return false. Also write a program to test your method.

package HelloWorld;
import java.util.Scanner;
public class Trial{
public static boolean isVowel(char let) {
switch(let) {
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
return true;
default:
return false;
}
}

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println();
System.out.print(" Enter a letter: ");
char letter = s.next().charAt(0);
System.out.println(" OUTPUT :" + isVowel(letter));

}
}

OUTPUT:

2. Write a program that prompts the user to enter a sequence of charcters and output the number
of vowels. (use the method isVowel written in problem 1).

package HelloWorld;
import java.util.Scanner;
public class Trial{

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
System.out.println();
System.out.print(" Type a sentence/word: ");
String words = s.nextLine();
int count = 0;
words = words.toLowerCase();

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


char ch = words.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o'
|| ch == 'u')
{
count = count + 1;
}
else if((ch >= 'a' && ch <= 'z'));
}
System.out.println(" Vowels Count : " + count);
}
}

OUTPUT:

3. Write a program that output inflation rates for two successive years and whether the inflation
rates are increasing or decreasing. Ask the user to input the current price of an item and its price
one year and two years ago. To calculate the inflation rate for a year, subtract the price of the
item for that year and from the price of the item from one year ago and then divide the result
by the price a year ago. Your program must contain a method to calculate the results. Use
appropriate parameters to pass information in and out of the method. Do not use any global
variables.
package HelloWorld;
import java.util.Scanner;
public class Trial{
public static double calcInflation(double updatedPrice, double
currentPrice) {
double result;
result = (updatedPrice - currentPrice) / currentPrice ;
return result;
}
public static void main(String[] args) {
double inflateAfterTwoYears, inflateAfterOneYear;

Scanner s = new Scanner(System.in);


System.out.println();
System.out.print(" Enter the price of item for 2014: ");
double currentPrice = s.nextDouble();
System.out.print(" Enter the price of item for 2015: ");
double priceAfterOneYear = s.nextDouble();
System.out.print(" Enter the price of item for 2016: ");
double priceAfterTwoYears = s.nextDouble();
System.out.println();

inflateAfterOneYear = calcInflation(priceAfterOneYear,
currentPrice);
inflateAfterTwoYears = calcInflation(priceAfterTwoYears,
currentPrice);

System.out.println(" Inflation for the year 2015 : " +


inflateAfterOneYear + "%");
System.out.println(" Inflation for the year 2016 : " +
inflateAfterTwoYears + "%");
}
}

OUTPUT:

You might also like