231k views
2 votes
Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.

What is the difference between getter and setter methods
Write a Java program to test if the array Q contains this value (5) by using two algorithms:
Sequential Search:
Binary Search:
Q = [2,4,5,7,9,11,15]
Write a java program to do the following:
Create an ArrayList of fruit
Add the elements Banana, Apple, Orange, and Grape to the fruit list.
Use for loop to display the values in the fruit list as following:
Banana Apple Orange Grape
Sort the list then print it again without loop.
Write a program to find the sum and product of all elements of array A:
A = [10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60]

User JJ Beck
by
7.8k points

1 Answer

4 votes

Final Answer:

The Java program for the given tasks is as follows:

```java

class Student {

String name;

int roll_no;

}

public class Main {

public static void main(String[] args) {

Student student = new Student();

student.name = "John";

student.roll_no = 2;

// Sequential Search

int[] Q = {2, 4, 5, 7, 9, 11, 15};

boolean containsValueSequential = false;

for (int num : Q) {

if (num == 5) {

containsValueSequential = true;

break;

}

}

System.out.println("Sequential Search: " + containsValueSequential);

// Binary Search (Array Q must be sorted)

Arrays.sort(Q);

boolean containsValueBinary = Arrays.binarySearch(Q, 5) >= 0;

System.out.println("Binary Search: " + containsValueBinary);

// ArrayList of fruit

ArrayList<String> fruitList = new ArrayList<>();

fruitList.add("Banana");

fruitList.add("Apple");

fruitList.add("Orange");

fruitList.add("Grape");

// Display values with for loop

System.out.print("Fruit List: ");

for (String fruit : fruitList) {

System.out.print(fruit + " ");

}

// Sort and print without loop

Collections.sort(fruitList);

System.out.println("\\Sorted Fruit List: " + fruitList);

// Sum and product of array A

int[] A = {10, 12, 20, 30, 25, 40, 32, 31, 35, 50, 60};

int sum = Arrays.stream(A).sum();

int product = Arrays.stream(A).reduce(1, (a, b) -> a * b);

System.out.println("Sum of elements in array A: " + sum);

System.out.println("Product of elements in array A: " + product);

}

}

```

Explanation:

In the provided Java program, we first create a class named 'Student' with string variable 'name' and integer variable 'roll_no'. We then instantiate an object of the 'Student' class, setting 'name' to "John" and 'roll_no' to 2.

For the array Q, we perform a Sequential Search and a Binary Search to check if the value 5 is present. Sequential Search involves iterating through each element in the array until the target value is found. Binary Search, on the other hand, requires the array to be sorted and employs a divide-and-conquer strategy, making it more efficient.

Next, an ArrayList 'fruitList' is created and populated with the elements Banana, Apple, Orange, and Grape. Using a for loop, we display the elements in the original order and then sort the list. The sorted list is printed without using a loop.

Finally, for array A, the program calculates the sum and product of its elements using the Arrays.stream() method in Java. The sum is obtained by calling the sum() method, and the product is calculated using the reduce() method with the initial value set to 1.

These calculations ensure a comprehensive and accurate solution to the given programming tasks.

User ChrisPatrick
by
8.2k points