Recommended Videos

Collapse

Java Programming

Collapse
X
Collapse
 
  • Time
  • Show
Clear All
new posts
  • #1
    SuperBowlNachos
    All Star
    • Jul 2004
    • 10218

    Java Programming


    Anyone good at Java wanna help me out? I have literally 1 line, I think, that I need done. Just can't figure out exactly what it should be.

    Driver Program(EssayDemo)...I get a syntax error at the termPaper.setScore line. I know I need to add something to the Essay class. Something using the Super command. Just not sure exactly where it goes or what it should be. We were given the first two classes. I had to build the last and that is the only one I modify.

    Spoiler


    This is the second class(GradedActivity)

    Spoiler


    This is the third class(Essay), which is extended off the one above.
    Spoiler
    Last edited by SuperBowlNachos; 10-04-2011, 05:16 PM.
  • #2
    stizz
    MVP
    • Sep 2006
    • 1718

    Re: Java Programming


    Re: Java Programming

    Your calling the GradeActivity's setScore() in your main method and that only takes in one double, but you're trying to give it four.

    Either, set them manually by creating the object using the second constructor with the values, by calling the appropriate methods (eg. setContent, setGrammar), or override the original setScore method in your Essay class so that it takes in four parameters.

    Comment

    • #3
      duffman
      Rookie
      • Jul 2004
      • 309

      Re: Java Programming


      Re: Java Programming

      If i understand correctly you can't modify either of the first two classes you were given, just the Essay class, right? If that's the case then you would need to add a setScore(double, double, double, double) function to the Essay class. From there, you can set each individual field (grammar, spelling, etc.) inside of that function. You probably want to use the GradedActivity.setScore function to store the total score (just adding a "setScore(double);" line inside of Essay.setScore(double, double, double, double) should do it). That way when the main class calls termPaper.getScore() it will return the total score. When the main class calls each individual get() function to display the individual values they will use the code you have in your Essay class.

      Comment

      • #4
        duffman
        Rookie
        • Jul 2004
        • 309

        Re: Java Programming


        Re: Java Programming

        Oh and just a quick tip about dealing with inheritance and extending class/overriding functions. If you ever start to get confused about which function call is being used (and from what class) just throw an output statement to dump out some garbage in each call in each class. Makes it real easy to track where the code is going and how if you start dealing with more complex situations since you can just follow what text got outputted and in what order.

        Comment

        • #5
          SuperBowlNachos
          All Star
          • Jul 2004
          • 10218

          Re: Java Programming


          Re: Java Programming

          Uhg, still can't get this **** to work. I'm terrible at programming. I am in the required advanced class and I could not do something from the first chapter of the beginners if my life depended on it.

          Comment

          • #6
            stizz
            MVP
            • Sep 2006
            • 1718

            Re: Java Programming


            Re: Java Programming

            In your Essay class, create a method named setScore which takes in 4 doubles. Then assign each parameter to the appropriate field.

            So pretty much copy one of the existing setter methods, but instead of taking in 1 double and having 1 assignment statement, modify it so that it takes in 4 doubles and has 4 assignment statements.

            Comment

            • #7
              SuperBowlNachos
              All Star
              • Jul 2004
              • 10218

              Re: Java Programming


              Re: Java Programming

              There should be a super somewhere in there though.

              Comment

              • #8
                stizz
                MVP
                • Sep 2006
                • 1718

                Re: Java Programming


                Re: Java Programming

                Add super.setScore(grammar + spelling + correctLength + content); at the end of the method I told you to create.

                Comment

                • #9
                  SuperBowlNachos
                  All Star
                  • Jul 2004
                  • 10218

                  Re: Java Programming


                  Re: Java Programming

                  Not sure if it's right, but it works

                  Code:
                  public class Essay extends GradedActivity{
                      
                      private double grammar;
                      private double spelling;
                      private double correctLength;
                      private double content;
                      
                    //constructors
                  
                    //no arg
                  
                      public Essay()
                      {
                          grammar = 0.0;
                          spelling = 0.0;
                          correctLength = 0.0;
                          content = 0.0;        
                      }
                      
                      
                      public Essay(double g, double s, double cl, double c)
                      {
                          grammar = g;
                          spelling = s;
                          correctLength = cl;
                          content = c;
                  
                      }
                      
                      // mutators
                      public void setScore(double g, double s, double cl, double c)
                      {
                  	setGrammar(g);
                  	setSpelling(s);
                  	setCorrectLength(cl);
                  	setContent(c);
                      super.setScore(grammar + spelling + correctLength + content);
                      }
                      
                  
                      public void setGrammar(double g)
                      {
                          grammar = g;
                      }
                      public void setSpelling(double s)
                      {
                          spelling = s;
                      }
                      public void setCorrectLength(double cl)
                      {
                          correctLength = cl;
                      }
                      public void setContent(double c)
                      {
                          content = c;
                      }
                  
                  
                  
                      // accessors
                      
                      public double getGrammar()
                      {
                          return grammar;
                      }
                      public double getSpelling()
                      {
                          return spelling;
                      }
                      public double getCorrectLength()
                      {
                          return correctLength;
                      }
                      public double getContent()
                      {
                          return content;
                      }
                      
                    
                              }

                  Comment

                  • #10
                    SuperBowlNachos
                    All Star
                    • Jul 2004
                    • 10218

                    Re: Java Programming


                    Re: Java Programming

                    Up for helping me again? I've been on a roll lately, but this one just has me staring at my screen. i think it's the days worth of plane rides I've taken the past 5 days. I just need help getting it started then I should be fine. I hope.

                    This is what I have to do

                    1. Complete Programming Challenge #1, "TestScores Class" on page 671 of the textbook.

                    Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program.

                    2. One file has been provided for you, TestScoresDemo, which is partially complete. Add the appropriate code where designated in comment statements.

                    2a. YOU MUST CREATE THE TestScores CLASS

                    3. Submit the assignment via Blackboard as instructed in the course syllabus under "Lab Projects"

                    4. Sample Output is provide below:

                    Invalid score found.

                    Element: 3 Score: 101.0

                    The average of the good scores is 90.24
                    Spoiler


                    Spoiler

                    Comment

                    Working...