In: Computer Science
Q2.1 Write a Java program called Div that takes 2 (two) double command-line arguments as inputs, dividend and divisor (in that order) and performs a division operation. Your program either prints the quotient or an error if the divisor is zero. The divisor is the number you divide the dividend by.
public class Div
{
public static void main ( String[] args )
{
// WRITE YOUR CODE HERE
}
}
Q2.2
Write a Java program called IntCheck that examines the integer variable x, printing GT (greater than) if x is greater than 100, LT (less than) if x is less than 100 and EQ (equal) if x is 100.
public class IntCheck { public static void main ( String[] args ) { int x = Integer.parseInt(args[0]); // WRITE YOUR CODE HERE } }
Code for Q2.1 in java , given below :-
public class Div
{
public static void main(String[] args) {
double ans;
double dividend=1;
double divisor=1;
for(int
i=0;i<args.length;i++)
{
dividend =
Double.parseDouble(args[0]); // get command line argument dividend
as a string and convert to double
divisor =
Double.parseDouble(args[1]); //get command line
argument divisor as a stringand convert to double
System.out.println(args[i]); //print dividend and
divisor
}
if(divisor==0)
{
System.out.println("error, divisor is Zero!!!;"); //if divisor is
zero
}
else
if(divisor!=0)
{
ans=dividend/divisor;
System.out.println(ans); //print answer
}
}
}
Code for Q2.2 in java , given below :-
public class IntCheck
{
public static void main(String[] args) {
int x=0;
x = Integer.parseInt(args[0]); //
get number as command line argument as a string and convert it into
integer
if(x==100)
{
System.out.println("EQ (equal)"); // if x if equal to 100
}
else
if(x>100)
{
System.out.println("GT (greater than) "); // if x is greater than
100
}
else
if(x<100)
{
System.out.println("LT (less than) "); // if x is less than
100
}
}
}
Screenshot of code and output:-