0% found this document useful (0 votes)
30 views11 pages

Java String and Date Manipulation Programs

The document describes a program to assign letter grades to students based on their marks. It includes: 1. A UserMainCode class with a static calculateGrade method that takes a HashMap of student names and marks as input and returns a HashMap of names and grades (PASS/FAIL). 2. A Main class to get student details from the user, build a HashMap of names and marks, and call the calculateGrade method. 3. Business rules to assign grades: marks < 60 = FAIL, marks >= 60 = PASS (in uppercase). The program reads student names and marks, maps them to a HashMap, calculates grades using the business rules, and returns a HashMap of

Uploaded by

Advaid santhosh
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)
30 views11 pages

Java String and Date Manipulation Programs

The document describes a program to assign letter grades to students based on their marks. It includes: 1. A UserMainCode class with a static calculateGrade method that takes a HashMap of student names and marks as input and returns a HashMap of names and grades (PASS/FAIL). 2. A Main class to get student details from the user, build a HashMap of names and marks, and call the calculateGrade method. 3. Business rules to assign grades: marks < 60 = FAIL, marks >= 60 = PASS (in uppercase). The program reads student names and marks, maps them to a HashMap, calculates grades using the business rules, and returns a HashMap of

Uploaded by

Advaid santhosh
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

Set-1

[Link] finder
Given three strings say Searchstring, Str1 and Str2 as input, write a program to find out if Str2
comes after Str1 in the Searchstring.
Include a class UserMainCode with a static method “stringFinder” that accepts 3 String
arguments and returns an integer. The 3 arguments correspond to SearchString, Str1 and Str2.
The function returns 1 if Str2 appears after Str1 in the Searchtring. 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 Str1.
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
UserMainCode
public class UserMainCode {
public static int stringFinder(String s1,String s2,String s3)
{
String a1=[Link]();
String a2=[Link]();
String a3=[Link]();
if([Link](a2)&&[Link](a3))
{
if([Link](a2)<[Link](a3))
{
return 1;
}
else
return 2;
}
return 0;
}}

Main
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner([Link]);
String s1=[Link]();
String s2=[Link]();
String s3=[Link]();
int b=[Link](s1, s2, s3);
if(b==1)
{
[Link]("Yes");
}
else
[Link]("No");
[Link]();
}
}

geniousRajKumarDev
Dev
Raj
No
[Link] Shrinking
Write a program that accepts a string as input and converts the first two names into
dotseparated
initials and printa the output.
Input string format is 'fn mn ln'. Output string format is 'ln [mn's 1st character].[fn's 1st
character]'
Include a class UserMainCode with a static method getFormatedString 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
present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of a String.
Refer sample output for formatting specifications.
Sample Input:
Sachin Ramesh Tendulkar
Sample Output:
Tendulkar R.S

UserMainCode
import [Link];
public class UserMainCode {
public static String getFormatedString(String s1) {
StringBuffer sb=new StringBuffer();
StringTokenizer st=new StringTokenizer(s1," ");
String s2=[Link]();
String s3=[Link]();
String s4=[Link]();
[Link](s4).append(" ");
[Link]([Link](0,1));
[Link](".");
[Link]([Link](0,1));
return [Link]();
}
}

Main
import [Link].*;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner([Link]);
String s1=[Link]();
[Link]([Link](s1));
}
}

Sachin Ramesh Tendulkar


Tendulkar R.S
3.digits2
Write a program to read a non-negative integer n, compute the sum of its digits. If sum is
greater than 9 repeat the process and calculate the sum once again until the final sum comes
to single [Link] the single digit. Include a class UserMainCode with a static method
getDigitSum 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 present in UserMainCode.
Input and Output Format: Input consists of a integer. Output consists of integer. Refer sample
output for formatting specifications.
Sample Input 1: 9999 Sample Output 1: 9
Sample Input 2: 698 Sample Output 2: 5
UserMainCode
public class UserMainCode
{
public static int getDigitSum(int n)
{
int sum = 0 ;
while(n>10)
{
int a = 0 ; sum = 0;
while(n!=0)
{
a = n%10;
sum+=a;
n=n/10;
}
n=sum;
}
return sum;
}
}
Main
import [Link];
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner([Link]);
int a=[Link]();
int sum=[Link](a);
[Link](sum);
}
}698
5
[Link] Substring
Write a program to accept two string inputs. The first being a source string and second
one
a delimiter. The source string contains the delimiter at various locations. Your job is to
return the substring with maximum number of characters. If two or more substrings
have
maximim number of characters return the substring which appears first. The size of the
delimiter is 1.
Include a class UserMainCode 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
present in UserMainCode.
Input and Output Format:
Input consists of a source string and delimiter.
Output consists of a string.
Refer sample output for formatting specifications.
Sample Input 1:
delhi-pune-patna
-
Sample Output 1:
Delhi\

