Question

In: Computer Science

1: Answer these questions: (a) Write a Java program to print whole numbers between 1 to...

1: Answer these questions:

(a) Write a Java program to print whole numbers between 1 to 1000 which are divisible by 3, 5 and by both numbers.
(b) Differentiate between instance and local variable in Java
(c) In a tabular form, differentiate between instance and class variable

Solutions

Expert Solution

(a)

Source Code:

class ex
{
   public static void main(String[] args) {
       for(int i=1;i<=1000;i++)
       {
           if(i%3==0 && i%5==0)
           {
               System.out.println(i);
           }
       }
   }
}

Sample input and output:

(b)

Differences between instance and local variable in Java :

Instance Variables:

Variables that are declared inside the class but outside of the methods of class.

Ex: class Student{

String name;

int id;

}

Local variables:

Variables that are declared in any method, constructor or any block is considered as local variables.

Ex: int age;

String name ;

(c)

differences between instance and class variable :

Instance Variables Class Variables
1.variables declared inside class but ouside of methods. 1. Variables of a class declared as final or static.
2. These variables are referenced with objects.it means every object have these variables once initialized. 2. The values of this variables does not change.
3. These variables need to be private in order to satisfy the Encapsulation and other features. 3. we must use static keyword before it. and the access specifier is public in nature

Related Solutions

I need it in java. Write a program that will print if n numbers that the...
I need it in java. Write a program that will print if n numbers that the user will input are or not within a range of numbers. For that, your program needs to ask first for an integer number called N that will represent the number of times that will ask for other integer numbers. Right after, it should ask for two numbers that will represent the Min and Max for a range. Lastly. it will iterate N number times...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified maximum. Display the total amount of numbers printed.
write a java program that takes three numbers from the user and print the greatest number...
write a java program that takes three numbers from the user and print the greatest number (using if-statement). sample output: input the first number:35 input the second number:28 input the third number:87 the greatest number:87
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
in java Write a program that will print the following output 1 1 2 1 1...
in java Write a program that will print the following output 1 1 2 1 1 2 4 2 1 1 2 4 8 4 2 1 1 2 4 8 16 8 4 2 1 1 2 4 8 16 32 18 8 4 2 1
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in...
JAVA: Display all even numbers between 1 and 20 --Optional write a program that reads in 20 numbers. A method with an int parameter should display whether the number is odd or even for the number passed
Write a Java program to print the pattern of asterisks shown below. For i=1 * For...
Write a Java program to print the pattern of asterisks shown below. For i=1 * For i=2 * * * For i=3 * ** * * * For i=n * * * * * * … … … * * * * * * ……… n
java please 1. Write a Java program to generate random numbers in the following range a....
java please 1. Write a Java program to generate random numbers in the following range a. 1 <=n <= 3 b. 1 <= n <= 200 c. 0 <= n <= 9 d. 1000 <= n <= 2112 e. -1 <= n <= 5 2. Write statements that assign random integers to the variable n in the following ranges: a) 1 ≤ n ≤ 2 b) 1 ≤ n ≤ 100 c) 0 ≤ n ≤ 9 d) 1000 ≤...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by...
JAVA Write a program to sum the numbers from 1 to 100 that are divisible by 7, and compute the average of those numbers, print both the sum and the average with appropriate messages to the screen. Run the program. Capture the console output. Put the program code and console output at the end of your text file,
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT