A. List All The Primitive Data Types and Explain The Difference Between Them
A. List All The Primitive Data Types and Explain The Difference Between Them
Question 1
a. List all the primitive Data types and explain the difference between them.
The program will print 1. The value of the variable z becomes 1 after(z=x;) the
the value of the variable y become 1 after (y=z;)
c. Change the program First.java such the variable y will hold the sum of x, y, z. Save
this program as sum1.java. The program should print the value of y.
Solution
e.
double myValue = a + b % d – c * d / b;
System.out.println(myValue);
Solutions
}
}
The program outputs:
10.34..
10.34..
1.34..
Question 2
a. Find all the errors in the following program:
Once you think you've found the errors, compile/run a corrected version of this
program
Solution:
Solution:
Question 3
a. Write a program. Double.java, which takes one integer command-line argument
and prints out double the number.
Solution:
// Integer.parseInt(args[0]) will convert the first argument from a string to integer
//Integer.parseInt(args[1]) * 2 will double it.
// SumInt will print the double
System.out.println(Integer.parseInt(args[0]) * 2);
}
}
b. Write a program, Sum.java which takes two integers command-line arguments. The
computer adds them up and prints out the answer
Solution.
System.out.println(Integer.parseInt(args[0]) +
Integer.parseInt(args[1]));
}
}
c. Write a program, Multiply.java which takes two integers command-line arguments.
The computer multiplies them together and prints out the answer.
Solution:
System.out.println(Integer.parseInt(args[0]) *
Integer.parseInt(args[1]));
}
}
d. Write a program, Divide.java; which takes two integers command-line arguments.
The computer divides the first argument by the second one and prints out the
answer.
Solution:
// Integer.parseInt(args[0]) = a will convert the first argument from a string to
integer
//Integer.parseInt(args[1])=b will convert the second argument from a string to
integer
// SumInt will print the a/b
System.out.println(Integer.parseInt(args[0]) /
(Integer.parseInt(args[1])*1.0));
}
}
}
}
Question 4:
Question 5
a. Write a program, Even.java, which prints out ‘yes’ if the number is even and
‘no’ otherwise.
Solution:
{
System.out.println("Yes");
}
else
{
System.out.println("NO");
}
}
}
b. Write a program. Multiple.java, which takes two integer arguments and prints
out ‘yes’ if one is a multiple of the other
Solution
// Integer.parseInt(args[0]) will convert the first argument from a string to
integer
//Integer.parseInt(args[1]) % 2 will check if the integer is a multiple of 2.
// Even will print the yes if the argument is even and no otherwise.
if (first % second == 0)
{
System.out.println("Yes");
}
else
{
if (second % first == 0)
{
System.out.println("Yes");
}
else
{
System.out.println("NO");
}
}
}
}