7.17 (Sort students) Write a program that prompts the user to enter the number o
ID: 3850776 • Letter: 7
Question
7.17 (Sort students) Write a program that prompts the user to enter the number of students, the students' names, and their scores, and prints student names and scores in decreasing order of their scores. Assume that the name is a string without spaces, use the Scanner's next method to read a name. Sample Run for Exercise07 17 Enter input data for the program Sample data provided below You may moday it). John 71.5 Peter 34.9 Susan 94.2 Kim 79.1 Joan 56.4 Show the Sample Output Using the Preceeding Input Reset command java Exercisee7 17 Enter the number of students: 5 Enter a student name: John Enter a student score: 71.5 Enter a student name: Peter Enter a student score: 34.9 Enter a student name: Susan Enter a student score: 94.2 Enter a student name: Kim Enter a student score: 79.1 Enter a student name: Joan Enter a student score: 56.4 Names in decreasing order of their scores are: Susan 94.2 Kim 79.1 John 71.5 Joan 56.4 Peter 34.9 commandExplanation / Answer
7.17:
Source Code:
package com.sample;
import java.util.Scanner;
public class Student {
/**
* @param args
*/
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Student:");
int numberOfStudent = sc.nextInt();
String[] studentName = new String[numberOfStudent];
Double[] studentScore = new Double[numberOfStudent];
for (int i=0;i<numberOfStudent;i++) {
System.out.println("Enter a Student Name:");
studentName[i] = sc.next();
System.out.println("Enter a Student Score:");
studentScore[i] = sc.nextDouble();
}
double tempScore;
String tempName;
//sorting array in decending order
for (int i=0;i<numberOfStudent;i++) {
for (int j = i + 1; j < numberOfStudent; j++)
{
if (studentScore[i] < studentScore[j])
{
tempScore = studentScore[i];
tempName = studentName[i];
studentScore[i] = studentScore[j];
studentName[i] = studentName[j];
studentScore[j] = tempScore;
studentName [j] = tempName;
}
}
}
System.out.print(" ");
//Showing the sorted array
for (int i = 0; i <numberOfStudent; i++)
{
System.out.print(studentName[i] + " "+studentScore[i]+" ");
}
}
}
Output:
Enter Number of Student:
3
Enter a Student Name:
Sachin
Enter a Student Score:
93.03
Enter a Student Name:
Ponting
Enter a Student Score:
93
Enter a Student Name:
Lara
Enter a Student Score:
89
Sachin 93.03
Ponting 93.0
Lara 89.0
7.18: Bubble Sort algorithm compare first two elements of an array and swap them if required.
Explanation:
First Step
( 5 1 4 2 6 ) –> ( 1 5 4 2 6 ), algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 6 ) –> ( 1 4 5 2 6 ), Swap 5 > 4
( 1 4 5 2 6 ) –> ( 1 4 2 5 6 ), Swap 5 > 2
( 1 4 2 5 6 ) –> ( 1 4 2 5 6 ), these elements are already in order (6 > 5), so algorithm does not swap them.
Second Step
( 1 4 2 5 6 ) –> ( 1 4 2 5 6 )
( 1 4 2 5 6) –> ( 1 2 4 5 6 ), Swap 4 > 2
( 1 2 4 5 6 ) –> ( 1 2 4 5 6 )
( 1 2 4 5 6 ) –> ( 1 2 4 5 6 )
The algorithm needs one whole pass without any swap to know it is sorted.
Third Step
( 1 2 4 5 6 ) –> ( 1 2 4 5 6 )
( 1 2 4 5 6 ) –> ( 1 2 4 5 6 )
( 1 2 4 5 6 ) –> ( 1 2 4 5 6 )
( 1 2 4 5 6 ) –> ( 1 2 4 5 6 )
Source Code Example:
package com.sample;
import java.util.Scanner;
public class BubbleSort {
/**
* @param args
*/
public static void main(String[] args) {
int n, c, d, swap;
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.println("Input Number of Integers to Sort");
n = in.nextInt();
int array[] = new int[n];
System.out.println("Enter " + n + " Integers");
for (c = 0; c < n; c++) {
array[c] = in.nextInt();
}
for (c = 0; c < (n - 1); c++) {
for (d = 0; d < n - c - 1; d++) {
/* For descending order use < */
if (array[d] > array[d + 1]) {
swap = array[d];
array[d] = array[d + 1];
array[d + 1] = swap;
}
}
}
System.out.println("Sorted list of Numbers");
for (c = 0; c < n; c++) {
System.out.println(array[c]);
}
}
}
Output:
Input Number of Integers to Sort
5
Enter 5 Integers
7
2
9
5
3
Sorted list of Numbers
2
3
5
7
9
8.4:
Source Code:
package com.sample;
public class LargestNumber {
/**
* @param args
*/
public static void main(String[] args) {
int[][] twodArray = { { 2, 4, 3, 4, 5, 8, 8 }, { 7, 3, 4, 3, 3, 4, 4 },
{ 3, 3, 4, 3, 3, 2, 2 }, { 9, 3, 4, 7, 3, 4, 1 },
{ 3, 5, 4, 3, 6, 3, 8 }, { 3, 4, 4, 6, 3, 4, 4 },
{ 3, 7, 4, 8, 3, 8, 4 }, { 6, 3, 5, 9, 2, 7, 9 } };
int totalHours[] = new int[8];
String employeeNumber[] = new String[8];
for (int i = 0; i < 8; i++) {
int sum = 0;
for (int j = 0; j < 7; j++) {
sum = sum + twodArray[i][j];
}
totalHours[i] = sum;
employeeNumber[i] = "Employee "+i;
}
int tempHours;
String tempEmployeeNumber;
// sorting array in decending order
for (int i = 0; i < 8; i++) {
for (int j = i + 1; j < 8; j++) {
if (totalHours[i] < totalHours[j]) {
tempHours = totalHours[i];
tempEmployeeNumber = employeeNumber[i];
totalHours[i] = totalHours[j];
employeeNumber[i]=employeeNumber[j];
totalHours[j] = tempHours;
employeeNumber[j]=tempEmployeeNumber;
}
}
}
// Showing the sorted array
for (int i = 0; i < 8; i++) {
System.out.print(employeeNumber[i]+" "+totalHours[i] + " ");
}
}
}
Output:
Employee 7 41
Employee 6 37
Employee 0 34
Employee 4 32
Employee 3 31
Employee 1 28
Employee 5 28
Employee 2 20
8.13:
Source Code:
package com.sample;
import java.util.Scanner;
public class LargestNumber {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of rows and column of the array:");
int row = sc.nextInt();
int column = sc.nextInt();
double [][] data = new double[row][column];
System.out.println("Enter the array:");
for (int i=0;i<row;i++) {
System.out.println("Enter the "+(i+1)+" row's data:");
for (int j=0;j<column;j++) {
data[i][j] = sc.nextDouble();
}
}
int [] location = locateLargest (data);
System.out.println("The location of largest is at ("+location[0]+","+location[1]+")");
}
public static int[] locateLargest (double [][] a) {
int [] location = new int[2];
double maxValue = 0;
int maxRow = 0;
int maxColumn = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] > maxValue) {
maxValue = a[i][j];
maxRow = i;
maxColumn = j;
}
}
}
location [0] = (maxRow+1);
location [1] = (maxColumn+1);
return location;
}
}
Output:
Enter the number of rows and column of the array:
3 4
Enter the array:
Enter the 1 row's data:
7 5 9 9.9
Enter the 2 row's data:
12 67 87.3 78
Enter the 3 row's data:
87.4 6 0 77
The location of largest is at (3,1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.