1

In a beginners programming class we were assigned to store classmarks, average, lowest mark, highestmark etc.

We were also asked to shutdown the computer.

I outputted a menu and one of the cases was to shut down the computer.

However it isn't working. Method 4 and method 8 are the only ones associated with the problem.

import javax.swing.*;
import java.io.*;

class ClassMarks {

    String names[];
    int marks[];
    int counter;
    String marksString;

    final String PASSWORD = "Top Secret";
    String name, surname;
    int mark;

    int total;
    double average;

    //method to check password
    public void checkPassword() {
        int counter = 0;
        String password_user;
        do {
            password_user = JOptionPane.showInputDialog("Enter password: ");

            if (password_user.equals(PASSWORD)) {

                JOptionPane.showMessageDialog(null, "Access Granted");
                outputMenu();
            } else {
                JOptionPane.showMessageDialog(null, "Access Denied");
            }
            counter++;
        } while ((counter < 4) && !(password_user.equals(PASSWORD)));
        JOptionPane.showMessageDialog(null, "No more attempts available");

    }

    public void compulsoryMethod() {

        String namesString = JOptionPane.showInputDialog("Enter number of students");
        int noOfNames = Integer.parseInt(namesString);

        //print all the array elements
        for (counter = 0; counter < noOfNames; counter++) {
            names[counter] = JOptionPane.showInputDialog("Enter names");
            marksString = JOptionPane.showInputDialog("Enter Mark for" + names[counter] + " ");
            marks[counter] = Integer.parseInt(marksString);
        }
    }

    // method 4
    public void outputMenu() {
        int input;
        do {

            String stringInput = JOptionPane.showInputDialog("Choose the decision you want to make: \n\n 1.Enter marks \n 2. See marks \n 3.Find Average \n 4.See highest mark \n 5.See lowest mark \n 6.See any mark above average \n 7.Turn off this Pc/Laptop/any other device\n 8.See Grade");

            input = Integer.parseInt(stringInput); // to convert stringInput which is String to input which is int

            switch (input) {
                case 1:
                    enterMarks();
                    break;
                case 2:
                    viewMarks();
                    break;
                case 3:
                    averageMark();
                    break;
                case 4:
                    highestMark();
                    break;
                case 5:
                    lowestMark();
                    break;
                case 6:
                    markAboveAverage();
                    break;
                case 7:
                    shutDown();
                    break;
                case 8:
                    viewMarks();
                    break;

                default:
                    JOptionPane.showMessageDialog(null, "Invalid choice");
            }
        } while (input != 7);
    }

    //method 3
    public void enterMarks() {

        String namesString = JOptionPane.showInputDialog("Enter no of students:");
        int noOfNames = Integer.parseInt(namesString);

        names = new String[noOfNames];
        marks = new int[noOfNames];
        for (counter = 0; counter < marks.length; counter++) {
            names[counter] = JOptionPane.showInputDialog("Enter names:");

            marksString = JOptionPane.showInputDialog("Enter Mark for " + names[counter] + " ");
            marks[counter] = Integer.parseInt(marksString);

        }

    }

    // method 5
    public void viewMarks() {

        for (counter = 0; counter < marks.length; counter++) {
            JOptionPane.showMessageDialog(null, new JTextArea(names[counter] + "\t\t" + marks[counter] + "\t\t" + displayGrade(marks[counter])));
        }
    }

    //method 6
    public void averageMark() {
        int total = 0;
        for (counter = 0; counter < marks.length; counter++) {
            total = total + marks[counter];
        }

        average = total / 5;
        JOptionPane.showMessageDialog(null, "Average is:" + average);
    }

    //method 7
    public void highestMark() {
        int large = 0;
        int num;

        // i starts from 2 because we already took one num value
        for (int counter = 0; counter < marks.length; counter++) {

            if (marks[counter] > large) {
                large = marks[counter];
            }
        }
        JOptionPane.showMessageDialog(null, large);
    }

    //method 8
    public void lowestMark() {

        int small = 100;
        int num;

        for (int counter = 0; counter < marks.length; counter++) {

            if (marks[counter] < small) {
                small = marks[counter];
            }

        }
        JOptionPane.showMessageDialog(null, small);

    }

    //method 9
    public void markAboveAverage() {

        averageMark();

        for (int counter = 0; counter < marks.length; counter++) {

            if (marks[counter] > average) {

                JOptionPane.showMessageDialog(null, marks[counter] + ": This mark is above average");
            } else {
                JOptionPane.showMessageDialog(null, marks[counter] + "This mark is below average");
            }
        }
    }

