0% found this document useful (0 votes)
21 views

Muhammad Hisyam Bin Malang Matric No: 1822907 Lab #4section #1

The document contains code for two Java programs. The first program takes student names and scores as input, stores them in arrays, and sorts the arrays in descending order by score. The second program calculates the average of 10 double values input by the user. It overloads the average method to handle both int and double array types.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Muhammad Hisyam Bin Malang Matric No: 1822907 Lab #4section #1

The document contains code for two Java programs. The first program takes student names and scores as input, stores them in arrays, and sorts the arrays in descending order by score. The second program calculates the average of 10 double values input by the user. It overloads the average method to handle both int and double array types.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

/**

*MUHAMMAD HISYAM BIN MALANG


* Matric no: 1822907
* Lab #4Section #1
*/
Exercise 1

package score;

import java.util.Scanner;

public class Score {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter the number of students: ");

String[] students = new String[input.nextInt()];

int[] scores = new int[students.length];

System.out.println("Enter the name a score for each student:");

for (int i = 0; i < students.length; i++) {

System.out.print("Student " + (i + 1) + ": ");

students[i] = input.next();

System.out.print("Score: ");

scores[i] = input.nextInt();

sortDecreasing(students, scores);
for (int i = 0 ;i < students.length ; i++) {

System.out.print(students[i]+" "+scores[i]);

public static void sortDecreasing(String[] strs, int[] nums) {

for (int i = 0; i < nums.length; i++) {

int max = nums[i];

int maxIndex = i;

String temp;

for (int j = i + 1; j < nums.length; j++) {

if (nums[j] > max) {

max = nums[j];

maxIndex = j;

if (maxIndex != i) {

temp = strs[i];

strs[i] = strs[maxIndex];

strs[maxIndex] = temp;

nums[maxIndex] = nums[i];

nums[i] = max;

}
}

}
Exercise 2

public class average {

public static void main(String[] args) {

java.util.Scanner in = new java.util.Scanner(System.in);

double[] vals = new double[10];

System.out.print("Please enter to double values: ");

for (int i = 0; i < 10; i++)

vals[i] = in.nextDouble();

System.out.printf("The average is: %.2f", average(vals));

public static int average(int[] array) {

int sum = 0;

for (int val : array)

sum += val;

return sum / array.length;

public static double average(double[] array) {

double sum = 0.0;

for (double val : array)

sum += val;

return sum / array.length;

You might also like