-1

I posted a question last night about making a grad calculator while using methods in java (netbeans) I'm still really struggling and wondering if anyone can help me with my code below? We have to make a grade calculator and take inputs from the user for their test mark, the max mark possible for that test, and the weighting. Eg. 30/50 *50% = overall weighted mark. I have to use methods but I'm still so confused about parameters and where to put the user input part. Any help would be greatly appreciated!

     import java.util.Scanner;

    public class GradeCalculator {


    public static void main()
{


    System.out.println("Your overall score is: " +CalculateMark(finalMark)); 

}


  public static double CalculateMark (int overallscore) 
{

    Scanner in = new Scanner(System.in);
    int score1 = in.nextInt();
    System.out.print("Enter mark: ");
    if(score1 >=0 || score1<1000){

    } System.out.print("Enter Max mark: ");
     int maxMark = in.nextInt();
     if (maxMark >=0 || maxMark<1000);{

    } System.out.print("Enter weighting as a decimal (eg. 75% = 0.75): ");
        double weighting = in.nextDouble();
         if (weighting <0 && weighting>=10){

            } double finalMark;
             finalMark= (score1/maxMark)*weighting;

  return  finalMark;

  }

}

lildizzle63
  • 1
  • 2
  • 2
  • 2
    This website is not here to do your homework. you have major issues in your code that you need to study and understand them. I really do not know how this code even runs?!!!! You do not even know the main method signature which is** public static void main(String[] args)** – Kick Buttowski Sep 01 '14 at 23:39
  • I would put the Scanners in the main method, and get your values there, instead of calling a function passing an undefined variable as a parameter, which shouldn't even work – Shadow Sep 01 '14 at 23:45

3 Answers3

2

You need to break down you code into logical points, for example...

  • Ask the user for input...
  • Take that input and pass it to your "calculate" method
  • Allow the calculate method to validate the input...
  • If valid, calculate the final score, if not, pass back an error code
  • Repeat as required...

So, the first thing you need to do is get the input from the user. Next you need to provide some kind of validation to that value. Next you need to (if valid), calculate the score and return it back to the caller.

Start by trying to make each step work first, before writing the next. Be prepared to re-structure the code as you go as required...nothing is set in stone.

You might like to take a look at the Getting Started tutorial and the Learning the Java Language tutorial, in particular, the section on Classes and Objects and Defining Methods

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

First of all, your main method signature is wrong, should be:

public static void main (String[] args) {
    //Code here
}

In Netbeans, you need only type psvm and press tab, it will fill out the above for you.

Second, I would not get your input from the user in your Calculate method, I would create 3 variables in main, and get them there, performing validation after you get each variable, then pass your 3 variables to the Calculate method, which would look something like:

public static double CalculateMark(int mark, int maxMark, double weight) {
    //Code here
}
Shadow
  • 3,926
  • 5
  • 20
  • 41
0

I may have not gotten the point of your program, but i hope this helps:

import java.util.Scanner;

public class GradCalc {
private double finalMark = 0; //I would make this an instance variable
private static double maxMark = 0; //these need to be double if you want decimal answers
private static double score1 = 0;


public static void main(String[] args) // you need the String[] args for main to run
{


    System.out.println("Your overall score is: " +String.format("%.2f", CalculateMark())); 
    //String.format was to make it 2 decimal place lengths

}


  public static double CalculateMark ()// remove parameters to alter instance var
{

    Scanner in = new Scanner(System.in);
    System.out.print("Enter Max mark: "); //call this first
    maxMark = in.nextInt();
    System.out.print("Enter mark: "); //I switched these next two lines
    score1 = in.nextInt();
    while(true){ // this makes sure the user won't give a neg answer
                // or one that is over the max Mark (you can change this if you want)
        if(score1 >=0 && score1<=maxMark){
            //nothing was in this if statement
            break;
        }
        System.out.print("Enter mark again. The latter was not applicable: ");
        score1 = in.nextInt();
    } 
    System.out.print("Enter weighting as a decimal (eg. 75% = 0.75): ");
    double weighting = in.nextDouble();
    while (true){
        if (weighting > 0 && weighting<=1){ //you probably had the conditional signs mixed up
        //nothing was in this if statement either
        break;
        } 
        System.out.print("Weighting decimal was not between 0 and 1. Please enter again: ");
        weighting = in.nextDouble();
    }
    double finalMark;
    finalMark= (score1/maxMark)*weighting;

  return  finalMark;

  }
}