Java Programs and VDTs
Question 8: Function Overloading
Design a class to overload a function num_cal() as follows:
(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");
}
}
}
VDT for Question 8
| Variable | Data Type | Description | Initial Value | Change in
Value |
|-----------------|-------------|------------------------------------------------------|-----------------|------------
--------------|
| sc | Scanner | To read user input |- | Changed after input
|
| n, n_1, n_2 | int | User input for number values |- | Changed after
input |
| c, c_1 | char | User input for character values |- | Changed after
input |
| str1, str2 | String | User input for strings |- | Changed after
input |
Question 9: Constructor (Finding the roots of a quadratic equation)
Write a program in java to find the roots of a quadratic equation ax + bx + c = 0 with the
following specifications:
Class Name: Quad
Data Members: float a,b,c,d (a,b,c are the coefficients and d is the discriminant), r1 and r2
are the roots of the equation.
Member methods:
Quad(float x, float y, float z): to initialize a with x, b with y, c with z and d with 0
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();
}
}
VDT for Question 9
| Variable | Data Type | Description | Initial Value | Change in
Value |
|-----------------|-------------|------------------------------------------------------|-----------------|------------
--------------|
| sc | Scanner | To read user input |- | Changed after input
|
| a, b, c | float | User input for coefficients of quadratic equation | - |
Changed after input |
|d | float | Discriminant value |0 | Calculated from a, b, c
|
| r1, r2 | float | Roots of the equation |- | Calculated from d
|
Question 10: String and Character functions
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");
}
}
}
VDT for Question 10
| Variable | Data Type | Description | Initial Value | Change in
Value |
|-----------------|-------------|------------------------------------------------------|-----------------|------------
--------------|
| sc | Scanner | To read user input |- | Changed after input
|
| str1, str2 | String | User input for two strings |- | Changed after
input |
|s | String | New string formed by combining the characters | "" |
Updated during loop |