Java Programs and VDTs New
Java Programs and VDTs New
(1) void num_cal(int num, char ch) with one integer argument and one character argument.
It computes the square of an integer if choice ch is 's' or 'S' else computes its cube.
(ii) void num_cal(int a, int b, char ch) with two integer arguments and one character
argument.
It computes the product of integer arguments if ch is 'p' or 'P' else adds the integers.
(iii) void num_cal(String str1. String str2) with two String arguments prints whether the
two strings are equal or not.
Java Program:
import java.util.Scanner;
public class Question_8 {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
Question_8 O = new Question_8();
System.out.print("Enter a number : ");
int n = sc.nextInt();
System.out.print("Enter character 's' or any character: ");
char c = sc.next().charAt(0);
O.cal_num(n,c);
System.out.print("Enter a number : ");
int n_1 = sc.nextInt();
System.out.println("Enter a number : ");
int n_2 = sc.nextInt();
System.out.print("Enter a character 'p' or any character : ");
char c_1 = sc.next().charAt(0);
O.cal_num(n_1 , n_2 , c_1);
System.out.print("Enter a String : ");
String str1 = sc.next();
System.out.print("Enter a String : ");
String str2 = sc.next();
O.cal_num(str1 , str2);
}
void cal_num(int num, char ch) {
if( ch =='s' || ch =='S') {
System.out.println("Square : "+(num*num));
}
else {
System.out.println("Cube : "+(num*num*num));
}
}
void cal_num(int a, int b, char ch) {
if(ch == 'p' || ch == 'P') {
System.out.println("Product : "+(a*b));
}
else {
System.out.println("Sum : "+(a+b));
}
}
void cal_num(String str1, String str2) {
if( str1.equalsIgnoreCase(str2)) {
System.out.println("Both are equal");
}
else {
System.out.println("Both are not equal");
}
}
}
Write a program in java to find the roots of a quadratic equation ax + bx + c = 0 with the
following specifications:
import java.util.Scanner;
public class Quad {
float a,b,c,d,r1,r2;
Quad( float x ,float y ,float z) {
this.a = x;
this.b = y;
this.c = z;
d = 0;
}
void calculate() {
d = (float)(Math.pow(b,2)- 4*a*c);
if( d < 0 ) {
System.out.println("Roots not possible");
}
else if( d == 0 ) {
System.out.println("Root : "+ (-b/2*a));
System.out.println("Roots are equal");
}
else {
r1 = (float) (-b + Math.sqrt(d))/2*a;
r2 = (float) (-b - Math.sqrt(d))/2*a;
System.out.println("Root 1 : "+r1);
System.out.println("Root 2 : "+r2);
}
}
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Value for 'a' :");
float a = sc.nextFloat();
System.out.print("Enter the Value for 'b' :");
float b = sc.nextFloat();
System.out.print("Enter the Value for 'c' :");
float c = sc.nextFloat();
Quad O = new Quad(a,b,c);
O.calculate();
}
}
Write a program in Java to input two strings of the same length and form a new word in
such a way that,
the first character of the first word is followed by the first character of the second word and
so on.
If the strings are not of the same length, then print the message "Invalid Input".
import java.util.Scanner;
public class Question_10 {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a String : ");
String str1 = sc.next();
System.out.print("Enter a String with same length as String 1: ");
String str2 = sc.next();
String s = "";
if( str1.length() == str2.length()) {
for( int i = 0; i < str1.length(); i++) {
char a = str1.charAt(i);
char b = str2.charAt(i);
s = s + a + b;
}
System.out.println("Output : "+s);
} else {
System.out.println("Invalid input");
}
}
}