    //method 10
    public void (main String[]) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec("shutdown -s -t 0");
        System.exit(0);
    }

    //method11
    public char displayGrade(int marks) {

        char grade = ' ';
        if ((marks >= 0) && (marks <= 20)) {
            grade = 'U';

        }
        if ((marks >= 21) && (marks <= 40)) {
            grade = 'E';

        }

        if ((marks >= 41) && (marks <= 60)) {
            grade = 'D';

        }

        if ((marks >= 61) && (marks <= 80)) {
            grade = 'C';

        }

        if ((marks >= 81) && (marks <= 90)) {
            grade = 'B';

        }

        if ((marks >= 91) && (marks <= 100)) {
            grade = 'A';

        }

        return grade;
    }    
}
4
  • 1
    Your code doesn't work? Commented Dec 23, 2016 at 14:25
  • 2
    We need a lot less code and a lot more background information. What happens when you try to run your shutdown code? Does it give you a specific error message? Does it do nothing and quit? Does it cause a freeze? Does it make your computer play Joy to the World? What operating system are you running? Mac (version?) Linux (what kind, what version?) Commented Dec 23, 2016 at 14:29
  • Robert Columbia - You should always keep in mind that i am a beginner so go easy on me :). I am running this program on windows and this error is popping up error: <identifier> expected --->public void (main String[]) throws IOException{ Commented Dec 24, 2016 at 10:15
  • Robert, the program does work without the shutdown choice so the problem is from the shut down Commented Dec 24, 2016 at 10:23

2 Answers 2

4

You can use CMD command to shut or restart your computer like

shutdown -s -t 10

Here is a way to shut and restart your computer that using windows :

Shut down your computer:

public void shutdownPC(int time){
    try {
        Runtime r = Runtime.getRuntime();
        // Shutdown system time mean, time to wait before my system will shutdow or restart
        r.exec("shutdown -s -t " + time);
    } catch (NumberFormatException | IOException e) {
        JOptionPane.showMessageDialog(null, "Restart failed.");
    }
}

Restart your computer:

public void restartPC(int time){
    try {
        Runtime r = Runtime.getRuntime();
        // Restart system
        r.exec("shutdown -r -t " + time);
    } catch (NumberFormatException | IOException e) {
        JOptionPane.showMessageDialog(null, "Restart failed.");
    }
}

If you are using linux or mac you can use this two command :

sudo poweroff

and

sudo reboot

You can learn about command line linux and mac


EDIT

First

You get and error in the 4th method in the case 7 because you call shutDown();, and this not exist in your code, so you should to call the right method.

Second

You get an error in the main method public void (main String[]) throws IOException {, because it is not created correctly, so you need to create it like this:

public static void main(String[] args) throws IOException {
   //your code here
}

don't forgot to import this import java.io.IOException;

Third

And about the two method that i set them in the previous answer, you need to make them static:

public static void shutdownPC(int time) {}

public static void restartPC(int time) {}

Fourth

And to call them from your method 4, you need to specify the time to restart or to shutdown you pc like this:

case 7:
    shutdownPC(5);//5 mean 5 second if you want to shut the pc imidattilly just set it 0
    break;

Hope this can help you.

Sign up to request clarification or add additional context in comments.

6 Comments

So i tried the first example but then an error popped up in the menu at method 2 saying cannot find symbol ...MIND YOU I ARRANGED THE OBJECT TO SHUTDOWNPC so the error isn't actually about the object name.
what did you mean @NicholasBorg ? what error you get in my solution or what?
I arranged the previous error now what appeared was error: method shutdownPC in class ClassMarks cannot be applied to given types;
ok i will create another answer, this will help you @NicholasBorg
i already create another answer you can check it @NicholasBorg
|
0

I would like to add some MrLy code to make programme very intelligent

public static void shutdownPC(int time){
    try {
        Runtime r = Runtime.getRuntime();
        // Shutdown system time mean, time to wait before my system will shutdow or restart
        if(System.getProperty("sun.desktop").equalsIgnoreCase("windows"))
        {
            r.exec("shutdown -s -t " + time);
        }
        else
        {
            r.exec("sudo poweroff");
        }  
    } catch (NumberFormatException | IOException e) {
        JOptionPane.showMessageDialog(null, "Restart failed.");
    }
}

1 Comment

error: method shutdownPC in class ClassMarks cannot be applied to given types;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.