Dears,, Kindly, I need full and unique answers to these questions: Q1)Give the t
ID: 3605060 • Letter: D
Question
Dears,,
Kindly, I need full and unique answers to these questions:
Q1)Give the type and value of each result of the following Java expressions.
a) (5 / 2) * 2.0
b) (5/2.0) * 2
c) "1.3" + "5.2"
d) 1 + 7.0 + "2" + "x"
Q2) For the following mathematical expressions, write the corresponding Java expressions.
Q3)
Given the following declaration:
int i1 = 1;
int i2 = 2;
String S1 = "Hello";
String S2 = "HELLO";
double d1 = 1.0;
double d2 = 2;
For each of the following comparisons, specify:
1) Which are syntactically correct?
2) Which are syntactically incorrect?
3) Which are syntactically correct but logically questionable?
Only specify the answer on the following table; an example is given.
Comparisons
Syntactically correct
Syntactically incorrect
Syntactically correct, but logically questionable
i1 == i2
X
d1 == d2
s1 == "Hello"
s1 == s2
s1 == d1
i2-d2 == null
i1.equals(i2)
d1.equals(d2)
(s1-s2).equals(null)
i1-i2 == 0
s1.equals(s2)
Q4) Write program that takes the user age as input. Then the program will display whether the person is authorized to participate in vote or not. A person’s age who is authorized to vote should be 18 years or older.
Example output:
Enter your age: 18
You are authorized to vote.
Comparisons
Syntactically correct
Syntactically incorrect
Syntactically correct, but logically questionable
i1 == i2
X
d1 == d2
s1 == "Hello"
s1 == s2
s1 == d1
i2-d2 == null
i1.equals(i2)
d1.equals(d2)
(s1-s2).equals(null)
i1-i2 == 0
s1.equals(s2)
Explanation / Answer
If you have any doubts kindly you can comment. I'll reply as soon as possible.
Kindly rate the answer.All the best. :)
*********************************************************************
Q1)Give the type and value of each result of the following Java expressions.
a) (5 / 2) * 2.0
Value: 4.0
Explanation: (5/2) evaluates to 2 because both 5 and 2 are integers and,
(5/2)*2.0 evaluates to 4.0 since 2.0 is a double. int*double = double(2*2.0=4.0)
b) (5/2.0) * 2
Value: 5.0
Explanation: (5/2.0) evaluates to 2.5 because 5 is int and 2 is doubleand,
(5/2.0)*2 evaluates to 5.0 since 2.5 is double and 2 is int. double*int = double(2.5*2=5.0)
c) "1.3" + "5.2"
Value:1.35.2
Explanation: Here + acts as concatenation operator and concatenates 2 strings "1.3" and "5.2".
d) 1 + 7.0 + "2" + "x"
Value:8.02x
Explanation: First the sum of 1 and 7.0 is calculated which is 8.0, then it is concatenated with "2" which becomes 8.02 and then again concatenates with "x" and becomes 8.02x
*********************************************************************
Q2.
double squareroot = Math.sqrt((x*x)-y);
Math.sqrt method in java finds the squareroot of the expression.
*********************************************************************
*********************************************************************
Q3.
Comparisons
Syntactically correct
Syntactically incorrect
Syntactically correct, but logically questionable
i1 == i2
X
d1 == d2
X
s1 == "Hello"
X
s1 == s2
X
s1 == d1
X
i2-d2 == null
X
i1.equals(i2)
X
d1.equals(d2)
X
(s1-s2).equals(null)
X
i1-i2 == 0
X
s1.equals(s2)
X
*********************************************************************
*********************************************************************
Q4.
The code is pretty straight :)
Use Scanner to take input.
Chec if age>=18 or not and print the appropriate message.
Code:
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
System.out.println("Enter your age: ");
Scanner sc = new Scanner(System.in);
int age=sc.nextInt();
if(age>=18)
System.out.println("You are authorized to vote.");
else
System.out.println("You are not authorized to vote.");
sc.close();
}
}
Sample outputs:
***********************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Comparisons
Syntactically correct
Syntactically incorrect
Syntactically correct, but logically questionable
i1 == i2
X
d1 == d2
X
s1 == "Hello"
X
s1 == s2
X
s1 == d1
X
i2-d2 == null
X
i1.equals(i2)
X
d1.equals(d2)
X
(s1-s2).equals(null)
X
i1-i2 == 0
X
s1.equals(s2)
X
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.