Question

JAVA a. An integer is a "Lucky Number" if it is divisible by 7 or is...

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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
Compile and execute the application. You will discover that is has a bug in it -...
Compile and execute the application. You will discover that is has a bug in it - the filled checkbox has no effect - filled shapes are not drawn. Your first task is to debug the starter application so that it correctly draws filled shapes. The bug can be corrected with three characters at one location in the code. Java 2D introduces many new capabilities for creating unique and impressive graphics. We’ll add a small subset of these features to the...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT