4 Replies - 670 Views - Last Post: 17 October 2013 - 11:21 PM Rate Topic: -----

#1 AbstractedAndroid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 13

OOP java why is everything printing out as 0?

Posted 17 October 2013 - 09:55 PM

It's probably something really simple, but I just can't seem to figure it out Any advice to send me in the right direction? (Also I'm not sure I've put the nested if else statements to determine the letter grade in the right place...)
The Class I coded:
import java.util.Scanner;
public class StudentRecord
{
    private int quizOne;
    private int quizTwo;
    private int midTermExam;
    private int finalExam;
    private double totalScore;
    private char finalGrade;

    Scanner keyboard = new Scanner (System.in);
    public void setQuizOne(int inQuizOne)
    {
        quizOne= inQuizOne;
    }  

    public void setQuizTwo(int inQuizTwo)
    {
        quizOne= inQuizTwo;
    }  

    public void setMidTermExam(int inMidTermExam)
    {
        midTermExam= inMidTermExam;
    }

    public void setFinalExam(int inFinalExam)
    {
        finalExam= inFinalExam;
    }

    public void setTotalScore(double inTotalScore)
    {
        totalScore=((quizOne+quizTwo)/20.0*25+midTermExam/100.0*25+finalExam/100.0*50);
    }

    public void setFinalGrade (char inFinalGrade)
    {
        if(totalScore>=90)
        {    
            finalGrade='A';
        }
        else if (totalScore>=80)
        {    
            finalGrade ='B';
        }
        else if (totalScore >= 70)
        {   
            finalGrade = 'C';
        }
        else if (totalScore >= 60)
        {
            finalGrade = 'D';
        }
        else
        {
            finalGrade = 'F';
        }
    }

    public void readInput()
    {   
        System.out.println("Enter the scores for your first quiz.");
        int quizOne = keyboard.nextInt();
        System.out.println("Enter the scores for your second quiz.");
        int quizTwo = keyboard.nextInt();
        System.out.println("Enter the scores for your midterm.");
        int midTermExam = keyboard.nextInt();
        System.out.println("Enter the scores for your final.");
        int finalExam = keyboard.nextInt();
    }

    public void writeOutput()
    {
        System.out.println("Quiz One is " + quizOne);
        System.out.println("Quiz Two is " + quizTwo);
        System.out.println("Midterm is " + midTermExam);
        System.out.println("Final is " + finalExam);
        System.out.println("Total score is " + totalScore);
        System.out.println("Final grade: " + finalGrade);
    }

    public int getQuizOne()
    {        
        return quizOne;
    }

    public int getQuizTwo()
    {        
        return quizTwo;
    }

    public int getMidTermExam()
    {
        return midTermExam;
    }

    public int getFinalExam()
    {
        return finalExam;
    }

    public double getTotalScore()
    {
        return totalScore;
    }

    public char getFinalGrade()
    {
        return finalGrade;
    }
    
}




And here's the given driver (can't change anything about it):
public class StudentRecordDriver 
{
    public static void main (String args[])
    {
        StudentRecord coder1 = new StudentRecord();
        System.out.println("Time to process coder1");
        coder1.readInput();
        coder1.writeOutput();
        
        StudentRecord coder2 = new StudentRecord();
        System.out.println("\nTime to process coder2");
        coder2.readInput();
        coder2.writeOutput();
        
        System.out.println("\nGood a regrade for coder2");
        coder2.setFinalExam(93);
        System.out.println("Confirm that Final Exam score has been updated\n"
                + "Score is now: " + coder2.getFinalExam() + "\n");
        coder2.writeOutput();
      
    }  //end main           
} //end class



Is This A Good Question/Topic? 0
  • +

Replies To: OOP java why is everything printing out as 0?

#2 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: OOP java why is everything printing out as 0?

Posted 17 October 2013 - 10:11 PM

Here you declare local variables that have the same name with your fields and assign values to them and not to your fields.So your fields get the default value which is 0.

  public void readInput()
  {  
        System.out.println("Enter the scores for your first quiz.");
        int quizOne = keyboard.nextInt();
        System.out.println("Enter the scores for your second quiz.");
        int quizTwo = keyboard.nextInt();
        System.out.println("Enter the scores for your midterm.");
        int midTermExam = keyboard.nextInt();
        System.out.println("Enter the scores for your final.");
        int finalExam = keyboard.nextInt();
  }


Was This Post Helpful? 3
  • +
  • -

#3 AbstractedAndroid   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-September 13

Re: OOP java why is everything printing out as 0?

Posted 17 October 2013 - 10:43 PM

That was definitely helpful thank you! Any suggestions to fix the total score and final grade? They're printing out as 0.0 and just blank. I thought maybe using this.setTotalScore and this.getTotalScore would fix it, but I just get an "method setTotalScore in class StudentRecord cannot be applied to given types" error :/
Was This Post Helpful? 0
  • +
  • -

#4 ChrisNt   User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 896
  • Joined: 31-July 13

Re: OOP java why is everything printing out as 0?

Posted 17 October 2013 - 10:59 PM

Why those methods need parameters? Also you dont call them anywhere so its an expected result.

A solution could be in your readInput() after you have finished with getting input from the user to call methods
setFinalGrade() and setTotalScore() .

This post has been edited by ChrisNt: 17 October 2013 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky   User is offline

  • Beginner
  • member icon


Reputation: 11071
  • View blog
  • Posts: 18,909
  • Joined: 19-March 11

Re: OOP java why is everything printing out as 0?

Posted 17 October 2013 - 11:21 PM

One nice approach would be to let getTotalScore calculate the total when it's called, and return that.

You could improve on that a little by setting a "stale" flag, which is initially true. Now, when getTotalScore() is called, it checks that flag to see if totalScoreStale is true - if so, it calculates the value, caches it, and returns it. If not, it returns the cached value. Now, when you perform an operation which invalidates totalScore, you just "taint" that value (set the totalScoreStale flag). This gives you a "lazy" getter - it only calculates the value when it needs it, which is nice. Doesn't make a big difference here, but it's good practice for later, when you'll find that lazy evaluation is a really nice trick.

NOTE: Needless to say, when the value is calculated and cached, we set the "taint" flag to false, since we then have a fresh value...

This post has been edited by jon.kiparsky: 18 October 2013 - 01:18 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1