UserMainCode
import [Link];
public class UserMainCode {
public static String extractmax(String input1, String input2){
int max = 0;
String s3 = null;
StringTokenizer st = new StringTokenizer(input1,input2);
while ([Link]()){
String s2 = [Link]();
int n = [Link]();
if(n > max){
max = n;
s3 = s2;
}
}
return s3;
}
}
Main
package Maxstring;
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
String input1 = [Link]();
String input2 = [Link]();
[Link]([Link](input1,input2));
}
}
delhi-pune-patna, output : - delhi
[Link]
Write a program to validate the Date of Birth given as input in String format (MM/dd/yyyy)
as per the validation rules given below. Return true for valid dates else return false.
1. Value should not be null
2. month should be between 1-12, date should be between 1-31 and year should be a four
digit number.
Include a class UserMainCode with a static method ValidateDOB which accepts the string.
The return type is TRUE / FALSE.
Create a Class Main which would be used to accept the string and call the static method
present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of TRUE / FALSE.
Refer sample output for formatting specifications.
Sample Input 1:
12/23/1985
Sample Output 1:
TRUE
Sample Input 2:
31/12/1985
Sample Output 2:
FALSE
UserMainCode
import [Link];
import [Link];
public class UserMainCode {
public static Boolean ValidateDOB(String str){
Boolean b=false;
SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy");
[Link](false);
try
{
Date d1=[Link](str);
return b=true;
}
catch(Exception e)
{
return b=false;
}
}
}
Main
package DateOfBirth;
import [Link];
public class Main {
public static void main(String[] args)
{
String str=new String();
Scanner sc=new Scanner([Link]);
str=[Link]();
Boolean b=[Link](str);
if(b==true)
[Link]("TRUE");
if(b==false)
[Link]("FALSE");
}
}

31/12/1985
FALSE
[Link]
Given an input as date of birth of person, write a program to calculate on which day
(MONDAY,TUESDAY....) he was born store and print the day in Upper Case letters.
Include a class UserMainCode with a static method calculateBornDay which accepts a
string
as input.
The return type of the output is a string which should be the day in which the person was
born.
Create a class Main which would get the input and call the static
method calculateBornDay present in the UserMainCode.
Input and Output Format:
NOTE: date format should be(dd-MM-yyyy)
Input consists a date string.
Output is a string which the day in which the person was born.
Refer sample output for formatting specifications.
Sample Input 1:
29-07-2013
Sample Output 1:
MONDAY
Sample Input 2:
14-12-1992
Sample Output 2:
MONDAY

UserMainCode
import [Link];
import [Link];
import [Link].*;
public class UserMainCode {
public static String calculatebornday(String input) throws ParseException{
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("EEEEE");
Date d = [Link](input);
String s1 = [Link](d);
String s2 = [Link]();
return s2;
}
}
Main
import [Link];
import [Link].*;
public class Main {
public static void main(String[] args) throws ParseException {
Scanner scanner = new Scanner([Link]);
String input = [Link]();
[Link]([Link](input));
}
}

14-12-1992 Output:- MONDAY


[Link] calculator (code:53)
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 like to showcase your
skills by creating a quick prototype. The prototype consists of the following steps:
Read student details from the User. The details would include name, mark 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 with name as Key and grade as value.
4. You decide to write a function calculateGrade which takes the above hashmap as input
and returns the hashmap as output. Include this function in class UserMainCode.
Create a Class Main which would be used to read student details in step 1 and build the
hashmap. Call the static method present in UserMainCode.
Input and Output Format:
Input consists of student details. The first number indicates the size of the students. The
next two values indicate the 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
Raja
36.25
Sample Output 1:
Avi
PASS
Sunil
PASS
Raja
FAIL
UserMainCode
import [Link];
import [Link];
import [Link];
public class UserMainCode {
public static TreeMap<String, String>calculategrade(HashMap<String, Float>hm){
TreeMap<String, String> tm = new TreeMap<String, String>();
Iterator<String> it = [Link]().iterator();
while([Link]()){
String name = [Link]();
float mark = [Link](name);
if(mark >= 60){
[Link](name,"PASS");
} else if(mark <= 60){
[Link](name,"FAIL");
}
}
return tm;
}
}
Main

import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
float s = [Link]();
[Link]();
HashMap<String, Float> hm = new HashMap<String, Float>();
for(int i = 0; i < s; i++){
[Link]([Link](), [Link]());
[Link]();
}
TreeMap<String, String> tm = new TreeMap<String, String>();
tm = [Link](hm);
Iterator<String> it = [Link]().iterator();
for(int i = 0; i < s; i++){
String n = [Link]();
String fac = [Link](n);
[Link](n);
[Link](fac);
}
}
}

