0

I had a question about the proper way on creating a math operator in Java from user input. I'm probably over complicating the math operations.

The assignment asked to calculate input from user in regards to a sample grade sheet.

The weights for attendance are 5%, homework 20%...

I want the ending output to show full percentage values

I hope I described the problem correctly.

public class Work1 {
public static void main(String[] args){

    // I can't seem to figure out how to prevent the calculator from selecting 0.00. I'm not confident
    //enough with math operations in Java

    System.out.print("Note: This program will ignore symbols and letters on integer only questions. "
            + "Please resubmit your response if program ignores input"); 
    // Introduces answer to number 5. Program is improved by removing ExpectionError to non Integer response

    double attendweight = (0.05);
    double homeworkweight = (0.2);
    double projectmathweight1 = (0.15);
    double projectmathweight2 = (0.15);  //weights
    double midtermweight = (0.2);
    double finalexamweight = (0.25);

    int operations = (100);  //used for *100 and /100 operation

    Scanner askquestion= new Scanner(System.in);


while (!askquestion.hasNextInt()) askquestion.next();   // Ignore input of non Integer response. Targeted at % symbol.


int attend = askquestion.nextInt();
System.out.print("What % grade did you get for Homework?: ");
while (!askquestion.hasNextInt()) askquestion.next();
int homework = askquestion.nextInt();
System.out.print("What is your % grade for Project 1?: ");
while (!askquestion.hasNextInt()) askquestion.next();
int project1 = askquestion.nextInt();
System.out.print("Project 2?: ");
while (!askquestion.hasNextInt()) askquestion.next();
int project2 = askquestion.nextInt();
System.out.print("What % grade did you get on the midterm exam?: ");
while (!askquestion.hasNextInt()) askquestion.next();
int midterm = askquestion.nextInt();
System.out.print("What % grade did you get on the final exam?: ");
while (!askquestion.hasNextInt()) askquestion.next();
int finalexam = askquestion.nextInt();
System.out.print("Insert the amount of extra points between 0-4. If none enter zero: ");
while (!askquestion.hasNextInt()) askquestion.next();
int extrapoints = askquestion.nextInt();


float attend2 = ((attend / operations ));
float homework2 = ((homework / operations));
float projectmath1 = ((project1 / operations ));
float projectmath2 = ((project2 / operations ));
float midterm2 = ((midterm / operations ));
float finalexam2 = ((finalexam / operations));
float extrapoints2 = ((extrapoints / operations));

float totalscore = attend2 + homework2 + projectmath1 + projectmath2 + midterm2 + finalexam2 + extrapoints2;

double attend3 = ((attend2 * attendweight));
double homework3 = ((homework2 * homeworkweight));
double projectmath1p3 = (projectmath1 * projectmathweight1);
double projectmath2p3 = (projectmath2 * projectmathweight2); // for some reason this is returning 0.00
double midterm3 = (midterm2 * midtermweight);
double finalexam3 = (finalexam2 * finalexamweight);

double finalattend = ((attend3 * operations));
double finalhomework = (homework3 * operations);
double projectmath1final = (projectmath1p3 * operations);
double projectmath2final = (projectmath2p3 * operations);
double midtermfinal = (midterm3 * operations);
double finalfinalexam = (finalexam3 * operations);

double totalscore2 = finalattend + finalhomework + projectmath1final + projectmath2final + midtermfinal +finalfinalexam;enter code here{
    System.out.println("\nYour total score is " + totalscore2 + " %");

System.out.printf("\nYour name is: " + name1 + "  " + name2 + " and your Major is: " + major);
System.out.printf("\nYour Attendance grade is: %.2f", finalattend+ "%");
System.out.printf("\nYour Homework grade is: %.2f"  , finalhomework+ "%");
System.out.printf("\nYour Project 1 grade is: %.2f", projectmath1final+ "%");
System.out.printf("\nYour Project 2 grade is: %.2f",  projectmath2final + "%"); 
System.out.printf("\nYour Midterm grade is: %.2f",  midtermfinal+ "%"); 
System.out.printf("\nYour Final exam grade is: %.2f",  finalfinalexam+ "%");
System.out.printf("\nYou have " + extrapoints + " Extra points");

}

} finally { askquestion.close();

}
}

}

  • Forgot to say the output shows up as 0.00% for all outputs – user3005327 Sep 18 '15 at 18:30
  • 2
    Never put "New to Java." in question statement – rajuGT Sep 18 '15 at 18:33
  • whey dont you start by to simplifying your code like `double attendence = attend / operations * attendweight * operations;` (which is essentially `double attendence = attend / attendweight`). Do all the operations on say, attendance in one line, instead of creating variables like attend1, 2.... – Jos Sep 18 '15 at 18:41
  • It is because you are doing an Integer Division and 5/100 is 0. Just change `int operations = (100);` to `float operations = (100.0f);` in order to perform a float division. – user2004685 Sep 18 '15 at 18:57
  • Sorry about my newbishness. I'll change the title in the future. The float operation worked! – user3005327 Sep 18 '15 at 19:18

0 Answers0