I am trying to create this code to complete these exercises and do not know what
ID: 3814119 • Letter: I
Question
I am trying to create this code to complete these exercises and do not know what to do. Can someone please help? I am not too good with java programming so nothing too complicated please. Thanks!!
The application (a Java program with a main method) allows the user to select the type of project they would like to execute and then calls the appropriate method to complete the processing for this selection.
Your program will display a menu similar to the following (the menu processing has already been com- pleted).
(1) Sort Students
(2) Identical Arrays
(3) Exit the program
(Exercise 1, Sort Students). Complete the method sortStudents() that prompts the user to enter the number of students, the students’ names, and their scores, and prints students names in decreasing order of their scores (both name and score needs to be printed for each student after sort is complete).
(Exercise 2, Identical Arrays) The arrays list1 and list2 are identical if they have the same con- tents. Complete the code for the method that returns true if list1 and list2 are identical, using the following header:
publicstaticbooleanequals(int[]list1,int[]list2). Complete the code in the main method to prompt the user to enter two lists of integers, passes the two arrays as parameters to the method equals, and displays whether the two are identical. Here are the same runs. Note that the first number in the input indicates the number of elements in a list. This number is not part of the list.
Explanation / Answer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.Scanner;
/**
*
* @author Namburi Ramesh
*/
public class Project {
public static void main(String[] args){
int selection;
//print the menu
System.out.println("Enter the type of processing to execute "
+ "(1) Sort Students "
+ "(2) Identical Arrays "
+ "(3) Exit the program ");
Scanner s=new Scanner(System.in);
selection=s.nextInt();//take the user choice
if(selection==1){
sortStudents();
}else if(selection==2){
int[] list1,list2;
int n1,n2;
//prompt the user to enter list elements and store it in array
System.out.println("Enter the number of elements in list1 ");
n1=s.nextInt();
System.out.println("Enter the elements of the list1 ");
list1=new int[n1+1];
list1[0]=n1;
for(int i=1;i<n1+1;i++){
list1[i]=s.nextInt();
}
System.out.println("Enter the number of elements in list2 ");
n2=s.nextInt();
System.out.println("Enter the elements of the list2 ");
list2=new int[n2+1];
list2[0]=n2;
for(int i=1;i<n2+1;i++){
list2[i]=s.nextInt();
}
//call the equals method and print the output
System.out.println("list1 equals list2: "+equals(list1,list2));
}
}
private static void sortStudents() {
int n;
String[][] stu;
Scanner s=new Scanner(System.in);
System.out.println("Enter the number of students ");
n=Integer.valueOf(s.nextLine());
stu=new String[n][2];
//take the input of student names and scores from user
for(int i=0;i<n;i++){
System.out.println("Enter the student number "+(i+1)+" name and score in separate lines");
stu[i][0]=s.nextLine();
stu[i][1]=s.nextLine();
}
//sort the scores using bubble sort algorithm
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(Double.valueOf(stu[j][1])<Double.valueOf(stu[j+1][1])){
String temp1=stu[j][0];
String temp2=stu[j][1];
stu[j][0]=stu[j+1][0];
stu[j][1]=stu[j+1][1];
stu[j+1][0]=temp1;
stu[j+1][1]=temp2;
}
}
}
System.out.println("Sorted order");
for(int i=0;i<n;i++){
System.out.println(stu[i][0]+" "+stu[i][1]);
}
}
private static boolean equals(int[] list1, int[] list2) {
int counter=0;
for(int i=0;i<list1.length;i++){
if(list1[i]!=list2[i]){
counter=1;
break;
}
}
if(counter==0){
return true;
}else{
return false;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.