Q:

Write a Java program for students marks by using class Arrays

0

Write a Java  program to implement the following by using class Arrays:

-        Read 4 student marks and keep them in array marks.

-        Print the elements of array marks.

-        Copy the content of array marks to another array copyMarks.

-        Sort the array marks in ascending order and print the sorted array.

-        Compare whether the two arrays marks and copyMarks are equal.

-        Ask the user to enter a mark and serch for it in the array markse and print appropriate message.

Hint: use toString() for print, arraycopy() for copy, sort() for sort, equals() for comparison, and binarySearch() for search.

 

Sample of output:


Enter 5 student marks:

Student 1: 100

Student 2: 85

Student 3: 70

Student 4: 66

Student marks: [100.0, 85.0, 70.0, 66.0]

Copy of marks is made ...

Student marks sorted in ascending order:  [66.0, 70.0, 85.0, 100.0]

is marks = copy of marks? true

Enter mark to search for it: 85

85.0 is found at position 2

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Arrays;
import java.util.Scanner;


public class Mavenproject1 {

    public static void main(String[] args) {
 Scanner read = new Scanner(System.in);
        double[] marks = new double[4];
        System.out.println("Enter 5 student marks:");
        double mark;
        for(int i =0 ; i<marks.length;i++){
            System.out.print("Student "+(i+1)+":");
            mark = read.nextDouble();
            marks[i]=mark;
        }
        System.out.println("Student marks: " +Arrays.toString(marks)); 
        double[] copyMarks = new double [marks.length];
        Arrays.sort(marks);
        System.out.println("Student marks sorted in ascending order: "+Arrays.toString(marks));
        System.arraycopy(marks, 0, copyMarks, 0, marks.length);
        System.out.println("is marks = copyMarks?  " + Arrays.equals(marks,copyMarks) ); 
        System.out.print("Enter mark to search for it: ");
        mark=read.nextDouble();
        int s = Arrays.binarySearch(marks, mark);
        if( s>=0){
            System.out.println(mark+" is found at position "+s);
        } else {
            System.out.println(mark+" ia not found");
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Computer Programming-2 (CS2301) java examples Pass by Value, Pass by Reference , Arrays class and ArrayList

Similar questions


need a help?


find thousands of online teachers now
<< java book management system using ArrayList...