java

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

String Finder

Given three strings say Searchstring, Strl and Str2 as input, write a program to find out if Str2 comes
after Strl in the Searchstring.
Include a class UserMalnCode with a static method strlngFlnder that accepts 3 String arguments
and returns an integer. The 3 arguments correspond to Searchstring, Strl and Str2. The function
returns 1 if Str2 appears after Strl in the Searchstring. Else it returns 2. Create a class Main which
would get 3 Strings as input and call the static method stringfinder present in the UserMainCode.

Input and Output Format:


Input consists of 3 strings.
The first input corresponds to the SearchString.
The second input corresponds to Strl.
The third input corresponds to Str2.
Output consists of a string that is either "Yes" or "No".

Sample Input 1: Sample Output 1:


geniousRajKumarDev Yes
Raj
Dev

Sample Input 2: Sample Output 2:


geniousRajKumarDev No
Dev
Raj

Ma in.java

i mport j ava . util .*;

publi c cl ass Main {


public static void main ( String [] args ) {
Scanner sc • new Scanner ( System . i n );
String searchSt r ing = sc . next () ;
String strl = sc . next ( );
String str2 = sc . next ();
int b = UserMainCode . stringFinder ( sea r chSt ring , strl , st r 2 );
if ( b == 1) {
System. out . println ( "Yes" );
} else
System. out . pri ntln ( "No" );
sc . close ();
}
}
UserMainCode.java

pub l i c class UserMainCode {


pu blic s t ati c int stringFinder ( String sl , String s2 , St r ing s3 ) {
String al z sl . tolowerCase ();
String a2 • s2 . tolowerCase ();
String a3 • s3 . tolowerCase ();
if ( al . contains (a2 ) && al . contains ( a3 )) {
if ( al . indexOf ( a2 ) < al . indexOf ( a3 )) {
return 1 J·
} else
return 2 J·
}
return 0;
}
}

