Java Programming

Discussion in 'The Cheap Seats' started by SuperBowlNachos, Oct 4, 2011.

  1. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    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.

    Code:
    public class EssayDemo
    {
       public static void main(String[] args)
       {
          // Create an Essay object.
          Essay termPaper = new Essay();
    
          // Assign scores to the object.
          // Grammer = 25 points, Spelling = 18 points,
          // Length = 20 points, and Content = 25 points.
          termPaper.setScore(25.0, 18.0, 20.0, 25.0);
    
          // Display the score details.
          System.out.println("Term paper:");
          System.out.println("Grammar points: " +
                             termPaper.getGrammar());
          System.out.println("Spelling points: " +
                             termPaper.getSpelling());
          System.out.println("Length points: " +
                             termPaper.getCorrectLength());
          System.out.println("Content points: " +
                             termPaper.getContent());
          System.out.println("Total points: " +
                             termPaper.getScore());
          System.out.println("Grade: " +
                             termPaper.getGrade());
       }
    }
    

    This is the second class(GradedActivity)

    Code:
    public class GradedActivity
    {
       private double score;  // Numeric score
    
       /**
        * The setScore method stores its argument in
        * the score field.
        */
    
       public void setScore(double s)
       {
          score = s;
       }
    
       /**
        * The getScore method returns the score field.
        */
    
       public double getScore()
       {
          return score;
       }
    
       /**
        * The getGrade method returns a letter grade  
        * determined from the score field.
        */
    
       public char getGrade()
       {
          char letterGrade;  // To hold the grade
    
          if (score >= 90)
             letterGrade = 'A';
          else if (score >= 80)
             letterGrade = 'B';
          else if (score >= 70)
             letterGrade = 'C';
          else if (score >= 60)
             letterGrade = 'D';
          else
             letterGrade = 'F';
    
          return letterGrade;
       }
    }

    This is the third class(Essay), which is extended off the one above.
    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 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;
        }
        
        
        
        
                }
    
    

     
  2. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    (Sorry, I don't program in Java often)

    Assuming the score variable in the GradedActivity class is just the sum of each section, you want something like this for your constructor in the Essay class:

    Code:
    
    public Essay(double g, double s, double cl, double c)
    {
         super().setScore(g+s+cl+c);
         grammar = g;
         spelling = s;
         correctLength = cl;
         content = c;
    }
    
    
    Then you can correctly call the getter methods in the inherited class.
     
  3. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    Uhg, still can't get this shit 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.
     
  4. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    Well, post some error messages and I will help.:)
     
  5. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    How is that any different then the one I have under the no arg? Other then the Super part isn't in there? Do I add all of the code you listed, or just the Super part?

    Code:
        public Essay(double g, double s, double cl, double c)
        {
            grammar = g;
            spelling = s;
            correctLength = cl;
            content = c;
        }
    
    Shaun Mason
     
  6. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    I get "Essay(double, double, double, double) is already defined in Essay.

    and I get "Call to super must be first statement in constructor"

    when I put in the code you wrote out. If I delete the one that is repeating. I still get that second error.
     
  7. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    You can only have one constructor with that signature. The only thing I added was that call to super(). You also need to add that call to the constructor with no arguments.

    So:

    Code:
    Public Essay()
    {
         super();
         grammar = 0.0;
         spelling = 0.0;
         correctLength = 0.0;
         content = 0.0;
    }
    
    and
    Code:
    Public Essay(double g, double s, double cl, double c)
    {
         super.setScore(g+s+cl+c);
         grammar = g;
         spelling = s;
         correctLength = cl;
         content = c;
    }
    
    You MAY need a super(); call before super.SetScore()...I'm not a Java programmer.:)
     
  8. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    Public Essay() {
    super();
    grammar = 0.0; spelling = 0.0;
    correctLength = 0.0;
    content = 0.0;
    }

    and

    Public Essay(double g, double s, double cl, double c) {
    super.setScore(g+s+cl+c);

    grammar = g; spelling = s; correctLength = cl; content = c;

    }

    You MAY need a super(); call before super.SetScore()...I'm not a Java programmer.:)

    ----
    just had to reply to get the forum code right
     
  9. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    Did you get it working?
     
  10. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    Ya. I ended up finding it on the internet lol. I'll post it later.
     
  11. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    Yea, I'm curious now.
     
  12. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    Shaun Mason

    Up for helping me again? :D

    This is what I have to do

    Code:
    public class TestScoresDemo
    {
       public static void main(String[] args)
       {
          // An array with test scores.
          // Notice that element 3 contains an invalid score.
          double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };
    
          // Another array with test scores.
          // All of these scores are good.
          double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };
    
          // *** Create a TestScores object initialized with badScores. ***
    
    
          // ********** PLACE CODE HERE **************
    
    
          // Create a TestScores object initialized with goodScores.
    
    
          // ********** PLACE CODE HERE **************
    
    
       }
    }
    
    Code:
    public class TestScores {
        
          
        
    }
    
    I don't know where to even start. Have I mentioned I HATE java?
     
  13. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    Alright, I'll help you, but because I:

    1) Don't know Java that well anymore.
    2) Am too lazy to look at JavaDocs to code in Java.
    3) Feel guilty doing your homework for you.

    I am going to give you an implementation in VB.NET and you can translate it to Java, that way you have to understand what's going on.:)

    Code:
    Public Class TestScoreDemo
    
        ''' <summary>
        ''' Driver Method
        ''' </summary>
        Public Sub Main()
            '// An array with test scores.
            '// Notice that element 3 contains an invalid score.
            Dim badScores As Double() = {97.5, 66.7, 88.0, 101.0, 99.0}
    
            '// Another array with test scores.
            '// All of these scores are good.
            Dim goodScores As Double() = {97.5, 66.7, 88.0, 100.0, 99.0}
    
            Try
                '// *** Create a TestScores object initialized with badScores. ***
                Dim badTestScoreObject As New TestScores(badScores)
    
                '// ********** PLACE CODE HERE **************
                Call PrintTestScores(badTestScoreObject)
            Catch ex As Exception
                Console.Write(ex.Message)
            End Try
    
            Try
                '// Create a TestScores object initialized with goodScores.
                Dim goodTestScoreObject As New TestScores(goodScores)
    
                '// ********** PLACE CODE HERE **************
                Call PrintTestScores(goodTestScoreObject)
            Catch ex As Exception
                Console.Write(ex.Message)
            End Try
        End Sub
    
        ''' <summary>
        ''' Prints the test scores.
        ''' </summary>
        ''' <param name="testScoreObject">The test score object.</param>
        Private Sub PrintTestScores(ByVal testScoreObject As TestScores)
            Console.WriteLine(String.Format("The average of the good scores is {0}", testScoreObject.AverageScore))
        End Sub
    
    End Class
    
    Public Class TestScores
    
        Private _scores As Double()
        Private _Count As Integer
        Private _Average As Double
    
        ''' <summary>
        ''' Gets the count.
        ''' </summary>
        Public ReadOnly Property Count As Integer
            Get
                Return _Count
            End Get
        End Property
    
        ''' <summary>
        ''' Gets the average.
        ''' </summary>
        Public ReadOnly Property AverageScore As Double
            Get
                Return _scores.Average
            End Get
        End Property
    
        ''' <summary>
        ''' Initializes a new instance of the <see cref="TestScores" /> class.
        ''' </summary>
        ''' <param name="scores">The scores.</param>
        Public Sub New(ByVal scores As Double())
            If _scores.Min < 0 Then Throw New ArgumentException(String.Format("Invalid Score found. {0}Element: {1} Score: {2}", vbCrLf, Array.IndexOf(_scores, _scores.Min), _scores.Min))
            If _scores.Max > 100 Then Throw New ArgumentException(String.Format("Invalid Score found. {0}Element: {1} Score: {2}", vbCrLf, Array.IndexOf(_scores, _scores.Max), _scores.Max))
    
            _scores = scores
        End Sub
    
    End Class
    
     
    • Like Like x 1
  14. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
  15. SuperBowlNachos

    SuperBowlNachos Walk On

    Joined:
    Jul 1, 2010
    Messages:
    4,611
    Shaun Mason

    Thanks for the help btw. See anything with your non-java skills that could improve this? One thing I couldn't figure out was how to make it output which Element was wrong. In the example it shoots out a 3. I just deleted it entirely and said f it.

    Code:
    public class TestScoresDemo
    {
      public static void main(String[] args) throws Exception
      {
          // An array with test scores.
          // Notice that element 3 contains an invalid score.
          double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };
    
          // Another array with test scores.
          // All of these scores are good.
          double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };
    
          // *** Create a TestScores object initialized with badScores. ***
    
          try
            {
                TestScores number = new TestScores(badScores);
            }
    
          catch(IllegalArgumentException e)
            {
                System.out.println(e.getMessage());
            }
    
          // Create a TestScores object initialized with goodScores.
    
          try
            {
                TestScores number = new TestScores(goodScores);
            }
    
          catch(IllegalArgumentException e)
            {
                System.out.println(e.getMessage());
            }
    
            TestScores number = new TestScores(goodScores);
    
            System.out.println("The average of the good scores is " + number.getAverage());
      }
    }
    
    Code:
    public class TestScores {
    
        // private member variables
    
        private double[] score;
    
        // constructors
    
        public TestScores(double[] s) throws IllegalArgumentException
    
        {
            score = new double[s.length];
            for (int n = 0; n < s.length; n++)
                {
                    if(s[n] <0 || s[n] >100)
                {
                    throw new IllegalArgumentException("Invalid score found." + "\n" + "Score: " + s[n]);
                        }
                            score[n] = s[n];
                        }
    
                        }
                            public double getSum()
                        {
    
                            double total = 0.0;
    
                            for (double value : score)
    
                            total = total + value;
    
                            return total;
        }
    
            // accessor method
    
            public double getAverage()
                {
                    return getSum()/score.length;
                }
    }
    
     
  16. Shaun Mason

    Shaun Mason Somebody you used to know.

    Joined:
    Feb 9, 2009
    Messages:
    24,887
    Featured Threads:
    5
    If you want to send back what element is wrong, just set a local variable when you find one in your loop. There 'i' would be the element.
     

Share This Page