1 Write a java method that takes a string as a parameter and
1. Write a java method that takes a string as a parameter and returns a string that replace each \'s\' or \'S\' by 5 and each \'z\' or \'Z\' by 2
2. Write a java program that uses a loop to input 10 doubles from the user and print the second minimum
Solution
1)
package org.students;
import java.util.Scanner;
public class ReplaceSswith5Zzwith2 {
public static void main(String[] args) {
//Declaring variables
String str,newStr=\"\";
char ch;
//Scanner class object is used to read the inputs enteredby the user
Scanner sc=new Scanner(System.in);
//Getting the string entered by the user
System.out.print(\"Enter String :\");
str=sc.nextLine();
/* This for loop will take each character from the String
* and check whether that character is \'s\' or \'S\' or \'z\' or \'Z\'
* if yes replace with values and make a new string
*/
for(int i=0;i<str.length();i++)
{
//Checking whether the character is \'s\' or \'S\'
if(str.charAt(i)==\'s\' || str.charAt(i)==\'S\')
{
ch=\'5\';
}
//Checking whether the character is \'z\' or \'Z\'
else if(str.charAt(i)==\'z\' || str.charAt(i)==\'Z\')
{
ch=\'2\';
}
else
ch=str.charAt(i);
//Appending each character to the \'newStr\' variable
newStr+=ch;
}
//Displaying the string
System.out.println(\"String After Replacing :\"+newStr);
}
}
___________________________________
Output:
Enter String :Stranzer
String After Replacing :5tran2er
____________________________________
2)
package org.students;
import java.util.Scanner;
public class SecondMinimum {
public static void main(String[] args) {
//Creating an double array
double arr[]=new double[10];
double min,second_min;
//Scanner class object is used to read the inputs entered by the user
Scanner sc=new Scanner(System.in);
/* This for loop will prompt the user to enter
* the values and populate them into an array
*/
for(int i=0;i<10;i++)
{
//Getting the number entered by the user
System.out.print(\"Enter number \"+(i+1)+\":\");
arr[i]=sc.nextDouble();
}
min=arr[0];
second_min=arr[0];
//This for loop will finds the minimum element
for(int i=0;i<10;i++)
{
if(arr[i]<min)
min=arr[i];
}
//This for loop will finds the second minimum element
for(int i=0;i<10;i++)
{
if(arr[i]<second_min && arr[i]!=min)
second_min=arr[i];
}
//Displaying the second minimum element
System.out.println(\"The Second minimum Element is :\"+second_min);
}
}
___________________________________
output:
Enter number 1:23.34
Enter number 2:34.45
Enter number 3:45.56
Enter number 4:56.67
Enter number 5:67.78
Enter number 6:78.89
Enter number 7:11.12
Enter number 8:22.23
Enter number 9:33.34
Enter number 10:44.45
The Second minimum Element is :22.23
________________Thank You


