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...
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 ≤...
Write a complete Java program to print out the name bob
Write a complete Java program to print out the name bob
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,
Write a java program that will take a line of input and go through and print...
Write a java program that will take a line of input and go through and print out that line again with all the word numbers swapped with their corresponding numeric representations (only deal with numbers from one to nine). Sample runs might look like this: Please enter a line of input to process: My four Grandparents had five grandchildren My 4 grandparents had 5 grandchildren without array and methods.
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT