|
-
February 2nd, 2017, 01:33 PM
#1
Can you help me with this java exercise
Hi! I have An exercise with which i need some help. I already did this code but i have to find the frequency of each marks and i don"t know how to do it
this is the code:
Code:
import javax.swing.*;
import java.util.Collections;
import java.util.Vector;
public class VectorMarks {
/**
* Main method
* @param args
*/
public static void main(String[] args) {
Vector<Integer> marks = getMarks();
displayMarks(marks);
displayAveragePassingMark(marks);
displayHighestMarkAndStudentsWithIt(marks);
displayStudentWithMark(8, marks);
displayMarksInAscendingOrder(marks);
}
private static void displayMarksInAscendingOrder(Vector<Integer> marks) {
// sort the marks in ascending order and display them
Collections.sort(marks);
String output = "Marks in ascending order:\n";
for(int i=0; i<marks.size(); i++) {
output += marks.get(i) + "\n";
}
JOptionPane.showMessageDialog(null, output);
}
private static void displayStudentWithMark(int markValue, Vector<Integer> marks) {
// check how many students have this mark and display result
int count = 0;
for(int i=0; i<marks.size(); i++) {
if(marks.get(i) == markValue) {
count++;
}
}
String output = "Students with mark " + markValue + ": " + count;
JOptionPane.showMessageDialog(null, output);
}
private static void displayHighestMarkAndStudentsWithIt(Vector<Integer > marks) {
// find the highest mark and shows students with it
int highestMark = findHighestMark(marks);
String output = "Highest mark is " + highestMark + "\n";
// count how many students have it
int count = 0;
for(int i=0; i<marks.size(); i++) {
if(marks.get(i) == highestMark) {
count++;
}
}
output += count + " student(s) have it";
JOptionPane.showMessageDialog(null, output);
}
private static int findHighestMark(Vector<Integer> marks) {
int highest = 0;
for(int i=0; i<marks.size(); i++) {
if(marks.get(i) > highest) {
highest = marks.get(i);
}
}
return highest;
}
private static void displayAveragePassingMark(Vector<Integer> marks) {
// get the total and compute the average mark
double total = 0;
for(int i=0; i<marks.size(); i++) {
total += marks.get(i);
}
double averageMark = total / marks.size();
JOptionPane.showMessageDialog(null, "Average passing mark is " + String.format("%.2f", averageMark));
}
private static void displayMarks(Vector<Integer> marks) {
// display all the marks
String output = "";
for(int i=0; i<marks.size(); i++) {
output += "Mark #" + (i+1) + ": " + marks.get(i) + "\n";
}
JOptionPane.showMessageDialog(null, output);
}
private static Vector<Integer> getMarks() {
// create vector for the marks
Vector<Integer> marks = new Vector<>();
// get 20 marks
for(int i=1; i<=20; i++) {
String input = JOptionPane.showInputDialog(null, "Enter mark #" + i + ":");
int mark = Integer.parseInt(input);
marks.add(mark);
}
return marks;
}
}
Last edited by 2kaud; February 2nd, 2017 at 02:17 PM.
Reason: Code tags added
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|