Name Shrinking
Write a program that accepts a string as input and converts the first two names into dotseparated
initials and print the output. Input string format is 'fn mn In'. Output string format is 'In [mn's 1st
character] .[fn's 1st character]'.
Include a class UserMalnCode with a static method getformatedStrlng which accepts a string. The
return type (String) should return the shrinked name. Create a Class Main which would be used to
accept Input String and call the static method getFormatedStrlng present in UserMalnCode.

Input and Output Format:


Input consists of a st ring.
Output consists of a String.
Refer sample output for fo rmatting specifications.

Sample Input:
Sachin Ramesh Tendulkar

Sample Output:
Tendulkar R.S

Ma in.java

. por t Java
1m . . ut 1· 1 . * ;

pub l i c class Main {


publ i c static void mai n( String [ ] args ) {
Scanner sc = new Scanner ( System . in );
String sl = sc . next l ine ();
System. out . println ( UserMainCode . getFormatedString ( sl ));
sc . c l ose ();
}
}

UserMainCode . java

i mport java . util . *;

pub l i c clas s UserMainCode {


public stat ic String getFormatedString (String sl } {
StringBuffer sb • new StringBuffer();
StringTokenizer st • new StringTokenizer( s1 , " " );
Stri ng s2 • st . nextToken ();
String s3 • st . nextToken ();
String s4 • st . nextToken ();
sb . append ( s4). append ( " " );
sb . append ( s3 . substring (0, 1));
sb . append ( " . " );
sb . append ( s2 . substring (0, 1));
r eturn sb . toString ();
}
}

Digits - II
Write a program to read a non-negative integer n, compute the sum of its digits. If sum is greater
tha n 9 repeat the process and calculate the sum once again until the final sum comes to single digit.
Return the single digit.
Include a class UserMalnCode with a static method getDlgltSum which accepts the integer value.
The return type is integer. Create a Class Main which would be used to accept the string and call the
static method getDlgltSum present in UserMalnCode.

Input and Output Format:


Input consists of a Integer. Output consists of integer.
Refer sample output for fo rmatting specifications.

Sample Input 1: 9999 Sample Output 1: 9


Sample Input 2: 698 Sample Output 2: 5

Ma in.lava

i mpo rt java . uti l .*;

public class Main {


publ i c stat ic void main (St ring [] args ) {
Scanner sc = new Scanner(System . i n);
int a = sc . nextint ();
int sum = UserMainCode . getDigitSum (a );
System. out . println (sum );
sc . close ();
}
}

UserMa lnCode.lava

public cl ass UserMainCode {


publi c stat ic int getDigitSum( int n) {
i nt sum = 0;
while ( n > 9 ) {
i nt a • 0;
sum • 0;
whi le ( n I• 0) {
a • n % 10 ;
sum +• a;
n • n / 10;
}
n = sum ;
}
return sum;
}
}

Max Substring
Write a program to accept two string Inputs. The first being a source string and second one a
deli miter. The source string contains the delimiter at various locations. Your Job Is to return the
substring w ith maximum number of characters. If two or more substrings have maximum number
of characters return the substring which appears first. The size of the delimiter Is 1.
Include a class UserMalnCode with a static method extractMax which accepts the string. The return
type (string) should be the max substring. Create a Class Main which would be used to accept Input
string and call the static method extractMax present in UserMalnCode.

Input and Output Format:


Input consists of a source string and delimiter.
Output consists of a string.
Refer sample out put for formatting specifications.

Sample Input 1: Sample Output 1:


delhi-pune-patna Delhi

Main.lava

import java.util . *;

public cl ass Main {


public static void main (St r ing [] args ) {
Scanner sc = new Sc anner (System. in );
Str i ng inputl = sc . next l ine ();
String input2 = sc . nextline ();
System. out . pr int l n(UserMainCode . extractMax (inputl, input2));
sc . close ();
}
}

UserMainCode.java

import java.util.*;

public class UserMai nCode {


public static String extractMax (String inputl, Str ing input2 ) {
i nt max = 0;
String s3 = null ;
StringTokenizer st = new StringTokenizer(inputl, input2);
while (st . hasMor eTokens ()) {
St ring s2 = st . nextToken ();
int n = s2 . length ();
if (n > max) {
max= n;
s3 = s2;
}
}
return (s3 . substring(0, 1). toUpperCase ()+s3 . substring (l ));
}
}

Date Validation
Write a program to read a string representing a date. The date can be in any of the three formats
1:dd-MM-yyyy 2:dd/MM/yyyy 3:dd.MM.yyyy. If the date is valid, print valid else print invalid.
Include a class UserMainCode with a static method getValidDate which accepts a string. The return
type (integer) should be based on the validity of the date. Create a class Main which would be used
to accept Input string and call the static method getValidDate present in UserMainCode.

Input and Output Fromat:


Input consists of a string. Output consists of a string.
Refer sample output for formatting specifications.

Sample Input 1: Sample Output 1:


03.12.2013 Valid
Sample Input 2: Sample Output 2:
03$12$2013 Invalid
Maln.java

import java . util . Scanner;

publi c class Main {


public static void main (String[] args ) {
Scanner sc = new Scanner (System. i n);
String s = sc . next ();
int b = UserMainCode .getValidDate( s );
if (b == 1)
System . out . printf( "Valid" );
else
System . out . printf( "Invalid" );
sc . close ();
}
}

UserMainCode.java
import java . text . ParseException;
import java . text . SimpleDateFormat ;
import java . util . Date;

publi c class UserMainCode {


public static int getValidDate (String s ) {
i f (s . matches ( "[0-9]{2}[.]{1}[0-9]{2}[.]{ 1}[0-9]{4}" )) {
SimpleDateFormat sdf = new SimpleDateFormat( "dd.MM.yyyy" );
sdf . setlenient (false );
try {
Date dl • sdf . parse ( s );
return 1;
} catch (ParseException e ) {
return -1;
}
} el se if ( s . matches ( "(0-9]{ 2}[ -]{1}(0-9]{ 2}[ -]{1}(0-9]{4}" )) {
SimpleDateFormat sdf = new SimpleOateFormat( "dd-MM-yyyy" );
sdf . setlenient (f alse );
try {
Date dl = sdf . parse( s );
return 1;
} catch (ParseException e ) {
return -1;
}
} el se if ( s . matches ( "[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}" )) {
SimpleDateFormat sdf • new SimpleDateFormat( "dd/MM/yyyy" );
sdf . setlenient (fa lse );
try {
Date dl = sdf . parse ( s );
return 1;
} catch (ParseException e ) {
return - 1 ;
}
} else
r eturn - 1;
}
}

Grade Calculator I
A School wants to give assign grades to its students based on their marks. You have been assigned
as the programmer to automate this process. You would lik.e to showcase your skills by creating a
quick prototype.
The prototype consists of the following steps:
Read student details from the User. The details w ould include name, m ark in the given order. The
datatype for name is string. mark is float. You decide to build a hashmap. The hashmap contains
name as key and mark as value.
BUSINESS RULE:
1. If Mark is less than 60, then grade is FAIL.
2. If Mark is greater than or equal to 60, then grade is PASS.
Note: FAIL/PASS should be In uppercase.
Store the result In a new HashMap w ith name as Key and grade as Value. You decide to write a
fu nction calculateGrade which takes the above HashMap as Input and returns the HashMap as
output. Include this function in class UserMalnCode. Create a class Main which would be used to
read student details In step 1 and build the hashmap. canthe stat ic method cakulateGrade present
In UserMalnCode.

Input and Output Format:


Input consists of studen t details. The first number indicates the size of the students. The next two
values indicate t he name, mark.
Output consists of a name and corresponding grade for each student.
Refer sample output for formatting specifications.

Sample Input 1:
3
Avi
76.36
Sunil
68.42
Raj a
36.25
Sample Output 1:
Avi
PASS
Sunil
PASS
Raj a
FAIL
Maln.java

i mport java . util .*;

public class Main {


publ ic static void main (String[] args ) {
Scanner sc = new Scanner (System. in );
float s = sc . nextFloat ();
sc . nextL ine ();
Map<String, Float > i nput = new LinkedHashMap<>();
for ( int i = 0; i < s ; i ++) {
input . put ( sc . nextline (), sc . nextFloat ());
sc . next line ();
}
Map<String, String> lhml = UserMainCode . calculateGrade ( input );
for (Map . Entry<String, String> me : lhml . entrySet ()) {
System . out . println (me . getKey ());
System . out . println (me . getValue ());
}
sc . close ();
}
}

UserMainCode.java

import java . util . •;

publi c cl as s UserMainCode {
publ ic static Map<String, String>
calculateGrade(Map <String, Float > input ) {
Map<String, String> output = new LinkedHashMap <>();
for (Map . Entry <String, Float > me : input . entrySet ()) {
if (me . getValue () < 60) {
output . put (me . getKey (), "FAIL" );
} else {
output . put (me . get Key (), "PASS" );
}
}
return output ;
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy