JAVA Programming with Strings and
Random Numbers
1. We need to generate 20
random numbers in the range specified by the user. Write a Java program to take
starting and ending of the range of random numbers from user and display 20
random numbers generated between the range. Display the random numbers
separated by comma (,).
Code :
// Question 1
import java.util.Random;
import java.util.Scanner;
public class GenerateRandom{
public static void main(String args[]){
Scanner s1= new Scanner(System.in);
System.out.println("Enter start value :-");
int start =s1.nextInt();
System.out.println("Enter end value :-");
int end =s1.nextInt();
System.out.println(" \n Top 20 numbers generated between "+start+" to "+end+" are as below. ");
Random rand2 = new Random(end-start);
for(int i= 0; i<20; i++){
int R2= (int)(Math.random()*(end-start+1)+start);
if(i==19){
System.out.print(R2);
}else
System.out.print(R2+",");
}
}
}
OUTPUT :
2. Write a Java program that
creates a Random object with seed 1000 and displays the first 100 random
integers between 1 and 49 using the NextInt (49) method.
Code :
// Question 2
import java.util.Random;
public class Random_object {
public static void main(String[] args) {
Random rand = new Random();
rand.setSeed(1000);
for (int i = 1; i < 50; i++) {
System.out.println(rand.nextInt(100));
}
}
}
OUTPUT :
3. Write a Java program for
calculator to accept an expression as a string in which the operands and
operator are separated by zero or more spaces. Print the answer of the
expression entered by user.(3+4 and 3 + 4 are acceptable expressions.)
Code:
// Question 3
import java.util.Scanner;
public class Evaluate{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter Equation : ");
String str = input.nextLine();
String a = str.replaceAll(" ","");
if (a.length() < 3) {
System.out.println(
"Minimum 2 Opearator and 1 Opearand Required");
System.exit(0);
}
int result = 0;
int i = 0;
while(a.charAt(i)!='+' && a.charAt(i)!='-' && a.charAt(i)!='*' && a.charAt(i)!='/')
{
i++;
}
switch (a.charAt(i)) {
case '+' :
result = Integer.parseInt(a.substring(0,i))+Integer.parseInt(a.substring(i+1,a.length()));
break;
case '-' :
result = Integer.parseInt(a.substring(0,i))-Integer.parseInt(a.substring(i+1,a.length()));
break;
case '*' :
result = Integer.parseInt(a.substring(0,i))*Integer.parseInt(a.substring(i+1,a.length()));
break;
case '/' :
result = Integer.parseInt(a.substring(0,i))/Integer.parseInt(a.substring(i+1,a.length()));
break;
}
System.out.println("Answer of expression "+str + " is " + result);
}
}
OUTPUT :
4.Write a Java program to
compare two strings by ignoring the case of the characters. Take two strings
from user and print appropriate output after comparing them.
Code :
// Question 4
import java.util.Scanner;
public class Comparison{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println(" Enter String 1: ");
String str1 = s.nextLine();
System.out.println(" Enter String 2: ");
String str2 = s.nextLine();
if(str1.equalsIgnoreCase(str2)){
System.out.println("Both Strings are equal.");
} else {
System.out.println("Both Strings are not equal.");
}
}
}
OUTPUT :
5. Write a java program that
checks if a substring is present in the string or not. Ignore the case while
comparing.
Code :
// Question 5
import java.util.Scanner;
public class String_Comparison{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("Enter String : ");
String str1 = s.nextLine();
str1 = str1.toLowerCase();
System.out.println("Enter Sub String : ");
String str2 = s.nextLine();
str2 = str2.toLowerCase();
if(str1.contains(str2)){
System.out.println("The substring is present in the string.");
} else {
System.out.println("The substring is not present in the string.");
}
}
}
OUTPUT :
Download pdf here...... |
No comments:
Post a Comment