Raja
36.35
Avi
PASS
Raja
FAIL
Sunil
PASS
[Link] calculator (code:28)
A School wants to assign grades to its students based on their marks. You have been assigned as the
programmer to
automate this process. You would like to showcase your skills by creating a quick prototype. The
prototype consists of the
following steps:
1. Read student details from the User. The details would include roll no, mark in the given order. The
datatype for id
is integer, mark is integer.
2. You decide to build a hashmap. The hashmap contains roll no as key and mark as value.
3. BUSINESS RULE:
1. If Mark is greater than or equal to 80 store medal as ""GOLD"".
2. If Mark is less then to 80 and greater than or equal to 60 store medal as ""SILVER"".
3 .If Mark is less then to 60 and greater than or equal to 45 store medal as ""BRONZE"" else store
""FAIL"".
Store the result in TreeMap in which Roll No as Key and grade as value.
4. You decide to write a function calculateGrade which takes the above hashmaps as input and
returns the treemap as
output. Include this function in class UserMainCode.
Create a Class Main which would be used to read employee details in step 1 and build the
two hashmaps. Call the static
method present in UserMainCode.
Input and Output Format:
Input consists of employee details. The first number indicates the size of the students. The next two
values indicate the roll
id, mark.
Output consists of a single string.
Refer sample output for formatting specifications.
Sample Input 1:
2
1010
80
100
40
Sample Output 1:
100
FAIL
1010
GOLD
UserMainCode
import [Link];
import [Link];
import [Link];
public class UserMainCode
{
public static TreeMap<Integer,String>calculateGrade(HashMap<Integer,Integer>hm)
{
TreeMap<Integer,String>tm=new TreeMap<Integer,String>();
Iterator<Integer> it=[Link]().iterator();
while([Link]())
{
int id=[Link]();
int mark=[Link](id);
if(mark>=80)
[Link](id,"GOLD");
else if(mark<80 && mark>=60)
[Link](id,"SILVER");
else if(mark<60 && mark>=45)
[Link](id,"BRONZE");
else
[Link](id,"FAIL");
}
return tm;
}}Main
import [Link];
import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String []args){
Scanner sc=new Scanner([Link]);
int s=[Link]();
HashMap<Integer,Integer>hm=new HashMap<Integer,Integer>();
for(int i=0;i<s;i++)
{
[Link]([Link](),[Link]());
}
TreeMap<Integer,String>tm=new TreeMap<Integer,String>();
tm=[Link](hm);
Iterator<Integer> it=[Link]().iterator();
for(int i=0;i<s;i++)
{
int n=[Link]();
String fac=[Link](n);
[Link](n);
[Link](fac);
}
}
}
1010
80
100
40
100
FAIL
1010
GOLD

Common questions

Powered by AI

The 'calculateGrade' method, using student roll numbers as keys and their marks as values from a HashMap, assigns medals according to the following rules: 'GOLD' for marks 80 and above, 'SILVER' for marks between 60 and 79, 'BRONZE' for marks between 45 and 59, and 'FAIL' for marks below 45. The output is a TreeMap where each roll number is matched to the respective medal .

The 'extractMax' method identifies substrings within a source string that are separated by a specified one-character delimiter. It returns the substring with the maximum number of characters. In case of a tie, where two or more substrings have the maximum length, it returns the substring that appears first in the source string .

The 'getFormatedString' method transforms a full name string by separating it into three parts: the first name, middle name, and last name. It then constructs a new string using the last name followed by the initials of the middle and first names, each followed by a dot. The resulting output format is 'LastName M.F.' where 'M' and 'F' are the initials of the middle and first names respectively .

The 'calculateBornDay' method parses the input date of birth string using a SimpleDateFormat with the pattern 'dd-MM-yyyy'. After successfully parsing the date, it formats the resulting Date object to extract the day of the week using another SimpleDateFormat with the pattern 'EEEEE'. The day is returned in upper case as a string representing the weekday on which the person was born .

The 'ValidateDOB' method checks the validity of a date of birth input string formatted as 'MM/dd/yyyy'. It ensures the input is not null and verifies the month is between 1 and 12, the date is between 1 and 31, and the year has four digits. The method uses a SimpleDateFormat object with leniency set to false to attempt parsing the date string, returning true if parsing is successful and false otherwise .

The 'calculateGrade' method assigns grades based on the following business rules: Marks less than 60 result in a 'FAIL'; a mark of 60 or more results in a 'PASS'. The method structures the output as a TreeMap, with each student's name as the key and the corresponding grade as the value. The output maintains the sorting of student names .

The program transforms the list of student-grade pairs by converting it into a TreeMap, which organizes the entries by student names due to the natural sorting order of the keys in TreeMap. This structure ensures that when the grades are printed, they appear in alphabetical order of the students' names, enhancing readability and consistency .

The 'getDigitSum' method repeatedly sums the digits of a given non-negative integer. If the resulting sum has more than one digit, the process repeats with the new sum until a single-digit sum is obtained. The method uses a loop to extract and sum each digit, then iterates the process as long as the sum exceeds 9 .

The method used is 'stringFinder' from the class 'UserMainCode'. It accepts three strings as input: the search string, Str1, and Str2. The method converts these strings to lowercase to handle case sensitivity and checks if both Str1 and Str2 are present in the search string. It returns 1 if Str2 appears after Str1; otherwise, it returns 2 .

Delimiters in the 'extractMax' method impact the substring extraction process by separating segments of the source string. The method scans the string, breaking it into substrings using the specified delimiter. The positioning and frequency of delimiters determine the potential number and length of substrings. The method evaluates each substring to identify the longest one and handles ties by selecting the earliest longest substring, highlighting the importance of delimiter placement .

You might also like