CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2017
    Posts
    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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,923

    Re: Can you help me with this java exercise

    When posting code, please use code tags so that the code is readable. Go advanced, select the formatted code and click '#'.

    Cheers!
    Moderator.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Can you help me with this java exercise

    have to find the frequency of each marks
    Can you explain what that means and show an example?
    Norm

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
  •  





Click Here to Expand Forum to Full Width

Featured