0% found this document useful (0 votes)
68 views8 pages

Assignment 15

The document contains five programming assignments in Java, each requiring the implementation of specific functionalities. These include a quiz program, substring extraction, word frequency counting, double letter sequence counting, and a class for function overloading to check character frequency and display vowels. Each assignment is accompanied by sample code and expected outputs.

Uploaded by

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

Assignment 15

The document contains five programming assignments in Java, each requiring the implementation of specific functionalities. These include a quiz program, substring extraction, word frequency counting, double letter sequence counting, and a class for function overloading to check character frequency and display vowels. Each assignment is accompanied by sample code and expected outputs.

Uploaded by

bossariyan777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

.

Assignment 15
Question 1

Write a program to do the following :

(a) To output the question "Who is the inventor of Java" ?

(b) To accept an answer.

(c) To print out "Good" and then stop, if the answer is correct.

(d) To output the message "try again", if the answer is wrong.

(e) To display the correct answer when the answer is wrong even at the third attempt and stop.

import java.util.Scanner;

public class KboatQuiz


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);

String q = "Who is the inventor of JAVA?";


String a = "James Gosling";
int i;

System.out.println(q);

for(i = 1; i <= 3; i++)


{
String ans = in.nextLine();
ans.trim();
if(ans.equalsIgnoreCase(a))
{
System.out.println("Good");
break;
}
else if(i < 3)
{
System.out.println("Try Again");
}
}

if(i == 4)
{
System.out.println("Correct Answer: " + a);
}
}
}

Output
Question 2

Write a program to extract a portion of a character string and print the extracted string. Assume that m characters
are extracted, starting with the nth character.

import java.util.Scanner;

public class KboatExtractSubstring


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter string : ");
String s = in.nextLine();
int len = s.length();
System.out.print("Enter start index : ");
int n = in.nextInt();

if(n < 0 || n >= len)


{
System.out.println("Invalid index");
System.exit(1);
}

System.out.print("Enter no. of characters to extract : ");


int m = in.nextInt();

int substrlen = n + m;
if( m <= 0 || substrlen > len)
{
System.out.println("Invalid no. of characters");
System.exit(1);
}

String substr = s.substring(n, substrlen);


System.out.println("Substring = " + substr);
}
}

Output

Question 3

Write a program, which will get text string and count all occurrences of a particular word.

import java.util.Scanner;

public class KboatWordFrequency


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence:");
String str = in.nextLine();
System.out.println("Enter a word:");
String ipWord = in.nextLine();
str += " ";
String word = "";
int count = 0;
int len = str.length();

for (int i = 0; i < len; i++) {


if (str.charAt(i) == ' ') {

if (word.equalsIgnoreCase(ipWord))
count++ ;
word = "";
}
else {
word += str.charAt(i);
}
}

if (count > 0) {
System.out.println(ipWord + " is present " + count + " times.");
}
else {
System.out.println(ipWord + " is not present in sentence.");
}

}
}

Output

Question 4

Write a program to accept a string. Convert the string to uppercase. Count and output the number of double letter
sequences that exist in the string.

Sample Input : "SHE WAS FEEDING THE LITTLE RABBIT WITH AN APPLE"

Sample Output : 4

import java.util.Scanner;

public class KboatLetterSeq


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter string: ");
String s = in.nextLine();
String str = s.toUpperCase();
int count = 0;
int len = str.length();

for (int i = 0; i < len - 1; i++) {


if (str.charAt(i) == str.charAt(i + 1))
count++;
}

System.out.println("Double Letter Sequence Count = " + count);

}
}

Output

Question 5

Design a class to overload a function check( ) as follows :

(i) void check(String str, char ch) - to find and print the frequency of a character in a string.
Example:
Input :
str = "success" ch= 's'

Output :
number of s present is = 3

(ii) void check (String s1) - to display only vowels from string s1, after converting it to lower case.
Example:
Input:
sl = "computer"
Output : o u e

public class KboatOverload


{
void check (String str , char ch ) {
int count = 0;
int len = str.length();
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (ch == c) {
count++;
}
}
System.out.println("Frequency of " + ch + " = " + count);
}

void check(String s1) {


String s2 = s1.toLowerCase();
int len = s2.length();
System.out.println("Vowels:");
for (int i = 0; i < len; i++) {
char ch = s2.charAt(i);
if (ch == 'a' ||
ch == 'e' ||
ch == 'i' ||
ch == 'o' ||
ch == 'u')
System.out.print(ch + " ");
}
}
}

Output

You might also like