JAVA
a.
An integer is a "Lucky Number" if it is divisible by 7 or is
divisible by 11 and it is in the range 1000 through 4000.
Write a Boolean expression that is true if and only if myNum (an
int variable) contains a Lucky Number.
b.
Let a and b represent the length and the width of a
rectangle.
The length of the diagonal of the rectangle can be calculated by
the following mathematical expression.
diagonalLength = squareRoot(a x a + b x b)
If len and wid are double variables holding the values of the
length and the width of some rectangle, write an expression in Java
for the length of the diagonal of that rectangle.
c.
Write a Java method named ran5 that receives no arguments and
returns a random integer between 0 and 5.
d. Suppose I am doing a binary search on the following array for
the number 117:
int nums[] = {1, 77, 78, 89, 100, 125, 235, 390, 1000};
List the numbers (or indices) that are checked by the search before
it reports that 117 is NOT found in the array.
Solution
a)
import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number:");
int myNum=sc.nextInt();
if((myNum%7==0 || myNum%11==0)&&(myNum>=1000 ||
myNum<=4000))
System.out.println("The Number is a Lucky Number");
else
System.out.println("The Number is not a Lucky Number");
}
}
Screenshot
Output
---
b)
Code
import java.util.*;
public class Main
{
public static void main(String args[])
{
double length,width,diagonalLength;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length of the rectangle:");
length=sc.nextDouble();
System.out.println("Enter the width of the rectangle:");
width=sc.nextDouble();
diagonalLength=Math.sqrt(length*length+width*width);
System.out.println("Diagonal of the Rectangle:
"+diagonalLength);
}
}
Screenshot
Output
---
all the best
Get Answers For Free
Most questions answered within 1 hours.