Lab 3 Questions
Lab 3 Questions
Exercises:
1. Write a java program that will print the sum, the average and the product of
numbers from 1 till n. The program will ask the user to enter the value of n,
and then the program will return the value of the sum, the value of the
average and the value of the product of numbers from 1 till n.
Sample output:
Enter a number n: 6
The sum of numbers from 1 till 6 is: 21.0
The average of numbers from 1 till 6 is: 3.5
The product of numbers from 1 till 6 is: 720.0
2. Write a java program that will count all the even numbers, prime numbers
and odd numbers found in a series of numbers that the user will specify.
Your program will ask the user to enter a lower bound and an upper bound,
and then it will print the series of even, odd & prime numbers that are found
between the ranges specified by the user.
1|Page
Hint: Remember that a prime number is a number that is only divisible it
itself.
Sample Output:
Enter the lower bound
3
Enter the upper bound
31
Number of even numbers found: 14
Number of odd numbers found: 15
Number of prime numbers found: 10
3. Write a Java program that prints the following shape and asks the user if
he/she wants to print more or stop. When the user enters stop, the stop shape
must be printed. Use static methods to show structure and eliminate
redundancy in your solution.
Sample Output:
________
/ \
/ \
\ /
\________ /
Sample Output:
Enter a number: 74
3, 5, 7, 11, 13, 17, 19, 29, 31, 41, 43, 59, 61, 71, 73
Sample Output:
What is the number of elements you want to enter into the array? 6
Enter element [1]=1
Enter element [2]=22
Enter element [3]=43
3|Page
Enter element [4]=67
Enter element [5]=88
Enter element [6]=19
Second smallest element: 19
7. Write a java program that will ask the user to enter five digits into the array
and then the program will return the first number that is greater than the
average numbers of the array.
Note that you should use Array.sort() to sort your array before using it.
Sample Output:
8. Write a java program that will initialize a 3 * 3, 2-D array and calculates the
sum and the average of the elements of the array.
The program will first print the initialized array using function
printArray(int[][]a, int row, int col) and then create the function
calculateSum(int[][]a, int row, int col) that will take as an input the array
with its rows and columns and returns the sum of the array, and similarly the
function CalculateAverage(int[][]a, int r, int c) that will return the average
of the elements of the array.
Note that you will use the result of function CalculateSum in calculating the
average of elements in the array.
Your program will print the output using the function printResult(double x)
that will take a double variable as an argument and print it.
Sample Output:
Array:
012
345
4|Page
678
Sum:
36.0
Average:
4.0
9. Write a java program that will calculate the max of each row in a 2d array,
and store it in a 1-d array. The program will ask the user to enter the size of
rows and columns of the 2-d array, and enter the elements of the array using
function readArray(int[][] a, int row, int col), the program will then create
function maxRows(int[][] a, int[]b int r, int c) that will return a 1-d array b
that has the maximum of each row. Finally the program will print the result
of array b using function printArray(int[] x).
Sample Output:
Enter number of rows: 5
Enter numbers of columns: 3
a[0][0]= 1
a[0][1]= 2
a[0][2]= 3
a[1][0]= 4
a[1][1]= 5
a[1][2]= 6
a[2][0]= 7
a[2][1]= 8
a[2][2]= 9
a[3][0]= 0
a[3][1]= 9
a[3][2]= 8
a[4][0]= 8
a[4][1]= 7
a[4][2]= 6
Maximum of the rows:
Result[0]= 3 Result[1]= 6 Result[2]= 9 Result[3]= 9 Result[4]= 8
# 0 1 2 3 4
0 * * *
1 * * *
2 *
3 * *
4 * * *
(* means “friends”)
a. Write a method to count how many pairs of friends are represented in the
array. Note that each friendship pair appears twice in the array, so in the
example above there are 6 pairs of friends.
b. Write a method to check whether two people have a common friend. For
example, in the example above, 0 and 4 are both friends with 3 (so they have
a common friend), whereas 1 and 2 have no common friends. The method
should have three parameters: a 2D array of Boolean representing the
friendship relationships and two integers i, j. The method should return true
if there is an integer k such that i is a friend of k and k is a friend of j and
return false otherwise. Make this method static and test it from the main()
method.
11.Write a java program that implements the class Rectangle contains the
following variables and methods:
6|Page
Write a java program that implements the class TestRectangle:
- Your program should create two objects of type Rectangle as follows:
(11, 5).
(3 ,15).
- Your program should print the area of above declared objects as follows:
The area of rectangle with length 11 and width 5 is 55.
The area of rectangle with length 3 and width 15 is 45.
12.Write a java program that implements the class Vehicle contains the
following variables and methods:
- name: name of the vehicle is the type of it for example Volvo, Mercedes,
BMW, Buick, Ford, Honda, Toyota, …. etc.
- id: id of the vehicle is the vehicle number for example M1687952
- insertVehicle(n: String, d: String): inserts the name and the id of the
vehicle.
- display(): displays the name and id of the vehicle.
("Volvo", "M123").
("Nissan", "N123").
("Mazda","M87654").
7|Page