Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Problem with a weigted average code

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with a weigted average code

    I am doing a program that has a weighted average calculation and everything compiles and runs. It is just that my weighted average seems to be calculating incorrectly.

    This is the type of output I should see:


    Homework:
    Number of assignments? 3
    Assignment 1 score and max? 14 15
    Assignment 2 score and max? 16 20
    Assignment 3 score and max? 19 25
    Sections attended? 4
    Total points = 65 / 80
    Weighted score = 40.63
    Exam 1:
    Score? 81
    Curve? 0
    Total points = 81 / 100
    Weighted score = 16.2
    Exam 2:
    Score? 95
    Curve? 10
    Total points = 100 / 100
    Weighted score = 30.0
    Course grade = 86.83


    Below is my code and I think even after getting up this morning and looking at it, I have an error in the calculations, but I can;t pinpoint it. Any useful suggestions would be helpful.
    //This program is supposed to receive input from user, and calculated the grades of a
    //student with a weighted average
     
    import java.util.Scanner;
     
    public class Grades{
    	private double weightExam;
    	private double score;
    	private double curveAmount;
    	private double scoreTotal;
    	private double scoreWeighted;
    	private double weightHomeWork; 
    	private int section;
    	private int sections;
    	private double sumScore;
    	private double number;
    	private double assignMax;
    	private double assignScore;
    	private int sumMax;
     
    	Scanner console = new Scanner(System.in);
     
    	public Grades() {
    		intro();
    		exam();
    		homework();
    	}
    	public void intro() {
    		System.out.println("This program accepts scores from two exams and");
    		System.out.println("scores from your homework as input and computes");
    		System.out.println("your grade in the course.");
    		System.out.println();
    	}
    	public void exam() {
    		for (int i = 0; i < 2; i++) { // 2 exams
    			System.out.print("What is the weight of exam " + i + " (0-100)?");
    			weightExam = console.nextInt();
    			System.out.print("Score earned?");
    			score = console.nextInt();
    			System.out.print("Was there a curve (1=yes, 2=no)?");
    			int curve = console.nextInt();
    			if (curve == 1) {
    				System.out.print("How much was the curve?");
    				curveAmount = console.nextInt();
    			} 
    			else {
    				curveAmount = 0;
    			}
    			totalScore();
    			weightedScore();
     
    			System.out.println("Total points = " + scoreTotal + "/" + "100");
    			System.out.println("Weighted score = " + scoreWeighted + "/" + weightExam);
    		}
    	}
    	public void totalScore() {
    		scoreTotal = Math.min(score + curveAmount, 100);
    	}
    	public void weightedScore() {
    		scoreWeighted = (score + curveAmount)/200 * weightExam;
    	}
    	public void homework() {
    		System.out.print("What is the Homework weight (0-100)?");
    		weightHomeWork = console.nextInt();
    		System.out.print("Number of assignments?");
    		number = console.nextInt();
     
    		for (int i = 0; i < number; i++) {
    			System.out.println("Assignment " + i+1 + " score and max?");
    			assignScore = console.nextInt();
    			assignMax = console.nextInt();
    			sumScore += assignScore; 
    			sumMax += assignMax; 
    		}
    		System.out.print("How many sections attended?");
    		section = console.nextInt();
    		sections = Math.min(3 * section, 20);
    		System.out.println("Section points = " + sections);
     
    		weightedHomework();
     
    		System.out.println("Total points = " + (sections + sumScore) + "/"
    				+ sumMax);
    		System.out.println("Weighted score = " + scoreWeighted + "/" + weightHomeWork);
    	}
    	public void weightedHomework() {
    		scoreWeighted = ((sections + sumScore)/sumMax)*weightHomeWork;
    	}
    	public static void main(String[] args) {
    		new Grades();
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with a weigted average code

    I have an error in the calculations
    Post the program's output or something that shows the error and add some comments saying what the output should be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Problem with a weigted average code

    We need to understand what you think is wrong or specifically what indications you have that the output is incorrect before we can help.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a weigted average code

    I apologize, I had it highlighted to copy and paste, then forgot to add it to the post.

    Output from the program:



    This program accepts scores from two exams and
    scores from your homework as input and computes
    your grade in the course.

    What is the weight of exam 0 (0-100)?20
    Score earned?98
    Was there a curve (1=yes, 2=no)?1
    How much was the curve?5
    Total points = 100.0/100
    Weighted score = 10.3/20.0
    What is the weight of exam 1 (0-100)?30
    Score earned?88
    Was there a curve (1=yes, 2=no)?1
    How much was the curve?10
    Total points = 98.0/100
    Weighted score = 14.7/30.0
    What is the Homework weight (0-100)?50
    Number of assignments?3
    Assignment 01 score and max?
    15 15
    Assignment 11 score and max?
    19 20
    Assignment 21 score and max?
    21 25
    How many sections attended?
    5
    Section points = 15
    Total points = 70.0/60
    Weighted score = 58.333333333333336/50.0

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with a weigted average code

    What is the error? Can you show what the output should be?
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a weigted average code

    The mathematical formula should be:
    ((15+19+21+(5x4))/(15+20+25+20))x50 + ((98/100)x20) + ((97/100)x30)

    The first parenthetical x 50 is the sum of the actual grades/the max grades for homework x 50% of the total grade. The second and third parenthetical are each and exam and the x20 and x30 is the percentage of the total grade for those exams.

    I am afraid I know the mathematical but not the coding replacement for those formulas.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Problem with a weigted average code

    The java expression for that formula is very similiar. The * operator for multiply.

    One problem is integer division: 4/5 = 0 vs float division: 4/5.0 = .8
    Make sure the divisions are not integer by making one of the parts floating
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with a weigted average code

    Thank you! I will go back through it and try to restructure the formulas.

Similar Threads

  1. How do I get an average using an Array?
    By cheshire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 22nd, 2014, 05:46 AM
  2. Average code not working
    By Gerock7 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 25th, 2014, 11:49 PM
  3. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  4. [SOLVED] Average and counting problem (sorta)
    By Elyril in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 19th, 2014, 07:04 PM
  5. problem in my code java code graph editeur
    By kisokiso in forum Java Theory & Questions
    Replies: 5
    Last Post: January 6th, 2012, 08:36 AM